Visualization Library 2.1.0

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

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
VisitorLinker.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 VLXVisitorLinker_INCLUDE_ONCE
33 #define VLXVisitorLinker_INCLUDE_ONCE
34 
35 #include <vlX/Visitor.hpp>
36 #include <vlX/Value.hpp>
37 
38 namespace vlX
39 {
41  class VisitorLinker: public Visitor
42  {
44 
45  public:
46  typedef enum
47  {
50  } EError;
51 
52  public:
53  VisitorLinker(const std::map< std::string, vl::ref<VLXStructure> >* map=NULL)
54  {
55  mLinkMap = map;
56  mError = NoError;
57  }
58 
59  void setLinkMap(const std::map< std::string, vl::ref<VLXStructure> >* map)
60  {
61  mLinkMap = map;
62  }
63 
64  VLXStructure* link(const std::string& uid)
65  {
66  VL_CHECK(mLinkMap)
67  VL_CHECK(!uid.empty())
68  std::map< std::string, vl::ref<VLXStructure> >::const_iterator it = mLinkMap->find(uid);
69  if( it != mLinkMap->end() )
70  {
71  // this should never happen
72  VL_CHECK(uid != "#NULL")
73 
74  /* vl::Log::debug( Say("- ID '%s' linked to '%s'.\n") << uid << it->second->tag() ); */
75  return it->second.get_writable();
76  }
77  else
78  {
79  if (uid != "#NULL")
80  {
81  mError = UnresolvedID;
82  vl::Log::error( vl::Say("Could not link ID '%s' to anything!\n") << uid );
83  }
84  return NULL;
85  }
86  }
87 
88  virtual void visitStructure(VLXStructure* obj)
89  {
90  if (isVisited(obj))
91  return;
92 
93  for(size_t i=0; i<obj->value().size(); ++i)
94  {
95  if (obj->value()[i].value().type() == VLXValue::Structure)
96  obj->value()[i].value().getStructure()->acceptVisitor(this);
97  else
98  if (obj->value()[i].value().type() == VLXValue::List)
99  obj->value()[i].value().getList()->acceptVisitor(this);
100  else
101  /*
102  if (obj->value()[i].value().type() == VLXValue::ArrayID)
103  obj->value()[i].value().getArrayID()->acceptVisitor(this);
104  else
105  */
106  if (obj->value()[i].value().type() == VLXValue::ID)
107  {
108  // transform ID -> Structure
109  VLXStructure* lnk_obj = link( obj->value()[i].value().getID() );
110  obj->value()[i].value().setStructure( lnk_obj );
111  }
112  }
113  }
114 
115  virtual void visitList(VLXList* list)
116  {
117  // this should happen only if the user manually creates loops
118  if (isVisited(list))
119  {
120  vl::Log::warning("VisitorLinker: cycle detected on VLXList.\n");
121  return;
122  }
123 
124  for(size_t i=0; i<list->value().size(); ++i)
125  {
126  if (list->value()[i].type() == VLXValue::Structure)
127  list->value()[i].getStructure()->acceptVisitor(this);
128  if (list->value()[i].type() == VLXValue::List)
129  list->value()[i].getList()->acceptVisitor(this);
130  else
131  /*
132  if (list->value()[i].type() == VLXValue::ArrayID)
133  list->value()[i].getArrayID()->acceptVisitor(this);
134  else
135  */
136  if (list->value()[i].type() == VLXValue::ID)
137  {
138  // transform ID -> Structure
139  VLXStructure* lnk_obj = link( list->value()[i].getID() );
140  list->value()[i].setStructure( lnk_obj );
141  }
142  }
143  }
144 
145  /*
146  virtual void visitArray(VLXArrayString*) {}
147 
148  virtual void visitArray(VLXArrayID* arr)
149  {
150  // retrieves the assigned Structure
151  for(size_t i=0 ;i<arr->value().size(); ++i)
152  arr->value()[i].setStructure ( link(arr->value()[i].uid()) );
153  }
154 
155  virtual void visitArray(VLXArrayIdentifier*) {}
156  */
157 
158  virtual void visitArray(VLXArrayInteger*) {}
159 
160  virtual void visitArray(VLXArrayReal*) {}
161 
162  EError error() const { return mError; }
163 
164  void setError(EError err) { mError = err; }
165 
166  private:
167  const std::map< std::string, vl::ref<VLXStructure> >* mLinkMap;
168  EError mError;
169  };
170 }
171 
172 #endif
EError error() const
A simple String formatting class.
Definition: Say.hpp:124
Substitutes IDs into VLXStructures using the provided link map.
static void warning(const String &message)
Use this function to provide information about situations that might lead to errors or loss of data...
Definition: Log.cpp:155
virtual void visitList(VLXList *list)
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
Definition: Log.cpp:165
An array of 64 bits floating point numbers, can also have a tag.
Definition: Value.hpp:144
virtual void visitArray(VLXArrayReal *)
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
void setError(EError err)
std::vector< VLXValue > & value()
Definition: Value.hpp:652
A list of key/VLXValue pairs, can also have a tag.
Definition: Value.hpp:541
A simple sequence of VLXValue objects, can also have a tag.
Definition: Value.hpp:634
std::vector< KeyValue > & value()
Definition: Value.hpp:607
#define NULL
Definition: OpenGLDefs.hpp:81
An array of 64 bits integers, can also have a tag.
Definition: Value.hpp:133
virtual void visitStructure(VLXStructure *obj)
VisitorLinker(const std::map< std::string, vl::ref< VLXStructure > > *map=NULL)
Base class for all visitors visiting a VLX hierarchy.
Definition: Visitor.hpp:53
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
bool isVisited(void *node)
Definition: Visitor.hpp:69
virtual void visitArray(VLXArrayInteger *)
#define VL_CHECK(expr)
Definition: checks.hpp:73
void setLinkMap(const std::map< std::string, vl::ref< VLXStructure > > *map)
VLXStructure * link(const std::string &uid)