template<class T = MapList<String,SharedPtr<PureBase> >>
class evo::Cortex< T >
A Map with a collection of independent abstract context items.
- This creates a kind of modular plug-in architecture, where items (modules or contexts) in the the Cortex may be accessed without depending on all the concrete types stored in it
- Expand further by adding "hook" methods to the base interface, where you can apply design patterns like Observer, Visitor, Mediator, etc
- PureBase is used as the base interface by default, an alternative base type must either inherit from PureBase or at least have a virtual destructor
- See helper macros for making a custom Cortex type:
- This basically wraps and inherits a Map container, where each value is a SharedPtr to an abstract base class such as PureBase
- Though you can use the Map interface, the Cortex interface is preferred:
- See also alternative approach with: CortexModuleBase
- Template Parameters
-
T | Map type to use, values must be a SharedPtr<PureBase> (or SharedPtr to derived type, or at least have a virtual destructor) – defaults to MapList using String keys
- Default can be changed to MapHash with String keys by defining
EVO_CORTEX_DEFAULT_HASHMAP (before including this)
|
- Examples
This example shows a couple modules store in a Cortex, one of which has a dependency on the other.
static const char* MODULE_FOO = "foo";
void foo() {
}
};
static const char* MODULE_BAR = "bar";
}
};
int main() {
modules.reserve(2);
try {
modules.
get_create<ModuleBar>(MODULE_BAR).bar(modules);
return 0;
}
This example shows an interface with an event/hook: event_thing()
virtual void event_thing() = 0;
};
static const char* MODULE_FOO = "foo";
struct ModuleFoo : ModuleBase {
void event_thing() {
con().
out <<
"The thing happened!" <<
NL;
}
};
int main() {
MyCortex modules;
modules.
create<ModuleFoo>(MODULE_FOO);
for (MyCortex::IterM iter(modules); iter; ++iter)
iter->value()->event_thing();
for (auto& mod : modules)
mod.value()->event_thing();
)
return 0;
}