Evo C++ Library v0.5.1
cortex.h
Go to the documentation of this file.
1 // Evo C++ Library
2 /* Copyright 2019 Justin Crowell
3 Distributed under the BSD 2-Clause License -- see included file LICENSE.txt for details.
4 */
6 
7 #pragma once
8 #ifndef INCL_evo_cortex_h
9 #define INCL_evo_cortex_h
10 
11 #include "ptr.h"
12 #if defined(EVO_CORTEX_DEFAULT_HASHMAP)
13  #include "maphash.h"
14  #define EVO_CORTEX_IMPL_DEFAULT =MapHash<String,SharedPtr<PureBase> >
15 #elif !defined(EVO_CORTEX_NO_DEFAULT)
16  #include "maplist.h"
17  #define EVO_CORTEX_IMPL_DEFAULT =MapList<String,SharedPtr<PureBase> >
18 #else
19  #define EVO_CORTEX_IMPL_DEFAULT
20 #endif
21 
31 #define EVO_CORTEX_MAPLIST(KEY, BASE) evo::Cortex< evo::MapList<KEY, evo::SharedPtr<BASE> > >
32 
42 #define EVO_CORTEX_MAPHASH(KEY, BASE) evo::Cortex< evo::MapHash<KEY, evo::SharedPtr<BASE> > >
43 
44 namespace evo {
47 
48 #if !defined(EVO_CORTEX_DYNAMIC_CAST)
49 
54  #define EVO_CORTEX_DYNAMIC_CAST 1
55 #endif
56 
58 #if EVO_CORTEX_DYNAMIC_CAST
59  #define EVO_CORTEX_IMPL_CAST dynamic_cast
60 #else
61  #define EVO_CORTEX_IMPL_CAST static_cast
62 #endif
63 
65 
171 template<class T EVO_CORTEX_IMPL_DEFAULT>
172 class Cortex : public T {
173 public:
174  typedef Cortex<T> This;
175  typedef T MapType;
176  typedef typename T::Value::Item ValueBase;
177 
178 #if defined(EVO_OLDCC)
179  typedef typename T::Key Key;
180  typedef typename T::Value Value;
181 #else
182  using typename T::Key;
183  using typename T::Value;
184 #endif
185 
187  Cortex() {
188  #if defined(EVO_CPP11)
189  static_assert(IsSmartPtr<Value>::value, "Cortex Map Value must be a SharedPtr to a type derived from evo::PureBase");
190  #else
191  STATIC_ASSERT( IsSmartPtr<Value>::value, ERROR__Cortex_Map_Value_must_be_a_SharedPtr_to_type_derived_from_PureBase );
192  #endif
193  }
194 
202  template<class ItemT>
203  This& create(const Key& key) {
204  bool created = false;
205  Value& item = MapType::get(key, &created);
206  if (created)
207  item = new ItemT;
208  return *this;
209  }
210 
222  template<class ItemT>
223  ItemT& get_create(const Key& key) {
224  bool created = false;
225  Value& item = MapType::get(key, &created);
226  if (created)
227  item = new ItemT;
228  return *(EVO_CORTEX_IMPL_CAST <ItemT*>(item.ptr()));
229  }
230 
241  template<class ItemT>
242  ItemT* getptr(const Key& key) {
243  Value* item = MapType::findM(key);
244  if (item == NULL)
245  return NULL;
246  return EVO_CORTEX_IMPL_CAST <ItemT*>(item->ptr());
247  }
248 
259  template<class ItemT>
260  const ItemT* getptr_const(const Key& key) const {
261  const Value* item = MapType::find(key);
262  if (item == NULL)
263  return NULL;
264  return EVO_CORTEX_IMPL_CAST <const ItemT*>(item->ptr());
265  }
266 
267 private:
268  Cortex(const Cortex&) EVO_ONCPP11(= delete);
269  Cortex& operator=(const Cortex&) EVO_ONCPP11(= delete);
270 };
271 
273 
367  virtual ~CortexModuleBase() {
368  }
369 
379  template<class T>
380  T& as() {
381  return * EVO_CORTEX_IMPL_CAST <T*>(this);
382  }
383 };
384 
386 
393 
395 
406 template<class T>
418  template<class U>
419  static T& get(U& ptr) {
420  if (!ptr)
421  ptr = new T;
422  return *EVO_CORTEX_IMPL_CAST <T*>(ptr.ptr());
423  }
424 
435  template<class U>
436  static T* getptr(U& ptr) {
437  if (ptr)
438  return EVO_CORTEX_IMPL_CAST <T*>(ptr.ptr());
439  return NULL;
440  }
441 };
442 
444 
445 #undef EVO_CORTEX_IMPL_CAST
446 
448 
449 }
450 #endif
Check if type is a SmartPtr or SharedPtr.
Definition: ptr.h:1093
ItemT * getptr(const Key &key)
Get stored item pointer using key (mutable).
Definition: cortex.h:242
Evo Smart Pointers.
Helper for getting a concrete module from a CortexModulePtr (or similar pointer). ...
Definition: cortex.h:407
Cortex< T > This
This type.
Definition: cortex.h:174
Cortex()
Constructor.
Definition: cortex.h:187
A Map with a collection of independent abstract context items.
Definition: cortex.h:172
Evo MapHash container.
#define EVO_ONCPP11(EXPR)
Compile EXPR only if C++11 support is detected, otherwise this is a no-op.
Definition: sys.h:259
SharedPtr< CortexModuleBase > CortexModulePtr
Smart/Shared pointer to a class inheriting CortexModuleBase.
Definition: cortex.h:392
Evo MapList container.
static T * getptr(U &ptr)
Get module pointer from stored pointer.
Definition: cortex.h:436
A simple base class for defining a module using a similar pattern to Cortex, but without the dynamic ...
Definition: cortex.h:365
const ItemT * getptr_const(const Key &key) const
Get stored item pointer using key (const).
Definition: cortex.h:260
T MapType
Map type (from T)
Definition: cortex.h:175
virtual ~CortexModuleBase()
Destructor.
Definition: cortex.h:367
T & as()
Get this module as a concrete type.
Definition: cortex.h:380
Evo C++ Library namespace.
Definition: alg.h:11
T::Value::Item ValueBase
Value base type, normally PureBase.
Definition: cortex.h:176
This & create(const Key &key)
Create item for key, if needed.
Definition: cortex.h:203
Shared smart pointer to single object.
Definition: ptr.h:399
#define STATIC_ASSERT(EXP, TOKEN)
Assert compile-time expression is true or trigger compiler error.
Definition: meta.h:54
ItemT & get_create(const Key &key)
Get stored item using key, create if needed.
Definition: cortex.h:223