Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / src / OCRepresentation.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 /**
22  * @file
23  *
24  * This file contains the implementation of classes and its members related
25  * to OCRepresentation.
26  */
27
28
29 #include <OCRepresentation.h>
30
31 #include <boost/lexical_cast.hpp>
32 #include <algorithm>
33 #include "ocpayload.h"
34 #include "ocrandom.h"
35 #include "oic_malloc.h"
36 #include "oic_string.h"
37
38 namespace OC
39 {
40
41     void MessageContainer::setPayload(const OCPayload* rep)
42     {
43         switch(rep->type)
44         {
45             case PAYLOAD_TYPE_REPRESENTATION:
46                 setPayload(reinterpret_cast<const OCRepPayload*>(rep));
47                 break;
48             case PAYLOAD_TYPE_DEVICE:
49                 setPayload(reinterpret_cast<const OCDevicePayload*>(rep));
50                 break;
51             case PAYLOAD_TYPE_PLATFORM:
52                 setPayload(reinterpret_cast<const OCPlatformPayload*>(rep));
53                 break;
54             default:
55                 throw OC::OCException("Invalid Payload type in setPayload");
56                 break;
57         }
58     }
59
60     void MessageContainer::setPayload(const OCDevicePayload* payload)
61     {
62         OCRepresentation rep;
63         rep.setUri(payload->uri);
64         char uuidString[UUID_STRING_SIZE];
65         if(payload->sid && RAND_UUID_OK == OCConvertUuidToString(payload->sid, uuidString))
66         {
67             rep[OC_RSRVD_DEVICE_ID] = std::string(uuidString);
68         }
69         else
70         {
71             rep[OC_RSRVD_DEVICE_ID] = std::string();
72         }
73         rep[OC_RSRVD_DEVICE_NAME] = payload->deviceName ?
74             std::string(payload->deviceName) :
75             std::string();
76         rep[OC_RSRVD_SPEC_VERSION] = payload->specVersion ?
77             std::string(payload->specVersion) :
78             std::string();
79         rep[OC_RSRVD_DATA_MODEL_VERSION] = payload->dataModelVersion ?
80             std::string(payload->dataModelVersion) :
81             std::string();
82         m_reps.push_back(std::move(rep));
83     }
84
85     void MessageContainer::setPayload(const OCPlatformPayload* payload)
86     {
87         OCRepresentation rep;
88         rep.setUri(payload->uri);
89
90         rep[OC_RSRVD_PLATFORM_ID] = payload->info.platformID ?
91             std::string(payload->info.platformID) :
92             std::string();
93         rep[OC_RSRVD_MFG_NAME] = payload->info.manufacturerName ?
94             std::string(payload->info.manufacturerName) :
95             std::string();
96         rep[OC_RSRVD_MFG_URL] = payload->info.manufacturerUrl ?
97             std::string(payload->info.manufacturerUrl) :
98             std::string();
99         rep[OC_RSRVD_MODEL_NUM] = payload->info.modelNumber ?
100             std::string(payload->info.modelNumber) :
101             std::string();
102         rep[OC_RSRVD_MFG_DATE] = payload->info.dateOfManufacture ?
103             std::string(payload->info.dateOfManufacture) :
104             std::string();
105         rep[OC_RSRVD_PLATFORM_VERSION] = payload->info.platformVersion ?
106             std::string(payload->info.platformVersion) :
107             std::string();
108         rep[OC_RSRVD_OS_VERSION] = payload->info.operatingSystemVersion ?
109             std::string(payload->info.operatingSystemVersion) :
110             std::string();
111         rep[OC_RSRVD_HARDWARE_VERSION] = payload->info.hardwareVersion ?
112             std::string(payload->info.hardwareVersion) :
113             std::string();
114         rep[OC_RSRVD_FIRMWARE_VERSION] = payload->info.firmwareVersion ?
115             std::string(payload->info.firmwareVersion) :
116             std::string();
117         rep[OC_RSRVD_SUPPORT_URL] = payload->info.supportUrl ?
118             std::string(payload->info.supportUrl) :
119             std::string();
120         rep[OC_RSRVD_SYSTEM_TIME] = payload->info.systemTime ?
121             std::string(payload->info.systemTime) :
122             std::string();
123
124         m_reps.push_back(std::move(rep));
125     }
126
127     void MessageContainer::setPayload(const OCRepPayload* payload)
128     {
129         const OCRepPayload* pl = payload;
130         while(pl)
131         {
132             OCRepresentation cur;
133             cur.setPayload(pl);
134
135             pl = pl->next;
136             this->addRepresentation(cur);
137         }
138     }
139
140     OCRepPayload* MessageContainer::getPayload() const
141     {
142         OCRepPayload* root = nullptr;
143         for(const auto& r : representations())
144         {
145             if(!root)
146             {
147                 root = r.getPayload();
148             }
149             else
150             {
151                 OCRepPayloadAppend(root, r.getPayload());
152             }
153         }
154
155         return root;
156     }
157
158     const std::vector<OCRepresentation>& MessageContainer::representations() const
159     {
160         return m_reps;
161     }
162
163     void MessageContainer::addRepresentation(const OCRepresentation& rep)
164     {
165         m_reps.push_back(rep);
166     }
167 }
168
169 namespace OC
170 {
171     struct get_payload_array: boost::static_visitor<>
172     {
173         template<typename T>
174         void operator()(T& /*arr*/)
175         {
176             throw std::logic_error("Invalid calc_dimensions_visitor type");
177         }
178
179         template<typename T>
180         void operator()(std::vector<T>& arr)
181         {
182             root_size_calc<T>();
183             dimensions[0] = arr.size();
184             dimensions[1] = 0;
185             dimensions[2] = 0;
186             dimTotal = calcDimTotal(dimensions);
187
188             array = (void*)OICMalloc(dimTotal * root_size);
189
190             for(size_t i = 0; i < dimensions[0]; ++i)
191             {
192                 copy_to_array(arr[i], array, i);
193             }
194
195         }
196         template<typename T>
197         void operator()(std::vector<std::vector<T>>& arr)
198         {
199             root_size_calc<T>();
200             dimensions[0] = arr.size();
201             dimensions[1] = 0;
202             dimensions[2] = 0;
203             for(size_t i = 0; i < arr.size(); ++i)
204             {
205                 dimensions[1] = std::max(dimensions[1], arr[i].size());
206             }
207             dimTotal = calcDimTotal(dimensions);
208             array = (void*)OICCalloc(1, dimTotal * root_size);
209
210             for(size_t i = 0; i < dimensions[0]; ++i)
211             {
212                 for(size_t j = 0; j < dimensions[1] && j < arr[i].size(); ++j)
213                 {
214                     copy_to_array(arr[i][j], array, i*dimensions[1] + j);
215                 }
216             }
217         }
218         template<typename T>
219         void operator()(std::vector<std::vector<std::vector<T>>>& arr)
220         {
221             root_size_calc<T>();
222             dimensions[0] = arr.size();
223             dimensions[1] = 0;
224             dimensions[2] = 0;
225             for(size_t i = 0; i < arr.size(); ++i)
226             {
227                 dimensions[1] = std::max(dimensions[1], arr[i].size());
228
229                 for(size_t j = 0; j < arr[i].size(); ++j)
230                 {
231                     dimensions[2] = std::max(dimensions[2], arr[i][j].size());
232                 }
233             }
234
235             dimTotal = calcDimTotal(dimensions);
236             array = (void*)OICCalloc(1, dimTotal * root_size);
237
238             for(size_t i = 0; i < dimensions[0]; ++i)
239             {
240                 for(size_t j = 0; j < dimensions[1] && j < arr[i].size(); ++j)
241                 {
242                     for(size_t k = 0; k < dimensions[2] && k < arr[i][j].size(); ++k)
243                     {
244                         copy_to_array(arr[i][j][k], array,
245                                 dimensions[2] * j +
246                                 dimensions[2] * dimensions[1] * i +
247                                 k);
248                     }
249                 }
250             }
251         }
252
253         template<typename T>
254         void root_size_calc()
255         {
256             root_size = sizeof(T);
257         }
258
259         template<typename T>
260         void copy_to_array(T item, void* array, size_t pos)
261         {
262             ((T*)array)[pos] = item;
263         }
264
265         size_t dimensions[MAX_REP_ARRAY_DEPTH];
266         size_t root_size;
267         size_t dimTotal;
268         void* array;
269     };
270
271     template<>
272     void get_payload_array::root_size_calc<int>()
273     {
274         root_size = sizeof(int64_t);
275     }
276
277     template<>
278     void get_payload_array::root_size_calc<std::string>()
279     {
280         root_size = sizeof(char*);
281     }
282
283     template<>
284     void get_payload_array::root_size_calc<OC::OCRepresentation>()
285     {
286         root_size = sizeof(OCRepPayload*);
287     }
288
289     template<>
290     void get_payload_array::copy_to_array(int item, void* array, size_t pos)
291     {
292         ((int64_t*)array)[pos] = item;
293     }
294
295     template<>
296     void get_payload_array::copy_to_array(std::_Bit_reference br, void* array, size_t pos)
297     {
298         ((bool*)array)[pos] = static_cast<bool>(br);
299     }
300
301     template<>
302     void get_payload_array::copy_to_array(std::string item, void* array, size_t pos)
303     {
304         ((char**)array)[pos] = OICStrdup(item.c_str());
305     }
306
307     template<>
308     void get_payload_array::copy_to_array(std::string& item, void* array, size_t pos)
309     {
310         ((char**)array)[pos] = OICStrdup(item.c_str());
311     }
312
313     template<>
314     void get_payload_array::copy_to_array(const std::string& item, void* array, size_t pos)
315     {
316         ((char**)array)[pos] = OICStrdup(item.c_str());
317     }
318
319     template<>
320     void get_payload_array::copy_to_array(OC::OCRepresentation item, void* array, size_t pos)
321     {
322         ((OCRepPayload**)array)[pos] = item.getPayload();
323     }
324
325     void OCRepresentation::getPayloadArray(OCRepPayload* payload,
326                     const OCRepresentation::AttributeItem& item) const
327     {
328         get_payload_array vis{};
329         boost::apply_visitor(vis, m_values[item.attrname()]);
330
331
332         switch(item.base_type())
333         {
334             case AttributeType::Integer:
335                 OCRepPayloadSetIntArrayAsOwner(payload, item.attrname().c_str(),
336                         (int64_t*)vis.array,
337                         vis.dimensions);
338                 break;
339             case AttributeType::Double:
340                 OCRepPayloadSetDoubleArrayAsOwner(payload, item.attrname().c_str(),
341                         (double*)vis.array,
342                         vis.dimensions);
343                 break;
344             case AttributeType::Boolean:
345                 OCRepPayloadSetBoolArrayAsOwner(payload, item.attrname().c_str(),
346                         (bool*)vis.array,
347                         vis.dimensions);
348                 break;
349             case AttributeType::String:
350                 OCRepPayloadSetStringArrayAsOwner(payload, item.attrname().c_str(),
351                         (char**)vis.array,
352                         vis.dimensions);
353                 break;
354             case AttributeType::OCRepresentation:
355                 OCRepPayloadSetPropObjectArrayAsOwner(payload, item.attrname().c_str(),
356                         (OCRepPayload**)vis.array, vis.dimensions);
357                 break;
358             default:
359                 throw std::logic_error(std::string("GetPayloadArray: Not Implemented") +
360                         std::to_string((int)item.base_type()));
361         }
362     }
363
364     OCRepPayload* OCRepresentation::getPayload() const
365     {
366         OCRepPayload* root = OCRepPayloadCreate();
367         if(!root)
368         {
369             throw std::bad_alloc();
370         }
371
372         OCRepPayloadSetUri(root, getUri().c_str());
373
374         for(const std::string& type : getResourceTypes())
375         {
376             OCRepPayloadAddResourceType(root, type.c_str());
377         }
378
379         for(const std::string& iface : getResourceInterfaces())
380         {
381             OCRepPayloadAddInterface(root, iface.c_str());
382         }
383
384         for(auto& val : *this)
385         {
386             switch(val.type())
387             {
388                 case AttributeType::Null:
389                     OCRepPayloadSetNull(root, val.attrname().c_str());
390                     break;
391                 case AttributeType::Integer:
392                     OCRepPayloadSetPropInt(root, val.attrname().c_str(), static_cast<int>(val));
393                     break;
394                 case AttributeType::Double:
395                     OCRepPayloadSetPropDouble(root, val.attrname().c_str(),
396                             val.getValue<double>());
397                     break;
398                 case AttributeType::Boolean:
399                     OCRepPayloadSetPropBool(root, val.attrname().c_str(), val.getValue<bool>());
400                     break;
401                 case AttributeType::String:
402                     OCRepPayloadSetPropString(root, val.attrname().c_str(),
403                             static_cast<std::string>(val).c_str());
404                     break;
405                 case AttributeType::OCRepresentation:
406                     OCRepPayloadSetPropObjectAsOwner(root, val.attrname().c_str(),
407                             static_cast<OCRepresentation>(val).getPayload());
408                     break;
409                 case AttributeType::Vector:
410                     getPayloadArray(root, val);
411                     break;
412                 default:
413                     throw std::logic_error(std::string("Getpayload: Not Implemented") +
414                             std::to_string((int)val.type()));
415                     break;
416             }
417         }
418
419         return root;
420     }
421
422     size_t calcArrayDepth(const size_t dimensions[MAX_REP_ARRAY_DEPTH])
423     {
424         if(dimensions[0] == 0)
425         {
426             throw std::logic_error("invalid calcArrayDepth");
427         }
428         else if(dimensions[1] == 0)
429         {
430             return 1;
431         }
432         else if (dimensions[2] == 0)
433         {
434             return 2;
435         }
436         else
437         {
438             return 3;
439         }
440     }
441
442     template<typename T>
443     T OCRepresentation::payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl)
444     {
445         throw std::logic_error("payload_array_helper_copy: unsupported type");
446     }
447     template<>
448     int OCRepresentation::payload_array_helper_copy<int>(size_t index, const OCRepPayloadValue* pl)
449     {
450         return pl->arr.iArray[index];
451     }
452     template<>
453     double OCRepresentation::payload_array_helper_copy<double>(size_t index, const OCRepPayloadValue* pl)
454     {
455         return pl->arr.dArray[index];
456     }
457     template<>
458     bool OCRepresentation::payload_array_helper_copy<bool>(size_t index, const OCRepPayloadValue* pl)
459     {
460         return pl->arr.bArray[index];
461     }
462     template<>
463     std::string OCRepresentation::payload_array_helper_copy<std::string>(
464             size_t index, const OCRepPayloadValue* pl)
465     {
466         if (pl->arr.strArray[index])
467         {
468             return std::string(pl->arr.strArray[index]);
469         }
470         else
471         {
472             return std::string{};
473         }
474     }
475     template<>
476     OCRepresentation OCRepresentation::payload_array_helper_copy<OCRepresentation>(
477             size_t index, const OCRepPayloadValue* pl)
478     {
479         OCRepresentation r;
480         if (pl->arr.objArray[index])
481         {
482             r.setPayload(pl->arr.objArray[index]);
483         }
484         return r;
485     }
486
487     template<typename T>
488     void OCRepresentation::payload_array_helper(const OCRepPayloadValue* pl, size_t depth)
489     {
490         if(depth == 1)
491         {
492             std::vector<T> val(pl->arr.dimensions[0]);
493
494             for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
495             {
496                 val[i] = payload_array_helper_copy<T>(i, pl);
497             }
498             this->setValue(std::string(pl->name), val);
499         }
500         else if (depth == 2)
501         {
502             std::vector<std::vector<T>> val(pl->arr.dimensions[0]);
503             for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
504             {
505                 val[i].resize(pl->arr.dimensions[1]);
506                 for(size_t j = 0; j < pl->arr.dimensions[1]; ++j)
507                 {
508                     val[i][j] = payload_array_helper_copy<T>(
509                             i * pl->arr.dimensions[1] + j, pl);
510                 }
511             }
512             this->setValue(std::string(pl->name), val);
513         }
514         else if (depth == 3)
515         {
516             std::vector<std::vector<std::vector<T>>> val(pl->arr.dimensions[0]);
517             for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
518             {
519                 val[i].resize(pl->arr.dimensions[1]);
520                 for(size_t j = 0; j < pl->arr.dimensions[1]; ++j)
521                 {
522                     val[i][j].resize(pl->arr.dimensions[2]);
523                     for(size_t k = 0; k < pl->arr.dimensions[2]; ++k)
524                     {
525                         val[i][j][k] = payload_array_helper_copy<T>(
526                                 pl->arr.dimensions[2] * j +
527                                 pl->arr.dimensions[2] * pl->arr.dimensions[1] * i +
528                                 k,
529                                 pl);
530                     }
531                 }
532             }
533             this->setValue(std::string(pl->name), val);
534         }
535         else
536         {
537             throw std::logic_error("Invalid depth in payload_array_helper");
538         }
539     }
540
541     void OCRepresentation::setPayloadArray(const OCRepPayloadValue* pl)
542     {
543
544         switch(pl->arr.type)
545         {
546             case OCREP_PROP_INT:
547                 payload_array_helper<int>(pl, calcArrayDepth(pl->arr.dimensions));
548                 break;
549             case OCREP_PROP_DOUBLE:
550                 payload_array_helper<double>(pl, calcArrayDepth(pl->arr.dimensions));
551                 break;
552             case OCREP_PROP_BOOL:
553                 payload_array_helper<bool>(pl, calcArrayDepth(pl->arr.dimensions));
554                 break;
555             case OCREP_PROP_STRING:
556                 payload_array_helper<std::string>(pl, calcArrayDepth(pl->arr.dimensions));
557                 break;
558             case OCREP_PROP_OBJECT:
559                 payload_array_helper<OCRepresentation>(pl, calcArrayDepth(pl->arr.dimensions));
560                 break;
561             default:
562                 throw std::logic_error("setPayload array invalid type");
563                 break;
564         }
565     }
566
567     void OCRepresentation::setPayload(const OCRepPayload* pl)
568     {
569         setUri(pl->uri);
570
571         OCStringLL* ll = pl->types;
572         while(ll)
573         {
574             addResourceType(ll->value);
575             ll = ll->next;
576         }
577
578         ll = pl->interfaces;
579         while(ll)
580         {
581             addResourceInterface(ll->value);
582             ll = ll->next;
583         }
584
585         OCRepPayloadValue* val = pl->values;
586
587         while(val)
588         {
589             switch(val->type)
590             {
591                 case OCREP_PROP_NULL:
592                     setNULL(val->name);
593                     break;
594                 case OCREP_PROP_INT:
595                     setValue<int>(val->name, val->i);
596                     break;
597                 case OCREP_PROP_DOUBLE:
598                     setValue<double>(val->name, val->d);
599                     break;
600                 case OCREP_PROP_BOOL:
601                     setValue<bool>(val->name, val->b);
602                     break;
603                 case OCREP_PROP_STRING:
604                     setValue<std::string>(val->name, val->str);
605                     break;
606                 case OCREP_PROP_OBJECT:
607                     {
608                         OCRepresentation cur;
609                         cur.setPayload(val->obj);
610                         setValue<OCRepresentation>(val->name, cur);
611                     }
612                     break;
613                 case OCREP_PROP_ARRAY:
614                     setPayloadArray(val);
615                     break;
616                 default:
617                     throw std::logic_error(std::string("Not Implemented!") +
618                             std::to_string((int)val->type));
619                     break;
620             }
621             val = val->next;
622         }
623     }
624
625     void OCRepresentation::addChild(const OCRepresentation& rep)
626     {
627         m_children.push_back(rep);
628     }
629
630     void OCRepresentation::clearChildren()
631     {
632         m_children.clear();
633     }
634
635     const std::vector<OCRepresentation>& OCRepresentation::getChildren() const
636     {
637         return m_children;
638     }
639
640     void OCRepresentation::setChildren(const std::vector<OCRepresentation>& children)
641     {
642         m_children = children;
643     }
644     void OCRepresentation::setUri(const char* uri)
645     {
646         m_uri = uri ? uri : "";
647     }
648
649     void OCRepresentation::setUri(const std::string& uri)
650     {
651         m_uri = uri;
652     }
653
654     std::string OCRepresentation::getUri() const
655     {
656         return m_uri;
657     }
658
659     const std::vector<std::string>& OCRepresentation::getResourceTypes() const
660     {
661         return m_resourceTypes;
662     }
663
664     void OCRepresentation::setResourceTypes(const std::vector<std::string>& resourceTypes)
665     {
666         m_resourceTypes = resourceTypes;
667     }
668
669     void OCRepresentation::addResourceType(const std::string& str)
670     {
671         m_resourceTypes.push_back(str);
672     }
673
674     const std::vector<std::string>& OCRepresentation::getResourceInterfaces() const
675     {
676         return m_interfaces;
677     }
678
679     void OCRepresentation::addResourceInterface(const std::string& str)
680     {
681         m_interfaces.push_back(str);
682     }
683
684     void OCRepresentation::setResourceInterfaces(const std::vector<std::string>& resourceInterfaces)
685     {
686         m_interfaces = resourceInterfaces;
687     }
688
689     bool OCRepresentation::hasAttribute(const std::string& str) const
690     {
691         return m_values.find(str) != m_values.end();
692     }
693
694     bool OCRepresentation::emptyData() const
695     {
696         // This logic is meant to determine whether based on the JSON serialization rules
697         // if this object will result in empty JSON.  URI is only serialized if there is valid
698         // data, ResourceType and Interfaces are only serialized if we are a nothing, a
699         // child of a default or link item.
700         // Our values array is only printed in the if we are the child of a Batch resource,
701         // the parent in a 'default' situation, or not in a child/parent relationship.
702         if(!m_uri.empty())
703         {
704             return false;
705         }
706         else if ((m_interfaceType == InterfaceType::None
707                         || m_interfaceType==InterfaceType::DefaultChild
708                         || m_interfaceType==InterfaceType::LinkChild)
709                     && (m_resourceTypes.size()>0 || m_interfaces.size()>0))
710         {
711             return false;
712         }
713         else if((m_interfaceType == InterfaceType::None
714                         || m_interfaceType == InterfaceType::BatchChild
715                         || m_interfaceType == InterfaceType::DefaultParent)
716                     && m_values.size()>0)
717         {
718             return false;
719         }
720
721         if(m_children.size() > 0)
722         {
723             return false;
724         }
725
726         return true;
727     }
728
729     int OCRepresentation::numberOfAttributes() const
730     {
731         return m_values.size();
732     }
733
734     bool OCRepresentation::erase(const std::string& str)
735     {
736         return m_values.erase(str);
737     }
738
739     void OCRepresentation::setNULL(const std::string& str)
740     {
741         m_values[str] = OC::NullType();
742     }
743
744     bool OCRepresentation::isNULL(const std::string& str) const
745     {
746         auto x = m_values.find(str);
747
748         if(m_values.end() != x)
749         {
750             return x->second.which() == AttributeValueNullIndex;
751         }
752         else
753         {
754             throw OCException(OC::Exception::INVALID_ATTRIBUTE+ str);
755         }
756     }
757 }
758
759 namespace OC
760 {
761     std::ostream& operator <<(std::ostream& os, const AttributeType at)
762     {
763         switch(at)
764         {
765             case AttributeType::Null:
766                 os << "Null";
767                 break;
768             case AttributeType::Integer:
769                 os << "Integer";
770                 break;
771             case AttributeType::Double:
772                 os << "Double";
773                 break;
774             case AttributeType::Boolean:
775                 os << "Boolean";
776                 break;
777             case AttributeType::String:
778                 os << "String";
779                 break;
780             case AttributeType::OCRepresentation:
781                 os << "OCRepresentation";
782                 break;
783             case AttributeType::Vector:
784                 os << "Vector";
785                 break;
786         }
787         return os;
788     }
789 }
790
791 // STL Container For OCRepresentation
792 namespace OC
793 {
794     OCRepresentation::AttributeItem::AttributeItem(const std::string& name,
795             std::map<std::string, AttributeValue>& vals):
796             m_attrName(name), m_values(vals){}
797
798     OCRepresentation::AttributeItem OCRepresentation::operator[](const std::string& key)
799     {
800         OCRepresentation::AttributeItem attr{key, m_values};
801         return std::move(attr);
802     }
803
804     const OCRepresentation::AttributeItem OCRepresentation::operator[](const std::string& key) const
805     {
806         OCRepresentation::AttributeItem attr{key, m_values};
807         return std::move(attr);
808     }
809
810     const std::string& OCRepresentation::AttributeItem::attrname() const
811     {
812         return m_attrName;
813     }
814
815     template<typename T, typename = void>
816     struct type_info
817     {
818         // contains the actual type
819         typedef T type;
820         // contains the inner most vector-type
821         typedef T base_type;
822         // contains the AttributeType for this item
823         constexpr static AttributeType enum_type =
824             AttributeTypeConvert<T>::type;
825         // contains the AttributeType for this base-type
826         constexpr static AttributeType enum_base_type =
827             AttributeTypeConvert<T>::type;
828         // depth of the vector
829         constexpr static size_t depth = 0;
830     };
831
832     template<typename T>
833     struct type_info<T, typename std::enable_if<is_vector<T>::value>::type>
834     {
835         typedef T type;
836         typedef typename type_info<typename T::value_type>::base_type base_type;
837         constexpr static AttributeType enum_type = AttributeType::Vector;
838         constexpr static AttributeType enum_base_type =
839             type_info<typename T::value_type>::enum_base_type;
840         constexpr static size_t depth = 1 +
841             type_info<typename T::value_type>::depth;
842     };
843
844     struct type_introspection_visitor : boost::static_visitor<>
845     {
846         AttributeType type;
847         AttributeType base_type;
848         size_t depth;
849
850         type_introspection_visitor() : boost::static_visitor<>(),
851             type(AttributeType::Null), base_type(AttributeType::Null), depth(0){}
852
853         template <typename T>
854         void operator()(T const& /*item*/)
855         {
856             type = type_info<T>::enum_type;
857             base_type = type_info<T>::enum_base_type;
858             depth = type_info<T>::depth;
859         }
860     };
861
862     AttributeType OCRepresentation::AttributeItem::type() const
863     {
864         type_introspection_visitor vis;
865         boost::apply_visitor(vis, m_values[m_attrName]);
866         return vis.type;
867     }
868
869     AttributeType OCRepresentation::AttributeItem::base_type() const
870     {
871         type_introspection_visitor vis;
872         boost::apply_visitor(vis, m_values[m_attrName]);
873         return vis.base_type;
874     }
875
876     size_t OCRepresentation::AttributeItem::depth() const
877     {
878         type_introspection_visitor vis;
879         boost::apply_visitor(vis, m_values[m_attrName]);
880         return vis.depth;
881     }
882
883     OCRepresentation::iterator OCRepresentation::begin()
884     {
885         return OCRepresentation::iterator(m_values.begin(), m_values);
886     }
887
888     OCRepresentation::const_iterator OCRepresentation::begin() const
889     {
890          return OCRepresentation::const_iterator(m_values.begin(), m_values);
891     }
892
893     OCRepresentation::const_iterator OCRepresentation::cbegin() const
894     {
895         return OCRepresentation::const_iterator(m_values.cbegin(), m_values);
896     }
897
898     OCRepresentation::iterator OCRepresentation::end()
899     {
900         return OCRepresentation::iterator(m_values.end(), m_values);
901     }
902
903     OCRepresentation::const_iterator OCRepresentation::end() const
904     {
905         return OCRepresentation::const_iterator(m_values.end(), m_values);
906     }
907
908     OCRepresentation::const_iterator OCRepresentation::cend() const
909     {
910         return OCRepresentation::const_iterator(m_values.cend(), m_values);
911     }
912
913     size_t OCRepresentation::size() const
914     {
915         return m_values.size();
916     }
917
918     bool OCRepresentation::empty() const
919     {
920         return m_values.empty();
921     }
922
923     bool OCRepresentation::iterator::operator==(const OCRepresentation::iterator& rhs) const
924     {
925         return m_iterator == rhs.m_iterator;
926     }
927
928     bool OCRepresentation::iterator::operator!=(const OCRepresentation::iterator& rhs) const
929     {
930         return m_iterator != rhs.m_iterator;
931     }
932
933     bool OCRepresentation::const_iterator::operator==(
934             const OCRepresentation::const_iterator& rhs) const
935     {
936         return m_iterator == rhs.m_iterator;
937     }
938
939     bool OCRepresentation::const_iterator::operator!=(
940             const OCRepresentation::const_iterator& rhs) const
941     {
942         return m_iterator != rhs.m_iterator;
943     }
944
945     OCRepresentation::iterator::reference OCRepresentation::iterator::operator*()
946     {
947         return m_item;
948     }
949
950     OCRepresentation::const_iterator::const_reference
951         OCRepresentation::const_iterator::operator*() const
952     {
953         return m_item;
954     }
955
956     OCRepresentation::iterator::pointer OCRepresentation::iterator::operator->()
957     {
958         return &m_item;
959     }
960
961     OCRepresentation::const_iterator::const_pointer
962         OCRepresentation::const_iterator::operator->() const
963     {
964         return &m_item;
965     }
966
967     OCRepresentation::iterator& OCRepresentation::iterator::operator++()
968     {
969         m_iterator++;
970         if(m_iterator != m_item.m_values.end())
971         {
972             m_item.m_attrName = m_iterator->first;
973         }
974         else
975         {
976             m_item.m_attrName = "";
977         }
978         return *this;
979     }
980
981     OCRepresentation::const_iterator& OCRepresentation::const_iterator::operator++()
982     {
983         m_iterator++;
984         if(m_iterator != m_item.m_values.end())
985         {
986             m_item.m_attrName = m_iterator->first;
987         }
988         else
989         {
990             m_item.m_attrName = "";
991         }
992         return *this;
993     }
994
995     OCRepresentation::iterator OCRepresentation::iterator::operator++(int)
996     {
997         OCRepresentation::iterator itr(*this);
998         ++(*this);
999         return itr;
1000     }
1001
1002     OCRepresentation::const_iterator OCRepresentation::const_iterator::operator++(int)
1003     {
1004         OCRepresentation::const_iterator itr(*this);
1005         ++(*this);
1006         return itr;
1007     }
1008
1009     struct to_string_visitor : boost::static_visitor<>
1010     {
1011         std::string str;
1012         template <typename T>
1013         void operator()(T const& item)
1014         {
1015             str = boost::lexical_cast<std::string>(item);
1016         }
1017
1018         template <typename T>
1019         void operator()(std::vector<T> const& item)
1020         {
1021             to_string_visitor vis;
1022             std::ostringstream stream;
1023             stream << "[";
1024
1025             for(const auto& i : item)
1026             {
1027                 vis(i);
1028                 stream << vis.str  << " ";
1029             }
1030             stream << "]";
1031             str = stream.str();
1032         }
1033     };
1034
1035     template<>
1036     void to_string_visitor::operator()(bool const& item)
1037     {
1038         str = item ? "true" : "false";
1039     }
1040
1041     template<>
1042     void to_string_visitor::operator()(std::string const& item)
1043     {
1044         str = item;
1045     }
1046
1047     template<>
1048     void to_string_visitor::operator()(NullType const& /*item*/)
1049     {
1050         str = "(null)";
1051     }
1052
1053     template<>
1054     void to_string_visitor::operator()(OCRepresentation const& /*item*/)
1055     {
1056         str = "OC::OCRepresentation";
1057     }
1058
1059     std::string OCRepresentation::getValueToString(const std::string& key) const
1060     {
1061         auto x = m_values.find(key);
1062         if(x != m_values.end())
1063         {
1064             to_string_visitor vis;
1065             boost::apply_visitor(vis, x->second);
1066             return std::move(vis.str);
1067         }
1068
1069         return "";
1070     }
1071
1072     std::string OCRepresentation::AttributeItem::getValueToString() const
1073     {
1074         to_string_visitor vis;
1075         boost::apply_visitor(vis, m_values[m_attrName]);
1076         return std::move(vis.str);
1077     }
1078
1079     std::ostream& operator<<(std::ostream& os, const OCRepresentation::AttributeItem& ai)
1080     {
1081         os << ai.getValueToString();
1082         return os;
1083     }
1084 }
1085