gfactory
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 using std::map;
8 using std::string;
9 
10 // managerA: loads known classes Triangle and Box from the base Shape, in ShapeFactory.h
11 // managerB: loads DLL that instantiate derived factories - we only know of the base class, in this case "Car"
12 
13 int main(int argc, char *argv[]) {
14 
15  auto gopts = new GOptions(argc, argv, gfactory::defineOptions());
16  auto log = std::make_shared<GLogger>(gopts, PLUGIN_LOGGER, "plugin_example");
17 
18  GManager managerAV(log, "GManager Static");
19  managerAV.RegisterObjectFactory<Triangle>("triangle");
20  managerAV.RegisterObjectFactory<Box>("box1");
21  managerAV.RegisterObjectFactory<Box>("box2");
22 
23  // putting A factory in map
24  // notice we're putting the base class in the map so we can call its virtual methods
25  map<string, Shape *> fffv;
26  fffv["triangle"] = managerAV.CreateObject<Shape>("triangle");
27  fffv["box1"] = managerAV.CreateObject<Shape>("box1");
28  fffv["box2"] = managerAV.CreateObject<Shape>("box2");
29 
30  // aShape is same pointer as map element
31  Shape *aShape = fffv["triangle"];
32 
33  // calling base and derived methods
34  aShape->Area();
35  fffv["triangle"]->Area();
36  fffv["box1"]->Area();
37 
38  log->info(0, " Shape pointers from map: ", fffv["triangle"], ", from direct pointer:", aShape);
39 
40  // once we're done with it
41  managerAV.clearDLMap();
42 
43 
44  // B manages Cars. Notice, we do not need the derived class headers here!
45  // PRAGMA: These two names must match in registerDL and in LoadAndRegisterObjectFromLibrary:
46  // that's ok but need to spit error if that doesn't happen
47  GManager managerB(log, "GManager Dynamic");
48 
49  map<string, Car *> ggg;
50  ggg["tesla"] = managerB.LoadAndRegisterObjectFromLibrary<Car>("test_dyn_factory1", gopts);
51  ggg["ford"] = managerB.LoadAndRegisterObjectFromLibrary<Car>("test_dyn_factory2", gopts);
52  Car *aCar = ggg["ford"];
53 
54 
55  // calling base and derived method
56  ggg["tesla"]->go();
57  ggg["ford"]->go();
58 
59  log->info(0, " Car pointers from map: ", ggg["ford"], ", from direct pointer:", aCar);
60  log->info(0, " run generalCarVar method from factory map: ", ggg["tesla"]->generalCarVar);
61 
62  // clearing the map - this should be done in classes destructors
63  managerB.clearDLMap();
64 }
Definition: Car.h:5
virtual void go()=0
Owns factories and dynamically‑loaded libraries, providing run‑time creation.
Definition: gfactory.h:53
Base * CreateObject(std::string_view name) const
Create an instance of previously registered factory as Base*.
Definition: gfactory.h:120
T * LoadAndRegisterObjectFromLibrary(std::string_view name, GOptions *gopts)
Load a shared library, look up its instantiate symbol, and return object.
Definition: gfactory.h:131
void clearDLMap() noexcept
Explicit cleanup (also called by destructor) – idempotent.
Definition: gfactory.h:104
void RegisterObjectFactory(std::string_view name)
Register a concrete factory under name.
Definition: gfactory.h:114
virtual void Area()=0
Generic factory/manager for GEMC plugin objects.
constexpr const char * PLUGIN_LOGGER
GOptions defineOptions()
int main(int argc, char *argv[])