Visualization Library 2.0.0-b3

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

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
VLXSerializer.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2017, 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 <vlCore/VLXRegistry.hpp>
36 #include <vlCore/VLXValue.hpp>
37 #include <vlCore/String.hpp>
38 #include <string>
39 #include <map>
40 
41 namespace vl
42 {
43  class VirtualFile;
46  {
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 String& path, const Object* obj, bool start_fresh=true);
61 
62  bool saveVLT(VirtualFile* file, const Object* obj, bool start_fresh=true);
63 
64  bool saveVLB(const String& path, const Object* obj, bool start_fresh=true);
65 
66  bool saveVLB(VirtualFile* file, const Object* obj, bool start_fresh=true);
67 
68  ref<Object> loadVLT(const String& path, bool start_fresh=true);
69 
70  ref<Object> loadVLT(VirtualFile* file, bool start_fresh=true);
71 
72  ref<Object> loadVLB(const String& path, bool start_fresh=true);
73 
74  ref<Object> loadVLB(VirtualFile* file, bool start_fresh=true);
75 
76  Object* importVLX(const VLXStructure* st);
77 
78  VLXStructure* exportVLX(const Object* obj);
79 
80  bool canExport(const 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 Object* obj, VLXStructure* st);
87 
88  Object* getImportedStructure(const VLXStructure* st);
89 
90  VLXStructure* getExportedObject(const Object* obj);
91 
93  VLXRegistry* registry() { return mRegistry.get(); }
94 
96  const VLXRegistry* registry() const { return mRegistry.get(); }
97 
99  void setRegistry(const VLXRegistry* 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 String& str);
144 
145  void signalExportError(const String& str);
146 
148  void setDocumentURL(const String& location) { mDocumentURL = location; }
149 
151  const 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  String mDocumentURL;
182  std::map<std::string, std::string> mDirectives;
183  EError mError;
184  int mIDCounter;
185  std::map< ref<VLXStructure>, ref<Object> > mImportedStructures; // structure --> object
186  std::map< ref<Object>, ref<VLXStructure> > mExportedObjects; // object --> structure
187  std::map< std::string, VLXValue > mMetadata; // metadata to import or to export
188  ref<VLXRegistry> mRegistry;
189  };
190 }
191 
192 #endif
void setError(EError err)
Sets a serialization error.
VLCORE_EXPORT VLXRegistry * defVLXRegistry()
Definition: init_core.cpp:168
VLXRegistry * registry()
The VLXRegistry used by the serializer, by default set to vl::defVLXRegistry().
An abstract class representing a file.
Definition: VirtualFile.hpp:60
std::map< std::string, VLXValue > & metadata()
The metadata to be imported or exported.
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
const VLXValue * getMetadata(const char *key) const
Returns the value of the given metadata key or NULL if no such metadata was found.
void setDirective(const char *directive, const char *value)
Sets a serialization directive that can be used by VLXClassWrapper objects to program the serializati...
void setDocumentURL(const String &location)
The URL of the document used to resolve document-relative file paths.
VLGRAPHICS_EXPORT bool saveVLT(VirtualFile *file, const ResourceDatabase *)
Definition: ioVLX.cpp:90
VLGRAPHICS_EXPORT ref< ResourceDatabase > loadVLB(VirtualFile *file)
Definition: ioVLX.cpp:67
VLGRAPHICS_EXPORT ref< ResourceDatabase > loadVLT(VirtualFile *file)
Definition: ioVLX.cpp:44
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
EError error() const
The last signaled error.
const String & documentURL() const
The URL of the document used to resolve document-relative file paths.
Visualization Library main namespace.
void eraseDirective(const char *directive)
Removes a serialization directive.
The base class for all the reference counted objects.
Definition: Object.hpp:158
const std::string & directive(const char *directive) const
Returns the value of a serialization directive.
bool hasDirective(const char *directive)
Returns true if the given directive has been set.
void eraseAllDirectives()
Erases all previously set directives.
Wrapper for all VLX value types.
Definition: VLXValue.hpp:239
const std::map< std::string, VLXValue > & metadata() const
The metadata to be imported or exported.
#define NULL
Definition: OpenGLDefs.hpp:81
void setRegistry(const VLXRegistry *registry)
The VLXRegistry used by the serializer, by default set to vl::defVLXRegistry().
VLXValue * getMetadata(const char *key)
Returns the value of the given metadata key or NULL if no such metadata was found.
Registry of vl::VLXClassWrapper objects, used by vl::VLXSerializer, see also vl::defVLXRegistry().
Definition: VLXRegistry.hpp:42
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
VLGRAPHICS_EXPORT bool saveVLB(VirtualFile *file, const ResourceDatabase *)
Definition: ioVLX.cpp:111
A list of key/VLXValue pairs, can also have a tag.
Definition: VLXValue.hpp:540
Translates an arbitrary set of vl::Object (and subclasses) into VLB and VLT format.
const VLXRegistry * registry() const
The VLXRegistry used by the serializer, by default set to vl::defVLXRegistry().