Lumiera  0.pre.03
»edityourfreedom«
gen-node.cpp
Go to the documentation of this file.
1 /*
2  GenNode - generic node element for tree like data representation
3 
4  Copyright (C) Lumiera.org
5  2015, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
23 
44 #include "lib/error.hpp"
46 #include "lib/diff/gen-node.hpp"
47 #include "lib/util-quant.hpp"
48 #include "lib/variant.hpp"
49 
50 #include <boost/lexical_cast.hpp>
51 
52 
53 using boost::lexical_cast;
55 using util::almostEqual;
56 using lib::hash::LuidH;
57 
58 
59 namespace lib {
60 namespace diff{
61 
62  /* symbolic marker ID references
63  * used within the tree diff language
64  * to mark specific scopes and situations
65  */
66  const Ref Ref::I ("_I_");
67  const Ref Ref::NO ("_NO_");
68  const Ref Ref::END ("_END_");
69  const Ref Ref::THIS ("_THIS_");
70  const Ref Ref::CHILD ("_CHILD_");
71  const Ref Ref::ATTRIBS("_ATTRIBS_");
72 
73 
74 
75 
84  bool
85  DataCap::matchData (DataCap const& o) const
86  {
87  class EqualityTest
88  : public Variant<DataValues>::Predicate
89  {
90  DataCap const& o_;
91 
92 #define DERIVE_EQUALITY(_TY_) \
93  virtual bool handle (_TY_ const& val) override { return (o_.get<_TY_>() == val); }
94 
95  DERIVE_EQUALITY (int)
96  DERIVE_EQUALITY (int64_t)
97  DERIVE_EQUALITY (short)
98  DERIVE_EQUALITY (char)
99  DERIVE_EQUALITY (bool)
100  DERIVE_EQUALITY (double)
101  DERIVE_EQUALITY (string)
108 
109 
111  virtual bool
112  handle (RecRef const& val) override
113  {
114  return o_.matchRec(val);
115  }
116 
117  public:
118  EqualityTest(DataCap const& o)
119  : o_(o)
120  { }
121  };
122 
123  EqualityTest visitor(o);
124  return accept(visitor);
125  }
126 
127 
128  bool
129  DataCap::matchNum (int64_t num) const
130  {
131  class MatchNumber
132  : public Variant<DataValues>::Predicate
133  {
134  int64_t num_;
135 
136 #define MATCH_NUMBER(_TY_) \
137  virtual bool handle (_TY_ const& val) override { return val == num_; }
138 
139  MATCH_NUMBER (int)
140  MATCH_NUMBER (int64_t)
141  MATCH_NUMBER (short)
142  MATCH_NUMBER (char)
143  MATCH_NUMBER (double)
144 
145  public:
146  MatchNumber(int64_t num)
147  : num_(num)
148  { }
149  };
150 
151  MatchNumber visitor(num);
152  return accept(visitor);
153  }
154 
155 
156  bool
157  DataCap::matchDbl (double d) const
158  {
159  class MatchDouble
160  : public Variant<DataValues>::Predicate
161  {
162  double num_;
163 
164 #define MATCH_DOUBLE(_TY_) \
165  virtual bool handle (_TY_ const& val) override { return almostEqual (double(val), num_); }
166 
167  MATCH_DOUBLE (int)
168  MATCH_DOUBLE (int64_t)
169  MATCH_DOUBLE (short)
170  MATCH_DOUBLE (char)
171  MATCH_DOUBLE (double)
172 
173  public:
174  MatchDouble(double d)
175  : num_(d)
176  { }
177  };
178 
179  MatchDouble visitor(d);
180  return accept(visitor);
181  }
182 
183 
184  bool
185  DataCap::matchTxt (string const& text) const
186  {
187  class MatchString
188  : public Variant<DataValues>::Predicate
189  {
190  string const& txt_;
191 
192 #define MATCH_STRING(_TY_) \
193  virtual bool handle (_TY_ const& val) override { return lexical_cast<string>(val) == txt_; }
194 
195  MATCH_STRING (int)
196  MATCH_STRING (int64_t)
197  MATCH_STRING (short)
198  MATCH_STRING (double)
199  MATCH_STRING (bool)
200 
201  virtual bool handle (string const& str) override { return str == txt_; }
202  virtual bool handle (char const& c ) override { return 1 == txt_.length() && txt_.front() == c; }
203 
204  public:
205  MatchString(string const& text)
206  : txt_(text)
207  { }
208  };
209 
210  MatchString visitor(text);
211  return accept(visitor);
212  }
213 
214 
215  bool
217  {
218  class MatchTime
219  : public Variant<DataValues>::Predicate
220  {
221  TimeValue& t_;
222 
223 #define MATCH_TIME(_TY_) \
224  virtual bool handle (_TY_ const& val) override { return val == t_; }
225 
231 
232  public:
233  MatchTime(TimeValue& t)
234  : t_(t)
235  { }
236  };
237 
238  MatchTime visitor(time);
239  return accept(visitor);
240  }
241 
242 
243  bool
244  DataCap::matchBool (bool b) const
245  {
246  bool* val = unConst(this)->maybeGet<bool>();
247  return val && (b == *val);
248  }
249 
250 
251  bool
253  {
254  LuidH* val = unConst(this)->maybeGet<LuidH>();
255  return val && (hash == *val);
256  }
257 
258 
259  bool
260  DataCap::matchRec (RecRef const& ref) const
261  {
262  if (ref)
263  return matchRec (*ref.get());
264  else
265  {
266  RecRef* val = unConst(this)->maybeGet<RecRef>();
267  return val && val->empty();
268  }
269  }
270 
271 
272  bool
273  DataCap::matchRec (Rec const& rec) const
274  {
275  Rec* val = unConst(this)->maybeGet<Rec>();
276  if (!val)
277  {
278  RecRef* r = unConst(this)->maybeGet<RecRef>();
279  if (r) val = r->get();
280  }
281  return val && (rec == *val);
282  }
283 
284 
285 
286 
287  DataCap::operator string() const
288  {
289  return "DataCap|"+string(this->buffer());
290  }
291 
292 
293 }} // namespace lib::diff
static const Ref I
symbolic ID ref "_I_"
Definition: gen-node.hpp:753
Fundamental definitions for a representation of changes.
#define hash
bool almostEqual(double d1, double d2, unsigned int ulp=2)
epsilon comparison of doubles.
Definition: util-quant.hpp:143
bool matchTxt(string const &) const
Definition: gen-node.cpp:185
bool empty() const
Definition: record.hpp:656
Typesafe union record.
Definition: variant.hpp:223
#define MATCH_TIME(_TY_)
static const Ref CHILD
symbolic ID ref "_CHILD_"
Definition: gen-node.hpp:757
#define DERIVE_EQUALITY(_TY_)
Implementation namespace for support and library code.
#define MATCH_STRING(_TY_)
Lumiera&#39;s internal time value datatype.
Definition: timevalue.hpp:226
bool matchNum(int64_t) const
Definition: gen-node.cpp:129
static const Ref END
symbolic ID ref "_END_"
Definition: gen-node.hpp:755
bool matchData(DataCap const &) const
Implementation of content equality test, delgating to content.
Definition: gen-node.cpp:85
bool matchBool(bool) const
Definition: gen-node.cpp:244
A typesafe union record to carry embedded values of unrelated type.
void accept(Visitor &visitor)
Definition: variant.hpp:524
Target * get() const noexcept
Definition: record.hpp:673
wrapped record reference.
Definition: record.hpp:611
Utilities for quantisation (grid alignment) and comparisons.
Generic building block for tree shaped (meta)data structures.
bool matchRec(RecRef const &) const
Definition: gen-node.cpp:260
static const Ref NO
symbolic ID ref "_NO_"
Definition: gen-node.hpp:754
bool matchTime(time::TimeValue) const
Definition: gen-node.cpp:216
Lumiera error handling (C++ interface).
OBJ * unConst(const OBJ *o)
shortcut to save some typing when having to define const and non-const variants of member functions ...
Definition: util.hpp:319
Hash implementation based on a lumiera unique object id (LUID) When invoking the default ctor...
static const Ref ATTRIBS
symbolic ID ref "_ATTRIBS_"
Definition: gen-node.hpp:758
Offset measures a distance in time.
Definition: timevalue.hpp:283
Duration is the internal Lumiera time metric.
Definition: timevalue.hpp:380
#define MATCH_DOUBLE(_TY_)
A time interval anchored at a specific point in time.
Definition: timevalue.hpp:467
#define MATCH_NUMBER(_TY_)
object-like record of data.
Definition: record.hpp:143
static const Ref THIS
symbolic ID ref "_THIS_"
Definition: gen-node.hpp:756
basic constant internal time value.
Definition: timevalue.hpp:80
bool matchLuid(hash::LuidH) const
Definition: gen-node.cpp:252
LumieraPriQueue r
Definition: test-priqueue.c:82
bool matchDbl(double) const
Definition: gen-node.cpp:157