UVariant.cpp
Go to the documentation of this file.
1 /*
2 * utilite is a cross-platform library with
3 * useful utilities for fast and small developing.
4 * Copyright (C) 2010 Mathieu Labbe
5 *
6 * utilite is free library: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * utilite is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
22 #include <limits>
23 #include <string.h>
24 
26  type_(kUndef)
27 {
28 }
29 UVariant::UVariant(const bool & value) :
30  type_(kBool),
31  data_(1)
32 {
33  data_[0] = value?1:0;
34 }
35 UVariant::UVariant(const char & value) :
36  type_(kChar),
37  data_(sizeof(char))
38 {
39  memcpy(data_.data(), &value, sizeof(char));
40 }
41 UVariant::UVariant(const unsigned char & value) :
42  type_(kUChar),
43  data_(sizeof(unsigned char))
44 {
45  memcpy(data_.data(), &value, sizeof(unsigned char));
46 }
47 UVariant::UVariant(const short & value) :
48  type_(kShort),
49  data_(sizeof(short))
50 {
51  memcpy(data_.data(), &value, sizeof(short));
52 }
53 UVariant::UVariant(const unsigned short & value) :
54  type_(kUShort),
55  data_(sizeof(unsigned short))
56 {
57  memcpy(data_.data(), &value, sizeof(unsigned short));
58 }
59 UVariant::UVariant(const int & value) :
60  type_(kInt),
61  data_(sizeof(int))
62 {
63  memcpy(data_.data(), &value, sizeof(int));
64 }
65 UVariant::UVariant(const unsigned int & value) :
66  type_(kUInt),
67  data_(sizeof(unsigned int))
68 {
69  memcpy(data_.data(), &value, sizeof(unsigned int));
70 }
71 UVariant::UVariant(const float & value) :
72  type_(kFloat),
73  data_(sizeof(float))
74 {
75  memcpy(data_.data(), &value, sizeof(float));
76 }
77 UVariant::UVariant(const double & value) :
78  type_(kDouble),
79  data_(sizeof(double))
80 {
81  memcpy(data_.data(), &value, sizeof(double));
82 }
83 UVariant::UVariant(const char * value) :
84  type_(kStr)
85 {
86  std::string str(value);
87  data_.resize(str.size()+1);
88  memcpy(data_.data(), str.data(), str.size()+1);
89 }
90 UVariant::UVariant(const std::string & value) :
91  type_(kStr),
92  data_(value.size()+1) // with null character
93 {
94  memcpy(data_.data(), value.data(), value.size()+1);
95 }
96 
97 bool UVariant::toBool() const
98 {
99  if(type_ ==kStr)
100  {
101  return uStr2Bool(toStr().c_str());
102  }
103  else if(data_.size())
104  {
105  return memcmp(data_.data(), std::vector<unsigned char>(data_.size(), 0).data(), data_.size()) != 0;
106  }
107  return false;
108 }
109 
110 char UVariant::toChar(bool * ok) const
111 {
112  if(ok)
113  {
114  *ok = false;
115  }
116  char v = 0;
117  if(type_ == kChar)
118  {
119  memcpy(&v, data_.data(), sizeof(char));
120  if(ok)
121  {
122  *ok = true;
123  }
124  }
125  else if(type_ == kUChar)
126  {
127  unsigned char tmp = toUChar();
128  if(tmp <= std::numeric_limits<char>::max())
129  {
130  v = (char)tmp;
131  if(ok)
132  {
133  *ok = true;
134  }
135  }
136  }
137  else if(type_ == kShort)
138  {
139  short tmp = toShort();
141  {
142  v = (char)tmp;
143  if(ok)
144  {
145  *ok = true;
146  }
147  }
148  }
149  else if(type_ == kUShort)
150  {
151  unsigned short tmp = toUShort();
152  if(tmp <= std::numeric_limits<char>::max())
153  {
154  v = (char)tmp;
155  if(ok)
156  {
157  *ok = true;
158  }
159  }
160  }
161  else if(type_ == kInt)
162  {
163  int tmp = toInt();
165  {
166  v = (char)tmp;
167  if(ok)
168  {
169  *ok = true;
170  }
171  }
172  }
173  else if(type_ == kUInt)
174  {
175  unsigned int tmp = toUInt();
176  if(tmp <= (unsigned int)std::numeric_limits<char>::max())
177  {
178  v = (char)tmp;
179  if(ok)
180  {
181  *ok = true;
182  }
183  }
184  }
185  return v;
186 }
187 unsigned char UVariant::toUChar(bool * ok) const
188 {
189  if(ok)
190  {
191  *ok = false;
192  }
193  unsigned char v = 0;
194  if(type_ == kUChar)
195  {
196  memcpy(&v, data_.data(), sizeof(unsigned char));
197  if(ok)
198  {
199  *ok = true;
200  }
201  }
202  else if(type_ == kChar)
203  {
204  char tmp = toChar();
206  {
207  v = (unsigned char)tmp;
208  if(ok)
209  {
210  *ok = true;
211  }
212  }
213  }
214  else if(type_ == kShort)
215  {
216  short tmp = toShort();
218  {
219  v = (unsigned char)tmp;
220  if(ok)
221  {
222  *ok = true;
223  }
224  }
225  }
226  else if(type_ == kUShort)
227  {
228  unsigned short tmp = toUShort();
230  {
231  v = (unsigned char)tmp;
232  if(ok)
233  {
234  *ok = true;
235  }
236  }
237  }
238  else if(type_ == kInt)
239  {
240  int tmp = toInt();
242  {
243  v = (unsigned char)tmp;
244  if(ok)
245  {
246  *ok = true;
247  }
248  }
249  }
250  else if(type_ == kUInt)
251  {
252  unsigned int tmp = toUInt();
254  {
255  v = (unsigned char)tmp;
256  if(ok)
257  {
258  *ok = true;
259  }
260  }
261  }
262  return v;
263 }
264 short UVariant::toShort(bool * ok) const
265 {
266  if(ok)
267  {
268  *ok = false;
269  }
270  short v = 0;
271  if(type_ == kShort)
272  {
273  memcpy(&v, data_.data(), sizeof(short));
274  if(ok)
275  {
276  *ok = true;
277  }
278  }
279  else if(type_ == kChar)
280  {
281  v = (short)toChar();
282  if(ok)
283  {
284  *ok = true;
285  }
286  }
287  else if(type_ == kUChar)
288  {
289  v = (short)toUChar();
290  if(ok)
291  {
292  *ok = true;
293  }
294  }
295  else if(type_ == kUShort)
296  {
297  unsigned short tmp = toUShort();
299  {
300  v = (short)tmp;
301  if(ok)
302  {
303  *ok = true;
304  }
305  }
306  }
307  else if(type_ == kInt)
308  {
309  int tmp = toInt();
311  {
312  v = (short)tmp;
313  if(ok)
314  {
315  *ok = true;
316  }
317  }
318  }
319  else if(type_ == kUInt)
320  {
321  unsigned int tmp = toUInt();
322  if(tmp <= (unsigned int)std::numeric_limits<short>::max())
323  {
324  v = (short)tmp;
325  if(ok)
326  {
327  *ok = true;
328  }
329  }
330  }
331  return v;
332 }
333 unsigned short UVariant::toUShort(bool * ok) const
334 {
335  if(ok)
336  {
337  *ok = false;
338  }
339  unsigned short v = 0;
340  if(type_ == kUShort)
341  {
342  memcpy(&v, data_.data(), sizeof(unsigned short));
343  if(ok)
344  {
345  *ok = true;
346  }
347  }
348  else if(type_ == kChar)
349  {
350  char tmp = toChar();
351  if(tmp >= 0)
352  {
353  v = (unsigned short)tmp;
354  if(ok)
355  {
356  *ok = true;
357  }
358  }
359  }
360  else if(type_ == kUChar)
361  {
362  v = (unsigned short)toUChar();
363  if(ok)
364  {
365  *ok = true;
366  }
367  }
368  else if(type_ == kShort)
369  {
370  short tmp = toShort();
372  {
373  v = (unsigned short)tmp;
374  if(ok)
375  {
376  *ok = true;
377  }
378  }
379  }
380  else if(type_ == kInt)
381  {
382  int tmp = toInt();
384  {
385  v = (unsigned short)tmp;
386  if(ok)
387  {
388  *ok = true;
389  }
390  }
391  }
392  else if(type_ == kUInt)
393  {
394  unsigned int tmp = toUInt();
396  {
397  v = (unsigned short)tmp;
398  if(ok)
399  {
400  *ok = true;
401  }
402  }
403  }
404  return v;
405 }
406 int UVariant::toInt(bool * ok) const
407 {
408  if(ok)
409  {
410  *ok = false;
411  }
412  int v = 0;
413  if(type_ == kInt)
414  {
415  memcpy(&v, data_.data(), sizeof(int));
416  if(ok)
417  {
418  *ok = true;
419  }
420  }
421  else if(type_ == kChar)
422  {
423  v = (int)toChar();
424  if(ok)
425  {
426  *ok = true;
427  }
428  }
429  else if(type_ == kUChar)
430  {
431  v = (int)toUChar();
432  if(ok)
433  {
434  *ok = true;
435  }
436  }
437  else if(type_ == kShort)
438  {
439  v = (int)toShort();
440  if(ok)
441  {
442  *ok = true;
443  }
444  }
445  else if(type_ == kUShort)
446  {
447  v = (int)toUShort();
448  if(ok)
449  {
450  *ok = true;
451  }
452  }
453  else if(type_ == kUInt)
454  {
455  unsigned int tmp = toUInt();
456  if(tmp <= (unsigned int)std::numeric_limits<int>::max())
457  {
458  v = (int)tmp;
459  if(ok)
460  {
461  *ok = true;
462  }
463  }
464  }
465  return v;
466 }
467 unsigned int UVariant::toUInt(bool * ok) const
468 {
469  if(ok)
470  {
471  *ok = false;
472  }
473  unsigned int v = 0;
474  if(type_ == kUInt)
475  {
476  memcpy(&v, data_.data(), sizeof(unsigned int));
477  if(ok)
478  {
479  *ok = true;
480  }
481  }
482  else if(type_ == kChar)
483  {
484  char tmp = toChar();
485  if(tmp >= 0)
486  {
487  v = (unsigned int)tmp;
488  if(ok)
489  {
490  *ok = true;
491  }
492  }
493  }
494  else if(type_ == kUChar)
495  {
496  v = (unsigned int)toUChar();
497  if(ok)
498  {
499  *ok = true;
500  }
501  }
502  else if(type_ == kShort)
503  {
504  short tmp = toShort();
505  if(tmp >= 0)
506  {
507  v = (unsigned int)tmp;
508  if(ok)
509  {
510  *ok = true;
511  }
512  }
513  }
514  else if(type_ == kUShort)
515  {
516  v = (unsigned int)toUShort();
517  if(ok)
518  {
519  *ok = true;
520  }
521  }
522  else if(type_ == kInt)
523  {
524  int tmp = toInt();
525  if(tmp >= 0)
526  {
527  v = (unsigned int)tmp;
528  if(ok)
529  {
530  *ok = true;
531  }
532  }
533  }
534  return v;
535 }
536 float UVariant::toFloat(bool * ok) const
537 {
538  if(ok)
539  {
540  *ok = false;
541  }
542  float v = 0;
543  if(type_ == kFloat)
544  {
545  memcpy(&v, data_.data(), sizeof(float));
546  if(ok)
547  {
548  *ok = true;
549  }
550  }
551  else if(type_ == kDouble)
552  {
553  double tmp = toDouble();
555  {
556  v = (float)tmp;
557  if(ok)
558  {
559  *ok = true;
560  }
561  }
562  }
563  return v;
564 }
565 double UVariant::toDouble(bool * ok) const
566 {
567  if(ok)
568  {
569  *ok = false;
570  }
571  double v = 0;
572  if(type_ == kDouble)
573  {
574  memcpy(&v, data_.data(), sizeof(double));
575  if(ok)
576  {
577  *ok = true;
578  }
579  }
580  else if(type_ == kFloat)
581  {
582  v = (double)toFloat(ok);
583  }
584  return v;
585 }
586 std::string UVariant::toStr(bool * ok) const
587 {
588  if(ok)
589  {
590  *ok = false;
591  }
592  std::string v;
593  if(type_ == kStr)
594  {
595  v = std::string((const char *)data_.data());
596  if(ok)
597  {
598  *ok = true;
599  }
600  }
601  else if(type_ == kBool)
602  {
603  v = toBool()?"true":"false";
604  if(ok)
605  {
606  *ok = true;
607  }
608  }
609  else if(type_ == kChar)
610  {
611  v = " ";
612  v.at(0) = toChar(ok);
613  }
614  else if(type_ == kUChar)
615  {
616  v = uNumber2Str(toUChar(ok));
617  }
618  else if(type_ == kShort)
619  {
620  v = uNumber2Str(toShort(ok));
621  }
622  else if(type_ == kUShort)
623  {
624  v = uNumber2Str(toUShort(ok));
625  }
626  else if(type_ == kInt)
627  {
628  v = uNumber2Str(toInt(ok));
629  }
630  else if(type_ == kUInt)
631  {
632  v = uNumber2Str(toUInt(ok));
633  }
634  else if(type_ == kFloat)
635  {
636  v = uNumber2Str(toFloat(ok));
637  }
638  else if(type_ == kDouble)
639  {
640  v = uNumber2Str(toDouble(ok));
641  }
642  return v;
643 }
GLM_FUNC_DECL genType min(genType const &x, genType const &y)
bool UTILITE_EXP uStr2Bool(const char *str)
float toFloat(bool *ok=0) const
Definition: UVariant.cpp:536
UVariant()
Definition: UVariant.cpp:25
Some conversion functions.
unsigned char toUChar(bool *ok=0) const
Definition: UVariant.cpp:187
short toShort(bool *ok=0) const
Definition: UVariant.cpp:264
std::vector< unsigned char > data_
Definition: UVariant.h:89
int toInt(bool *ok=0) const
Definition: UVariant.cpp:406
bool toBool() const
Definition: UVariant.cpp:97
unsigned int toUInt(bool *ok=0) const
Definition: UVariant.cpp:467
unsigned short toUShort(bool *ok=0) const
Definition: UVariant.cpp:333
double toDouble(bool *ok=0) const
Definition: UVariant.cpp:565
char toChar(bool *ok=0) const
Definition: UVariant.cpp:110
GLM_FUNC_DECL genType max(genType const &x, genType const &y)
std::string toStr(bool *ok=0) const
Definition: UVariant.cpp:586
Type type_
Definition: UVariant.h:88
std::string UTILITE_EXP uNumber2Str(unsigned int number)
Definition: UConversion.cpp:90


rtabmap
Author(s): Mathieu Labbe
autogenerated on Wed Jun 5 2019 22:43:41