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 UVariant::UVariant(const std::vector<char> & value) :
97  type_(kCharArray),
98  data_(sizeof(char)*value.size())
99 {
100  memcpy(data_.data(), value.data(), sizeof(char)*value.size());
101 }
102 UVariant::UVariant(const std::vector<unsigned char> & value) :
103  type_(kUCharArray),
104  data_(sizeof(unsigned char)*value.size())
105 {
106  memcpy(data_.data(), value.data(), sizeof(unsigned char)*value.size());
107 }
108 UVariant::UVariant(const std::vector<short> & value) :
109  type_(kShortArray),
110  data_(sizeof(short)*value.size())
111 {
112  memcpy(data_.data(), value.data(), sizeof(short)*value.size());
113 }
114 UVariant::UVariant(const std::vector<unsigned short> & value) :
115  type_(kUShortArray),
116  data_(sizeof(unsigned short)*value.size())
117 {
118  memcpy(data_.data(), value.data(), sizeof(unsigned short)*value.size());
119 }
120 UVariant::UVariant(const std::vector<int> & value) :
121  type_(kIntArray),
122  data_(sizeof(int)*value.size())
123 {
124  memcpy(data_.data(), value.data(), sizeof(int)*value.size());
125 }
126 UVariant::UVariant(const std::vector<unsigned int> & value) :
127  type_(kUIntArray),
128  data_(sizeof(unsigned int)*value.size())
129 {
130  memcpy(data_.data(), value.data(), sizeof(unsigned int)*value.size());
131 }
132 UVariant::UVariant(const std::vector<float> & value) :
133  type_(kFloatArray),
134  data_(sizeof(float)*value.size())
135 {
136  memcpy(data_.data(), value.data(), sizeof(float)*value.size());
137 }
138 UVariant::UVariant(const std::vector<double> & value) :
139  type_(kDoubleArray),
140  data_(sizeof(double)*value.size())
141 {
142  memcpy(data_.data(), value.data(), sizeof(double)*value.size());
143 }
144 
145 bool UVariant::toBool() const
146 {
147  if(type_ ==kStr)
148  {
149  return uStr2Bool(toStr().c_str());
150  }
151  else if(data_.size())
152  {
153  return memcmp(data_.data(), std::vector<unsigned char>(data_.size(), 0).data(), data_.size()) != 0;
154  }
155  return false;
156 }
157 
158 char UVariant::toChar(bool * ok) const
159 {
160  if(ok)
161  {
162  *ok = false;
163  }
164  char v = 0;
165  if(type_ == kChar)
166  {
167  memcpy(&v, data_.data(), sizeof(char));
168  if(ok)
169  {
170  *ok = true;
171  }
172  }
173  else if(type_ == kUChar)
174  {
175  unsigned char tmp = toUChar();
176  if(tmp <= std::numeric_limits<char>::max())
177  {
178  v = (char)tmp;
179  if(ok)
180  {
181  *ok = true;
182  }
183  }
184  }
185  else if(type_ == kShort)
186  {
187  short tmp = toShort();
189  {
190  v = (char)tmp;
191  if(ok)
192  {
193  *ok = true;
194  }
195  }
196  }
197  else if(type_ == kUShort)
198  {
199  unsigned short tmp = toUShort();
200  if(tmp <= std::numeric_limits<char>::max())
201  {
202  v = (char)tmp;
203  if(ok)
204  {
205  *ok = true;
206  }
207  }
208  }
209  else if(type_ == kInt)
210  {
211  int tmp = toInt();
213  {
214  v = (char)tmp;
215  if(ok)
216  {
217  *ok = true;
218  }
219  }
220  }
221  else if(type_ == kUInt)
222  {
223  unsigned int tmp = toUInt();
224  if(tmp <= (unsigned int)std::numeric_limits<char>::max())
225  {
226  v = (char)tmp;
227  if(ok)
228  {
229  *ok = true;
230  }
231  }
232  }
233  return v;
234 }
235 unsigned char UVariant::toUChar(bool * ok) const
236 {
237  if(ok)
238  {
239  *ok = false;
240  }
241  unsigned char v = 0;
242  if(type_ == kUChar)
243  {
244  memcpy(&v, data_.data(), sizeof(unsigned char));
245  if(ok)
246  {
247  *ok = true;
248  }
249  }
250  else if(type_ == kChar)
251  {
252  char tmp = toChar();
254  {
255  v = (unsigned char)tmp;
256  if(ok)
257  {
258  *ok = true;
259  }
260  }
261  }
262  else if(type_ == kShort)
263  {
264  short tmp = toShort();
266  {
267  v = (unsigned char)tmp;
268  if(ok)
269  {
270  *ok = true;
271  }
272  }
273  }
274  else if(type_ == kUShort)
275  {
276  unsigned short tmp = toUShort();
278  {
279  v = (unsigned char)tmp;
280  if(ok)
281  {
282  *ok = true;
283  }
284  }
285  }
286  else if(type_ == kInt)
287  {
288  int tmp = toInt();
290  {
291  v = (unsigned char)tmp;
292  if(ok)
293  {
294  *ok = true;
295  }
296  }
297  }
298  else if(type_ == kUInt)
299  {
300  unsigned int tmp = toUInt();
302  {
303  v = (unsigned char)tmp;
304  if(ok)
305  {
306  *ok = true;
307  }
308  }
309  }
310  return v;
311 }
312 short UVariant::toShort(bool * ok) const
313 {
314  if(ok)
315  {
316  *ok = false;
317  }
318  short v = 0;
319  if(type_ == kShort)
320  {
321  memcpy(&v, data_.data(), sizeof(short));
322  if(ok)
323  {
324  *ok = true;
325  }
326  }
327  else if(type_ == kChar)
328  {
329  v = (short)toChar();
330  if(ok)
331  {
332  *ok = true;
333  }
334  }
335  else if(type_ == kUChar)
336  {
337  v = (short)toUChar();
338  if(ok)
339  {
340  *ok = true;
341  }
342  }
343  else if(type_ == kUShort)
344  {
345  unsigned short tmp = toUShort();
347  {
348  v = (short)tmp;
349  if(ok)
350  {
351  *ok = true;
352  }
353  }
354  }
355  else if(type_ == kInt)
356  {
357  int tmp = toInt();
359  {
360  v = (short)tmp;
361  if(ok)
362  {
363  *ok = true;
364  }
365  }
366  }
367  else if(type_ == kUInt)
368  {
369  unsigned int tmp = toUInt();
370  if(tmp <= (unsigned int)std::numeric_limits<short>::max())
371  {
372  v = (short)tmp;
373  if(ok)
374  {
375  *ok = true;
376  }
377  }
378  }
379  return v;
380 }
381 unsigned short UVariant::toUShort(bool * ok) const
382 {
383  if(ok)
384  {
385  *ok = false;
386  }
387  unsigned short v = 0;
388  if(type_ == kUShort)
389  {
390  memcpy(&v, data_.data(), sizeof(unsigned short));
391  if(ok)
392  {
393  *ok = true;
394  }
395  }
396  else if(type_ == kChar)
397  {
398  char tmp = toChar();
399  if(tmp >= 0)
400  {
401  v = (unsigned short)tmp;
402  if(ok)
403  {
404  *ok = true;
405  }
406  }
407  }
408  else if(type_ == kUChar)
409  {
410  v = (unsigned short)toUChar();
411  if(ok)
412  {
413  *ok = true;
414  }
415  }
416  else if(type_ == kShort)
417  {
418  short tmp = toShort();
420  {
421  v = (unsigned short)tmp;
422  if(ok)
423  {
424  *ok = true;
425  }
426  }
427  }
428  else if(type_ == kInt)
429  {
430  int tmp = toInt();
432  {
433  v = (unsigned short)tmp;
434  if(ok)
435  {
436  *ok = true;
437  }
438  }
439  }
440  else if(type_ == kUInt)
441  {
442  unsigned int tmp = toUInt();
444  {
445  v = (unsigned short)tmp;
446  if(ok)
447  {
448  *ok = true;
449  }
450  }
451  }
452  return v;
453 }
454 int UVariant::toInt(bool * ok) const
455 {
456  if(ok)
457  {
458  *ok = false;
459  }
460  int v = 0;
461  if(type_ == kInt)
462  {
463  memcpy(&v, data_.data(), sizeof(int));
464  if(ok)
465  {
466  *ok = true;
467  }
468  }
469  else if(type_ == kChar)
470  {
471  v = (int)toChar();
472  if(ok)
473  {
474  *ok = true;
475  }
476  }
477  else if(type_ == kUChar)
478  {
479  v = (int)toUChar();
480  if(ok)
481  {
482  *ok = true;
483  }
484  }
485  else if(type_ == kShort)
486  {
487  v = (int)toShort();
488  if(ok)
489  {
490  *ok = true;
491  }
492  }
493  else if(type_ == kUShort)
494  {
495  v = (int)toUShort();
496  if(ok)
497  {
498  *ok = true;
499  }
500  }
501  else if(type_ == kUInt)
502  {
503  unsigned int tmp = toUInt();
504  if(tmp <= (unsigned int)std::numeric_limits<int>::max())
505  {
506  v = (int)tmp;
507  if(ok)
508  {
509  *ok = true;
510  }
511  }
512  }
513  return v;
514 }
515 unsigned int UVariant::toUInt(bool * ok) const
516 {
517  if(ok)
518  {
519  *ok = false;
520  }
521  unsigned int v = 0;
522  if(type_ == kUInt)
523  {
524  memcpy(&v, data_.data(), sizeof(unsigned int));
525  if(ok)
526  {
527  *ok = true;
528  }
529  }
530  else if(type_ == kChar)
531  {
532  char tmp = toChar();
533  if(tmp >= 0)
534  {
535  v = (unsigned int)tmp;
536  if(ok)
537  {
538  *ok = true;
539  }
540  }
541  }
542  else if(type_ == kUChar)
543  {
544  v = (unsigned int)toUChar();
545  if(ok)
546  {
547  *ok = true;
548  }
549  }
550  else if(type_ == kShort)
551  {
552  short tmp = toShort();
553  if(tmp >= 0)
554  {
555  v = (unsigned int)tmp;
556  if(ok)
557  {
558  *ok = true;
559  }
560  }
561  }
562  else if(type_ == kUShort)
563  {
564  v = (unsigned int)toUShort();
565  if(ok)
566  {
567  *ok = true;
568  }
569  }
570  else if(type_ == kInt)
571  {
572  int tmp = toInt();
573  if(tmp >= 0)
574  {
575  v = (unsigned int)tmp;
576  if(ok)
577  {
578  *ok = true;
579  }
580  }
581  }
582  return v;
583 }
584 float UVariant::toFloat(bool * ok) const
585 {
586  if(ok)
587  {
588  *ok = false;
589  }
590  float v = 0;
591  if(type_ == kFloat)
592  {
593  memcpy(&v, data_.data(), sizeof(float));
594  if(ok)
595  {
596  *ok = true;
597  }
598  }
599  else if(type_ == kDouble)
600  {
601  double tmp = toDouble();
603  {
604  v = (float)tmp;
605  if(ok)
606  {
607  *ok = true;
608  }
609  }
610  }
611  return v;
612 }
613 double UVariant::toDouble(bool * ok) const
614 {
615  if(ok)
616  {
617  *ok = false;
618  }
619  double v = 0;
620  if(type_ == kDouble)
621  {
622  memcpy(&v, data_.data(), sizeof(double));
623  if(ok)
624  {
625  *ok = true;
626  }
627  }
628  else if(type_ == kFloat)
629  {
630  v = (double)toFloat(ok);
631  }
632  return v;
633 }
634 std::string UVariant::toStr(bool * ok) const
635 {
636  if(ok)
637  {
638  *ok = false;
639  }
640  std::string v;
641  if(type_ == kStr)
642  {
643  v = std::string((const char *)data_.data());
644  if(ok)
645  {
646  *ok = true;
647  }
648  }
649  else if(type_ == kBool)
650  {
651  v = toBool()?"true":"false";
652  if(ok)
653  {
654  *ok = true;
655  }
656  }
657  else if(type_ == kChar)
658  {
659  v = " ";
660  v.at(0) = toChar(ok);
661  }
662  else if(type_ == kUChar)
663  {
664  v = uNumber2Str(toUChar(ok));
665  }
666  else if(type_ == kShort)
667  {
668  v = uNumber2Str(toShort(ok));
669  }
670  else if(type_ == kUShort)
671  {
672  v = uNumber2Str(toUShort(ok));
673  }
674  else if(type_ == kInt)
675  {
676  v = uNumber2Str(toInt(ok));
677  }
678  else if(type_ == kUInt)
679  {
680  v = uNumber2Str(toUInt(ok));
681  }
682  else if(type_ == kFloat)
683  {
684  v = uNumber2Str(toFloat(ok));
685  }
686  else if(type_ == kDouble)
687  {
688  v = uNumber2Str(toDouble(ok));
689  }
690  return v;
691 }
692 
693 std::vector<char> UVariant::toCharArray(bool * ok) const
694 {
695  if(ok)
696  {
697  *ok = false;
698  }
699 
700  std::vector<char> v;
701  if(type_ == kCharArray)
702  {
703  if(ok)
704  {
705  *ok = true;
706  }
707  if(data_.size())
708  {
709  v.resize(data_.size() / sizeof(char));
710  memcpy(v.data(), data_.data(), data_.size());
711  }
712  }
713  return v;
714 }
715 
716 std::vector<unsigned char> UVariant::toUCharArray(bool * ok) const
717 {
718  if(ok)
719  {
720  *ok = false;
721  }
722 
723  std::vector<unsigned char> v;
724  if(type_ == kUCharArray)
725  {
726  if(ok)
727  {
728  *ok = true;
729  }
730  if(data_.size())
731  {
732  v.resize(data_.size() / sizeof(unsigned char));
733  memcpy(v.data(), data_.data(), data_.size());
734  }
735  }
736  return v;
737 }
738 
739 std::vector<short> UVariant::toShortArray(bool * ok) const
740 {
741  if(ok)
742  {
743  *ok = false;
744  }
745 
746  std::vector<short> v;
747  if(type_ == kShortArray)
748  {
749  if(ok)
750  {
751  *ok = true;
752  }
753  if(data_.size())
754  {
755  v.resize(data_.size() / sizeof(short));
756  memcpy(v.data(), data_.data(), data_.size());
757  }
758  }
759  return v;
760 }
761 std::vector<unsigned short> UVariant::toUShortArray(bool * ok) const
762 {
763  if(ok)
764  {
765  *ok = false;
766  }
767 
768  std::vector<unsigned short> v;
769  if(type_ == kUShortArray)
770  {
771  if(ok)
772  {
773  *ok = true;
774  }
775  if(data_.size())
776  {
777  v.resize(data_.size() / sizeof(unsigned short));
778  memcpy(v.data(), data_.data(), data_.size());
779  }
780  }
781  return v;
782 }
783 std::vector<int> UVariant::toIntArray(bool * ok) const
784 {
785  if(ok)
786  {
787  *ok = false;
788  }
789 
790  std::vector<int> v;
791  if(type_ == kIntArray)
792  {
793  if(ok)
794  {
795  *ok = true;
796  }
797  if(data_.size())
798  {
799  v.resize(data_.size() / sizeof(int));
800  memcpy(v.data(), data_.data(), data_.size());
801  }
802  }
803  return v;
804 }
805 std::vector<unsigned int> UVariant::toUIntArray(bool * ok) const
806 {
807  if(ok)
808  {
809  *ok = false;
810  }
811 
812  std::vector<unsigned int> v;
813  if(type_ == kUIntArray)
814  {
815  if(ok)
816  {
817  *ok = true;
818  }
819  if(data_.size())
820  {
821  v.resize(data_.size() / sizeof(unsigned int));
822  memcpy(v.data(), data_.data(), data_.size());
823  }
824  }
825  return v;
826 }
827 std::vector<float> UVariant::toFloatArray(bool * ok) const
828 {
829  if(ok)
830  {
831  *ok = false;
832  }
833 
834  std::vector<float> v;
835  if(type_ == kFloatArray)
836  {
837  if(ok)
838  {
839  *ok = true;
840  }
841  if(data_.size())
842  {
843  v.resize(data_.size() / sizeof(float));
844  memcpy(v.data(), data_.data(), data_.size());
845  }
846  }
847  return v;
848 }
849 std::vector<double> UVariant::toDoubleArray(bool * ok) const
850 {
851  if(ok)
852  {
853  *ok = false;
854  }
855 
856  std::vector<double> v;
857  if(type_ == kDoubleArray)
858  {
859  if(ok)
860  {
861  *ok = true;
862  }
863  if(data_.size())
864  {
865  v.resize(data_.size() / sizeof(double));
866  memcpy(v.data(), data_.data(), data_.size());
867  }
868  }
869  return v;
870 }
glm::min
GLM_FUNC_DECL genType min(genType const &x, genType const &y)
int
int
UVariant::toCharArray
std::vector< char > toCharArray(bool *ok=0) const
Definition: UVariant.cpp:693
UVariant::kUShortArray
@ kUShortArray
Definition: UVariant.h:47
UVariant::kDoubleArray
@ kDoubleArray
Definition: UVariant.h:51
UVariant::toDoubleArray
std::vector< double > toDoubleArray(bool *ok=0) const
Definition: UVariant.cpp:849
UVariant::kStr
@ kStr
Definition: UVariant.h:43
UVariant::toIntArray
std::vector< int > toIntArray(bool *ok=0) const
Definition: UVariant.cpp:783
UVariant::toUInt
unsigned int toUInt(bool *ok=0) const
Definition: UVariant.cpp:515
size
Index size
uStr2Bool
bool UTILITE_EXPORT uStr2Bool(const char *str)
Definition: UConversion.cpp:170
UVariant::kUIntArray
@ kUIntArray
Definition: UVariant.h:49
UVariant::kDouble
@ kDouble
Definition: UVariant.h:42
UVariant::kFloatArray
@ kFloatArray
Definition: UVariant.h:50
UVariant::UVariant
UVariant()
Definition: UVariant.cpp:25
UVariant::kBool
@ kBool
Definition: UVariant.h:34
UVariant::kIntArray
@ kIntArray
Definition: UVariant.h:48
UVariant::toDouble
double toDouble(bool *ok=0) const
Definition: UVariant.cpp:613
UVariant::toUCharArray
std::vector< unsigned char > toUCharArray(bool *ok=0) const
Definition: UVariant.cpp:716
UVariant.h
UVariant::kUChar
@ kUChar
Definition: UVariant.h:36
UVariant::kInt
@ kInt
Definition: UVariant.h:39
UVariant::kCharArray
@ kCharArray
Definition: UVariant.h:44
uNumber2Str
std::string UTILITE_EXPORT uNumber2Str(unsigned int number)
Definition: UConversion.cpp:91
UConversion.h
Some conversion functions.
UVariant::toUShort
unsigned short toUShort(bool *ok=0) const
Definition: UVariant.cpp:381
UVariant::toShortArray
std::vector< short > toShortArray(bool *ok=0) const
Definition: UVariant.cpp:739
glm::max
GLM_FUNC_DECL genType max(genType const &x, genType const &y)
UVariant::toUShortArray
std::vector< unsigned short > toUShortArray(bool *ok=0) const
Definition: UVariant.cpp:761
UVariant::toChar
char toChar(bool *ok=0) const
Definition: UVariant.cpp:158
UVariant::kChar
@ kChar
Definition: UVariant.h:35
UVariant::kFloat
@ kFloat
Definition: UVariant.h:41
str
UVariant::toFloatArray
std::vector< float > toFloatArray(bool *ok=0) const
Definition: UVariant.cpp:827
UVariant::kShortArray
@ kShortArray
Definition: UVariant.h:46
UVariant::toUChar
unsigned char toUChar(bool *ok=0) const
Definition: UVariant.cpp:235
UVariant::toStr
std::string toStr(bool *ok=0) const
Definition: UVariant.cpp:634
UVariant::kUCharArray
@ kUCharArray
Definition: UVariant.h:45
UVariant::kUShort
@ kUShort
Definition: UVariant.h:38
UVariant::kShort
@ kShort
Definition: UVariant.h:37
c_str
const char * c_str(Args &&...args)
v
Array< int, Dynamic, 1 > v
UVariant::kUInt
@ kUInt
Definition: UVariant.h:40
UVariant::toBool
bool toBool() const
Definition: UVariant.cpp:145
float
float
UVariant::toUIntArray
std::vector< unsigned int > toUIntArray(bool *ok=0) const
Definition: UVariant.cpp:805
UVariant::type_
Type type_
Definition: UVariant.h:120
UVariant::toShort
short toShort(bool *ok=0) const
Definition: UVariant.cpp:312
UVariant::toFloat
float toFloat(bool *ok=0) const
Definition: UVariant.cpp:584
value
value
UVariant::toInt
int toInt(bool *ok=0) const
Definition: UVariant.cpp:454
UVariant::data_
std::vector< unsigned char > data_
Definition: UVariant.h:121


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:24