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  const ContainerStore *getContainerStore() const;
164 
165  operator const void *();
167  };
168 
169  inline bool operator== (const Ptr& left, const Ptr& right)
170  {
171  return left.mRef==right.mRef;
172  }
173 
174  inline bool operator!= (const Ptr& left, const Ptr& right)
175  {
176  return !(left==right);
177  }
178 
179  inline bool operator< (const Ptr& left, const Ptr& right)
180  {
181  return left.mRef<right.mRef;
182  }
183 
184  inline bool operator>= (const Ptr& left, const Ptr& right)
185  {
186  return !(left<right);
187  }
188 
189  inline bool operator> (const Ptr& left, const Ptr& right)
190  {
191  return right<left;
192  }
193 
194  inline bool operator<= (const Ptr& left, const Ptr& right)
195  {
196  return !(left>right);
197  }
198 
199  inline bool operator== (const ConstPtr& left, const ConstPtr& right)
200  {
201  return left.mRef==right.mRef;
202  }
203 
204  inline bool operator!= (const ConstPtr& left, const ConstPtr& right)
205  {
206  return !(left==right);
207  }
208 
209  inline bool operator< (const ConstPtr& left, const ConstPtr& right)
210  {
211  return left.mRef<right.mRef;
212  }
213 
214  inline bool operator>= (const ConstPtr& left, const ConstPtr& right)
215  {
216  return !(left<right);
217  }
218 
219  inline bool operator> (const ConstPtr& left, const ConstPtr& right)
220  {
221  return right<left;
222  }
223 
224  inline bool operator<= (const ConstPtr& left, const ConstPtr& right)
225  {
226  return !(left>right);
227  }
228 }
229 
230 #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:205
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
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:52
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:53
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:194
Definition: containerstore.hpp:41
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:73
ConstPtr(const MWWorld::Ptr &ptr)
Definition: ptr.hpp:104
bool operator>(const Ptr &left, const Ptr &right)
Definition: ptr.hpp:189
bool operator<(const Ptr &left, const Ptr &right)
Definition: ptr.hpp:179
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:935
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:184
const char * name
Definition: crashcatcher.cpp:59
Ptr(MWWorld::LiveCellRefBase *liveCellRef=0, CellStore *cell=0)
Definition: ptr.hpp:28