ANNA Suite  2020b
Multipurpose development suite for Telco applications
ObjectFacade.hpp
Go to the documentation of this file.
1 // ANNA - Anna is Not Nothingness Anymore //
2 // //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
4 // //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7 
8 
9 #ifndef anna_dbos_ObjectFacade_hpp
10 #define anna_dbos_ObjectFacade_hpp
11 
13 
15 
17 #include <anna/dbos/Creator.hpp>
18 #include <anna/dbos/Loader.hpp>
19 #include <anna/dbos/Recorder.hpp>
20 #include <anna/dbos/Eraser.hpp>
21 
22 namespace anna {
23 
24 namespace dbms {
25 class Connection;
26 }
27 
28 namespace dbos {
29 
30 class Object;
31 
49 template <typename T> class ObjectFacade {
50 public:
57  static StorageId getStorageAreaId() { return (StorageId) anna_ptrnumber_cast(&st_storageArea); }
58 
63  static StorageArea* getStorageArea() { return st_storageArea; }
64 
73  static void setStorageArea(StorageArea* storageArea) {
74  (st_storageArea = storageArea)->setSizeof(sizeof(T));
75  }
76 
98  static T* instance(dbms::Connection& connection, Loader& loader)
99  noexcept(false) {
100  if(st_storageArea == NULL) {
101  std::string msg(loader.asString());
102  msg += " | ObjectFacade uninitialized ";
104  }
105 
106  return static_cast <T*>(st_storageArea->instance(connection, loader));
107  }
108 
127  static T* instance(Loader& loader)
128  noexcept(false) {
129  if(st_storageArea == NULL) {
130  std::string msg(loader.asString());
131  msg += " | ObjectFacade uninitialized ";
133  }
134 
135  return static_cast <T*>(st_storageArea->instance(loader));
136  }
137 
160  static T* instance(dbms::Connection& connection, CrossedLoader& crossedLoader, Loader& loader)
161  noexcept(false) {
162  if(st_storageArea == NULL) {
163  std::string msg(loader.asString());
164  msg += " | ObjectFacade uninitialized ";
166  }
167 
168  return static_cast <T*>(st_storageArea->instance(connection, crossedLoader, loader));
169  }
170 
189  static T* create(dbms::Connection& connection, Creator& creator)
190  noexcept(false) {
191  if(st_storageArea == NULL) {
192  std::string msg(creator.asString());
193  msg += " | ObjectFacade uninitialized ";
195  }
196 
197  return static_cast <T*>(st_storageArea->create(connection, creator));
198  }
199 
216  static T* find(Loader& loader)
217  noexcept(false) {
218  if(st_storageArea == NULL) {
219  std::string msg(loader.asString());
220  msg += " | ObjectFacade uninitialized ";
222  }
223 
224  return static_cast <T*>(st_storageArea->find(loader));
225  }
226 
240  static void release(T*& t)
241  {
242  if(st_storageArea == NULL)
243  return;
244 
245  try {
246  st_storageArea->release(reinterpret_cast <Object**>(&t));
247  } catch(RuntimeException& ex) {
248  ex.trace();
249  }
250  }
251 
255  static void clear()
256  noexcept(false) {
257  if(st_storageArea == NULL)
258  throw RuntimeException("ObjectFacade uninitialized ", ANNA_FILE_LOCATION);
259 
260  st_storageArea->clear();
261  }
262 
274  static T* duplicate(const T* t)
275  noexcept(false) {
276  if(st_storageArea == NULL)
277  throw RuntimeException("ObjectFacade uninitialized ", ANNA_FILE_LOCATION);
278 
279  return static_cast <T*>(st_storageArea->duplicate(t));
280  }
281 
290  static bool isLoaded(const Loader& loader)
291  noexcept(false) {
292  if(st_storageArea == NULL)
293  throw RuntimeException("ObjectFacade uninitialized ", ANNA_FILE_LOCATION);
294 
295  return st_storageArea->isLoaded(loader);
296  }
297 
305  static void apply(dbms::Connection& connection, Recorder& recorder)
306  noexcept(false) {
307  if(st_storageArea == NULL) {
308  std::string msg(recorder.asString());
309  msg += " | ObjectFacade uninitialized";
311  }
312 
313  st_storageArea->apply(connection, recorder);
314  }
315 
325  static void apply(dbms::Connection& connection, Eraser& eraser)
326  noexcept(false) {
327  if(st_storageArea == NULL) {
328  std::string msg(eraser.asString());
329  msg += " | ObjectFacade uninitialized";
331  }
332 
333  st_storageArea->apply(connection, eraser);
334  }
335 
347  static void erase(T*& t)
348  noexcept(false) {
349  if(st_storageArea == NULL)
350  return;
351 
352  st_storageArea->erase(reinterpret_cast <Object**>(&t));
353  }
354 
359  static T* data(StorageArea::iterator& ii) { return static_cast <T*>(StorageArea::data(ii)); }
360 
365  static const T* data(StorageArea::const_iterator& ii) { return static_cast <const T*>(StorageArea::data(ii)); }
366 
373  static Object* allocator() { return new T; }
374 
375 protected:
377 
382 };
383 
384 }
385 }
386 
393 #define dbos_prepare_object(T) \
394  template <> anna::dbos::StorageArea* anna::dbos::ObjectFacade< T >::st_storageArea = NULL
395 
402 #define dbos_declare_object(T) \
403  friend class anna::dbos::ObjectFacade <T>
404 
405 
406 #endif
static void erase(T *&t) noexcept(false)
Definition: ObjectFacade.hpp:347
#define anna_ptrnumber_cast(pointer)
Definition: defines.hpp:118
Definition: Connection.hpp:38
static void clear() noexcept(false)
Definition: ObjectFacade.hpp:255
static void apply(dbms::Connection &connection, Recorder &recorder) noexcept(false)
Definition: ObjectFacade.hpp:305
static bool isLoaded(const Loader &loader) noexcept(false)
Definition: ObjectFacade.hpp:290
static T * instance(Loader &loader) noexcept(false)
Definition: ObjectFacade.hpp:127
Definition: Object.hpp:35
static Object * allocator()
Definition: ObjectFacade.hpp:373
static StorageId getStorageAreaId()
Definition: ObjectFacade.hpp:57
void trace() const
static void apply(dbms::Connection &connection, Eraser &eraser) noexcept(false)
Definition: ObjectFacade.hpp:325
ObjectFacade()
Definition: ObjectFacade.hpp:381
static T * instance(dbms::Connection &connection, CrossedLoader &crossedLoader, Loader &loader) noexcept(false)
Definition: ObjectFacade.hpp:160
Definition: Creator.hpp:22
Definition: Recorder.hpp:22
Definition: CrossedLoader.hpp:42
static T * create(dbms::Connection &connection, Creator &creator) noexcept(false)
Definition: ObjectFacade.hpp:189
static StorageArea * getStorageArea()
Definition: ObjectFacade.hpp:63
static T * duplicate(const T *t) noexcept(false)
Definition: ObjectFacade.hpp:274
Definition: Loader.hpp:28
Definition: ObjectFacade.hpp:49
static void release(T *&t)
Definition: ObjectFacade.hpp:240
static const T * data(StorageArea::const_iterator &ii)
Definition: ObjectFacade.hpp:365
Definition: app.hpp:12
static T * data(StorageArea::iterator &ii)
Definition: ObjectFacade.hpp:359
std::map< Index, Instance * >::iterator iterator
Definition: StorageArea.hpp:136
ptrnumber StorageId
Definition: defines.hpp:19
std::map< Index, Instance * >::const_iterator const_iterator
Definition: StorageArea.hpp:137
Definition: Eraser.hpp:24
#define ANNA_FILE_LOCATION
Definition: defines.hpp:23
static T * instance(dbms::Connection &connection, Loader &loader) noexcept(false)
Definition: ObjectFacade.hpp:98
Definition: StorageArea.hpp:58
static T * find(Loader &loader) noexcept(false)
Definition: ObjectFacade.hpp:216
Definition: RuntimeException.hpp:23
static void setStorageArea(StorageArea *storageArea)
Definition: ObjectFacade.hpp:73
static StorageArea * st_storageArea
Definition: ObjectFacade.hpp:376