1 #pragma once
2 
3 #ifndef TPERSIST_INCLUDED
4 #define TPERSIST_INCLUDED
5 
6 //#include "tsmartpointer.h"
7 //#include "tpixel.h"
8 
9 #include "tcommon.h"
10 
11 #undef DVAPI
12 #undef DVVAR
13 #ifdef TSTREAM_EXPORTS
14 #define DVAPI DV_EXPORT_API
15 #define DVVAR DV_EXPORT_VAR
16 #else
17 #define DVAPI DV_IMPORT_API
18 #define DVVAR DV_IMPORT_VAR
19 #endif
20 
21 //===================================================================
22 
23 class TPersistDeclaration;
24 class TIStream;
25 class TOStream;
26 
27 //===================================================================
28 //! This is an abstract class for load and save data from and to the file system
29 //! (i.e files on the disk)
30 class DVAPI TPersist {
31 public:
~TPersist()32   virtual ~TPersist(){};
33   /*!
34           This is a pure virtual function and must be overridden with a method
35      that implements
36           the object loading from a stream (i.e take the object's features from
37      a formatted data stream).
38           For example the TStageObject class use his implementation to take the
39      values of the parameters
40           that characterizes a stage object as name, coordinates, etc... from a
41      file.
42           \a is is an input file stream
43   */
44   virtual void loadData(TIStream &is) = 0;
45   /*!
46           This is a pure virtual function and must be overridden with a method
47      that implements
48           the object saving to a data stream (i.e put the object's features on a
49      formatted data stream).
50           For example the TStageObject class use his implementation to save the
51      values of the parameters
52           that characterizes a stage object as name, coordinates, etc... on a
53      file
54           \a os is an output file stream.
55   */
56   virtual void saveData(TOStream &os) = 0;
57   /*!
58           Returns the string identifier of the object. For example a TXsheet
59      object is identified by "xsheet",
60           a TStageObjectTree is identified by PegbarTree.
61   */
62   inline std::string getStreamTag() const;
63 
64   /*!
65           This pure virtual method is used to define a global pointer to the
66 object.
67           This method is overridden with the macro PERSIST_DECLARATION(T).
68   \n	For example:
69           \code
70 class DVAPI TStageObjectTree final : public TPersist {
71 PERSIST_DECLARATION(TStageObjectTree)
72 public:
73 
74 ...
75 
76           \endcode
77   */
78   virtual const TPersistDeclaration *getDeclaration() const = 0;
79 
80   typedef TPersist *CreateProc();
81   static void declare(CreateProc *);
82   /*!
83           If the object identified by \a name doesn't exist,
84           this method creates it through TPersistDeclarationT class template.
85           \sa getDeclaration()
86   */
87   static TPersist *create(const std::string &name);
88 };
89 
90 //===================================================================
91 /*!
92                 This class is used to store and retrieve the id associated to an
93    object.
94                 \sa TPersist::getStreamTag().
95                 The class is istantiated by the macro  PERSIST_DECLARATION(T).
96         */
97 class DVAPI TPersistDeclaration {
98   std::string m_id;
99 
100 public:
101   TPersistDeclaration(const std::string &id);
~TPersistDeclaration()102   virtual ~TPersistDeclaration() {}
getId()103   std::string getId() const { return m_id; };
104   virtual TPersist *create() const = 0;
105 };
106 
107 //-------------------------------------------------------------------
108 
getStreamTag()109 inline std::string TPersist::getStreamTag() const {
110   return getDeclaration()->getId();
111 }
112 
113 //-------------------------------------------------------------------
114 /*!
115                 This template class is used to create an istance of the class \a
116    T.
117         */
118 template <class T>
119 class TPersistDeclarationT final : public TPersistDeclaration {
120 public:
121   /*!
122           This is the constructor. Its argument is the id of the object.
123           \sa TPersist::getStreamTag()
124   */
TPersistDeclarationT(const std::string & id)125   TPersistDeclarationT(const std::string &id) : TPersistDeclaration(id) {}
126   /*!
127           Returns a pointer to a newly created object of type TPersist.
128           This template class is called by the macro PERSIST_DECLARATION(T).
129           A class that calls PERSIST_DECLARATION(T) must inherits TPersist.
130   */
create()131   TPersist *create() const override { return new T; };
132 };
133 
134 //-------------------------------------------------------------------
135 /*!	\file tpersist.h
136         */
137 /*!
138                 This macro must be included at the beginning of a class
139    declaration,
140                 and permits to create a new object of type T with the use of
141    template
142                 class TPersistDeclarationT
143         */
144 #define PERSIST_DECLARATION(T)                                                 \
145   \
146 private:                                                                       \
147   static TPersistDeclarationT<T> m_declaration;                                \
148   \
149 public:                                                                        \
150   const TPersistDeclaration *getDeclaration() const override {                 \
151     return &m_declaration;                                                     \
152   }
153 
154 #define PERSIST_IDENTIFIER(T, I) TPersistDeclarationT<T> T::m_declaration(I);
155 
156 //===================================================================
157 
158 #endif
159