gfactory
Loading...
Searching...
No Matches
Car.h
Go to the documentation of this file.
1#pragma once
2
3// gemc
4#include "gbase.h"
5
6// gfactory
7#include "gdl.h"
8#include "gfactory_options.h"
9
21class Car : public GBase<Car>
22{
23public:
25 ~Car() override = default;
26
32 explicit Car(const std::shared_ptr<GOptions>& g) : GBase(g, PLUGIN_LOGGER) {
33 }
34
41 virtual void go() = 0;
42
49 void set_loggers([[ maybe_unused ]] const std::shared_ptr<GOptions>& g) {
50 }
51
57 double generalCarVar = 44;
58
59 // Historical note:
60 // The block below shows a previous approach that returned a no-arg factory function. The current example
61 // passes GOptions into the factory so the created object can be configured consistently.
62
81 static Car* instantiate(const dlhandle h, std::shared_ptr<GOptions> g) {
82 if (!h) return nullptr;
83
84 // Function pointer type matching the exported extern "C" symbol.
85 using fptr = Car* (*)(std::shared_ptr<GOptions>);
86
87 // Must match the extern "C" declaration in the derived factories.
88 // Symbol resolution uses the POSIX API \c dlsym.
89 auto sym = dlsym(h, "CarFactory");
90 if (!sym) return nullptr;
91
92 // The cast is required because dlsym returns void*.
93 // This pattern is common in plugin loaders: resolve symbol -> cast -> call.
94 auto func = reinterpret_cast<fptr>(sym);
95 return func(g);
96 }
97};
Example base class used to demonstrate dynamic factory loading.
Definition Car.h:22
static Car * instantiate(const dlhandle h, std::shared_ptr< GOptions > g)
Resolve the plugin factory symbol and instantiate a derived Car.
Definition Car.h:81
virtual void go()=0
Example pure-virtual behavior implemented by each plugin.
void set_loggers(const std::shared_ptr< GOptions > &g)
Wire loggers into the object.
Definition Car.h:49
Car(const std::shared_ptr< GOptions > &g)
Construct the base with a shared options/configuration instance.
Definition Car.h:32
~Car() override=default
Virtual destructor for polymorphic deletion.
double generalCarVar
Example data member shared by all derived cars.
Definition Car.h:57
void * dlhandle
Opaque handle returned by dlopen and consumed by dlsym / dlclose.
Definition gdl.h:19
constexpr const char * PLUGIN_LOGGER
Logger channel used by the gfactory module and plugins loaded through it.