gfactory
Loading...
Searching...
No Matches
static_and_dynamic_example.cc
Go to the documentation of this file.
1#include "gfactory.h"
2#include "gfactory_options.h"
3#include "ShapeFactory.h"
4#include "Car.h"
5
6// c++
7// See header for API docs.
8using std::map;
9using std::string;
10
11// This example demonstrates both workflows supported by GManager:
12//
13// 1) Static registration:
14// - We compile/link against the derived classes (Triangle, Box),
15// register them in the manager, and instantiate them by key.
16//
17// 2) Dynamic loading:
18// - We compile/link only against the base class (Car).
19// Derived classes live in shared libraries and are instantiated by symbol lookup.
20
21int main(int argc, char* argv[]) {
22 // Build the options object from argc/argv and the module-defined option set.
23 auto gopts = std::make_shared<GOptions>(argc, argv, gfactory::defineOptions());
24
25 // Extra plugins log in here, so we have 3 total (2 additional managers).
26 // This is ok in the example; in a practical application we would typically have one manager.
27 auto log = std::make_shared<GLogger>(gopts, SFUNCTION_NAME, PLUGIN_LOGGER);
28
29 // -------------------------
30 // A) Static factory example
31 // -------------------------
32 GManager managerAV(gopts);
33
34 // Register the compile-time-known derived classes under keys.
35 managerAV.RegisterObjectFactory<Triangle>("triangle", gopts);
36 managerAV.RegisterObjectFactory<Box>("box1", gopts);
37 managerAV.RegisterObjectFactory<Box>("box2", gopts);
38
39 // Store instances as base pointers to exercise virtual dispatch.
40 map<string, Shape*> fffv;
41 fffv["triangle"] = managerAV.CreateObject<Shape>("triangle");
42 fffv["box1"] = managerAV.CreateObject<Shape>("box1");
43 fffv["box2"] = managerAV.CreateObject<Shape>("box2");
44
45 // aShape is same pointer as map element.
46 Shape* aShape = fffv["triangle"];
47
48 // Calling base and derived methods (virtual dispatch via base pointer).
49 aShape->Area();
50 fffv["triangle"]->Area();
51 fffv["box1"]->Area();
52
53 log->info(0, " Shape pointers from map: ", fffv["triangle"], ", from direct pointer:", aShape);
54
55
56 // --------------------------
57 // B) Dynamic loading example
58 // --------------------------
59 // Notice: we do not need the derived class headers here.
60 //
61 // Convention: These names must match the library naming rule used by the loader
62 // (registerDL uses "<name>.gplugin") and the name passed to the load call.
63 GManager managerB(gopts);
64
65 map<string, std::shared_ptr<Car>> ggg;
66
67 // Load plugins and create instances. The returned shared_ptr keeps the library loaded
68 // for the object's lifetime.
69 ggg["tesla"] = managerB.LoadAndRegisterObjectFromLibrary<Car>("test_dyn_factory1", gopts);
70 ggg["ford"] = managerB.LoadAndRegisterObjectFromLibrary<Car>("test_dyn_factory2", gopts);
71 auto aCar = ggg["ford"];
72
73 // Calling base and derived method through base-class interface.
74 ggg["tesla"]->go();
75 ggg["ford"]->go();
76
77 log->info(0, " Car pointers from map: ", ggg["ford"], ", from direct pointer:", aCar);
78 log->info(0, " run generalCarVar method from factory map: ", ggg["tesla"]->generalCarVar);
79
80 return EXIT_SUCCESS;
81}
Example concrete Shape implementation used in static registration.
Example base class used to demonstrate dynamic factory loading.
Definition Car.h:22
Factory registry and dynamic-library manager for run-time creation of plugin objects.
Definition gfactory.h:122
std::shared_ptr< T > LoadAndRegisterObjectFromLibrary(std::string_view name, const std::shared_ptr< GOptions > &gopts)
Load a plugin library and instantiate an object from it.
Definition gfactory.h:270
Base * CreateObject(std::string_view name) const
Create an instance of a previously registered factory.
Definition gfactory.h:257
void RegisterObjectFactory(std::string_view name)
Register a concrete factory under a string key.
Definition gfactory.h:244
Example base class used to demonstrate static factory registration.
virtual void Area()=0
Compute or report the shape area (example API).
Example concrete Shape implementation used in static registration.
Generic factory/manager for GEMC plugin objects.
constexpr const char * PLUGIN_LOGGER
Logger channel used by the gfactory module and plugins loaded through it.
#define SFUNCTION_NAME
GOptions defineOptions()
Define the command-line/options set used by the gfactory module.
int main(int argc, char *argv[])