-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored example to show factory paradigm with Shape interface
- Loading branch information
Showing
9 changed files
with
276 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* | ||
* @copyright Copyright (c) 2021, Southwest Research Institute | ||
* | ||
* @par License | ||
* Software License Agreement (Apache License) | ||
* @par | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* @par | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include "printer.h" | ||
#include <iostream> | ||
|
||
namespace boost_plugin_loader | ||
{ | ||
struct ConsolePrinter : public Printer | ||
{ | ||
public: | ||
void operator()() const override | ||
{ | ||
std::cout << "IMPL: ConsolePrinter" << std::endl; | ||
} | ||
}; | ||
|
||
} // namespace boost_plugin_loader | ||
|
||
EXPORT_PRINTER_PLUGIN(boost_plugin_loader::ConsolePrinter, ConsolePrinter) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* | ||
* @copyright Copyright (c) 2021, Southwest Research Institute | ||
* | ||
* @par License | ||
* Software License Agreement (Apache License) | ||
* @par | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* @par | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <any> | ||
|
||
namespace boost_plugin_loader | ||
{ | ||
/** | ||
* @brief Interface class for representing shapes that can compute area | ||
*/ | ||
class Shape | ||
{ | ||
public: | ||
using Ptr = std::shared_ptr<Shape>; | ||
virtual double area() const = 0; | ||
}; | ||
|
||
// Forward declare PluginLoader and has_getSection classes | ||
class PluginLoader; | ||
|
||
template <typename T> | ||
struct has_getSection; | ||
|
||
/** | ||
* @brief Plugin interface for creating shape objects | ||
*/ | ||
class ShapeFactory | ||
{ | ||
public: | ||
using Ptr = std::shared_ptr<ShapeFactory>; | ||
virtual std::shared_ptr<Shape> create(const std::any& params) const = 0; | ||
|
||
private: | ||
friend class PluginLoader; | ||
friend struct has_getSection<ShapeFactory>; | ||
static std::string getSection(); | ||
}; | ||
|
||
} // namespace boost_plugin_loader | ||
|
||
#include <boost_plugin_loader/macros.h> | ||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) | ||
#define EXPORT_SHAPE_PLUGIN(DERIVED_CLASS, ALIAS) EXPORT_CLASS_SECTIONED(DERIVED_CLASS, ALIAS, shape) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* | ||
* @copyright Copyright (c) 2021, Southwest Research Institute | ||
* | ||
* @par License | ||
* Software License Agreement (Apache License) | ||
* @par | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* @par | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include "shape.h" | ||
|
||
namespace boost_plugin_loader | ||
{ | ||
/** | ||
* @brief Square shape implementation | ||
*/ | ||
class Square : public Shape | ||
{ | ||
public: | ||
Square(double w) : width(w) | ||
{ | ||
} | ||
|
||
double area() const override | ||
{ | ||
return width * width; | ||
} | ||
|
||
protected: | ||
double width; | ||
}; | ||
|
||
/** | ||
* @brief Square factory plugin implementation | ||
*/ | ||
class SquareFactory : public ShapeFactory | ||
{ | ||
std::shared_ptr<Shape> create(const std::any& params) const override | ||
{ | ||
double width = std::any_cast<double>(params); | ||
return std::make_shared<Square>(width); | ||
} | ||
}; | ||
|
||
} // namespace boost_plugin_loader | ||
|
||
// Export the factory as the plugin to allow for multiple differently configured instances of the Square shape | ||
EXPORT_SHAPE_PLUGIN(boost_plugin_loader::SquareFactory, Square) |
Oops, something went wrong.