summaryrefslogtreecommitdiff
path: root/SDL_Core/src/components/smart_objects/src/smart_object.cc
blob: 581b324e9db9ec30febf4538c1b5a756074c277f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
/**
 * @file SmartObject.cpp
 * @brief SmartObject source file.
 */
// Copyright (c) 2013, Ford Motor Company
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following
// disclaimer in the documentation and/or other materials provided with the
// distribution.
//
// Neither the name of the Ford Motor Company nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 'A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#include "smart_objects/smart_object.h"

#include <errno.h>
#include <stdlib.h>
#include <cstdio>
#include <algorithm>
#include <sstream>
#include <iomanip>
#include <iterator>

namespace NsSmartDeviceLink {
namespace NsSmartObjects {

SmartObject::SmartObject()
    : m_type(SmartType_Null),
      m_schema() {
  m_data.str_value = NULL;
}

SmartObject::SmartObject(const SmartObject& Other)
    : m_type(SmartType_Null),
      m_schema() {
  m_data.str_value = NULL;
  duplicate(Other);
}

SmartObject::SmartObject(SmartType Type)
    : m_type(SmartType_Null),
      m_schema() {
  switch (Type) {
    case SmartType_Integer:
      set_value_unsigned_int(0);
      break;
    case SmartType_Double:
      set_value_double(0);
      break;
    case SmartType_Boolean:
      set_value_bool(false);
      break;
    case SmartType_Character:
      set_value_char(' ');
      break;
    case SmartType_String:
      set_value_string("");
      break;
    case SmartType_Map:
      m_data.map_value = new SmartMap();
      m_type = SmartType_Map;
      break;
    case SmartType_Array:
      m_data.array_value = new SmartArray();
      m_type = SmartType_Array;
      break;
    case SmartType_Binary:
      set_value_binary(SmartBinary());
      break;
    case SmartType_Invalid:
      m_type = SmartType_Invalid;
      break;
    default: {
/*
#if !defined UNIT_TESTS
      NOTREACHED();
#endif
*/
      break;
    }
  }
}

SmartObject::~SmartObject() {
  cleanup_data();
}

SmartObject& SmartObject::operator=(const SmartObject& Other) {
  if (this != &Other)
    duplicate(Other);
  return *this;
}

bool SmartObject::operator==(const SmartObject& Other) const {
  if (m_type != Other.m_type)
    return false;

  switch (m_type) {
    case SmartType_Integer:
      return m_data.int_value == Other.m_data.int_value;
    case SmartType_Double:
      return m_data.double_value == Other.m_data.double_value;
    case SmartType_Boolean:
      return m_data.bool_value == Other.m_data.bool_value;
    case SmartType_Character:
      return m_data.char_value == Other.m_data.char_value;
    case SmartType_String:
      return *(m_data.str_value) == *(Other.m_data.str_value);
    case SmartType_Map:
      if (m_data.map_value == Other.m_data.map_value)
        return true;
      return std::equal(m_data.map_value->begin(), m_data.map_value->end(),
                        Other.m_data.map_value->begin());
    case SmartType_Array:
      if (m_data.array_value == Other.m_data.array_value)
        return true;
      return std::equal(m_data.array_value->begin(), m_data.array_value->end(),
                        Other.m_data.array_value->begin());
    case SmartType_Binary:
      if (m_data.binary_value == Other.m_data.binary_value)
        return true;
      return std::equal(m_data.binary_value->begin(),
                        m_data.binary_value->end(),
                        Other.m_data.binary_value->begin());
    case SmartType_Null:
      return true;
    case SmartType_Invalid:
      return true;
    default: {
/*
#if !defined UNIT_TESTS
      NOTREACHED();
#endif
*/
      break;
    }
  }
  return false;
}

// =============================================================
// INTEGER TYPE SUPPORT
// =============================================================
SmartObject::SmartObject(int32_t InitialValue)
    : m_type(SmartType_Null),
      m_schema() {
  m_data.str_value = NULL;
  set_value_integer(InitialValue);
}

int32_t SmartObject::asInt() const {
  return convert_int();
}

SmartObject& SmartObject::operator=(int32_t NewValue) {
  if (m_type != SmartType_Invalid) {
    set_value_integer(NewValue);
  }
  return *this;
}

bool SmartObject::operator==(int32_t Value) const {
  int32_t comp = convert_int();
  if (comp == invalid_int_value) {
    return false;
  } else {
    return comp == Value;
  }
}

void SmartObject::set_value_integer(int32_t NewValue) {
  set_new_type(SmartType_Integer);
  m_data.int_value = NewValue;
}

int32_t SmartObject::convert_int() const {
  int32_t retval;

  switch (m_type) {
    case SmartType_String:
      retval = convert_string_to_unsigned_int(m_data.str_value);
      break;
    case SmartType_Boolean:
      retval = (m_data.bool_value == true) ? 1 : 0;
      break;
    case SmartType_Integer:
      retval = m_data.int_value;
      break;
    case SmartType_Double:
      retval = static_cast<uint32_t>(m_data.double_value);
      break;
    default: {
/*
#if !defined UNIT_TESTS
      NOTREACHED();
#endif
*/
      retval = invalid_int_value;
      break;
    }
  }

  return static_cast<int32_t>(retval);
}

// =============================================================
// uint32_t TYPE SUPPORT
// =============================================================
SmartObject::SmartObject(uint32_t InitialValue)
    : m_type(SmartType_Null),
      m_schema() {
  m_data.str_value = NULL;
  set_value_unsigned_int(InitialValue);
}

uint32_t SmartObject::asUInt() const {
  return convert_unsigned_int();
}

SmartObject& SmartObject::operator=(uint32_t NewValue) {
  if (m_type != SmartType_Invalid) {
    set_value_unsigned_int(NewValue);
  }
  return *this;
}

bool SmartObject::operator==(uint32_t Value) const {
  int32_t comp = convert_unsigned_int();
  if (comp == invalid_int_value) {
    return false;
  } else {
    return comp == Value;
  }
}

void SmartObject::set_value_unsigned_int(uint32_t NewValue) {
  set_new_type(SmartType_Integer);
  m_data.int_value = NewValue;
}

uint32_t SmartObject::convert_unsigned_int() const {
  uint32_t retval;

  switch (m_type) {
    case SmartType_String:
      retval = convert_string_to_unsigned_int(m_data.str_value);
      break;
    case SmartType_Boolean:
      return (m_data.bool_value == true) ? 1 : 0;
    case SmartType_Integer:
      retval = m_data.int_value;
      break;
    case SmartType_Double:
      retval = static_cast<uint32_t>(m_data.double_value);
      break;
    default: {
/*
#if !defined UNIT_TESTS
      NOTREACHED();
#endif
*/
      return invalid_int_value;
    }
  }

  return retval;
}

// =============================================================
// DOUBLE TYPE SUPPORT
// =============================================================
SmartObject::SmartObject(double InitialValue)
    : m_type(SmartType_Null),
      m_schema() {
  m_data.str_value = NULL;
  set_value_double(InitialValue);
}

double SmartObject::asDouble() const {
  return convert_double();
}

SmartObject& SmartObject::operator=(double NewValue) {
  if (m_type != SmartType_Invalid) {
    set_value_double(NewValue);
  }
  return *this;
}

bool SmartObject::operator==(double Value) const {
  double comp = convert_double();
  if (comp == invalid_double_value) {
    return false;
  } else {
    return comp == Value;
  }
}

void SmartObject::set_value_double(double NewValue) {
  set_new_type(SmartType_Double);
  m_data.double_value = NewValue;
}

double SmartObject::convert_double(void) const {
  double retval;

  switch (m_type) {
    case SmartType_String:
      retval = convert_string_to_double(m_data.str_value);
      break;
    case SmartType_Boolean:
      retval = (m_data.bool_value) ? 1.0 : 0.0;
      break;
    case SmartType_Integer:
      retval = static_cast<double>(convert_int());
      break;
    case SmartType_Double:
      retval = m_data.double_value;
      break;
    default:
/*
#if !defined UNIT_TESTS
      NOTREACHED();
#endif
*/
      retval = invalid_double_value;
      break;
  }

  return retval;
}

// =============================================================
// BOOL TYPE SUPPORT
// =============================================================

SmartObject::SmartObject(bool InitialValue)
    : m_type(SmartType_Null),
      m_schema() {
  m_data.str_value = NULL;
  set_value_bool(InitialValue);
}

bool SmartObject::asBool() const {
  return convert_bool();
}

SmartObject& SmartObject::operator=(bool NewValue) {
  if (m_type != SmartType_Invalid) {
    set_value_bool(NewValue);
  }
  return *this;
}

bool SmartObject::operator==(bool Value) const {
  bool comp = convert_bool();
  if (comp == invalid_bool_value) {
    return false;
  } else {
    return comp == Value;
  }
}

void SmartObject::set_value_bool(bool NewValue) {
  set_new_type(SmartType_Boolean);
  m_data.bool_value = NewValue;
}

bool SmartObject::convert_bool() const {
  bool retval;

  switch (m_type) {
    case SmartType_Boolean:
      retval = m_data.bool_value;
      break;
    case SmartType_Integer:
      retval = (m_data.int_value != 0);
      break;
    case SmartType_Double:
      retval = (m_data.double_value != 0.0);
      break;
    default:
/*
#if !defined UNIT_TESTS
      NOTREACHED();
#endif
*/
      retval = invalid_bool_value;
      break;
  }

  return retval;
}

// =============================================================
// CHAR TYPE SUPPORT
// =============================================================

SmartObject::SmartObject(char InitialValue)
    : m_type(SmartType_Null),
      m_schema() {
  m_data.str_value = NULL;
  set_value_char(InitialValue);
}

char SmartObject::asChar() const {
  return convert_char();
}

SmartObject& SmartObject::operator=(char NewValue) {
  if (m_type != SmartType_Invalid) {
    set_value_char(NewValue);
  }
  return *this;
}

bool SmartObject::operator==(char Value) const {
  char comp = convert_char();
  if (comp == invalid_char_value) {
    return false;
  } else {
    return comp == Value;
  }
}

void SmartObject::set_value_char(char NewValue) {
  set_new_type(SmartType_Character);
  m_data.char_value = NewValue;
}

char SmartObject::convert_char() const {
  char retval;

  switch (m_type) {
    case SmartType_String:
      retval =
          (m_data.str_value->length() == 1) ?
              m_data.str_value->at(0) : invalid_char_value;
      break;
    case SmartType_Character:
      retval = m_data.char_value;
      break;
    default:
/*
#if !defined UNIT_TESTS
      NOTREACHED();
#endif
*/
      retval = invalid_char_value;
      break;
  }

  return retval;
}

// =============================================================
// STD::STRING TYPE SUPPORT
// =============================================================

SmartObject::SmartObject(const std::string& InitialValue)
    : m_type(SmartType_Null),
      m_schema() {
  m_data.str_value = NULL;
  set_value_string(InitialValue);
}

std::string SmartObject::asString() const {
  return convert_string();
}

const char* SmartObject::asCharArray() const {
  if (m_data.str_value != NULL) {
    return m_data.str_value->c_str();
  }

  return "";
}

SmartObject& SmartObject::operator=(const std::string& NewValue) {
  if (m_type != SmartType_Invalid) {
    set_value_string(NewValue);
  }
  return *this;
}

bool SmartObject::operator==(std::string Value) const {
  std::string comp = convert_string();
  if (comp == invalid_string_value) {
    return false;
  } else {
    return comp == Value;
  }
}

void SmartObject::set_value_string(const std::string& NewValue) {
  set_new_type(SmartType_String);
  m_data.str_value = new std::string(NewValue);
}

std::string SmartObject::convert_string(void) const {
  std::string retval;

  switch (m_type) {
    case SmartType_String:
      retval = *(m_data.str_value);
      break;
    case SmartType_Integer:
      char val[20];
      sprintf(val, "%d", m_data.int_value);
      retval = std::string(val);
      break;
    case SmartType_Character:
      retval = std::string(1, m_data.char_value);
      break;
    case SmartType_Double:
      retval = convert_double_to_string(m_data.double_value);
      break;
    default:
/*
#if !defined UNIT_TESTS
      NOTREACHED();
#endif
*/
      retval = invalid_cstr_value;
      break;
  }
  return retval;
}

// =============================================================
// CHAR* TYPE SUPPORT
// =============================================================

SmartObject::SmartObject(char* InitialValue)
    : m_type(SmartType_Null),
      m_schema() {
  m_data.str_value = NULL;
  set_value_cstr(InitialValue);
  return;
}

SmartObject& SmartObject::operator=(const char* NewValue) {
  if (m_type != SmartType_Invalid) {
    set_value_cstr(NewValue);
  }
  return *this;
}

bool SmartObject::operator==(const char* Value) const {
  std::string comp = convert_string();
  std::string val(Value);
  if (comp == invalid_string_value) {
    return false;
  } else {
    return comp == val;
  }
}

void SmartObject::set_value_cstr(const char* NewValue) {
  if (NewValue) {
    set_value_string(std::string(NewValue));
  } else {
    std::string tmp;
    set_value_string(tmp);
  }
}

// =============================================================
// BINARY TYPE SUPPORT
// =============================================================
SmartObject::SmartObject(const SmartBinary& InitialValue)
    : m_type(SmartType_Null),
      m_schema() {
  m_data.str_value = NULL;
  set_value_binary(InitialValue);
}

SmartBinary SmartObject::asBinary() const {
  return convert_binary();
}

SmartArray* SmartObject::asArray() const {
  if (m_type != SmartType_Array) {
/*
#if !defined UNIT_TESTS
    NOTREACHED();
#endif
*/
    return NULL;
  }

  return m_data.array_value;
}

SmartObject& SmartObject::operator=(SmartBinary NewValue) {
  if (m_type != SmartType_Invalid) {
    set_value_binary(NewValue);
  }
  return *this;
}

bool SmartObject::operator==(SmartBinary Value) const {
  SmartBinary comp = convert_binary();
  if (comp == invalid_binary_value) {
    return false;
  } else {
    return std::equal(comp.begin(), comp.end(), Value.begin());
  }
}

void SmartObject::set_value_binary(SmartBinary NewValue) {
  set_new_type(SmartType_Binary);
  m_data.binary_value = new SmartBinary(NewValue);
}

SmartBinary SmartObject::convert_binary(void) const {
  switch (m_type) {
    case SmartType_Binary:
      return *(m_data.binary_value);
    default: {
/*
#if !defined UNIT_TESTS
      NOTREACHED();
#endif
*/
      return invalid_binary_value;
    }
  }

  return invalid_binary_value;
}

// =============================================================
// ARRAY INTERFACE SUPPORT
// =============================================================

SmartObject& SmartObject::operator[](int32_t Index) {
  return handle_array_access(Index);
}

const SmartObject& SmartObject::operator[](int32_t Index) const {
  return getElement(Index);
}

inline SmartObject& SmartObject::handle_array_access(int32_t Index) {
  if (m_type == SmartType_Invalid) {
    return *this;
  }

  if (m_type != SmartType_Array) {
    cleanup_data();
    m_type = SmartType_Array;
    m_data.array_value = new SmartArray();
  }

  int32_t sz = m_data.array_value->size();
  if (Index == -1) {
    Index = sz;
  }
  if (Index == sz) {
    SmartObject uc;
    m_data.array_value->push_back(uc);
  }
  if (Index > sz || Index < 0) {
/*
#if !defined UNIT_TESTS
    NOTREACHED();
#endif
*/
    return invalid_object_value;
  }
  return m_data.array_value->at(Index);
}

// =============================================================
// MAP INTERFACE SUPPORT
// =============================================================

SmartObject& SmartObject::operator[](const std::string Key) {
  return handle_map_access(Key);
}

const SmartObject& SmartObject::operator[] (const std::string Key) const {
  return getElement(Key);
}

SmartObject& SmartObject::operator[](char* Key) {
  std::string str(Key);
  return handle_map_access(str);
}
 
const SmartObject& SmartObject::operator[](char* Key) const {
  std::string str(Key);
  return getElement(str);
}

SmartObject& SmartObject::operator[](const char* Key) {
  std::string str(Key);
  return handle_map_access(str);
}

const SmartObject& SmartObject::operator[](const char* Key) const {
  return getElement(Key);
}

const SmartObject& SmartObject::getElement(size_t Index) const {
  if (SmartType_Array == m_type) {
    if (Index < m_data.array_value->size()) {
      return m_data.array_value->at(Index);
    }
  }

/*
#if !defined UNIT_TESTS
  NOTREACHED();
#endif
*/
  return invalid_object_value;
}

const SmartObject& SmartObject::getElement(const std::string & Key) const {
  if (SmartType_Map == m_type) {
    SmartMap::const_iterator i = m_data.map_value->find(Key);

    if (i != m_data.map_value->end()) {
      return i->second;
    }
  }

/*
#if !defined UNIT_TESTS
  NOTREACHED();
#endif
*/
  return invalid_object_value;
}

SmartObject& SmartObject::handle_map_access(const std::string Key) {
  if (m_type == SmartType_Invalid) {
    return *this;
  }

  // TODO(404): implement handling of non-existing keys similar to array
  if (m_type != SmartType_Map) {
    cleanup_data();
    m_type = SmartType_Map;
    m_data.map_value = new SmartMap();
  }

  // TODO(404): Add check for key presense
  return (*(m_data.map_value))[Key];
}

// =============================================================
// OTHER METHODS
// =============================================================
void SmartObject::duplicate(const SmartObject& OtherObject) {
  SmartData newData;
  SmartType newType = OtherObject.m_type;
  CSmartSchema newSchema = OtherObject.m_schema;

  switch (newType) {
    case SmartType_Map:
      newData.map_value = new SmartMap(*OtherObject.m_data.map_value);
      break;
    case SmartType_Array:
      newData.array_value = new SmartArray(*OtherObject.m_data.array_value);
      break;
    case SmartType_Integer:
      newData.int_value = OtherObject.m_data.int_value;
      break;
    case SmartType_Double:
      newData.double_value = OtherObject.m_data.double_value;
      break;
    case SmartType_Boolean:
      newData.bool_value = OtherObject.m_data.bool_value;
      break;
    case SmartType_Character:
      newData.char_value = OtherObject.m_data.char_value;
      break;
    case SmartType_String:
      newData.str_value = new std::string(*OtherObject.m_data.str_value);
      break;
    case SmartType_Binary:
      newData.binary_value = new SmartBinary(*OtherObject.m_data.binary_value);
      break;
    default:
/*
#if !defined UNIT_TESTS
      NOTREACHED();
#endif
*/
      break;
  }

  cleanup_data();

  m_type = newType;
  m_data = newData;
  m_schema = newSchema;
}

void SmartObject::cleanup_data() {
  switch (m_type) {
    case SmartType_String:
      delete m_data.str_value;
      break;
    case SmartType_Map:
      delete m_data.map_value;
      break;
    case SmartType_Array:
      delete m_data.array_value;
      break;
    case SmartType_Binary:
      delete m_data.binary_value;
      break;
    default:
/*
#if !defined UNIT_TESTS
      NOTREACHED();
#endif
*/
      break;
  }
}

size_t SmartObject::length() const {
  size_t size = 0;

  switch (m_type) {
    case SmartType_String:
      size = m_data.str_value->size();
      break;
    case SmartType_Array:
      size = m_data.array_value->size();
      break;
    case SmartType_Map:
      size = m_data.map_value->size();
      break;
    default:
/*
#if !defined UNIT_TESTS
      NOTREACHED();
#endif
*/
      size = 0;
      break;
  }

  return size;
}

void SmartObject::set_new_type(SmartType NewType) {
  cleanup_data();
  m_type = NewType;
}

double SmartObject::convert_string_to_double(const std::string* Value) {
  if (0 == Value->size()) {
/*
#if !defined UNIT_TESTS
    NOTREACHED();
#endif
*/
    return invalid_double_value;
  }

  char* ptr;
  errno = 0;

  double result = strtod(Value->c_str(), &ptr);
  if (errno || (ptr != (Value->c_str() + Value->length()))) {
/*
#if !defined UNIT_TESTS
    NOTREACHED();
#endif
*/
    return invalid_double_value;
  }

  return result;
}

std::string SmartObject::convert_double_to_string(const double& Value) {
  std::stringstream ss;

  // convert double to string w fixed notation, hi precision
  ss << std::fixed << std::setprecision(10) << Value;

  // output to std::string
  std::string s = ss.str();

  // remove trailing 000s    (123.1200 => 123.12,  123.000 => 123.)
  s.erase(s.find_last_not_of('0') + 1, std::string::npos);
  if (s[s.size() - 1] == '.') {
    // remove dangling decimal (123. => 123)
    s.erase(s.end() - 1);
  }
  return s;
}

uint32_t SmartObject::convert_string_to_unsigned_int(
    const std::string* Value) {
  if (0 == Value->size()) {
/*
#if !defined UNIT_TESTS
    NOTREACHED();
#endif
*/
    return invalid_int_value;
  }

  char* ptr;
  errno = 0;
  uint32_t result = strtol(Value->c_str(), &ptr, 10);
  if (errno || (ptr != (Value->c_str() + Value->length()))) {
/*
#if !defined UNIT_TESTS
    NOTREACHED();
#endif
*/
    return invalid_int_value;
  }

  return result;
}

SmartType SmartObject::getType() const {
  return m_type;
}

std::string NsSmartDeviceLink::NsSmartObjects::SmartObject::OperatorToTransform(const SmartMap::value_type &pair) {
    return pair.first;
}

std::set<std::string> SmartObject::enumerate() const {
  std::set<std::string> keys;

  if(m_type == SmartType_Map) {
        std::transform(
            m_data.map_value->begin(),
            m_data.map_value->end(),
            std::inserter(keys, keys.end()),
            //operator[](const SmartMap::value_type &pair){return pair.first;}
            &NsSmartDeviceLink::NsSmartObjects::SmartObject::OperatorToTransform
        );
  }
  return keys;
}

bool SmartObject::keyExists(const std::string & Key) const {
  if (m_type != SmartType_Map) {
/*
#if !defined UNIT_TESTS
    NOTREACHED();
#endif
*/
    return false;
  }

  return m_data.map_value->count(Key);
}

bool SmartObject::erase(const std::string & Key) {
  if (m_type != SmartType_Map) {
/*
#if !defined UNIT_TESTS
    NOTREACHED();
#endif
*/
    return false;
  }

  return (1 == m_data.map_value->erase(Key));
}

bool SmartObject::isValid() const {
  return (Errors::OK == m_schema.validate(*this));
}

Errors::eType SmartObject::validate() {
  return m_schema.validate(*this);
}

void SmartObject::setSchema(CSmartSchema schema) {
  m_schema = schema;
}

CSmartSchema SmartObject::getSchema() {
  return m_schema;
}

}  // namespace NsSmartObjects
}  // namespace NsSmartDeviceLink