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]
VisitorLinkMapper.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 VLXVisitorLinkMapper_INCLUDE_ONCE
33 #define VLXVisitorLinkMapper_INCLUDE_ONCE
34 
35 #include <vlX/Visitor.hpp>
36 
37 namespace vlX
38 {
40  class VisitorLinkMapper: public Visitor
41  {
43 
44  public:
45  typedef enum
46  {
49  } EError;
50 
51  public:
52  VisitorLinkMapper(std::map< std::string, vl::ref<VLXStructure> >* map=NULL)
53  {
54  mLinkMap = map;
55  mError = NoError;
56  }
57 
58  void setLinkMap(std::map< std::string, vl::ref<VLXStructure> >* map)
59  {
60  mLinkMap = map;
61  }
62 
64  {
65  if (obj->uid() != "#NULL")
66  {
67  const std::map< std::string, vl::ref<VLXStructure> >::const_iterator it = mLinkMap->find(obj->uid());
68  if (it == mLinkMap->end())
69  (*mLinkMap)[obj->uid()] = obj;
70  else
71  {
72  if ( it->second != obj )
73  {
74  mError = DuplicateID;
75  vl::Log::error( vl::Say("ID '%s' used by '%s' is already assigned to another node '%s'!\n") << obj->uid() << obj->tag() << it->second->tag() );
76  }
77  }
78  }
79  }
80 
81  virtual void visitStructure(VLXStructure* obj)
82  {
83  if (isVisited(obj))
84  return;
85 
86  declareID(obj);
87 
88  for(size_t i=0; i<obj->value().size(); ++i)
89  {
90  if (obj->value()[i].value().type() == VLXValue::Structure)
91  obj->value()[i].value().getStructure()->acceptVisitor(this);
92  else
93  if (obj->value()[i].value().type() == VLXValue::List)
94  obj->value()[i].value().getList()->acceptVisitor(this);
95  }
96  }
97 
98  virtual void visitList(VLXList* list)
99  {
100  // this should happen only if the user manually creates loops
101  if (isVisited(list))
102  {
103  vl::Log::warning("VisitorLinkMapper: cycle detected on VLXList.\n");
104  return;
105  }
106 
107  for(size_t i=0; i<list->value().size(); ++i)
108  {
109  if (list->value()[i].type() == VLXValue::Structure)
110  list->value()[i].getStructure()->acceptVisitor(this);
111  else
112  if (list->value()[i].type() == VLXValue::List)
113  list->value()[i].getList()->acceptVisitor(this);
114  }
115  }
116 
117  /*
118  virtual void visitArray(VLXArrayString*) {}
119 
120  virtual void visitArray(VLXArrayIdentifier*) {}
121 
122  virtual void visitArray(VLXArrayID*) {}
123  */
124 
125  virtual void visitArray(VLXArrayInteger*) {}
126 
127  virtual void visitArray(VLXArrayReal*) {}
128 
129  EError error() const { return mError; }
130 
131  void setError(EError err) { mError = err; }
132 
133  private:
134  std::map< std::string, vl::ref<VLXStructure> >* mLinkMap;
135  EError mError;
136  };
137 }
138 
139 #endif
A simple String formatting class.
Definition: Say.hpp:124
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
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
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
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
const std::string & tag() const
Definition: Value.hpp:63
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
Base class for all visitors visiting a VLX hierarchy.
Definition: Visitor.hpp:53
const std::string & uid() const
Definition: Value.hpp:605
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
bool isVisited(void *node)
Definition: Visitor.hpp:69