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++
7using std::map;
8using 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
13int main(int argc, char *argv[]) {
14
15 auto gopts =std::make_shared<GOptions>(argc, argv, gfactory::defineOptions());
16 // extra plugins log in here, so we have 3 total (2 additional managers)
17 // this is ok in the example, in a practical application we would only have one manager
18 auto log = std::make_shared<GLogger>(gopts, SFUNCTION_NAME, PLUGIN_LOGGER);
19
20 GManager managerAV(gopts);
21 managerAV.RegisterObjectFactory<Triangle>("triangle");
22 managerAV.RegisterObjectFactory<Box>("box1");
23 managerAV.RegisterObjectFactory<Box>("box2");
24
25 // putting A factory in map
26 // notice we're putting the base class in the map so we can call its virtual methods
27 map<string, Shape *> fffv;
28 fffv["triangle"] = managerAV.CreateObject<Shape>("triangle");
29 fffv["box1"] = managerAV.CreateObject<Shape>("box1");
30 fffv["box2"] = managerAV.CreateObject<Shape>("box2");
31
32 // aShape is same pointer as map element
33 Shape *aShape = fffv["triangle"];
34
35 // calling base and derived methods
36 aShape->Area();
37 fffv["triangle"]->Area();
38 fffv["box1"]->Area();
39
40 log->info(0, " Shape pointers from map: ", fffv["triangle"], ", from direct pointer:", aShape);
41
42
43 // B manages Cars. Notice, we do not need the derived class headers here!
44 // PRAGMA: These two names must match in registerDL and in LoadAndRegisterObjectFromLibrary:
45 // that's ok but need to spit error if that doesn't happen
46 GManager managerB(gopts);
47
48 map<string, std::shared_ptr<Car>> ggg;
49 ggg["tesla"] = managerB.LoadAndRegisterObjectFromLibrary<Car>("test_dyn_factory1", gopts);
50 ggg["ford"] = managerB.LoadAndRegisterObjectFromLibrary<Car>("test_dyn_factory2", gopts);
51 auto aCar = ggg["ford"];
52
53
54 // calling base and derived method
55 ggg["tesla"]->go();
56 ggg["ford"]->go();
57
58 log->info(0, " Car pointers from map: ", ggg["ford"], ", from direct pointer:", aCar);
59 log->info(0, " run generalCarVar method from factory map: ", ggg["tesla"]->generalCarVar);
60
61 return EXIT_SUCCESS;
62}
Definition Car.h:5
Owns factories and dynamically‑loaded libraries, providing run‑time creation.
Definition gfactory.h:55
std::shared_ptr< T > LoadAndRegisterObjectFromLibrary(std::string_view name, const std::shared_ptr< GOptions > &gopts)
Load a shared library, look up its instantiate symbol, and return object.
Definition gfactory.h:122
Base * CreateObject(std::string_view name) const
Create an instance of the previously registered factory.
Definition gfactory.h:111
void RegisterObjectFactory(std::string_view name)
Register a concrete factory under name.
Definition gfactory.h:105
virtual void Area()=0
Generic factory/manager for GEMC plugin objects.
constexpr const char * PLUGIN_LOGGER
GOptions defineOptions()
int main(int argc, char *argv[])