Evo C++ Library v0.5.1
Public Member Functions | List of all members
CortexModuleBase Struct Reference

#include <evo/cortex.h>

Detailed Description

A simple base class for defining a module using a similar pattern to Cortex, but without the dynamic Map.

Example

Here are some examples using alternatives to Cortex.

This example uses a struct to hold modules.

#include <evo/cortex.h>
using namespace evo;
struct Modules {
CortexModulePtr foo, bar;
};
struct ModuleFoo : CortexModuleBase {
void foo() {
// ...
}
};
typedef CortexModuleAsBase<ModuleFoo> ModuleAsFoo;
struct ModuleBar : CortexModuleBase {
// Move implementation to a source (.cpp) file to decouple the dependency on ModuleFoo
void bar(Modules& modules) {
ModuleAsFoo::get(modules.foo).foo();
}
};
typedef CortexModuleAsBase<ModuleBar> ModuleAsBar;
int main() {
Modules modules;
// Uncomment to create modules in advance, otherwise created on demand
//modules.foo = new ModuleFoo;
//modules.bar = new ModuleBar;
// Call ModuleBar method
ModuleAsBar::get(modules.bar).bar(modules);
return 0;
}

This example uses a raw array – List or Array can also be used.

#include <evo/cortex.h>
using namespace evo;
// ID for each module, used as array index
enum ModuleId {
MODULE_FOO, // ID for module Foo
MODULE_BAR, // ID for module Bar
MODULE_COUNT // Guard used as number of modules
};
struct ModuleFoo : CortexModuleBase {
void foo() {
// ...
}
};
typedef CortexModuleAsBase<ModuleFoo> ModuleAsFoo;
struct ModuleBar : CortexModuleBase {
// Move implementation to a source (.cpp) file to decouple the dependency on ModuleFoo
void bar(CortexModulePtr modules[]) {
ModuleAsFoo::get(modules[MODULE_FOO]).foo();
}
};
typedef CortexModuleAsBase<ModuleBar> ModuleAsBar;
int main() {
CortexModulePtr modules[MODULE_COUNT];
// Uncomment to create modules in advance, otherwise created on demand
//modules[MODULE_FOO] = new ModuleFoo;
//modules[MODULE_BAR] = new ModuleBar;
// Call ModuleBar method
ModuleAsBar::get(modules[MODULE_BAR]).bar(modules);
return 0;
}

Public Member Functions

virtual ~CortexModuleBase ()
 Destructor. More...
 
template<class T >
T & as ()
 Get this module as a concrete type. More...
 

Constructor & Destructor Documentation

◆ ~CortexModuleBase()

virtual ~CortexModuleBase ( )
inlinevirtual

Destructor.

Member Function Documentation

◆ as()

T& as ( )
inline

Get this module as a concrete type.

  • This uses dynamic_cast to cast the base type to the requested type
    • This has some runtime overhead, and throws std::bad_cast if the cast fails
    • To use static_cast instead define EVO_CORTEX_DYNAMIC_CAST 0 (before including this)
  • Caution: Results are undefined if the concrete type (ItemT) doesn't match the current object for key
Template Parameters
TConcrete module type to cast this to
Returns
Concrete reference to this object (T&)

The documentation for this struct was generated from the following file: