Xpace
translate_byteptr_node.h
Go to the documentation of this file.
1 
2 
3 /**********************************************************//**
4  **
5  ** @file data/translate_byteptr_node.h
6  **
7  ** Copyright (C) 2012 Xpace, LLC. All rights reserved
8  **
9  ** www.xpace.net
10  **
11  **************************************************************/
12 
14 
15 namespace Xpace
16 {
17  template<typename LOC, typename STR>
18  class TranslatePtr : public TranslateNode<LOC>
19  {
20  public:
21  static
23  (const Configuration& config,
24  Sink<LOC>* sink,
26 
27  virtual bool isSkip
28  ()
29  const
30  {
31  return false;
32  }
33 
34  virtual bool operator()
35  (const typename STR::charType*)
36  = 0;
37 
38  protected:
40  (const Configuration& config,
41  BaseDataType t,
42  Sink<LOC>* sink,
44  TranslateNode<LOC>(t, sink, onErr),
46  {
47  if (!!config && (length == 0))
49  }
50 
51  const uint length;
52  };
53 
54  template<typename LOC, typename STR>
55  class SkipPtrNode : public TranslatePtr<LOC, STR>
56  {
57  public:
59  () :
61  {
62  }
63 
64  virtual bool operator()
65  (const typename STR::charType*) override
66  {
67  return true;
68  }
69  };
70 
71  // ================================ PASS-THROUGH TO STRING ==
72 
73  template <typename LOC, typename STR>
74  class PassNumeral : public TranslatePtr<LOC, STR>
75  {
76  public:
78  (const Configuration& config,
79  BaseDataType t,
80  Sink<LOC>* sink,
83  {
84  switch (t.getBaseType())
85  {
86  case BaseDataType::btInt :
87  translate_string.reset(new TranslateStringInt<LOC, STR>(config, sink, onError));
88  break;
89  case BaseDataType::btUint :
90  translate_string.reset(new TranslateStringUint<LOC, STR>(config, sink, onError));
91  break;
93  translate_string.reset(new TranslateStringFloat<LOC, STR>(config, sink, onError));
94  break;
95  default:
96  assert(0); // TODO: exception
97  }
98  }
99 
100  bool operator()
101  (const typename STR::charType* val) override
102  {
103  return (*translate_string)(STR((typename STR::charType*)val, this->length));
104  }
105 
106  private:
107  std::unique_ptr<TranslateString<LOC, STR>> translate_string;
108  };
109 
110  template <typename LOC, typename STR>
111  class PassString : public TranslatePtr<LOC, STR>
112  {
113  public:
114  PassString
115  (const Configuration& config,
116  BaseDataType t,
117  Sink<LOC>* sink,
119  TranslatePtr<LOC, STR>(config, t, sink),
120  translate_string(ts),
121  is_delim((sizeof(typename STR::charType) == 1) ? 256 : 65536)
122  {
123  String delims(config.getValue(TranslateNames::DELIMS_TAG));
124  for (size_t i(0); i < delims.getLength(); ++i)
125  {
126  utf16_t ch(delims.toUtf16()[i]);
127  if (ch < is_delim.size())
128  is_delim[ch] = true;
129  }
130  }
131 
132  bool operator()
133  (const typename STR::charType* val) override
134  {
135  STR v((typename STR::charType*)val, this->length);
136  while (v.length && is_delim[v.data[v.length - 1]])
137  --v.length;
138 
139  return (!v.length || (*translate_string)(v));
140  }
141 
142  private:
143  std::unique_ptr<TranslateString<LOC, STR>> translate_string;
144 
145  std::vector<bool> is_delim;
146  };
147 
148  // ================================ INT =====================
149 
150  template <typename LOC, typename STR, bool SIGNED, uint SIZE>
151  class TranslatePtrInt : public TranslatePtr<LOC, STR>
152  {
153  public:
155  (const Configuration& config,
156  Sink<LOC>* sink,
159  {
160  }
161 
162  virtual bool operator()
163  (const typename STR::charType* val) override
164  {
165  int64 v(0);
166  memcpy(&v, val, SIZE);
167  if (SIGNED && int8(*(val + SIZE - 1)) < 0)
168  v = -v;
169  return this->sink->set(this->loc, v);
170  }
171  };
172 
173  template <typename LOC, typename STR>
174  class TranslatePtrInt<LOC, STR, false, 1> : public TranslatePtr<LOC, STR>
175  {
176  public:
178  (const Configuration& config,
179  Sink<LOC>* sink,
182  {
183  }
184 
185  virtual bool operator()
186  (const typename STR::charType* val) override
187  {
188  return this->sink->set(this->loc, int64(*val));
189  }
190  };
191 
192  template <typename LOC, typename STR>
193  class TranslatePtrInt<LOC, STR, true, 1> : public TranslatePtr<LOC, STR>
194  {
195  public:
197  (const Configuration& config,
198  Sink<LOC>* sink,
201  {
202  }
203 
204  virtual bool operator()
205  (const typename STR::charType* val) override
206  {
207  return this->sink->set(this->loc, int64(*reinterpret_cast<const int8*>(val)));
208  }
209  };
210 
211  template <typename LOC, typename STR>
212  class TranslatePtrInt<LOC, STR, false, 2> : public TranslatePtr<LOC, STR>
213  {
214  public:
216  (const Configuration& config,
217  Sink<LOC>* sink,
220  {
221  }
222 
223  virtual bool operator()
224  (const typename STR::charType* val) override
225  {
226  return this->sink->set(this->loc, int64(*reinterpret_cast<const uint16*>(val)));
227  }
228  };
229 
230  template <typename LOC, typename STR>
231  class TranslatePtrInt<LOC, STR, true, 2> : public TranslatePtr<LOC, STR>
232  {
233  public:
235  (const Configuration& config,
236  Sink<LOC>* sink,
239  {
240  }
241 
242  virtual bool operator()
243  (const typename STR::charType* val) override
244  {
245  return this->sink->set(this->loc, int64(*reinterpret_cast<const int16*>(val)));
246  }
247  };
248 
249  template <typename LOC, typename STR>
250  class TranslatePtrInt<LOC, STR, false, 4> : public TranslatePtr<LOC, STR>
251  {
252  public:
254  (const Configuration& config,
255  Sink<LOC>* sink,
258  {
259  }
260 
261  virtual bool operator()
262  (const typename STR::charType* val) override
263  {
264  return this->sink->set(this->loc, int64(*reinterpret_cast<const uint32*>(val)));
265  }
266  };
267 
268  template <typename LOC, typename STR>
269  class TranslatePtrInt<LOC, STR, true, 4> : public TranslatePtr<LOC, STR>
270  {
271  public:
273  (const Configuration& config,
274  Sink<LOC>* sink,
277  {
278  }
279 
280  virtual bool operator()
281  (const typename STR::charType* val) override
282  {
283  return this->sink->set(this->loc, int64(*reinterpret_cast<const int32*>(val)));
284  }
285  };
286  template <typename LOC, typename STR>
287  class TranslatePtrInt<LOC, STR, false, 8> : public TranslatePtr<LOC, STR>
288  {
289  public:
291  (const Configuration& config,
292  Sink<LOC>* sink,
295  {
296  }
297 
298  virtual bool operator()
299  (const typename STR::charType* val) override
300  {
301  return this->sink->set(this->loc, int64(*reinterpret_cast<const uint64*>(val))); // TODO: check for overflow
302  }
303  };
304 
305  template <typename LOC, typename STR>
306  class TranslatePtrInt<LOC, STR, true, 8> : public TranslatePtr<LOC, STR>
307  {
308  public:
310  (const Configuration& config,
311  Sink<LOC>* sink,
314  {
315  }
316 
317  virtual bool operator()
318  (const typename STR::charType* val) override
319  {
320  return this->sink->set(this->loc, *reinterpret_cast<const int64*>(val));
321  }
322  };
323 
324  // ==========================================================
325  // ==========================================================
326  // ==========================================================
327 
328  template<typename LOC, typename STR>
329  inline
331  (const Configuration& config,
332  Sink<LOC>* sink,
334  {
335  #ifndef NDEBUG
336  QString qconfig(config.toString());
337  #endif
338 
342 
344  return new PassNumeral<LOC, STR>(config, BaseDataType::btInt, sink, onError);
345 
346  DerivedDataType dt(type);
347  switch (dt.getDerivedType())
348  {
353  default:
354  break;
355  }
356  switch (dt.getBaseType())
357  {
359  return new SkipPtrNode<LOC, STR>();
362  return new PassString<LOC, STR>(config, dt.getBaseType(), sink, new TranslateStringString<LOC, STR>(config, sink, onError));
364  switch (config.getValueInt(TranslateNames::LENGTH_TAG, sizeof(uint)))
365  {
366  case 1: return new TranslatePtrInt<LOC, STR, false, 1>(config, sink, onError);
367  case 2: return new TranslatePtrInt<LOC, STR, false, 2>(config, sink, onError);
368  case 3: return new TranslatePtrInt<LOC, STR, false, 3>(config, sink, onError);
369  case 4: return new TranslatePtrInt<LOC, STR, false, 4>(config, sink, onError);
370  case 5: return new TranslatePtrInt<LOC, STR, false, 5>(config, sink, onError);
371  case 6: return new TranslatePtrInt<LOC, STR, false, 6>(config, sink, onError);
372  case 7: return new TranslatePtrInt<LOC, STR, false, 7>(config, sink, onError);
373  default: return new TranslatePtrInt<LOC, STR, false, 8>(config, sink, onError);
374  }
375  case BaseDataType::btInt:
376  switch (config.getValueInt(TranslateNames::LENGTH_TAG, sizeof(int)))
377  {
378  case 1: return new TranslatePtrInt<LOC, STR, true, 1>(config, sink, onError);
379  case 2: return new TranslatePtrInt<LOC, STR, true, 2>(config, sink, onError);
380  case 3: return new TranslatePtrInt<LOC, STR, true, 3>(config, sink, onError);
381  case 4: return new TranslatePtrInt<LOC, STR, true, 4>(config, sink, onError);
382  case 5: return new TranslatePtrInt<LOC, STR, true, 5>(config, sink, onError);
383  case 6: return new TranslatePtrInt<LOC, STR, true, 6>(config, sink, onError);
384  case 7: return new TranslatePtrInt<LOC, STR, true, 7>(config, sink, onError);
385  default: return new TranslatePtrInt<LOC, STR, true, 8>(config, sink, onError);
386  }
387  #ifdef XPACE_DECIMAL_FLOAT_H
389  // TODO
390  #endif
391  default:
392  break;
393  }
394 
395  // TODO: throw
396  return new SkipPtrNode<LOC, STR>();
397  }
398 
399 }
const Xpace_Char16 Xpace_Data_Type type
Definition: table_c.h:141
virtual RET set(LOC location, int64 value)
Write an int64.
Definition: data.h:357
unsigned int uint
Definition: types.h:75
type getBaseType() const
Definition: data.h:517
Each high-level Xpace object has a Configuration.
Definition: config.h:29
char int8
Definition: types.h:80
A string, Unicode UTF-16 and reference-counted.
Definition: types.h:269
unsigned short utf16_t
Definition: types.h:146
TranslatePtr(const Configuration &config, BaseDataType t, Sink< LOC > *sink, TranslationError onErr=default_on_error)
virtual bool isSkip() const
bool getValueBool(const String &tag=String()) const
type getDerivedType() const
Definition: data.h:551
Can&#39;t find a named value.
Definition: config.h:283
int64 getValueInt(const String &tag=String(), int64 def=0, bool *ok=0) const
String getValue(const String &tag=String()) const
get a node&#39;s value or an attribute search for attribute first, then child value
static TranslatePtr * create(const Configuration &config, Sink< LOC > *sink, TranslationError onError=default_on_error)
static const char * NUMERAL_TAG
static const char * DELIMS_TAG
static const char * SUBTYPE_TAG
static const char * VECTOR_TAG
String toString() const
static const char * TYPE_TAG
Definition: data.h:469
long long int64
Definition: types.h:86
static const char * LENGTH_TAG
Translate content Input can be anything Output is to a Sink<LOC> so must be one of its supported type...
Definition: node.h:82
std::function< bool(utf16_t ch, const String &term)> TranslationError
Called on translation error.
Xpace project main namespace
Definition: datetime.h:18
bool default_on_error(utf16_t, const String &)
Default error handler.
Copyright (C) 2012 Xpace, LLC.

current as of Wed Jun 10 2026 12:00:05