Imported Upstream version 0.9.2
[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(), val);
378                     break;
379                 case AttributeType::Boolean:
380                     OCRepPayloadSetPropBool(root, val.attrname().c_str(), val);
381                     break;
382                 case AttributeType::String:
383                     OCRepPayloadSetPropString(root, val.attrname().c_str(),
384                             static_cast<std::string>(val).c_str());
385                     break;
386                 case AttributeType::OCRepresentation:
387                     OCRepPayloadSetPropObjectAsOwner(root, val.attrname().c_str(),
388                             static_cast<OCRepresentation>(val).getPayload());
389                     break;
390                 case AttributeType::Vector:
391                     getPayloadArray(root, val);
392                     break;
393                 default:
394                     throw std::logic_error(std::string("Getpayload: Not Implemented") +
395                             std::to_string((int)val.type()));
396                     break;
397             }
398         }
399
400         OCRepPayload* cur = root;
401         for(auto& child : this->getChildren())
402         {
403             cur->next = child.getPayload();
404             cur = cur->next;
405         }
406
407         return root;
408     }
409
410     size_t calcArrayDepth(const size_t dimensions[MAX_REP_ARRAY_DEPTH])
411     {
412         if(dimensions[0] == 0)
413         {
414             throw std::logic_error("invalid calcArrayDepth");
415         }
416         else if(dimensions[1] == 0)
417         {
418             return 1;
419         }
420         else if (dimensions[2] == 0)
421         {
422             return 2;
423         }
424         else
425         {
426             return 3;
427         }
428     }
429
430     template<typename T>
431     T OCRepresentation::payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl)
432     {
433         throw std::logic_error("payload_array_helper_copy: unsupported type");
434     }
435     template<>
436     int OCRepresentation::payload_array_helper_copy<int>(size_t index, const OCRepPayloadValue* pl)
437     {
438         return pl->arr.iArray[index];
439     }
440     template<>
441     double OCRepresentation::payload_array_helper_copy<double>(size_t index, const OCRepPayloadValue* pl)
442     {
443         return pl->arr.dArray[index];
444     }
445     template<>
446     bool OCRepresentation::payload_array_helper_copy<bool>(size_t index, const OCRepPayloadValue* pl)
447     {
448         return pl->arr.bArray[index];
449     }
450     template<>
451     std::string OCRepresentation::payload_array_helper_copy<std::string>(
452             size_t index, const OCRepPayloadValue* pl)
453     {
454         return std::string(pl->arr.strArray[index]);
455     }
456     template<>
457     OCRepresentation OCRepresentation::payload_array_helper_copy<OCRepresentation>(
458             size_t index, const OCRepPayloadValue* pl)
459     {
460         OCRepresentation r;
461         r.setPayload(pl->arr.objArray[index]);
462         return r;
463     }
464
465     template<typename T>
466     void OCRepresentation::payload_array_helper(const OCRepPayloadValue* pl, size_t depth)
467     {
468         if(depth == 1)
469         {
470             std::vector<T> val(pl->arr.dimensions[0]);
471
472             for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
473             {
474                 val[i] = payload_array_helper_copy<T>(i, pl);
475             }
476             this->setValue(std::string(pl->name), val);
477         }
478         else if (depth == 2)
479         {
480             std::vector<std::vector<T>> val(pl->arr.dimensions[0]);
481             for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
482             {
483                 val[i].reserve(pl->arr.dimensions[1]);
484                 for(size_t j = 0; j < pl->arr.dimensions[1]; ++j)
485                 {
486                     val[i][j] = payload_array_helper_copy<T>(
487                             i * pl->arr.dimensions[1] + j, pl);
488                 }
489             }
490             this->setValue(std::string(pl->name), val);
491         }
492         else if (depth == 3)
493         {
494             std::vector<std::vector<std::vector<T>>> val;
495             for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
496             {
497                 val[i].reserve(pl->arr.dimensions[1]);
498                 for(size_t j = 0; j < pl->arr.dimensions[1]; ++j)
499                 {
500                     val[i][j].reserve(pl->arr.dimensions[2]);
501                     for(size_t k = 0; k < pl->arr.dimensions[2]; ++k)
502                     {
503                         val[i][j][k] = payload_array_helper_copy<T>(
504                                 pl->arr.dimensions[2] * j +
505                                 pl->arr.dimensions[2] * pl->arr.dimensions[1] * i +
506                                 k,
507                                 pl);
508                     }
509                 }
510             }
511             this->setValue(std::string(pl->name), val);
512         }
513         else
514         {
515             throw std::logic_error("Invalid depth in payload_array_helper");
516         }
517     }
518
519     void OCRepresentation::setPayloadArray(const OCRepPayloadValue* pl)
520     {
521
522         switch(pl->arr.type)
523         {
524             case OCREP_PROP_INT:
525                 payload_array_helper<int>(pl, calcArrayDepth(pl->arr.dimensions));
526                 break;
527             case OCREP_PROP_DOUBLE:
528                 payload_array_helper<double>(pl, calcArrayDepth(pl->arr.dimensions));
529                 break;
530             case OCREP_PROP_BOOL:
531                 payload_array_helper<bool>(pl, calcArrayDepth(pl->arr.dimensions));
532                 break;
533             case OCREP_PROP_STRING:
534                 payload_array_helper<std::string>(pl, calcArrayDepth(pl->arr.dimensions));
535                 break;
536             case OCREP_PROP_OBJECT:
537                 payload_array_helper<OCRepresentation>(pl, calcArrayDepth(pl->arr.dimensions));
538                 break;
539             default:
540                 throw std::logic_error("setPayload array invalid type");
541                 break;
542         }
543     }
544
545     void OCRepresentation::setPayload(const OCRepPayload* pl)
546     {
547         setUri(pl->uri);
548
549         OCStringLL* ll = pl->types;
550         while(ll)
551         {
552             addResourceType(ll->value);
553             ll = ll->next;
554         }
555
556         ll = pl->interfaces;
557         while(ll)
558         {
559             addResourceInterface(ll->value);
560             ll = ll->next;
561         }
562
563         OCRepPayloadValue* val = pl->values;
564
565         while(val)
566         {
567             switch(val->type)
568             {
569                 case OCREP_PROP_NULL:
570                     setNULL(val->name);
571                     break;
572                 case OCREP_PROP_INT:
573                     setValue<int>(val->name, val->i);
574                     break;
575                 case OCREP_PROP_DOUBLE:
576                     setValue<double>(val->name, val->d);
577                     break;
578                 case OCREP_PROP_BOOL:
579                     setValue<bool>(val->name, val->b);
580                     break;
581                 case OCREP_PROP_STRING:
582                     setValue<std::string>(val->name, val->str);
583                     break;
584                 case OCREP_PROP_OBJECT:
585                     {
586                         OCRepresentation cur;
587                         cur.setPayload(val->obj);
588                         setValue<OCRepresentation>(val->name, cur);
589                     }
590                     break;
591                 case OCREP_PROP_ARRAY:
592                     setPayloadArray(val);
593                     break;
594                 default:
595                     throw std::logic_error(std::string("Not Implemented!") +
596                             std::to_string((int)val->type));
597                     break;
598             }
599             val = val->next;
600         }
601     }
602
603     void OCRepresentation::addChild(const OCRepresentation& rep)
604     {
605         m_children.push_back(rep);
606     }
607
608     void OCRepresentation::clearChildren()
609     {
610         m_children.clear();
611     }
612
613     const std::vector<OCRepresentation>& OCRepresentation::getChildren() const
614     {
615         return m_children;
616     }
617
618     void OCRepresentation::setChildren(const std::vector<OCRepresentation>& children)
619     {
620         m_children = children;
621     }
622     void OCRepresentation::setUri(const char* uri)
623     {
624         m_uri = uri ? uri : "";
625     }
626
627     void OCRepresentation::setUri(const std::string& uri)
628     {
629         m_uri = uri;
630     }
631
632     std::string OCRepresentation::getUri() const
633     {
634         return m_uri;
635     }
636
637     const std::vector<std::string>& OCRepresentation::getResourceTypes() const
638     {
639         return m_resourceTypes;
640     }
641
642     void OCRepresentation::setResourceTypes(const std::vector<std::string>& resourceTypes)
643     {
644         m_resourceTypes = resourceTypes;
645     }
646
647     void OCRepresentation::addResourceType(const std::string& str)
648     {
649         m_resourceTypes.push_back(str);
650     }
651
652     const std::vector<std::string>& OCRepresentation::getResourceInterfaces() const
653     {
654         return m_interfaces;
655     }
656
657     void OCRepresentation::addResourceInterface(const std::string& str)
658     {
659         m_interfaces.push_back(str);
660     }
661
662     void OCRepresentation::setResourceInterfaces(const std::vector<std::string>& resourceInterfaces)
663     {
664         m_interfaces = resourceInterfaces;
665     }
666
667     bool OCRepresentation::hasAttribute(const std::string& str) const
668     {
669         return m_values.find(str) != m_values.end();
670     }
671
672     bool OCRepresentation::emptyData() const
673     {
674         // This logic is meant to determine whether based on the JSON serialization rules
675         // if this object will result in empty JSON.  URI is only serialized if there is valid
676         // data, ResourceType and Interfaces are only serialized if we are a nothing, a
677         // child of a default or link item.
678         // Our values array is only printed in the if we are the child of a Batch resource,
679         // the parent in a 'default' situation, or not in a child/parent relationship.
680         if(!m_uri.empty())
681         {
682             return false;
683         }
684         else if ((m_interfaceType == InterfaceType::None
685                         || m_interfaceType==InterfaceType::DefaultChild
686                         || m_interfaceType==InterfaceType::LinkChild)
687                     && (m_resourceTypes.size()>0 || m_interfaces.size()>0))
688         {
689             return false;
690         }
691         else if((m_interfaceType == InterfaceType::None
692                         || m_interfaceType == InterfaceType::BatchChild
693                         || m_interfaceType == InterfaceType::DefaultParent)
694                     && m_values.size()>0)
695         {
696             return false;
697         }
698
699         if(m_children.size() > 0)
700         {
701             return false;
702         }
703
704         return true;
705     }
706
707     int OCRepresentation::numberOfAttributes() const
708     {
709         return m_values.size();
710     }
711
712     bool OCRepresentation::erase(const std::string& str)
713     {
714         return m_values.erase(str);
715     }
716
717     void OCRepresentation::setNULL(const std::string& str)
718     {
719         m_values[str] = OC::NullType();
720     }
721
722     bool OCRepresentation::isNULL(const std::string& str) const
723     {
724         auto x = m_values.find(str);
725
726         if(m_values.end() != x)
727         {
728             return x->second.which() == AttributeValueNullIndex;
729         }
730         else
731         {
732             throw OCException(OC::Exception::INVALID_ATTRIBUTE+ str);
733         }
734     }
735 }
736
737 namespace OC
738 {
739     std::ostream& operator <<(std::ostream& os, const AttributeType at)
740     {
741         switch(at)
742         {
743             case AttributeType::Null:
744                 os << "Null";
745                 break;
746             case AttributeType::Integer:
747                 os << "Integer";
748                 break;
749             case AttributeType::Double:
750                 os << "Double";
751                 break;
752             case AttributeType::Boolean:
753                 os << "Boolean";
754                 break;
755             case AttributeType::String:
756                 os << "String";
757                 break;
758             case AttributeType::OCRepresentation:
759                 os << "OCRepresentation";
760                 break;
761             case AttributeType::Vector:
762                 os << "Vector";
763                 break;
764         }
765         return os;
766     }
767 }
768
769 // STL Container For OCRepresentation
770 namespace OC
771 {
772     OCRepresentation::AttributeItem::AttributeItem(const std::string& name,
773             std::map<std::string, AttributeValue>& vals):
774             m_attrName(name), m_values(vals){}
775
776     OCRepresentation::AttributeItem OCRepresentation::operator[](const std::string& key)
777     {
778         OCRepresentation::AttributeItem attr{key, m_values};
779         return std::move(attr);
780     }
781
782     const OCRepresentation::AttributeItem OCRepresentation::operator[](const std::string& key) const
783     {
784         OCRepresentation::AttributeItem attr{key, m_values};
785         return std::move(attr);
786     }
787
788     const std::string& OCRepresentation::AttributeItem::attrname() const
789     {
790         return m_attrName;
791     }
792
793     template<typename T, typename = void>
794     struct type_info
795     {
796         // contains the actual type
797         typedef T type;
798         // contains the inner most vector-type
799         typedef T base_type;
800         // contains the AttributeType for this item
801         constexpr static AttributeType enum_type =
802             AttributeTypeConvert<T>::type;
803         // contains the AttributeType for this base-type
804         constexpr static AttributeType enum_base_type =
805             AttributeTypeConvert<T>::type;
806         // depth of the vector
807         constexpr static size_t depth = 0;
808     };
809
810     template<typename T>
811     struct type_info<T, typename std::enable_if<is_vector<T>::value>::type>
812     {
813         typedef T type;
814         typedef typename type_info<typename T::value_type>::base_type base_type;
815         constexpr static AttributeType enum_type = AttributeType::Vector;
816         constexpr static AttributeType enum_base_type =
817             type_info<typename T::value_type>::enum_base_type;
818         constexpr static size_t depth = 1 +
819             type_info<typename T::value_type>::depth;
820     };
821
822     struct type_introspection_visitor : boost::static_visitor<>
823     {
824         AttributeType type;
825         AttributeType base_type;
826         size_t depth;
827
828         type_introspection_visitor() : boost::static_visitor<>(),
829             type(AttributeType::Null), base_type(AttributeType::Null), depth(0){}
830
831         template <typename T>
832         void operator()(T const& item)
833         {
834             type = type_info<T>::enum_type;
835             base_type = type_info<T>::enum_base_type;
836             depth = type_info<T>::depth;
837         }
838     };
839
840     AttributeType OCRepresentation::AttributeItem::type() const
841     {
842         type_introspection_visitor vis;
843         boost::apply_visitor(vis, m_values[m_attrName]);
844         return vis.type;
845     }
846
847     AttributeType OCRepresentation::AttributeItem::base_type() const
848     {
849         type_introspection_visitor vis;
850         boost::apply_visitor(vis, m_values[m_attrName]);
851         return vis.base_type;
852     }
853
854     size_t OCRepresentation::AttributeItem::depth() const
855     {
856         type_introspection_visitor vis;
857         boost::apply_visitor(vis, m_values[m_attrName]);
858         return vis.depth;
859     }
860
861     OCRepresentation::iterator OCRepresentation::begin()
862     {
863         return OCRepresentation::iterator(m_values.begin(), m_values);
864     }
865
866     OCRepresentation::const_iterator OCRepresentation::begin() const
867     {
868          return OCRepresentation::const_iterator(m_values.begin(), m_values);
869     }
870
871     OCRepresentation::const_iterator OCRepresentation::cbegin() const
872     {
873         return OCRepresentation::const_iterator(m_values.cbegin(), m_values);
874     }
875
876     OCRepresentation::iterator OCRepresentation::end()
877     {
878         return OCRepresentation::iterator(m_values.end(), m_values);
879     }
880
881     OCRepresentation::const_iterator OCRepresentation::end() const
882     {
883         return OCRepresentation::const_iterator(m_values.end(), m_values);
884     }
885
886     OCRepresentation::const_iterator OCRepresentation::cend() const
887     {
888         return OCRepresentation::const_iterator(m_values.cend(), m_values);
889     }
890
891     size_t OCRepresentation::size() const
892     {
893         return m_values.size();
894     }
895
896     bool OCRepresentation::empty() const
897     {
898         return m_values.empty();
899     }
900
901     bool OCRepresentation::iterator::operator==(const OCRepresentation::iterator& rhs) const
902     {
903         return m_iterator == rhs.m_iterator;
904     }
905
906     bool OCRepresentation::iterator::operator!=(const OCRepresentation::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     bool OCRepresentation::const_iterator::operator!=(
918             const OCRepresentation::const_iterator& rhs) const
919     {
920         return m_iterator != rhs.m_iterator;
921     }
922
923     OCRepresentation::iterator::reference OCRepresentation::iterator::operator*()
924     {
925         return m_item;
926     }
927
928     OCRepresentation::const_iterator::const_reference
929         OCRepresentation::const_iterator::operator*() const
930     {
931         return m_item;
932     }
933
934     OCRepresentation::iterator::pointer OCRepresentation::iterator::operator->()
935     {
936         return &m_item;
937     }
938
939     OCRepresentation::const_iterator::const_pointer
940         OCRepresentation::const_iterator::operator->() const
941     {
942         return &m_item;
943     }
944
945     OCRepresentation::iterator& OCRepresentation::iterator::operator++()
946     {
947         m_iterator++;
948         if(m_iterator != m_item.m_values.end())
949         {
950             m_item.m_attrName = m_iterator->first;
951         }
952         else
953         {
954             m_item.m_attrName = "";
955         }
956         return *this;
957     }
958
959     OCRepresentation::const_iterator& OCRepresentation::const_iterator::operator++()
960     {
961         m_iterator++;
962         if(m_iterator != m_item.m_values.end())
963         {
964             m_item.m_attrName = m_iterator->first;
965         }
966         else
967         {
968             m_item.m_attrName = "";
969         }
970         return *this;
971     }
972
973     OCRepresentation::iterator OCRepresentation::iterator::operator++(int)
974     {
975         OCRepresentation::iterator itr(*this);
976         ++(*this);
977         return itr;
978     }
979
980     OCRepresentation::const_iterator OCRepresentation::const_iterator::operator++(int)
981     {
982         OCRepresentation::const_iterator itr(*this);
983         ++(*this);
984         return itr;
985     }
986
987     struct to_string_visitor : boost::static_visitor<>
988     {
989         std::string str;
990         template <typename T>
991         void operator()(T const& item)
992         {
993             str = boost::lexical_cast<std::string>(item);
994         }
995
996         template <typename T>
997         void operator()(std::vector<T> const& item)
998         {
999             to_string_visitor vis;
1000             std::ostringstream stream;
1001             stream << "[";
1002
1003             for(const auto& i : item)
1004             {
1005                 vis(i);
1006                 stream << vis.str  << " ";
1007             }
1008             stream << "]";
1009             str = stream.str();
1010         }
1011     };
1012
1013     template<>
1014     void to_string_visitor::operator()(bool const& item)
1015     {
1016         str = item ? "true" : "false";
1017     }
1018
1019     template<>
1020     void to_string_visitor::operator()(std::string const& item)
1021     {
1022         str = item;
1023     }
1024
1025     template<>
1026     void to_string_visitor::operator()(NullType const& item)
1027     {
1028         str = "(null)";
1029     }
1030
1031     template<>
1032     void to_string_visitor::operator()(OCRepresentation const& item)
1033     {
1034         str = "OC::OCRepresentation";
1035     }
1036
1037     std::string OCRepresentation::getValueToString(const std::string& key) const
1038     {
1039         auto x = m_values.find(key);
1040         if(x != m_values.end())
1041         {
1042             to_string_visitor vis;
1043             boost::apply_visitor(vis, x->second);
1044             return std::move(vis.str);
1045         }
1046
1047         return "";
1048     }
1049
1050     std::string OCRepresentation::AttributeItem::getValueToString() const
1051     {
1052         to_string_visitor vis;
1053         boost::apply_visitor(vis, m_values[m_attrName]);
1054         return std::move(vis.str);
1055     }
1056
1057     std::ostream& operator<<(std::ostream& os, const OCRepresentation::AttributeItem& ai)
1058     {
1059         os << ai.getValueToString();
1060         return os;
1061     }
1062 }
1063