Debian Codegraph @

src/Base/Persistence.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2011 Jürgen Riegel <[email protected]> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25
26 #ifndef APP_PERSISTENCE_H
27 #define APP_PERSISTENCE_H
28
29 #include "BaseClass.h"
30
31 namespace Base
32 {
33 class Reader;
34 class Writer;
35 class XMLReader;
36
37 /// Persistence class and root of the type system
38 class BaseExport Persistence: public BaseClass
39 {
40
41 TYPESYSTEM_HEADER();
42
43 public:
44 /**
45 * @brief Get the size of objects.
46 *
47 * It is not meant to have the exact size, it is more or less a fast
48 * estimation to tell whether it is two bytes or a GB.
49 */
50 virtual unsigned int getMemSize() const = 0;
51
52 /** This method is used to save properties to an XML document.
53 * A good example you'll find in PropertyStandard.cpp, e.g. the vector:
54 * \code
55 * void PropertyVector::Save (Writer &writer) const
56 * {
57 * writer << writer.ind() << "<PropertyVector valueX=\"" << _cVec.x <<
58 * "\" valueY=\"" << _cVec.y <<
59 * "\" valueZ=\"" << _cVec.z <<"\"/>" << endl;
60 * }
61 * \endcode
62 * The writer.ind() expression writes the indentation, just for pretty printing of the XML.
63 * As you see, the writing of the XML document is not done with a DOM implementation because
64 * of performance reasons. Therefore the programmer has to take care that a valid XML document
65 * is written. This means closing tags and writing UTF-8.
66 * @see Base::Writer
67 */
68 virtual void Save(Writer& /*writer*/) const = 0;
69 /** This method is used to restore properties from an XML document.
70 * It uses the XMLReader class, which bases on SAX, to read the in Save()
71 * written information. Again the Vector as an example:
72 * \code
73 * void PropertyVector::Restore(Base::XMLReader &reader)
74 * {
75 * // read my Element
76 * reader.readElement("PropertyVector");
77 * // get the value of my Attribute
78 * _cVec.x = reader.getAttribute<double>("valueX");
79 * _cVec.y = reader.getAttribute<double>("valueY");
80 * _cVec.z = reader.getAttribute<double>("valueZ");
81 * }
82 * \endcode
83 */
84 virtual void Restore(XMLReader& /*reader*/) = 0;
85 /** This method is used to save large amounts of data to a binary file.
86 * Sometimes it makes no sense to write property data as XML. In case the
87 * amount of data is too big or the data type has a more effective way to
88 * save itself. In this cases it is possible to write the data in a separate file
89 * inside the document archive. In case you want do so you have to re-implement
90 * SaveDocFile(). First, you have to inform the framework in Save() that you want do so.
91 * Here an example from the Mesh module which can save a (pontetionaly big) triangle mesh:
92 * \code
93 * void PropertyMeshKernel::Save (Base::Writer &writer) const
94 * {
95 * if (writer.isForceXML())
96 * {
97 * writer << writer.ind() << "<Mesh>" << std::endl;
98 * MeshCore::MeshDocXML saver(*_pcMesh);
99 * saver.Save(writer);
100 * }else{
101 * writer << writer.ind() << "<Mesh file=\"" << writer.addFile("MeshKernel.bms", this) <<
102 * "\"/>" << std::endl;
103 * }
104 * \endcode
105 * The writer.isForceXML() is an indication to force you to write XML. Regardless of size and
106 * effectiveness. The second part informs the Base::writer through
107 * writer.addFile("MeshKernel.bms", this) that this object wants to write a file with the given
108 * name. The method addFile() returns a unique name that then is written in the XML stream. This
109 * allows your RestoreDocFile() method to identify and read the file again. Later your
110 * SaveDocFile() method is called as many times as you issued the addFile() call: \code void
111 * PropertyMeshKernel::SaveDocFile (Base::Writer &writer) const
112 * {
113 * _pcMesh->Write( writer );
114 * }
115 * \endcode
116 * In this method you can simply stream your content to the file (Base::Writer inheriting from
117 * ostream).
118 */
119 virtual void SaveDocFile(Writer& /*writer*/) const;
120 /** This method is used to restore large amounts of data from a file
121 * In this method you simply stream in your SaveDocFile() saved data.
122 * Again you have to apply for the call of this method in the Restore() call:
123 * \code
124 * void PropertyMeshKernel::Restore(Base::XMLReader &reader)
125 * {
126 * reader.readElement("Mesh");
127 * std::string file (reader.getAttribute<const char*>("file") );
128 *
129 * if(file == "")
130 * {
131 * // read XML
132 * MeshCore::MeshDocXML restorer(*_pcMesh);
133 * restorer.Restore(reader);
134 * }else{
135 * // initiate a file read
136 * reader.addFile(file.c_str(),this);
137 * }
138 * }
139 * \endcode
140 * After you issued the reader.addFile() your RestoreDocFile() is called:
141 * \code
142 * void PropertyMeshKernel::RestoreDocFile(Base::Reader &reader)
143 * {
144 * _pcMesh->Read( reader );
145 * }
146 * \endcode
147 * @see Base::Reader,Base::XMLReader
148 */
149 virtual void RestoreDocFile(Reader& /*reader*/);
150 /// Encodes an attribute upon saving.
151 static std::string encodeAttribute(const std::string&);
152 /// Replaces all characters with '_' that are not allowed in XML
153 static std::string validateXMLString(const std::string& str);
154
155 // dump the binary persistence data into into the stream
156 void dumpToStream(std::ostream& stream, int compression);
157
158 // restore the binary persistence data from a stream. Must have the format used by dumpToStream
159 void restoreFromStream(std::istream& stream);
160
161 private:
162 /** This method is used at the end of restoreFromStream()
163 * after all data files have been read in.
164 * A subclass can set up some internals. The default
165 * implementation does nothing.
166 */
167 virtual void restoreFinished()
168 {}
169 };
170
171 } // namespace Base
172
173
174 #endif // APP_PERSISTENCE_H