OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
aistate.hpp
Go to the documentation of this file.
1 #ifndef AISTATE_H
2 #define AISTATE_H
3 
4 #include <typeinfo>
5 #include <stdexcept>
6 
7 namespace MWMechanics
8 {
9 
15  template< class Base >
16  class DerivedClassStorage
17  {
18  private:
19  Base* mStorage;
20 
21  //if needed you have to provide a clone member function
24 
25  public:
27  template< class Derived >
28  Derived& get()
29  {
30  Derived* result = dynamic_cast<Derived*>(mStorage);
31 
32  if(!result)
33  {
34  if(mStorage)
35  delete mStorage;
36  mStorage = result = new Derived();
37  }
38 
39  //return a reference to the (new allocated) object
40  return *result;
41  }
42 
43  template< class Derived >
44  void store( const Derived& payload )
45  {
46  if(mStorage)
47  delete mStorage;
48  mStorage = new Derived(payload);
49  }
50 
52  template< class Derived >
53  void moveIn( Derived* p )
54  {
55  if(mStorage)
56  delete mStorage;
57  mStorage = p;
58  }
59 
60  bool empty() const
61  {
62  return mStorage == NULL;
63  }
64 
65  const std::type_info& getType() const
66  {
67  return typeid(mStorage);
68  }
69 
70 
73  {
74  if(mStorage)
75  delete mStorage;
76  }
77 
78 
79 
80  };
81 
82 
84 
91  {
92  virtual ~AiTemporaryBase(){}
93  };
94 
96  typedef DerivedClassStorage<AiTemporaryBase> AiState;
97 }
98 
99 #endif // AISTATE_H
base class for the temporary storage of AiPackages.
Definition: aistate.hpp:90
DerivedClassStorage()
Definition: aistate.hpp:71
stores one object of any class derived from Base. Requesting a certain derived class via get() either...
Definition: aisequence.hpp:28
void moveIn(Derived *p)
takes ownership of the passed object
Definition: aistate.hpp:53
DerivedClassStorage< AiTemporaryBase > AiState
Container for AI package status.
Definition: aisequence.hpp:29
virtual ~AiTemporaryBase()
Definition: aistate.hpp:92
void store(const Derived &payload)
Definition: aistate.hpp:44
bool empty() const
Definition: aistate.hpp:60
Base * mStorage
Definition: aistate.hpp:19
~DerivedClassStorage()
Definition: aistate.hpp:72
DerivedClassStorage & operator=(const DerivedClassStorage &)
const std::type_info & getType() const
Definition: aistate.hpp:65