Merge branch 'master' into easysetup & CBOR changes
[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             dimTotal = calcDimTotal(dimensions);
185
186             array = (void*)OICMalloc(dimTotal * root_size);
187
188             for(size_t i = 0; i < dimensions[0]; ++i)
189             {
190                 copy_to_array(arr[i], array, i);
191             }
192
193         }
194         template<typename T>
195         void operator()(std::vector<std::vector<T>>& arr)
196         {
197             root_size_calc<T>();
198             dimensions[0] = arr.size();
199             for(size_t i = 0; i < arr.size(); ++i)
200             {
201                 dimensions[1] = std::max(dimensions[1], arr[i].size());
202             }
203             dimTotal = calcDimTotal(dimensions);
204             array = (void*)OICCalloc(1, dimTotal * root_size);
205
206             for(size_t i = 0; i < dimensions[0]; ++i)
207             {
208                 for(size_t j = 0; j < dimensions[1] && j < arr[i].size(); ++j)
209                 {
210                     copy_to_array(arr[i][j], array, i*dimensions[1] + j);
211                 }
212             }
213         }
214         template<typename T>
215         void operator()(std::vector<std::vector<std::vector<T>>>& arr)
216         {
217             root_size_calc<T>();
218             dimensions[0] = arr.size();
219             for(size_t i = 0; i < arr.size(); ++i)
220             {
221                 dimensions[1] = std::max(dimensions[1], arr[i].size());
222
223                 for(size_t j = 0; j < arr[i].size(); ++j)
224                 {
225                     dimensions[2] = std::max(dimensions[2], arr[i][j].size());
226                 }
227             }
228
229             dimTotal = calcDimTotal(dimensions);
230             array = (void*)OICCalloc(1, dimTotal * root_size);
231
232             for(size_t i = 0; i < dimensions[0]; ++i)
233             {
234                 for(size_t j = 0; j < dimensions[1] && j < arr[i].size(); ++j)
235                 {
236                     for(size_t k = 0; k < dimensions[2] && k < arr[i][j].size(); ++k)
237                     {
238                         copy_to_array(arr[i][j][k], array,
239                                 dimensions[2] * j +
240                                 dimensions[2] * dimensions[1] * i +
241                                 k);
242                     }
243                 }
244             }
245         }
246
247         template<typename T>
248         void root_size_calc()
249         {
250             root_size = sizeof(T);
251         }
252
253         template<typename T>
254         void copy_to_array(T item, void* array, size_t pos)
255         {
256             ((T*)array)[pos] = item;
257         }
258
259         size_t dimensions[MAX_REP_ARRAY_DEPTH];
260         size_t root_size;
261         size_t dimTotal;
262         void* array;
263     };
264
265     template<>
266     void get_payload_array::root_size_calc<int>()
267     {
268         root_size = sizeof(int64_t);
269     }
270
271     template<>
272     void get_payload_array::root_size_calc<std::string>()
273     {
274         root_size = sizeof(char*);
275     }
276
277     template<>
278     void get_payload_array::root_size_calc<OC::OCRepresentation>()
279     {
280         root_size = sizeof(OCRepPayload*);
281     }
282
283     template<>
284     void get_payload_array::copy_to_array(int item, void* array, size_t pos)
285     {
286         ((int64_t*)array)[pos] = item;
287     }
288
289     template<>
290     void get_payload_array::copy_to_array(std::_Bit_reference br, void* array, size_t pos)
291     {
292         ((bool*)array)[pos] = static_cast<bool>(br);
293     }
294
295     template<>
296     void get_payload_array::copy_to_array(const std::string& item, void* array, size_t pos)
297     {
298         ((char**)array)[pos] = OICStrdup(item.c_str());
299     }
300
301     template<>
302     void get_payload_array::copy_to_array(OC::OCRepresentation item, void* array, size_t pos)
303     {
304         ((OCRepPayload**)array)[pos] = item.getPayload();
305     }
306
307     void OCRepresentation::getPayloadArray(OCRepPayload* payload,
308                     const OCRepresentation::AttributeItem& item) const
309     {
310         get_payload_array vis{};
311         boost::apply_visitor(vis, m_values[item.attrname()]);
312
313
314         switch(item.base_type())
315         {
316             case AttributeType::Integer:
317                 OCRepPayloadSetIntArrayAsOwner(payload, item.attrname().c_str(),
318                         (int64_t*)vis.array,
319                         vis.dimensions);
320                 break;
321             case AttributeType::Double:
322                 OCRepPayloadSetDoubleArrayAsOwner(payload, item.attrname().c_str(),
323                         (double*)vis.array,
324                         vis.dimensions);
325                 break;
326             case AttributeType::Boolean:
327                 OCRepPayloadSetBoolArrayAsOwner(payload, item.attrname().c_str(),
328                         (bool*)vis.array,
329                         vis.dimensions);
330                 break;
331             case AttributeType::String:
332                 OCRepPayloadSetStringArrayAsOwner(payload, item.attrname().c_str(),
333                         (char**)vis.array,
334                         vis.dimensions);
335                 break;
336             case AttributeType::OCRepresentation:
337                 OCRepPayloadSetPropObjectArrayAsOwner(payload, item.attrname().c_str(),
338                         (OCRepPayload**)vis.array, vis.dimensions);
339                 break;
340             default:
341                 throw std::logic_error(std::string("GetPayloadArray: Not Implemented") +
342                         std::to_string((int)item.base_type()));
343         }
344     }
345
346     OCRepPayload* OCRepresentation::getPayload() const
347     {
348         OCRepPayload* root = OCRepPayloadCreate();
349         if(!root)
350         {
351             throw std::bad_alloc();
352         }
353
354         OCRepPayloadSetUri(root, getUri().c_str());
355
356         for(const std::string& type : getResourceTypes())
357         {
358             OCRepPayloadAddResourceType(root, type.c_str());
359         }
360
361         for(const std::string& iface : getResourceInterfaces())
362         {
363             OCRepPayloadAddInterface(root, iface.c_str());
364         }
365
366         for(auto& val : *this)
367         {
368             switch(val.type())
369             {
370                 case AttributeType::Null:
371                     OCRepPayloadSetNull(root, val.attrname().c_str());
372                     break;
373                 case AttributeType::Integer:
374                     OCRepPayloadSetPropInt(root, val.attrname().c_str(), static_cast<int>(val));
375                     break;
376                 case AttributeType::Double:
377                     OCRepPayloadSetPropDouble(root, val.attrname().c_str(),
378                             val.getValue<double>());
379                     break;
380                 case AttributeType::Boolean:
381                     OCRepPayloadSetPropBool(root, val.attrname().c_str(), val.getValue<bool>());
382                     break;
383                 case AttributeType::String:
384                     OCRepPayloadSetPropString(root, val.attrname().c_str(),
385                             static_cast<std::string>(val).c_str());
386                     break;
387                 case AttributeType::OCRepresentation:
388                     OCRepPayloadSetPropObjectAsOwner(root, val.attrname().c_str(),
389                             static_cast<OCRepresentation>(val).getPayload());
390                     break;
391                 case AttributeType::Vector:
392                     getPayloadArray(root, val);
393                     break;
394                 default:
395                     throw std::logic_error(std::string("Getpayload: Not Implemented") +
396                             std::to_string((int)val.type()));
397                     break;
398             }
399         }
400
401         return root;
402     }
403
404     size_t calcArrayDepth(const size_t dimensions[MAX_REP_ARRAY_DEPTH])
405     {
406         if(dimensions[0] == 0)
407         {
408             throw std::logic_error("invalid calcArrayDepth");
409         }
410         else if(dimensions[1] == 0)
411         {
412             return 1;
413         }
414         else if (dimensions[2] == 0)
415         {
416             return 2;
417         }
418         else
419         {
420             return 3;
421         }
422     }
423
424     template<typename T>
425     T OCRepresentation::payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl)
426     {
427         throw std::logic_error("payload_array_helper_copy: unsupported type");
428     }
429     template<>
430     int OCRepresentation::payload_array_helper_copy<int>(size_t index, const OCRepPayloadValue* pl)
431     {
432         return pl->arr.iArray[index];
433     }
434     template<>
435     double OCRepresentation::payload_array_helper_copy<double>(size_t index, const OCRepPayloadValue* pl)
436     {
437         return pl->arr.dArray[index];
438     }
439     template<>
440     bool OCRepresentation::payload_array_helper_copy<bool>(size_t index, const OCRepPayloadValue* pl)
441     {
442         return pl->arr.bArray[index];
443     }
444     template<>
445     std::string OCRepresentation::payload_array_helper_copy<std::string>(
446             size_t index, const OCRepPayloadValue* pl)
447     {
448         return std::string(pl->arr.strArray[index]);
449     }
450     template<>
451     OCRepresentation OCRepresentation::payload_array_helper_copy<OCRepresentation>(
452             size_t index, const OCRepPayloadValue* pl)
453     {
454         OCRepresentation r;
455         r.setPayload(pl->arr.objArray[index]);
456         return r;
457     }
458
459     template<typename T>
460     void OCRepresentation::payload_array_helper(const OCRepPayloadValue* pl, size_t depth)
461     {
462         if(depth == 1)
463         {
464             std::vector<T> val(pl->arr.dimensions[0]);
465
466             for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
467             {
468                 val[i] = payload_array_helper_copy<T>(i, pl);
469             }
470             this->setValue(std::string(pl->name), val);
471         }
472         else if (depth == 2)
473         {
474             std::vector<std::vector<T>> val(pl->arr.dimensions[0]);
475             for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
476             {
477                 val[i].reserve(pl->arr.dimensions[1]);
478                 for(size_t j = 0; j < pl->arr.dimensions[1]; ++j)
479                 {
480                     val[i][j] = payload_array_helper_copy<T>(
481                             i * pl->arr.dimensions[1] + j, pl);
482                 }
483             }
484             this->setValue(std::string(pl->name), val);
485         }
486         else if (depth == 3)
487         {
488             std::vector<std::vector<std::vector<T>>> val;
489             for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
490             {
491                 val[i].reserve(pl->arr.dimensions[1]);
492                 for(size_t j = 0; j < pl->arr.dimensions[1]; ++j)
493                 {
494                     val[i][j].reserve(pl->arr.dimensions[2]);
495                     for(size_t k = 0; k < pl->arr.dimensions[2]; ++k)
496                     {
497                         val[i][j][k] = payload_array_helper_copy<T>(
498                                 pl->arr.dimensions[2] * j +
499                                 pl->arr.dimensions[2] * pl->arr.dimensions[1] * i +
500                                 k,
501                                 pl);
502                     }
503                 }
504             }
505             this->setValue(std::string(pl->name), val);
506         }
507         else
508         {
509             throw std::logic_error("Invalid depth in payload_array_helper");
510         }
511     }
512
513     void OCRepresentation::setPayloadArray(const OCRepPayloadValue* pl)
514     {
515
516         switch(pl->arr.type)
517         {
518             case OCREP_PROP_INT:
519                 payload_array_helper<int>(pl, calcArrayDepth(pl->arr.dimensions));
520                 break;
521             case OCREP_PROP_DOUBLE:
522                 payload_array_helper<double>(pl, calcArrayDepth(pl->arr.dimensions));
523                 break;
524             case OCREP_PROP_BOOL:
525                 payload_array_helper<bool>(pl, calcArrayDepth(pl->arr.dimensions));
526                 break;
527             case OCREP_PROP_STRING:
528                 payload_array_helper<std::string>(pl, calcArrayDepth(pl->arr.dimensions));
529                 break;
530             case OCREP_PROP_OBJECT:
531                 payload_array_helper<OCRepresentation>(pl, calcArrayDepth(pl->arr.dimensions));
532                 break;
533             default:
534                 throw std::logic_error("setPayload array invalid type");
535                 break;
536         }
537     }
538
539     void OCRepresentation::setPayload(const OCRepPayload* pl)
540     {
541         setUri(pl->uri);
542
543         OCStringLL* ll = pl->types;
544         while(ll)
545         {
546             addResourceType(ll->value);
547             ll = ll->next;
548         }
549
550         ll = pl->interfaces;
551         while(ll)
552         {
553             addResourceInterface(ll->value);
554             ll = ll->next;
555         }
556
557         OCRepPayloadValue* val = pl->values;
558
559         while(val)
560         {
561             switch(val->type)
562             {
563                 case OCREP_PROP_NULL:
564                     setNULL(val->name);
565                     break;
566                 case OCREP_PROP_INT:
567                     setValue<int>(val->name, val->i);
568                     break;
569                 case OCREP_PROP_DOUBLE:
570                     setValue<double>(val->name, val->d);
571                     break;
572                 case OCREP_PROP_BOOL:
573                     setValue<bool>(val->name, val->b);
574                     break;
575                 case OCREP_PROP_STRING:
576                     setValue<std::string>(val->name, val->str);
577                     break;
578                 case OCREP_PROP_OBJECT:
579                     {
580                         OCRepresentation cur;
581                         cur.setPayload(val->obj);
582                         setValue<OCRepresentation>(val->name, cur);
583                     }
584                     break;
585                 case OCREP_PROP_ARRAY:
586                     setPayloadArray(val);
587                     break;
588                 default:
589                     throw std::logic_error(std::string("Not Implemented!") +
590                             std::to_string((int)val->type));
591                     break;
592             }
593             val = val->next;
594         }
595     }
596
597     void OCRepresentation::addChild(const OCRepresentation& rep)
598     {
599         m_children.push_back(rep);
600     }
601
602     void OCRepresentation::clearChildren()
603     {
604         m_children.clear();
605     }
606
607     const std::vector<OCRepresentation>& OCRepresentation::getChildren() const
608     {
609         return m_children;
610     }
611
612     void OCRepresentation::setChildren(const std::vector<OCRepresentation>& children)
613     {
614         m_children = children;
615     }
616     void OCRepresentation::setUri(const char* uri)
617     {
618         m_uri = uri ? uri : "";
619     }
620
621     void OCRepresentation::setUri(const std::string& uri)
622     {
623         m_uri = uri;
624     }
625
626     std::string OCRepresentation::getUri() const
627     {
628         return m_uri;
629     }
630
631     const std::vector<std::string>& OCRepresentation::getResourceTypes() const
632     {
633         return m_resourceTypes;
634     }
635
636     void OCRepresentation::setResourceTypes(const std::vector<std::string>& resourceTypes)
637     {
638         m_resourceTypes = resourceTypes;
639     }
640
641     void OCRepresentation::addResourceType(const std::string& str)
642     {
643         m_resourceTypes.push_back(str);
644     }
645
646     const std::vector<std::string>& OCRepresentation::getResourceInterfaces() const
647     {
648         return m_interfaces;
649     }
650
651     void OCRepresentation::addResourceInterface(const std::string& str)
652     {
653         m_interfaces.push_back(str);
654     }
655
656     void OCRepresentation::setResourceInterfaces(const std::vector<std::string>& resourceInterfaces)
657     {
658         m_interfaces = resourceInterfaces;
659     }
660
661     bool OCRepresentation::hasAttribute(const std::string& str) const
662     {
663         return m_values.find(str) != m_values.end();
664     }
665
666     bool OCRepresentation::emptyData() const
667     {
668         // This logic is meant to determine whether based on the JSON serialization rules
669         // if this object will result in empty JSON.  URI is only serialized if there is valid
670         // data, ResourceType and Interfaces are only serialized if we are a nothing, a
671         // child of a default or link item.
672         // Our values array is only printed in the if we are the child of a Batch resource,
673         // the parent in a 'default' situation, or not in a child/parent relationship.
674         if(!m_uri.empty())
675         {
676             return false;
677         }
678         else if ((m_interfaceType == InterfaceType::None
679                         || m_interfaceType==InterfaceType::DefaultChild
680                         || m_interfaceType==InterfaceType::LinkChild)
681                     && (m_resourceTypes.size()>0 || m_interfaces.size()>0))
682         {
683             return false;
684         }
685         else if((m_interfaceType == InterfaceType::None
686                         || m_interfaceType == InterfaceType::BatchChild
687                         || m_interfaceType == InterfaceType::DefaultParent)
688                     && m_values.size()>0)
689         {
690             return false;
691         }
692
693         if(m_children.size() > 0)
694         {
695             return false;
696         }
697
698         return true;
699     }
700
701     int OCRepresentation::numberOfAttributes() const
702     {
703         return m_values.size();
704     }
705
706     bool OCRepresentation::erase(const std::string& str)
707     {
708         return m_values.erase(str);
709     }
710
711     void OCRepresentation::setNULL(const std::string& str)
712     {
713         m_values[str] = OC::NullType();
714     }
715
716     bool OCRepresentation::isNULL(const std::string& str) const
717     {
718         auto x = m_values.find(str);
719
720         if(m_values.end() != x)
721         {
722             return x->second.which() == AttributeValueNullIndex;
723         }
724         else
725         {
726             throw OCException(OC::Exception::INVALID_ATTRIBUTE+ str);
727         }
728     }
729 }
730
731 namespace OC
732 {
733     std::ostream& operator <<(std::ostream& os, const AttributeType at)
734     {
735         switch(at)
736         {
737             case AttributeType::Null:
738                 os << "Null";
739                 break;
740             case AttributeType::Integer:
741                 os << "Integer";
742                 break;
743             case AttributeType::Double:
744                 os << "Double";
745                 break;
746             case AttributeType::Boolean:
747                 os << "Boolean";
748                 break;
749             case AttributeType::String:
750                 os << "String";
751                 break;
752             case AttributeType::OCRepresentation:
753                 os << "OCRepresentation";
754                 break;
755             case AttributeType::Vector:
756                 os << "Vector";
757                 break;
758         }
759         return os;
760     }
761 }
762
763 // STL Container For OCRepresentation
764 namespace OC
765 {
766     OCRepresentation::AttributeItem::AttributeItem(const std::string& name,
767             std::map<std::string, AttributeValue>& vals):
768             m_attrName(name), m_values(vals){}
769
770     OCRepresentation::AttributeItem OCRepresentation::operator[](const std::string& key)
771     {
772         OCRepresentation::AttributeItem attr{key, m_values};
773         return std::move(attr);
774     }
775
776     const OCRepresentation::AttributeItem OCRepresentation::operator[](const std::string& key) const
777     {
778         OCRepresentation::AttributeItem attr{key, m_values};
779         return std::move(attr);
780     }
781
782     const std::string& OCRepresentation::AttributeItem::attrname() const
783     {
784         return m_attrName;
785     }
786
787     template<typename T, typename = void>
788     struct type_info
789     {
790         // contains the actual type
791         typedef T type;
792         // contains the inner most vector-type
793         typedef T base_type;
794         // contains the AttributeType for this item
795         constexpr static AttributeType enum_type =
796             AttributeTypeConvert<T>::type;
797         // contains the AttributeType for this base-type
798         constexpr static AttributeType enum_base_type =
799             AttributeTypeConvert<T>::type;
800         // depth of the vector
801         constexpr static size_t depth = 0;
802     };
803
804     template<typename T>
805     struct type_info<T, typename std::enable_if<is_vector<T>::value>::type>
806     {
807         typedef T type;
808         typedef typename type_info<typename T::value_type>::base_type base_type;
809         constexpr static AttributeType enum_type = AttributeType::Vector;
810         constexpr static AttributeType enum_base_type =
811             type_info<typename T::value_type>::enum_base_type;
812         constexpr static size_t depth = 1 +
813             type_info<typename T::value_type>::depth;
814     };
815
816     struct type_introspection_visitor : boost::static_visitor<>
817     {
818         AttributeType type;
819         AttributeType base_type;
820         size_t depth;
821
822         type_introspection_visitor() : boost::static_visitor<>(),
823             type(AttributeType::Null), base_type(AttributeType::Null), depth(0){}
824
825         template <typename T>
826         void operator()(T const& /*item*/)
827         {
828             type = type_info<T>::enum_type;
829             base_type = type_info<T>::enum_base_type;
830             depth = type_info<T>::depth;
831         }
832     };
833
834     AttributeType OCRepresentation::AttributeItem::type() const
835     {
836         type_introspection_visitor vis;
837         boost::apply_visitor(vis, m_values[m_attrName]);
838         return vis.type;
839     }
840
841     AttributeType OCRepresentation::AttributeItem::base_type() const
842     {
843         type_introspection_visitor vis;
844         boost::apply_visitor(vis, m_values[m_attrName]);
845         return vis.base_type;
846     }
847
848     size_t OCRepresentation::AttributeItem::depth() const
849     {
850         type_introspection_visitor vis;
851         boost::apply_visitor(vis, m_values[m_attrName]);
852         return vis.depth;
853     }
854
855     OCRepresentation::iterator OCRepresentation::begin()
856     {
857         return OCRepresentation::iterator(m_values.begin(), m_values);
858     }
859
860     OCRepresentation::const_iterator OCRepresentation::begin() const
861     {
862          return OCRepresentation::const_iterator(m_values.begin(), m_values);
863     }
864
865     OCRepresentation::const_iterator OCRepresentation::cbegin() const
866     {
867         return OCRepresentation::const_iterator(m_values.cbegin(), m_values);
868     }
869
870     OCRepresentation::iterator OCRepresentation::end()
871     {
872         return OCRepresentation::iterator(m_values.end(), m_values);
873     }
874
875     OCRepresentation::const_iterator OCRepresentation::end() const
876     {
877         return OCRepresentation::const_iterator(m_values.end(), m_values);
878     }
879
880     OCRepresentation::const_iterator OCRepresentation::cend() const
881     {
882         return OCRepresentation::const_iterator(m_values.cend(), m_values);
883     }
884
885     size_t OCRepresentation::size() const
886     {
887         return m_values.size();
888     }
889
890     bool OCRepresentation::empty() const
891     {
892         return m_values.empty();
893     }
894
895     bool OCRepresentation::iterator::operator==(const OCRepresentation::iterator& rhs) const
896     {
897         return m_iterator == rhs.m_iterator;
898     }
899
900     bool OCRepresentation::iterator::operator!=(const OCRepresentation::iterator& rhs) const
901     {
902         return m_iterator != rhs.m_iterator;
903     }
904
905     bool OCRepresentation::const_iterator::operator==(
906             const OCRepresentation::const_iterator& rhs) const
907     {
908         return m_iterator == rhs.m_iterator;
909     }
910
911     bool OCRepresentation::const_iterator::operator!=(
912             const OCRepresentation::const_iterator& rhs) const
913     {
914         return m_iterator != rhs.m_iterator;
915     }
916
917     OCRepresentation::iterator::reference OCRepresentation::iterator::operator*()
918     {
919         return m_item;
920     }
921
922     OCRepresentation::const_iterator::const_reference
923         OCRepresentation::const_iterator::operator*() const
924     {
925         return m_item;
926     }
927
928     OCRepresentation::iterator::pointer OCRepresentation::iterator::operator->()
929     {
930         return &m_item;
931     }
932
933     OCRepresentation::const_iterator::const_pointer
934         OCRepresentation::const_iterator::operator->() const
935     {
936         return &m_item;
937     }
938
939     OCRepresentation::iterator& OCRepresentation::iterator::operator++()
940     {
941         m_iterator++;
942         if(m_iterator != m_item.m_values.end())
943         {
944             m_item.m_attrName = m_iterator->first;
945         }
946         else
947         {
948             m_item.m_attrName = "";
949         }
950         return *this;
951     }
952
953     OCRepresentation::const_iterator& OCRepresentation::const_iterator::operator++()
954     {
955         m_iterator++;
956         if(m_iterator != m_item.m_values.end())
957         {
958             m_item.m_attrName = m_iterator->first;
959         }
960         else
961         {
962             m_item.m_attrName = "";
963         }
964         return *this;
965     }
966
967     OCRepresentation::iterator OCRepresentation::iterator::operator++(int)
968     {
969         OCRepresentation::iterator itr(*this);
970         ++(*this);
971         return itr;
972     }
973
974     OCRepresentation::const_iterator OCRepresentation::const_iterator::operator++(int)
975     {
976         OCRepresentation::const_iterator itr(*this);
977         ++(*this);
978         return itr;
979     }
980
981     struct to_string_visitor : boost::static_visitor<>
982     {
983         std::string str;
984         template <typename T>
985         void operator()(T const& item)
986         {
987             str = boost::lexical_cast<std::string>(item);
988         }
989
990         template <typename T>
991         void operator()(std::vector<T> const& item)
992         {
993             to_string_visitor vis;
994             std::ostringstream stream;
995             stream << "[";
996
997             for(const auto& i : item)
998             {
999                 vis(i);
1000                 stream << vis.str  << " ";
1001             }
1002             stream << "]";
1003             str = stream.str();
1004         }
1005     };
1006
1007     template<>
1008     void to_string_visitor::operator()(bool const& item)
1009     {
1010         str = item ? "true" : "false";
1011     }
1012
1013     template<>
1014     void to_string_visitor::operator()(std::string const& item)
1015     {
1016         str = item;
1017     }
1018
1019     template<>
1020     void to_string_visitor::operator()(NullType const& /*item*/)
1021     {
1022         str = "(null)";
1023     }
1024
1025     template<>
1026     void to_string_visitor::operator()(OCRepresentation const& /*item*/)
1027     {
1028         str = "OC::OCRepresentation";
1029     }
1030
1031     std::string OCRepresentation::getValueToString(const std::string& key) const
1032     {
1033         auto x = m_values.find(key);
1034         if(x != m_values.end())
1035         {
1036             to_string_visitor vis;
1037             boost::apply_visitor(vis, x->second);
1038             return std::move(vis.str);
1039         }
1040
1041         return "";
1042     }
1043
1044     std::string OCRepresentation::AttributeItem::getValueToString() const
1045     {
1046         to_string_visitor vis;
1047         boost::apply_visitor(vis, m_values[m_attrName]);
1048         return std::move(vis.str);
1049     }
1050
1051     std::ostream& operator<<(std::ostream& os, const OCRepresentation::AttributeItem& ai)
1052     {
1053         os << ai.getValueToString();
1054         return os;
1055     }
1056 }
1057