Visualization Library 2.0.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Serializer.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef VLXSerializer_INCLUDE_ONCE
33 #define VLXSerializer_INCLUDE_ONCE
34 
35 #include <vlX/Registry.hpp>
36 #include <vlX/Value.hpp>
37 #include <vlCore/String.hpp>
38 #include <string>
39 #include <map>
40 
41 namespace vlX
42 {
43  class VirtualFile;
46  {
47  VL_INSTRUMENT_CLASS(vlX::Serializer, vl::Object)
48 
49  public:
50  typedef enum { NoError, ImportError, ExportError, ReadError, WriteError } EError;
51 
52  public:
53  VLXSerializer(): mError(NoError), mIDCounter(0)
54  {
55  setRegistry( defVLXRegistry() );
56  }
57 
58  const char* errorString() const;
59 
60  bool saveVLT(const vl::String& path, const vl::Object* obj, bool start_fresh=true);
61 
62  bool saveVLT(vl::VirtualFile* file, const vl::Object* obj, bool start_fresh=true);
63 
64  bool saveVLB(const vl::String& path, const vl::Object* obj, bool start_fresh=true);
65 
66  bool saveVLB(vl::VirtualFile* file, const vl::Object* obj, bool start_fresh=true);
67 
68  vl::ref<vl::Object> loadVLT(const vl::String& path, bool start_fresh=true);
69 
70  vl::ref<vl::Object> loadVLT(vl::VirtualFile* file, bool start_fresh=true);
71 
72  vl::ref<vl::Object> loadVLB(const vl::String& path, bool start_fresh=true);
73 
74  vl::ref<vl::Object> loadVLB(vl::VirtualFile* file, bool start_fresh=true);
75 
76  vl::Object* importVLX(const VLXStructure* st);
77 
78  VLXStructure* exportVLX(const vl::Object* obj);
79 
80  bool canExport(const vl::Object* obj) const;
81 
82  bool canImport(const VLXStructure* st) const;
83 
84  void registerImportedStructure(const VLXStructure* st, Object* obj);
85 
86  void registerExportedObject(const vl::Object* obj, VLXStructure* st);
87 
88  vl::Object* getImportedStructure(const VLXStructure* st);
89 
90  VLXStructure* getExportedObject(const vl::Object* obj);
91 
93  Registry* registry() { return mRegistry.get(); }
94 
96  const Registry* registry() const { return mRegistry.get(); }
97 
99  void setRegistry(const Registry* registry) { mRegistry = registry; }
100 
102  std::map< std::string, VLXValue >& metadata() { return mMetadata; }
103 
105  const std::map< std::string, VLXValue >& metadata() const { return mMetadata; }
106 
108  VLXValue* getMetadata(const char* key)
109  {
110  std::map< std::string, VLXValue >::iterator it = metadata().find(key);
111  if (it == metadata().end())
112  return NULL;
113  else
114  return &it->second;
115  }
116 
118  const VLXValue* getMetadata(const char* key) const
119  {
120  std::map< std::string, VLXValue >::const_iterator it = metadata().find(key);
121  if (it == metadata().end())
122  return NULL;
123  else
124  return &it->second;
125  }
126 
127  void reset()
128  {
129  mError = NoError;
130  mIDCounter = 0;
131  mImportedStructures.clear();
132  mExportedObjects.clear();
133  }
134 
135  std::string generateID(const char* prefix);
136 
138  void setError(EError err) { mError = err; }
139 
141  EError error() const { return mError; }
142 
143  void signalImportError(const vl::String& str);
144 
145  void signalExportError(const vl::String& str);
146 
148  void setDocumentURL(const vl::String& location) { mDocumentURL = location; }
149 
151  const vl::String& documentURL() const { return mDocumentURL; }
152 
154  void resolvePath(std::string& path);
155 
158  void setDirective(const char* directive, const char* value) { mDirectives[directive] = value; }
159 
161  void eraseDirective(const char* directive) { mDirectives.erase(directive); }
162 
164  const std::string& directive(const char* directive) const
165  {
166  static const std::string no_directive = "NO_SUCH_DIRECTIVE";
167  std::map<std::string, std::string>::const_iterator it = mDirectives.find(directive);
168  if (it != mDirectives.end())
169  return it->second;
170  else
171  return no_directive;
172  }
173 
175  bool hasDirective(const char* directive) { return mDirectives.find(directive) != mDirectives.end(); }
176 
178  void eraseAllDirectives() { mDirectives.clear(); }
179 
180  private:
181  vl::String mDocumentURL;
182  std::map<std::string, std::string> mDirectives;
183  EError mError;
184  int mIDCounter;
185  std::map< vl::ref<VLXStructure>, vl::ref<vl::Object> > mImportedStructures; // structure --> object
186  std::map< vl::ref<vl::Object>, vl::ref<VLXStructure> > mExportedObjects; // object --> structure
187  std::map< std::string, VLXValue > mMetadata; // metadata to import or to export
188  vl::ref<Registry> mRegistry;
189  };
190 }
191 
192 #endif
VLX_EXPORT bool saveVLT(vl::VirtualFile *file, const vl::ResourceDatabase *)
Definition: ioVLX.cpp:90
Wrapper for all VLX value types.
Definition: Value.hpp:240
An abstract class representing a file.
Definition: VirtualFile.hpp:60
VLXValue * getMetadata(const char *key)
Returns the value of the given metadata key or NULL if no such metadata was found.
Definition: Serializer.hpp:108
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
const std::string & directive(const char *directive) const
Returns the value of a serialization directive.
Definition: Serializer.hpp:164
Registry * registry()
The Registry used by the serializer, by default set to vl::defVLXRegistry().
Definition: Serializer.hpp:93
VLX_EXPORT Registry * defVLXRegistry()
Definition: pimpl.cpp:43
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
const VLXValue * getMetadata(const char *key) const
Returns the value of the given metadata key or NULL if no such metadata was found.
Definition: Serializer.hpp:118
const std::map< std::string, VLXValue > & metadata() const
The metadata to be imported or exported.
Definition: Serializer.hpp:105
bool hasDirective(const char *directive)
Returns true if the given directive has been set.
Definition: Serializer.hpp:175
A list of key/VLXValue pairs, can also have a tag.
Definition: Value.hpp:541
void setError(EError err)
Sets a serialization error.
Definition: Serializer.hpp:138
std::map< std::string, VLXValue > & metadata()
The metadata to be imported or exported.
Definition: Serializer.hpp:102
EError error() const
The last signaled error.
Definition: Serializer.hpp:141
The base class for all the reference counted objects.
Definition: Object.hpp:158
void eraseDirective(const char *directive)
Removes a serialization directive.
Definition: Serializer.hpp:161
void setDirective(const char *directive, const char *value)
Sets a serialization directive that can be used by ClassWrapper objects to program the serialization ...
Definition: Serializer.hpp:158
void eraseAllDirectives()
Erases all previously set directives.
Definition: Serializer.hpp:178
void setDocumentURL(const vl::String &location)
The URL of the document used to resolve document-relative file paths.
Definition: Serializer.hpp:148
#define NULL
Definition: OpenGLDefs.hpp:81
VLX_EXPORT vl::ref< vl::ResourceDatabase > loadVLT(vl::VirtualFile *file)
Definition: ioVLX.cpp:44
Translates an arbitrary set of vl::Object (and subclasses) into VLB and VLT format.
Definition: Serializer.hpp:45
Registry of vl::ClassWrapper objects, used by vl::VLXSerializer, see also vl::defVLXRegistry().
Definition: Registry.hpp:43
VLX_EXPORT bool saveVLB(vl::VirtualFile *file, const vl::ResourceDatabase *)
Definition: ioVLX.cpp:111
const Registry * registry() const
The Registry used by the serializer, by default set to vl::defVLXRegistry().
Definition: Serializer.hpp:96
void setRegistry(const Registry *registry)
The Registry used by the serializer, by default set to vl::defVLXRegistry().
Definition: Serializer.hpp:99
VLX_EXPORT vl::ref< vl::ResourceDatabase > loadVLB(vl::VirtualFile *file)
Definition: ioVLX.cpp:67
const vl::String & documentURL() const
The URL of the document used to resolve document-relative file paths.
Definition: Serializer.hpp:151