Evo C++ Library v0.5.1
Public Types | Public Member Functions | List of all members
Cortex< T > Class Template Reference

#include <evo/cortex.h>

Inheritance diagram for Cortex< T >:
Inheritance graph
[legend]

Detailed Description

template<class T = MapList<String,SharedPtr<PureBase> >>
class evo::Cortex< T >

A Map with a collection of independent abstract context items.

Template Parameters
TMap 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.

#include <evo/cortex.h>
#include <evo/io.h>
using namespace evo;
// Define module "foo"
static const char* MODULE_FOO = "foo";
struct ModuleFoo : PureBase {
void foo() {
// ...
}
};
// Define module "bar"
static const char* MODULE_BAR = "bar";
struct ModuleBar : PureBase {
// Move implementation to a source (.cpp) file to decouple the dependency on ModuleFoo
void bar(Cortex<>& modules) {
modules.get_create<ModuleFoo>(MODULE_FOO).foo();
}
};
int main() {
Cortex<> modules;
modules.reserve(2);
try {
// Uncomment to create modules in advance, otherwise created on demand
//modules.create<ModuleFoo>(MODULE_FOO);
//modules.create<ModuleBar>(MODULE_BAR);
// Call ModuleBar method
modules.get_create<ModuleBar>(MODULE_BAR).bar(modules);
} EVO_CATCH(return 1);
return 0;
}

This example shows an interface with an event/hook: event_thing()

#include <evo/cortex.h>
#include <evo/maplist.h>
#include <evo/io.h>
using namespace evo;
struct ModuleBase : PureBase {
// Hook for when the "thing" happens
virtual void event_thing() = 0;
};
// Define MyCortex type using ModuleBase interface
typedef EVO_CORTEX_MAPLIST(String, ModuleBase) MyCortex;
// Define module "foo"
static const char* MODULE_FOO = "foo";
struct ModuleFoo : ModuleBase {
void event_thing() {
con().out << "The thing happened!" << NL;
}
};
int main() {
MyCortex modules;
// Create module "foo"
modules.create<ModuleFoo>(MODULE_FOO);
// Notify each module that the "thing" is happening
for (MyCortex::IterM iter(modules); iter; ++iter)
iter->value()->event_thing();
// Notify again using C++11 loop (if supported)
for (auto& mod : modules)
mod.value()->event_thing();
)
return 0;
}

Public Types

typedef T MapType
 Map type (from T) More...
 
typedef Cortex< T > This
 This type. More...
 
typedef T::Value::Item ValueBase
 Value base type, normally PureBase. More...
 

Public Member Functions

 Cortex ()
 Constructor. More...
 
template<class ItemT >
Thiscreate (const Key &key)
 Create item for key, if needed. More...
 
template<class ItemT >
ItemT & get_create (const Key &key)
 Get stored item using key, create if needed. More...
 
template<class ItemT >
ItemT * getptr (const Key &key)
 Get stored item pointer using key (mutable). More...
 
template<class ItemT >
const ItemT * getptr_const (const Key &key) const
 Get stored item pointer using key (const). More...
 

Member Typedef Documentation

◆ MapType

typedef T MapType

Map type (from T)

◆ This

typedef Cortex<T> This

This type.

◆ ValueBase

typedef T::Value::Item ValueBase

Value base type, normally PureBase.

Constructor & Destructor Documentation

◆ Cortex()

Cortex ( )
inline

Constructor.

Member Function Documentation

◆ create()

This& create ( const Key &  key)
inline

Create item for key, if needed.

  • If the item doesn't exist this creates it, otherwise this is a no-op
Template Parameters
ItemTItem type for underlying key
Parameters
keyKey to find item
Returns
This

◆ get_create()

ItemT& get_create ( const Key &  key)
inline

Get stored item using key, create if needed.

  • If the item doesn't exist this creates it first
  • 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
ItemTItem type for underlying key
Parameters
keyKey to find item
Returns
Reference to item

◆ getptr()

ItemT* getptr ( const Key &  key)
inline

Get stored item pointer using key (mutable).

  • 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
ItemTItem type for underlying key
Parameters
keyKey to find item
Returns
Pointer to item found, NULL if not found

◆ getptr_const()

const ItemT* getptr_const ( const Key &  key) const
inline

Get stored item pointer using key (const).

  • 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
ItemTItem type for underlying key
Parameters
keyKey to find item
Returns
Pointer to item found, NULL if not found

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