voxelTerrain
 All Classes Functions Variables Typedefs Enumerations Pages
base.hpp
1 #ifndef PROCEDURAL_VOXEL_SIMPLE_BASE_HPP
2 #define PROCEDURAL_VOXEL_SIMPLE_BASE_HPP
3 
4 #include "blub/async/mutexReadWrite.hpp"
5 #include "blub/async/predecl.hpp"
6 #include "blub/core/bind.hpp"
7 #include "blub/core/globals.hpp"
8 #include "blub/core/hashMap.hpp"
9 #include "blub/core/signal.hpp"
10 #include "blub/async/dispatcher.hpp"
11 #include "blub/async/strand.hpp"
12 #include "blub/math/vector3int.hpp"
13 #include "blub/procedural/log/global.hpp"
14 #include "blub/procedural/predecl.hpp"
15 
16 #include <functional>
17 
18 
19 namespace blub
20 {
21 namespace procedural
22 {
23 namespace voxel
24 {
25 namespace simple
26 {
27 
28 
34 template <class tileType>
35 class base : public noncopyable
36 {
37 public:
38  typedef tileType t_tile;
39  typedef sharedPointer<t_tile> t_tilePtr;
40 
41  typedef base<t_tile> t_thisClass;
42 
46 
47  typedef std::function<t_tilePtr ()> t_createTileCallback;
48 
57  ~base();
58 
65  void lockForEdit();
71  void unlockForEdit();
75  void lockForRead();
79  void unlockRead();
80 
85  const t_tilesGotChangedMap &getTilesThatGotEdited() const;
86 
92  void setCreateTileCallback(const t_createTileCallback &callback);
93 
101 
107  t_sigEditDone* signalEditDone();
108 
109 protected:
116  void addToChangeList(const t_tileId& id, t_tilePtr toAdd);
117 
122  virtual bool tryLockForEditMaster();
126  virtual void lockForEditMaster();
130  virtual void unlockForEditMaster();
131 
136  virtual t_tilePtr createTile() const;
137 
138 protected:
149 
150  t_tilesGotChangedMap m_tilesThatGotEdited;
151 
152  t_createTileCallback m_createTileCallback;
153 
154  async::mutexReadWrite m_classLocker;
155 
156  t_sigEditDone m_sigEditDone;
157 };
158 
159 template <class tileType>
161  : m_master(worker)
162  , m_worker(worker)
163 // , m_createTileCallback(blub::bind(&t_tile::create)) // TODO good idea, techn difficult, via config
164 {
165  ;
166 }
167 
168 template <class tileType>
170 {
171 }
172 
173 template <class tileType>
175 {
176  m_master.post(boost::bind(&base::lockForEditMaster, this));
177 }
178 
179 template <class tileType>
181 {
182  m_master.post(boost::bind(&base::unlockForEditMaster, this));
183 }
184 
185 template <class tileType>
187 {
188  m_classLocker.lockForRead();
189 }
190 
191 template <class tileType>
193 {
194  m_classLocker.unlockRead();
195 }
196 
197 template <class tileType>
199 {
200  return m_tilesThatGotEdited;
201 }
202 
203 template <class tileType>
204 void base<tileType>::setCreateTileCallback(const t_createTileCallback &callback)
205 {
206  m_createTileCallback = callback;
207 }
208 
209 template <class tileType>
211 {
212  BASSERT(!m_classLocker.tryLockForWrite());
213  m_tilesThatGotEdited.insert(id, toAdd);
214 }
215 
216 template <class tileType>
218 {
219  const bool result(m_classLocker.tryLockForWrite());
220  if (result)
221  {
222  m_tilesThatGotEdited.clear();
223  }
224  return result;
225 }
226 
227 template <class tileType>
229 {
230  m_classLocker.lockForWrite();
231 
232  m_tilesThatGotEdited.clear();
233 }
234 
235 template <class tileType>
237 {
238  m_classLocker.unlock();
239 #ifdef BLUB_LOG_VOXEL
240  BLUB_PROCEDURAL_LOG_OUT() << "simple master unlock m_tilesThatGotEdited.size():" << m_tilesThatGotEdited.size();
241 #endif
242  if (!m_tilesThatGotEdited.empty())
243  {
244  m_sigEditDone();
245  }
246 }
247 
248 template <class tileType>
250 {
251  return m_createTileCallback();
252 }
253 
254 template <class tileType>
256 {
257  return m_master;
258 }
259 
260 template <class tileType>
262 {
263  return &m_sigEditDone;
264 }
265 
266 }
267 }
268 }
269 }
270 
271 
272 #endif // PROCEDURAL_VOXEL_SIMPLE_BASE_HPP
virtual void lockForEditMaster()
lockForEditMaster locks for write, or waits until possible. Call by master dispatcher.
Definition: base.hpp:228
vector3int32 t_tileId
Definition: base.hpp:44
virtual t_tilePtr createTile() const
createTile creates a new Tile. Uses callback set by setCreateTileCallback()
Definition: base.hpp:249
virtual bool tryLockForEditMaster()
tryLockForEditMaster tries to lock for write. Call by master dispatcher.
Definition: base.hpp:217
virtual void unlockForEditMaster()
unlockForEditMaster unlocks write. Call by master dispatcher.
Definition: base.hpp:236
base(blub::async::dispatcher &worker)
base constructor
Definition: base.hpp:160
Definition: strand.hpp:16
const t_tilesGotChangedMap & getTilesThatGotEdited() const
getTilesThatGotEdited returns a list of tiles which changed since the last call lockForEdit() / lockF...
Definition: base.hpp:198
Definition: sharedPointer.hpp:12
void addToChangeList(const t_tileId &id, t_tilePtr toAdd)
addToChangeList adds a tile to the change-list.
Definition: base.hpp:210
void unlockRead()
unlockRead unlocks the class after reading.
Definition: base.hpp:192
blub::async::strand & getMaster()
getMaster returns the master dispatcher. The master synchronises jobs for the worker-thread and write...
Definition: base.hpp:255
Definition: dispatcher.hpp:29
void lockForRead()
lockForRead locks the class for reading.
Definition: base.hpp:186
void lockForEdit()
lockForEdit locks the class for editing/writing it. Call unlockForEdit() after work done...
Definition: base.hpp:174
t_sigEditDone * signalEditDone()
signalEditDone gets called after unlockForEdit() got called.
Definition: base.hpp:261
blub::async::dispatcher & m_worker
m_worker use it to dispatch heavy work. Don't write to class member with it. Do not use any locks...
Definition: base.hpp:148
~base()
~base destructor
Definition: base.hpp:169
void unlockForEdit()
unlockForEdit unlocks edit/write-lock and calls signalEditDone() . Method executes unlock by the disp...
Definition: base.hpp:180
Definition: deadlineTimer.hpp:10
blub::async::strand m_master
m_master The master synchronises jobs for the worker-thread and writes to class member. The master calls all methods which end with *Master.
Definition: base.hpp:143
void setCreateTileCallback(const t_createTileCallback &callback)
setCreateTileCallback sets a callback for creating tiles. Use this method if you want to create custo...
Definition: base.hpp:204
Definition: mutexReadWrite.hpp:15