BALL  1.4.79
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
persistenceManager.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_CONCEPT_PERSISTENCEMANAGER_H
6 #define BALL_CONCEPT_PERSISTENCEMANAGER_H
7 
8 #ifndef BALL_COMMON_RTTI_H
9 # include <BALL/COMMON/rtti.h>
10 #endif
11 
12 #ifndef BALL_DATATYPE_HASHMAP_H
13 # include <BALL/DATATYPE/hashMap.h>
14 #endif
15 
16 #ifndef BALL_DATATYPE_STRINGHASHMAP_H
18 #endif
19 
20 #ifndef BALL_DATATYPE_HASHSET_H
21 # include <BALL/DATATYPE/hashSet.h>
22 #endif
23 
24 #ifndef BALL_CONCEPT_PERSISTENTOBJECT_H
26 #endif
27 
28 #ifndef BALL_COMMON_GLOBAL_H
29 # include <BALL/COMMON/global.h>
30 #endif
31 
32 #include <fstream>
33 #include <iomanip>
34 
35 #include <boost/shared_ptr.hpp>
36 
37 #define BALL_WRITE_PRIMITIVE_MEMBER(pm,x) pm.writePrimitive(x,#x)
38 #define BALL_READ_PRIMITIVE_MEMBER(pm,x) pm.readPrimitive(x,#x)
39 
40 namespace BALL
41 {
42 
73  {
74  public:
75 
79 
91  typedef void * (*CreateMethod) ();
93 
97 
101 
105 
109  PersistenceManager(::std::istream& is);
110 
114  PersistenceManager(::std::ostream& os);
115 
120  PersistenceManager(::std::istream& is, ::std::ostream& os);
121 
127  virtual ~PersistenceManager();
128 
130 
134 
159  virtual void registerClass(String signature, const CreateMethod m);
160 
171  virtual void* createObject(String signature) const;
172 
177  virtual Size getNumberOfClasses() const;
178 
182  virtual void setOstream(::std::ostream& s);
183 
187  virtual void setIstream(::std::istream& s);
188 
197  void startOutput();
198 
210  void endOutput();
211 
223  PersistentObject* readObject();
224 
229 
234 
236 
240 
247  template <typename T>
248  bool checkObjectHeader(const T& /* object */, const char* name = 0);
249 
254  bool checkObjectHeader(const char* type_name);
255 
262  template <typename T>
263  void writeObjectHeader(const T* object, const char* name = 0);
264 
268  void writeObjectTrailer(const char* name = 0);
269 
273  bool checkObjectTrailer(const char* name = 0);
274 
281  template <typename T>
282  void writePrimitive(const T& t, const char* name);
283 
290  template <typename T>
291  bool readPrimitive(T& t, const char* name);
292 
298  template <typename T>
299  void writeStorableObject(const T& t, const char* name);
300 
307  template <typename T>
308  bool readStorableObject(T& t, const char* name);
309 
315  template <typename T>
316  void writeObjectPointer(const T* object, const char* name);
317 
324  template <typename T>
325  bool readObjectPointer(T*& object, const char* name);
326 
334  template <typename T>
335  bool readObjectSmartPointer(boost::shared_ptr<T>& s_ptr, const char* name);
336 
342  template <typename T>
343  void writeObjectReference(const T& object, const char* name);
344 
351  template <typename T>
352  bool readObjectReference(T& object, const char* name);
353 
361  template <typename T>
362  void writeObjectArray(const T* array, const char* name, Size size);
363 
371  template <typename T>
372  bool readObjectArray(const T* array, const char* name, Size& size);
373 
381  template <typename T>
382  void writeObjectPointerArray(T** arr, const char* name, const Size size);
383 
391  template <typename T>
392  bool readObjectPointerArray(T** array, const char* name, Size& size);
393 
395 
399 
414  virtual void writeHeader(const char* type_name, const char* name,
415  LongSize ptr) = 0;
416 
426  virtual bool checkHeader(const char* type_name, const char* name,
427  LongSize& ptr) = 0;
428 
432  virtual void writeTrailer(const char* name = 0) = 0;
433 
439  virtual bool checkTrailer(const char* name = 0) = 0;
440 
441 
444  virtual void writeStreamHeader() = 0;
445 
446 
449  virtual void writeStreamTrailer() = 0;
450 
451 
455  virtual bool checkStreamHeader() = 0;
456 
457 
461  virtual bool checkStreamTrailer() = 0;
462 
463 
469  virtual bool getObjectHeader(String& type_name, LongSize& ptr) = 0;
470 
471 
475  virtual void writeName(const char* name) = 0;
476 
477 
482  virtual bool checkName(const char* name) = 0;
483 
484 
489  virtual void writeStorableHeader(const char* type_name,
490  const char* name) = 0;
491 
496  virtual bool checkStorableHeader(const char* type_name,
497  const char* name) = 0;
498 
501  virtual void writeStorableTrailer() = 0;
502 
503 
507  virtual bool checkStorableTrailer() = 0;
508 
509 
514  virtual void writePrimitiveHeader(const char* type_name,
515  const char* name) = 0;
516 
522  virtual bool checkPrimitiveHeader(const char* type_name,
523  const char* name) = 0;
524 
527  virtual void writePrimitiveTrailer() = 0;
528 
529 
533  virtual bool checkPrimitiveTrailer() = 0;
534 
535 
540  virtual void writeObjectPointerHeader(const char* type_name,
541  const char* name) = 0;
542 
543 
549  virtual bool checkObjectPointerHeader(const char* type_name,
550  const char* name) = 0;
551 
552 
557  virtual void writeObjectReferenceHeader(const char* type_name,
558  const char* name) = 0;
559 
560 
566  virtual bool checkObjectReferenceHeader(const char* type_name,
567  const char* name) = 0;
568 
569 
575  virtual void writeObjectPointerArrayHeader(const char* type_name,
576  const char* name, Size size) = 0;
577 
578 
585  virtual bool checkObjectPointerArrayHeader(const char* type_name,
586  const char* name, Size& size) = 0;
587 
588 
591  virtual void writeObjectPointerArrayTrailer() = 0;
592 
593 
597  virtual bool checkObjectPointerArrayTrailer() = 0;
598 
599 
602  virtual void initializeOutputStream();
603 
604 
607  virtual void finalizeOutputStream();
608 
609 
612  virtual void initializeInputStream();
613 
614 
617  virtual void finalizeInputStream();
618 
620 
639 
642  virtual void put(const char c) = 0;
643 
646  virtual void put(const Byte c) = 0;
647 
650  virtual void put(const Index i) = 0;
651 
654  virtual void put(const Size p) = 0;
655 
658  virtual void put(const bool b) = 0;
659 
662  virtual void put(const Real f) = 0;
663 
666  virtual void put(const DoubleReal d) = 0;
667 
670  virtual void put(const string& s) = 0;
671 
674  virtual void put(const LongSize p) = 0;
675 
677 
681 
684  virtual void get(char& c) = 0;
685 
688  virtual void get(Byte& b) = 0;
689 
692  virtual void get(Index& s) = 0;
693 
696  virtual void get(Size& s) = 0;
697 
700  virtual void get(bool& b) = 0;
701 
704  virtual void get(Real& f) = 0;
705 
708  virtual void get(DoubleReal& d) = 0;
709 
712  virtual void get(string& s) = 0;
713 
716  virtual void get(LongSize& p) = 0;
717 
719 
720  protected:
721 
722  /*_ Register all BALL kernel classes.
723  This method is automatically called in the constructor.
724  */
725  void registerKernelClasses_();
726 
727  /*_
728  */
729  void addPointerPair_(LongSize old_ptr, void* new_ptr);
730 
731  /*_
732  * \throws Exception::GeneralException
733  */
734  void addNeededObjects_();
735 
736  /*_
737  */
738  bool updatePointers_();
739 
740  /*_
741  */
743 
744  /*_
745  */
746  typedef std::list<const PersistentObject*> ObjectList;
747 
748  /*_
749  */
751 
752  /*_
753  */
754  typedef std::list<std::pair<void**, LongSize> > PointerList;
755 
756  /*_
757  */
758  typedef std::list<std::pair<boost::shared_ptr<PersistentObject>*, LongSize> > SmartPointerList;
759 
760  /*_
761  */
763 
764  /*_ a hash set containing the pointers of the
765  objects that were already written
766  */
768 
769  /*_ a list of object pointers that were referenced
770  by objects already written, but have not yet
771  been written themselves
772  */
774 
775  /*_ a map relating the pointers read from the stream (LongSize)
776  with the pointers of the persistent objects that were created
777  dynamically
778  */
780 
781  //_
783 
784  //_
786 
787  //_
789 
790  //_
791  ::std::ostream* ostr_;
792  //_
793  ::std::istream* istr_;
794  };
795 
796 
797  // implementation of templated methods
798 
799  template <typename T>
800  bool PersistenceManager::checkObjectHeader(const T& /* object */,
801  const char* name)
802  {
803  LongSize ptr;
804  return checkHeader(RTTI::getStreamName<T>(), name, ptr);
805  }
806 
807 
808  template <typename T>
810  const char* name)
811  {
812  object_out_.insert(object);
813  writeHeader(RTTI::getStreamName<T>(), name, (LongSize)reinterpret_cast<PointerSizeUInt>(object));
814  }
815 
816 
817  template <typename T>
818  void PersistenceManager::writePrimitive(const T& t, const char* name)
819  {
820  writePrimitiveHeader(RTTI::getStreamName<T>(), name);
821  put(t);
823  }
824 
825 
826  template <typename T>
827  bool PersistenceManager::readPrimitive(T& t, const char* name)
828  {
829  if (!checkPrimitiveHeader(RTTI::getStreamName<T>(), name))
830  {
831  return false;
832  }
833 
834  get(t);
835  return checkPrimitiveTrailer();
836  }
837 
838 
839  template <typename T>
840  void PersistenceManager::writeStorableObject(const T& t, const char* name)
841  {
842  writeStorableHeader(RTTI::getStreamName<T>(), name);
843  t.write(*this);
845  }
846 
847 
848  template <typename T>
849  bool PersistenceManager::readStorableObject(T& t, const char* name)
850  {
851  return (checkStorableHeader(RTTI::getStreamName<T>(), name)
852  && t.read(*this) && checkStorableTrailer());
853  }
854 
855 
856  template <typename T>
857  void PersistenceManager::writeObjectPointer(const T* object, const char* name)
858  {
859  if (object != 0 && !object_out_.has(object))
860  {
861  object_out_needed_.push_back(object);
862  }
863 
864  writeObjectPointerHeader(RTTI::getStreamName<T>(), name);
865  put(static_cast<LongSize>(reinterpret_cast<PointerSizeUInt>(object)));
867  }
868 
869 
870  template <typename T>
871  bool PersistenceManager::readObjectPointer(T*& object, const char* name)
872  {
873  if (!checkObjectPointerHeader(RTTI::getStreamName<T>(), name))
874  {
875  return false;
876  }
877 
878  LongSize ptr;
879  get(ptr);
880 
881  if (ptr != 0)
882  {
883  pointer_list_.push_back(std::make_pair((void**)&object, ptr));
884  }
885 
886  object = reinterpret_cast<T*>(static_cast<PointerSizeUInt>(ptr));
887 
888  return checkPrimitiveTrailer();
889  }
890 
891  template <typename T>
892  bool PersistenceManager::readObjectSmartPointer(boost::shared_ptr<T>& s_ptr, const char* name)
893  {
894  if (!checkObjectPointerHeader(RTTI::getStreamName<T>(), name))
895  {
896  return false;
897  }
898 
899  LongSize ptr;
900  get(ptr);
901 
902  if (ptr != 0)
903  {
904  smart_pointer_list_.push_back(std::make_pair((boost::shared_ptr<PersistentObject>*)&s_ptr, (LongSize)((PersistentObject*)ptr)));
905  }
906 
907  return checkPrimitiveTrailer();
908  }
909 
910  template <typename T>
912  const char* name)
913  {
914  if (&object != 0 && !object_out_.has(&object))
915  {
916  object_out_needed_.push_back(&object);
917  }
918 
919  writeObjectReferenceHeader(RTTI::getStreamName<T>(), name);
920  put((LongSize)(void*)&object);
922  }
923 
924 
925  template <typename T>
926  bool PersistenceManager::readObjectReference(T& object, const char* name)
927  {
928  if (!checkObjectReferenceHeader(RTTI::getStreamName<T>(), name))
929  {
930  return false;
931  }
932 
933  LongSize ptr;
934  get(ptr);
935 
936  // store a zero in the corresponding pointer
937  // since we cannot convert 64 bit pointers to
938  // 32 bit pointers - this is required, if an object
939  // written on a 64 bit architecture is read on a 32 bit
940  // machine
941  object = 0;
942 
943  if (ptr != 0)
944  {
945  pointer_list_.push_back(std::make_pair((void**)&object, ptr));
946  }
947 
948  return checkPrimitiveTrailer();
949  }
950 
951  template <typename T>
952  void PersistenceManager::writeObjectArray(const T* array, const char* name,
953  Size size)
954  {
955  writeObjectPointerArrayHeader(RTTI::getStreamName<T>(), name, size);
956 
957  for (Position i = 0; i < size; i++)
958  {
959  (*this) << array[i];
960  }
961 
963  }
964 
965  template <typename T>
967  (const T* array, const char* name, Size& size)
968  {
969  if (!checkObjectPointerArrayHeader(RTTI::getStreamName<T>(), name, size))
970  {
971  return false;
972  }
973 
974  T* ptr = const_cast<T*>(array);
975  for (Position i = 0; i < size; i++)
976  {
977  (*this) >> ptr[i];
978  }
979 
980  bool result = checkObjectPointerArrayTrailer();
981  return result;
982  }
983 
984  template <typename T>
986  (T** arr, const char* name, const Size size)
987  {
988  writeObjectPointerArrayHeader(RTTI::getStreamName<T>(), name, size);
989 
990  PersistentObject* ptr;
991  for (Position i = 0; i < size; i++)
992  {
993  ptr = (PersistentObject*)arr[i];
994  put(static_cast<LongSize>(reinterpret_cast<PointerSizeUInt>(ptr)));
995  if (ptr != 0 && !object_out_.has(ptr))
996  {
997  object_out_needed_.push_back(ptr);
998  }
999  }
1000 
1001  writeObjectPointerArrayTrailer();
1002  }
1003 
1004 
1005  template <typename T>
1006  bool PersistenceManager::readObjectPointerArray(T** array, const char* name,
1007  Size& size)
1008  {
1009  if (!checkObjectPointerArrayHeader(RTTI::getStreamName<T>(), name, size))
1010  {
1011  return false;
1012  }
1013 
1014  LongSize ptr;
1015  for (Position i = 0; i < size; i++)
1016  {
1017  get(ptr);
1018 
1019  if (ptr != 0)
1020  {
1021  pointer_list_.push_back(std::make_pair((void**)&(array[i]), ptr));
1022  }
1023 
1024  array[i] = reinterpret_cast<T*>(static_cast<PointerSizeUInt>(ptr));
1025  }
1026 
1028  }
1029 
1030 #ifndef BALL_NO_INLINE_FUNCTIONS
1031 # include <BALL/CONCEPT/persistenceManager.iC>
1032 #endif
1033 
1034 } // namespace BALL
1035 
1036 #endif // BALL_CONCEPT_PERSISTENCEMANAGER_H
void writeObjectHeader(const T *object, const char *name=0)
Model *(* CreateMethod)(const QSARData &q)
Definition: registry.h:20
void writeStorableObject(const T &t, const char *name)
bool readObjectPointer(T *&object, const char *name)
virtual bool checkObjectPointerArrayTrailer()=0
SmartPointerList smart_pointer_list_
BALL_POINTERSIZEUINT_TYPE PointerSizeUInt
virtual void put(const char c)=0
std::pair< Iterator, bool > insert(const ValueType &item)
Definition: hashSet.h:896
virtual void writeObjectReferenceHeader(const char *type_name, const char *name)=0
void writeObjectArray(const T *array, const char *name, Size size)
virtual void writeObjectPointerArrayHeader(const char *type_name, const char *name, Size size)=0
std::list< const PersistentObject * > ObjectList
virtual bool checkPrimitiveTrailer()=0
std::istream & operator>>(std::istream &is, TRegularData1D< ValueType > &grid)
Input operator.
bool readObjectReference(T &object, const char *name)
bool readStorableObject(T &t, const char *name)
std::list< std::pair< void **, LongSize > > PointerList
virtual bool checkObjectPointerArrayHeader(const char *type_name, const char *name, Size &size)=0
BALL_EXPORT std::ostream & operator<<(std::ostream &os, const Exception::GeneralException &e)
bool readPrimitive(T &t, const char *name)
virtual bool checkStorableHeader(const char *type_name, const char *name)=0
virtual bool checkStorableTrailer()=0
virtual bool checkObjectReferenceHeader(const char *type_name, const char *name)=0
void writePrimitive(const T &t, const char *name)
BALL_EXTERN_VARIABLE const double c
Definition: constants.h:149
StringHashMap< CreateMethod > create_methods_
virtual bool checkPrimitiveHeader(const char *type_name, const char *name)=0
virtual bool checkHeader(const char *type_name, const char *name, LongSize &ptr)=0
bool readObjectArray(const T *array, const char *name, Size &size)
virtual void writePrimitiveTrailer()=0
bool readObjectPointerArray(T **array, const char *name, Size &size)
void writeObjectPointerArray(T **arr, const char *name, const Size size)
virtual void writeStorableHeader(const char *type_name, const char *name)=0
BALL_ULONG64_TYPE LongSize
virtual void writeStorableTrailer()=0
bool readObjectSmartPointer(boost::shared_ptr< T > &s_ptr, const char *name)
void writeObjectReference(const T &object, const char *name)
virtual void writePrimitiveHeader(const char *type_name, const char *name)=0
virtual void writeHeader(const char *type_name, const char *name, LongSize ptr)=0
virtual bool checkObjectPointerHeader(const char *type_name, const char *name)=0
std::list< std::pair< boost::shared_ptr< PersistentObject > *, LongSize > > SmartPointerList
virtual void writeObjectPointerArrayTrailer()=0
virtual void writeObjectPointerHeader(const char *type_name, const char *name)=0
unsigned char Byte
HashMap< LongSize, void * > PointerMap
bool checkObjectHeader(const T &, const char *name=0)
HashSet< const PersistentObject * > ObjectSet
void writeObjectPointer(const T *object, const char *name)
bool has(const Key &key) const
Definition: hashSet.h:1120
#define BALL_EXPORT
Definition: COMMON/global.h:50