OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ptr.hpp
Go to the documentation of this file.
1 #ifndef GAME_MWWORLD_PTR_H
2 #define GAME_MWWORLD_PTR_H
3 
4 #include <cassert>
5 
6 #include <string>
7 #include <sstream>
8 
9 #include "livecellref.hpp"
10 
11 namespace MWWorld
12 {
13  class ContainerStore;
14  class CellStore;
15  struct LiveCellRefBase;
16 
18 
19  class Ptr
20  {
21  public:
22 
26 
27  public:
28  Ptr(MWWorld::LiveCellRefBase *liveCellRef=0, CellStore *cell=0)
29  : mRef(liveCellRef), mCell(cell), mContainerStore(0)
30  {
31  }
32 
33  bool isEmpty() const
34  {
35  return mRef == 0;
36  }
37 
38  const std::string& getTypeName() const;
39 
40  const Class& getClass() const
41  {
42  if(mRef != 0)
43  return *(mRef->mClass);
44  throw std::runtime_error("Cannot get class of an empty object");
45  }
46 
47  template<typename T>
49  {
51  if(ref) return ref;
52 
53  std::stringstream str;
54  str<< "Bad LiveCellRef cast to "<<typeid(T).name()<<" from ";
55  if(mRef != 0) str<< getTypeName();
56  else str<< "an empty object";
57 
58  throw std::runtime_error(str.str());
59  }
60 
62 
64 
65  RefData& getRefData() const;
66 
67  CellStore *getCell() const
68  {
69  assert(mCell);
70  return mCell;
71  }
72 
73  bool isInCell() const
74  {
75  return (mContainerStore == 0) && (mCell != 0);
76  }
77 
78  void setContainerStore (ContainerStore *store);
80 
83 
84  operator const void *();
86  };
87 
90  class ConstPtr
91  {
92  public:
93 
95  const CellStore *mCell;
97 
98  public:
99  ConstPtr(const MWWorld::LiveCellRefBase *liveCellRef=0, const CellStore *cell=0)
100  : mRef(liveCellRef), mCell(cell), mContainerStore(0)
101  {
102  }
103 
104  ConstPtr(const MWWorld::Ptr& ptr)
106  {
107  }
108 
109  bool isEmpty() const
110  {
111  return mRef == 0;
112  }
113 
114  const std::string& getTypeName() const;
115 
116  const Class& getClass() const
117  {
118  if(mRef != 0)
119  return *(mRef->mClass);
120  throw std::runtime_error("Cannot get class of an empty object");
121  }
122 
123  template<typename T>
124  const MWWorld::LiveCellRef<T> *get() const
125  {
126  const MWWorld::LiveCellRef<T> *ref = dynamic_cast<const MWWorld::LiveCellRef<T>*>(mRef);
127  if(ref) return ref;
128 
129  std::stringstream str;
130  str<< "Bad LiveCellRef cast to "<<typeid(T).name()<<" from ";
131  if(mRef != 0) str<< getTypeName();
132  else str<< "an empty object";
133 
134  throw std::runtime_error(str.str());
135  }
136 
137  const MWWorld::LiveCellRefBase *getBase() const;
138 
140  {
141  assert(mRef);
142  return mRef->mRef;
143  }
144 
145  const RefData& getRefData() const
146  {
147  assert(mRef);
148  return mRef->mData;
149  }
150 
151  const CellStore *getCell() const
152  {
153  assert(mCell);
154  return mCell;
155  }
156 
157  bool isInCell() const
158  {
159  return (mContainerStore == 0) && (mCell != 0);
160  }
161 
162  void setContainerStore (const ContainerStore *store);
164 
165  const ContainerStore *getContainerStore() const;
167 
168  operator const void *();
170  };
171 
172  inline bool operator== (const Ptr& left, const Ptr& right)
173  {
174  return left.mRef==right.mRef;
175  }
176 
177  inline bool operator!= (const Ptr& left, const Ptr& right)
178  {
179  return !(left==right);
180  }
181 
182  inline bool operator< (const Ptr& left, const Ptr& right)
183  {
184  return left.mRef<right.mRef;
185  }
186 
187  inline bool operator>= (const Ptr& left, const Ptr& right)
188  {
189  return !(left<right);
190  }
191 
192  inline bool operator> (const Ptr& left, const Ptr& right)
193  {
194  return right<left;
195  }
196 
197  inline bool operator<= (const Ptr& left, const Ptr& right)
198  {
199  return !(left>right);
200  }
201 
202  inline bool operator== (const ConstPtr& left, const ConstPtr& right)
203  {
204  return left.mRef==right.mRef;
205  }
206 
207  inline bool operator!= (const ConstPtr& left, const ConstPtr& right)
208  {
209  return !(left==right);
210  }
211 
212  inline bool operator< (const ConstPtr& left, const ConstPtr& right)
213  {
214  return left.mRef<right.mRef;
215  }
216 
217  inline bool operator>= (const ConstPtr& left, const ConstPtr& right)
218  {
219  return !(left<right);
220  }
221 
222  inline bool operator> (const ConstPtr& left, const ConstPtr& right)
223  {
224  return right<left;
225  }
226 
227  inline bool operator<= (const ConstPtr& left, const ConstPtr& right)
228  {
229  return !(left>right);
230  }
231 }
232 
233 #endif
MWWorld::LiveCellRefBase * getBase() const
Definition: ptr.cpp:16
Used to create pointers to hold any type of LiveCellRef<> object.
Definition: livecellref.hpp:22
Encapsulated variant of ESM::CellRef with change tracking.
Definition: cellref.hpp:15
const Class & getClass() const
Definition: ptr.hpp:40
const std::string & getTypeName() const
Definition: ptr.cpp:9
bool operator==(const LiveCellRef< X > &ref, int pRefnum)
Definition: cellstore.cpp:207
const Class & getClass() const
Definition: ptr.hpp:116
bool isInCell() const
Definition: ptr.hpp:73
const MWWorld::CellRef & getCellRef() const
Definition: ptr.hpp:139
MWWorld::CellRef mRef
Definition: livecellref.hpp:29
ContainerStore * mContainerStore
Definition: ptr.hpp:25
Definition: livecellref.hpp:77
const MWWorld::LiveCellRefBase * mRef
Definition: ptr.hpp:94
void setContainerStore(const ContainerStore *store)
Must not be called on references that are in a cell.
Definition: ptr.cpp:73
const Class * mClass
Definition: livecellref.hpp:24
const MWWorld::LiveCellRefBase * getBase() const
Definition: ptr.cpp:65
Definition: refdata.hpp:29
RefData mData
Definition: livecellref.hpp:32
CellStore * mCell
Definition: ptr.hpp:24
const CellStore * getCell() const
Definition: ptr.hpp:151
Base class for referenceable esm records.
Definition: class.hpp:51
const ContainerStore * mContainerStore
Definition: ptr.hpp:96
MWWorld::LiveCellRefBase * mRef
Definition: ptr.hpp:23
const RefData & getRefData() const
Definition: ptr.hpp:145
Mutable state of a cell.
Definition: cellstore.hpp:51
CellStore * getCell() const
Definition: ptr.hpp:67
Pointer to a const LiveCellRef.
Definition: ptr.hpp:90
bool operator<=(const Ptr &left, const Ptr &right)
Definition: ptr.hpp:197
Definition: containerstore.hpp:48
ConstPtr(const MWWorld::LiveCellRefBase *liveCellRef=0, const CellStore *cell=0)
Definition: ptr.hpp:99
const ContainerStore * getContainerStore() const
May return a 0-pointer, if reference is not in a container.
Definition: ptr.cpp:81
ConstPtr(const MWWorld::Ptr &ptr)
Definition: ptr.hpp:104
bool operator>(const Ptr &left, const Ptr &right)
Definition: ptr.hpp:192
bool operator<(const Ptr &left, const Ptr &right)
Definition: ptr.hpp:182
ContainerStore * getContainerStore() const
May return a 0-pointer, if reference is not in a container.
Definition: ptr.cpp:46
const std::string & getTypeName() const
Definition: ptr.cpp:58
bool isEmpty() const
Definition: ptr.hpp:33
bool isInCell() const
Definition: ptr.hpp:157
bool isEmpty() const
Definition: ptr.hpp:109
Pointer to a LiveCellRef.
Definition: ptr.hpp:19
bool operator!=(const CellStore &left, const CellStore &right)
Definition: cellstore.cpp:930
MWWorld::CellRef & getCellRef() const
Definition: ptr.cpp:24
RefData & getRefData() const
Definition: ptr.cpp:31
void setContainerStore(ContainerStore *store)
Must not be called on references that are in a cell.
Definition: ptr.cpp:38
const CellStore * mCell
Definition: ptr.hpp:95
bool operator>=(const Ptr &left, const Ptr &right)
Definition: ptr.hpp:187
const char * name
Definition: crashcatcher.cpp:67
Ptr(MWWorld::LiveCellRefBase *liveCellRef=0, CellStore *cell=0)
Definition: ptr.hpp:28