1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 * This file contains the implementation of classes and its members related
25 * to OCRepresentation.
29 #include <OCRepresentation.h>
31 #include <boost/lexical_cast.hpp>
33 #include "ocpayload.h"
35 #include "oic_malloc.h"
36 #include "oic_string.h"
40 static const char COAP[] = "coap://";
41 static const char COAPS[] = "coaps://";
42 static const char COAP_TCP[] = "coap+tcp://";
44 void MessageContainer::setPayload(const OCPayload* rep)
53 case PAYLOAD_TYPE_REPRESENTATION:
54 setPayload(reinterpret_cast<const OCRepPayload*>(rep));
56 case PAYLOAD_TYPE_DEVICE:
57 setPayload(reinterpret_cast<const OCDevicePayload*>(rep));
59 case PAYLOAD_TYPE_PLATFORM:
60 setPayload(reinterpret_cast<const OCPlatformPayload*>(rep));
64 setPayload(reinterpret_cast<const OCRDPayload*>(rep));
68 throw OC::OCException("Invalid Payload type in setPayload");
73 void MessageContainer::setPayload(const OCDevicePayload* payload)
75 if (payload == nullptr)
81 rep[OC_RSRVD_DEVICE_ID] = (payload->sid) ?
82 std::string(payload->sid) :
84 rep[OC_RSRVD_DEVICE_NAME] = payload->deviceName ?
85 std::string(payload->deviceName) :
87 rep[OC_RSRVD_SPEC_VERSION] = payload->specVersion ?
88 std::string(payload->specVersion) :
90 for (OCStringLL *strll = payload->dataModelVersions; strll; strll = strll->next)
92 rep.addDataModelVersion(strll->value);
94 for (OCStringLL *strll = payload->types; strll; strll = strll->next)
96 rep.addResourceType(strll->value);
98 m_reps.push_back(std::move(rep));
101 void MessageContainer::setPayload(const OCPlatformPayload* payload)
103 if (payload == nullptr)
108 OCRepresentation rep;
109 rep[OC_RSRVD_PLATFORM_ID] = payload->info.platformID ?
110 std::string(payload->info.platformID) :
112 rep[OC_RSRVD_MFG_NAME] = payload->info.manufacturerName ?
113 std::string(payload->info.manufacturerName) :
115 rep[OC_RSRVD_MFG_URL] = payload->info.manufacturerUrl ?
116 std::string(payload->info.manufacturerUrl) :
118 rep[OC_RSRVD_MODEL_NUM] = payload->info.modelNumber ?
119 std::string(payload->info.modelNumber) :
121 rep[OC_RSRVD_MFG_DATE] = payload->info.dateOfManufacture ?
122 std::string(payload->info.dateOfManufacture) :
124 rep[OC_RSRVD_PLATFORM_VERSION] = payload->info.platformVersion ?
125 std::string(payload->info.platformVersion) :
127 rep[OC_RSRVD_OS_VERSION] = payload->info.operatingSystemVersion ?
128 std::string(payload->info.operatingSystemVersion) :
130 rep[OC_RSRVD_HARDWARE_VERSION] = payload->info.hardwareVersion ?
131 std::string(payload->info.hardwareVersion) :
133 rep[OC_RSRVD_FIRMWARE_VERSION] = payload->info.firmwareVersion ?
134 std::string(payload->info.firmwareVersion) :
136 rep[OC_RSRVD_SUPPORT_URL] = payload->info.supportUrl ?
137 std::string(payload->info.supportUrl) :
139 rep[OC_RSRVD_SYSTEM_TIME] = payload->info.systemTime ?
140 std::string(payload->info.systemTime) :
143 for (OCStringLL *strll = payload->rt; strll; strll = strll->next)
145 rep.addResourceType(strll->value);
147 for (OCStringLL *strll = payload->interfaces; strll; strll = strll->next)
149 rep.addResourceInterface(strll->value);
152 m_reps.push_back(std::move(rep));
155 void MessageContainer::setPayload(const OCRepPayload* payload)
157 const OCRepPayload* pl = payload;
160 OCRepresentation cur;
164 this->addRepresentation(cur);
168 void MessageContainer::setPayload(const OCRDPayload* payload)
170 OCRepresentation rep;
171 rep[OC_RSRVD_DEVICE_ID] = (payload->rdPublish->tags->di.id) ?
172 std::string(reinterpret_cast<const char*>(payload->rdPublish->tags->di.id)) :
174 rep[OC_RSRVD_DEVICE_NAME] = (payload->rdPublish->tags->n.deviceName) ?
175 std::string(payload->rdPublish->tags->n.deviceName) :
177 this->addRepresentation(rep);
179 const OCLinksPayload* pl = payload->rdPublish->setLinks;
182 OCRepresentation cur;
186 this->addRepresentation(cur);
190 OCRepPayload* MessageContainer::getPayload() const
192 OCRepPayload* root = nullptr;
193 for(const auto& r : representations())
197 root = r.getPayload();
201 OCRepPayloadAppend(root, r.getPayload());
208 const std::vector<OCRepresentation>& MessageContainer::representations() const
213 void MessageContainer::addRepresentation(const OCRepresentation& rep)
215 m_reps.push_back(rep);
221 struct get_payload_array: boost::static_visitor<>
224 void operator()(T& /*arr*/)
226 throw std::logic_error("Invalid calc_dimensions_visitor type");
230 void operator()(std::vector<T>& arr)
233 dimensions[0] = arr.size();
236 dimTotal = calcDimTotal(dimensions);
238 array = (void*)OICMalloc(dimTotal * root_size);
240 for(size_t i = 0; i < dimensions[0]; ++i)
242 copy_to_array(arr[i], array, i);
247 void operator()(std::vector<std::vector<T>>& arr)
250 dimensions[0] = arr.size();
253 for(size_t i = 0; i < arr.size(); ++i)
255 dimensions[1] = std::max(dimensions[1], arr[i].size());
257 dimTotal = calcDimTotal(dimensions);
258 array = (void*)OICCalloc(1, dimTotal * root_size);
260 for(size_t i = 0; i < dimensions[0]; ++i)
262 for(size_t j = 0; j < dimensions[1] && j < arr[i].size(); ++j)
264 copy_to_array(arr[i][j], array, i*dimensions[1] + j);
269 void operator()(std::vector<std::vector<std::vector<T>>>& arr)
272 dimensions[0] = arr.size();
275 for(size_t i = 0; i < arr.size(); ++i)
277 dimensions[1] = std::max(dimensions[1], arr[i].size());
279 for(size_t j = 0; j < arr[i].size(); ++j)
281 dimensions[2] = std::max(dimensions[2], arr[i][j].size());
285 dimTotal = calcDimTotal(dimensions);
286 array = (void*)OICCalloc(1, dimTotal * root_size);
288 for(size_t i = 0; i < dimensions[0]; ++i)
290 for(size_t j = 0; j < dimensions[1] && j < arr[i].size(); ++j)
292 for(size_t k = 0; k < dimensions[2] && k < arr[i][j].size(); ++k)
294 copy_to_array(arr[i][j][k], array,
296 dimensions[2] * dimensions[1] * i +
304 void root_size_calc()
306 root_size = sizeof(T);
310 void copy_to_array(T item, void* array, size_t pos)
312 ((T*)array)[pos] = item;
315 size_t dimensions[MAX_REP_ARRAY_DEPTH];
322 void get_payload_array::root_size_calc<int>()
324 root_size = sizeof(int64_t);
328 void get_payload_array::root_size_calc<std::string>()
330 root_size = sizeof(char*);
334 void get_payload_array::root_size_calc<OC::OCRepresentation>()
336 root_size = sizeof(OCRepPayload*);
340 void get_payload_array::copy_to_array(int item, void* array, size_t pos)
342 ((int64_t*)array)[pos] = item;
345 #if !defined(_MSC_VER)
347 void get_payload_array::copy_to_array(std::_Bit_reference br, void* array, size_t pos)
349 ((bool*)array)[pos] = static_cast<bool>(br);
354 void get_payload_array::copy_to_array(std::string item, void* array, size_t pos)
356 ((char**)array)[pos] = OICStrdup(item.c_str());
360 void get_payload_array::copy_to_array(std::string& item, void* array, size_t pos)
362 ((char**)array)[pos] = OICStrdup(item.c_str());
366 void get_payload_array::copy_to_array(const std::string& item, void* array, size_t pos)
368 ((char**)array)[pos] = OICStrdup(item.c_str());
372 void get_payload_array::copy_to_array(OC::OCRepresentation item, void* array, size_t pos)
374 ((OCRepPayload**)array)[pos] = item.getPayload();
377 void OCRepresentation::getPayloadArray(OCRepPayload* payload,
378 const OCRepresentation::AttributeItem& item) const
380 get_payload_array vis{};
381 boost::apply_visitor(vis, m_values[item.attrname()]);
384 switch(item.base_type())
386 case AttributeType::Integer:
387 OCRepPayloadSetIntArrayAsOwner(payload, item.attrname().c_str(),
391 case AttributeType::Double:
392 OCRepPayloadSetDoubleArrayAsOwner(payload, item.attrname().c_str(),
396 case AttributeType::Boolean:
397 OCRepPayloadSetBoolArrayAsOwner(payload, item.attrname().c_str(),
401 case AttributeType::String:
402 OCRepPayloadSetStringArrayAsOwner(payload, item.attrname().c_str(),
406 case AttributeType::OCRepresentation:
407 OCRepPayloadSetPropObjectArrayAsOwner(payload, item.attrname().c_str(),
408 (OCRepPayload**)vis.array, vis.dimensions);
411 throw std::logic_error(std::string("GetPayloadArray: Not Implemented") +
412 std::to_string((int)item.base_type()));
416 OCRepPayload* OCRepresentation::getPayload() const
418 OCRepPayload* root = OCRepPayloadCreate();
421 throw std::bad_alloc();
424 OCRepPayloadSetUri(root, getUri().c_str());
426 for(const std::string& type : getResourceTypes())
428 OCRepPayloadAddResourceType(root, type.c_str());
431 for(const std::string& iface : getResourceInterfaces())
433 OCRepPayloadAddInterface(root, iface.c_str());
436 for(auto& val : *this)
440 case AttributeType::Null:
441 OCRepPayloadSetNull(root, val.attrname().c_str());
443 case AttributeType::Integer:
444 OCRepPayloadSetPropInt(root, val.attrname().c_str(), static_cast<int>(val));
446 case AttributeType::Double:
447 OCRepPayloadSetPropDouble(root, val.attrname().c_str(),
448 val.getValue<double>());
450 case AttributeType::Boolean:
451 OCRepPayloadSetPropBool(root, val.attrname().c_str(), val.getValue<bool>());
453 case AttributeType::String:
454 OCRepPayloadSetPropString(root, val.attrname().c_str(),
455 static_cast<std::string>(val).c_str());
457 case AttributeType::OCRepresentation:
458 OCRepPayloadSetPropObjectAsOwner(root, val.attrname().c_str(),
459 static_cast<OCRepresentation>(val).getPayload());
461 case AttributeType::Vector:
462 getPayloadArray(root, val);
464 case AttributeType::Binary:
465 OCRepPayloadSetPropByteString(root, val.attrname().c_str(),
466 OCByteString{const_cast<uint8_t*>(val.getValue<std::vector<uint8_t>>().data()),
467 val.getValue<std::vector<uint8_t>>().size()});
470 throw std::logic_error(std::string("Getpayload: Not Implemented") +
471 std::to_string((int)val.type()));
479 size_t calcArrayDepth(const size_t dimensions[MAX_REP_ARRAY_DEPTH])
481 if (dimensions[0] == 0)
483 throw std::logic_error("invalid calcArrayDepth");
485 else if (dimensions[1] == 0)
489 else if (dimensions[2] == 0)
500 T OCRepresentation::payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl)
502 throw std::logic_error("payload_array_helper_copy: unsupported type");
505 int OCRepresentation::payload_array_helper_copy<int>(size_t index, const OCRepPayloadValue* pl)
507 return pl->arr.iArray[index];
510 double OCRepresentation::payload_array_helper_copy<double>(size_t index, const OCRepPayloadValue* pl)
512 return pl->arr.dArray[index];
515 bool OCRepresentation::payload_array_helper_copy<bool>(size_t index, const OCRepPayloadValue* pl)
517 return pl->arr.bArray[index];
520 std::string OCRepresentation::payload_array_helper_copy<std::string>(
521 size_t index, const OCRepPayloadValue* pl)
523 if (pl->arr.strArray[index])
525 return std::string(pl->arr.strArray[index]);
529 return std::string{};
533 OCRepresentation OCRepresentation::payload_array_helper_copy<OCRepresentation>(
534 size_t index, const OCRepPayloadValue* pl)
537 if (pl->arr.objArray[index])
539 r.setPayload(pl->arr.objArray[index]);
545 void OCRepresentation::payload_array_helper(const OCRepPayloadValue* pl, size_t depth)
549 std::vector<T> val(pl->arr.dimensions[0]);
551 for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
553 val[i] = payload_array_helper_copy<T>(i, pl);
555 this->setValue(std::string(pl->name), val);
559 std::vector<std::vector<T>> val(pl->arr.dimensions[0]);
560 for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
562 val[i].resize(pl->arr.dimensions[1]);
563 for(size_t j = 0; j < pl->arr.dimensions[1]; ++j)
565 val[i][j] = payload_array_helper_copy<T>(
566 i * pl->arr.dimensions[1] + j, pl);
569 this->setValue(std::string(pl->name), val);
573 std::vector<std::vector<std::vector<T>>> val(pl->arr.dimensions[0]);
574 for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
576 val[i].resize(pl->arr.dimensions[1]);
577 for(size_t j = 0; j < pl->arr.dimensions[1]; ++j)
579 val[i][j].resize(pl->arr.dimensions[2]);
580 for(size_t k = 0; k < pl->arr.dimensions[2]; ++k)
582 val[i][j][k] = payload_array_helper_copy<T>(
583 pl->arr.dimensions[2] * j +
584 pl->arr.dimensions[2] * pl->arr.dimensions[1] * i +
590 this->setValue(std::string(pl->name), val);
594 throw std::logic_error("Invalid depth in payload_array_helper");
598 void OCRepresentation::setPayloadArray(const OCRepPayloadValue* pl)
604 payload_array_helper<int>(pl, calcArrayDepth(pl->arr.dimensions));
606 case OCREP_PROP_DOUBLE:
607 payload_array_helper<double>(pl, calcArrayDepth(pl->arr.dimensions));
609 case OCREP_PROP_BOOL:
610 payload_array_helper<bool>(pl, calcArrayDepth(pl->arr.dimensions));
612 case OCREP_PROP_STRING:
613 payload_array_helper<std::string>(pl, calcArrayDepth(pl->arr.dimensions));
615 case OCREP_PROP_OBJECT:
616 payload_array_helper<OCRepresentation>(pl, calcArrayDepth(pl->arr.dimensions));
619 throw std::logic_error("setPayload array invalid type");
624 void OCRepresentation::setPayload(const OCRepPayload* pl)
628 OCStringLL* ll = pl->types;
631 addResourceType(ll->value);
638 addResourceInterface(ll->value);
642 OCRepPayloadValue* val = pl->values;
648 case OCREP_PROP_NULL:
652 setValue<int>(val->name, val->i);
654 case OCREP_PROP_DOUBLE:
655 setValue<double>(val->name, val->d);
657 case OCREP_PROP_BOOL:
658 setValue<bool>(val->name, val->b);
660 case OCREP_PROP_STRING:
661 setValue<std::string>(val->name, val->str);
663 case OCREP_PROP_OBJECT:
665 OCRepresentation cur;
666 cur.setPayload(val->obj);
667 setValue<OCRepresentation>(val->name, cur);
670 case OCREP_PROP_ARRAY:
671 setPayloadArray(val);
673 case OCREP_PROP_BYTE_STRING:
676 (val->ocByteStr.bytes, val->ocByteStr.bytes + val->ocByteStr.len)
680 throw std::logic_error(std::string("Not Implemented!") +
681 std::to_string((int)val->type));
688 void OCRepresentation::setPayload(const OCLinksPayload* pl)
692 setValue<std::string>(OC_RSRVD_HREF, pl->href);
696 setValue<std::string>(OC_RSRVD_REL, pl->rel);
698 OCStringLL* ll = pl->rt;
701 addResourceType(ll->value);
707 addResourceInterface(ll->value);
711 setValue<int>(OC_RSRVD_POLICY, pl->p);
712 setValue<int>(OC_RSRVD_INS, pl->ins);
713 setValue<int>(OC_RSRVD_TTL, pl->ttl);
716 setValue<std::string>(OC_RSRVD_TITLE, pl->title);
720 setValue<std::string>(OC_RSRVD_URI, pl->anchor);
724 void OCRepresentation::addChild(const OCRepresentation& rep)
726 m_children.push_back(rep);
729 void OCRepresentation::clearChildren()
734 const std::vector<OCRepresentation>& OCRepresentation::getChildren() const
739 void OCRepresentation::setChildren(const std::vector<OCRepresentation>& children)
741 m_children = children;
744 void OCRepresentation::setDevAddr(const OCDevAddr m_devAddr)
746 std::ostringstream ss;
747 if (m_devAddr.flags & OC_SECURE)
751 else if (m_devAddr.adapter & OC_ADAPTER_TCP)
759 if (m_devAddr.flags & OC_IP_USE_V6)
761 ss << '[' << m_devAddr.addr << ']';
765 ss << m_devAddr.addr;
769 ss << ':' << m_devAddr.port;
774 const std::string OCRepresentation::getHost() const
779 void OCRepresentation::setUri(const char* uri)
781 m_uri = uri ? uri : "";
784 void OCRepresentation::setUri(const std::string& uri)
789 std::string OCRepresentation::getUri() const
794 const std::vector<std::string>& OCRepresentation::getResourceTypes() const
796 return m_resourceTypes;
799 void OCRepresentation::setResourceTypes(const std::vector<std::string>& resourceTypes)
801 m_resourceTypes = resourceTypes;
804 void OCRepresentation::addResourceType(const std::string& str)
806 m_resourceTypes.push_back(str);
809 const std::vector<std::string>& OCRepresentation::getResourceInterfaces() const
814 void OCRepresentation::addResourceInterface(const std::string& str)
816 m_interfaces.push_back(str);
819 void OCRepresentation::setResourceInterfaces(const std::vector<std::string>& resourceInterfaces)
821 m_interfaces = resourceInterfaces;
824 const std::vector<std::string>& OCRepresentation::getDataModelVersions() const
826 return m_dataModelVersions;
829 void OCRepresentation::addDataModelVersion(const std::string& str)
831 m_dataModelVersions.push_back(str);
834 bool OCRepresentation::hasAttribute(const std::string& str) const
836 return m_values.find(str) != m_values.end();
839 bool OCRepresentation::emptyData() const
841 // This logic is meant to determine whether based on the JSON serialization rules
842 // if this object will result in empty JSON. URI is only serialized if there is valid
843 // data, ResourceType and Interfaces are only serialized if we are a nothing, a
844 // child of a default or link item.
845 // Our values array is only printed in the if we are the child of a Batch resource,
846 // the parent in a 'default' situation, or not in a child/parent relationship.
851 else if ((m_interfaceType == InterfaceType::None
852 || m_interfaceType==InterfaceType::DefaultChild
853 || m_interfaceType==InterfaceType::LinkChild)
854 && (m_resourceTypes.size()>0 || m_interfaces.size()>0
855 || m_dataModelVersions.size()>0))
859 else if ((m_interfaceType == InterfaceType::None
860 || m_interfaceType == InterfaceType::BatchChild
861 || m_interfaceType == InterfaceType::DefaultParent)
862 && m_values.size()>0)
867 if (m_children.size() > 0)
875 int OCRepresentation::numberOfAttributes() const
877 return m_values.size();
880 bool OCRepresentation::erase(const std::string& str)
882 return m_values.erase(str);
885 void OCRepresentation::setNULL(const std::string& str)
887 m_values[str] = OC::NullType();
890 bool OCRepresentation::isNULL(const std::string& str) const
892 auto x = m_values.find(str);
894 if (m_values.end() != x)
896 return x->second.which() == AttributeValueNullIndex;
900 throw OCException(OC::Exception::INVALID_ATTRIBUTE+ str);
907 std::ostream& operator <<(std::ostream& os, const AttributeType at)
911 case AttributeType::Null:
914 case AttributeType::Integer:
917 case AttributeType::Double:
920 case AttributeType::Boolean:
923 case AttributeType::String:
926 case AttributeType::OCRepresentation:
927 os << "OCRepresentation";
929 case AttributeType::Vector:
932 case AttributeType::Binary:
939 // STL Container For OCRepresentation
942 OCRepresentation::AttributeItem::AttributeItem(const std::string& name,
943 std::map<std::string, AttributeValue>& vals):
944 m_attrName(name), m_values(vals){}
946 OCRepresentation::AttributeItem OCRepresentation::operator[](const std::string& key)
948 OCRepresentation::AttributeItem attr{key, m_values};
949 return std::move(attr);
952 const OCRepresentation::AttributeItem OCRepresentation::operator[](const std::string& key) const
954 OCRepresentation::AttributeItem attr{key, m_values};
955 return std::move(attr);
958 const std::string& OCRepresentation::AttributeItem::attrname() const
963 template<typename T, typename = void>
966 // contains the actual type
968 // contains the inner most vector-type
970 // contains the AttributeType for this item
971 BOOST_STATIC_CONSTEXPR AttributeType enum_type =
972 AttributeTypeConvert<T>::type;
973 // contains the AttributeType for this base-type
974 BOOST_STATIC_CONSTEXPR AttributeType enum_base_type =
975 AttributeTypeConvert<T>::type;
976 // depth of the vector
977 BOOST_STATIC_CONSTEXPR size_t depth = 0;
983 typename std::enable_if<
984 is_vector<T>::value &&
985 !std::is_same<uint8_t, typename T::value_type>::value
990 typedef typename type_info<typename T::value_type>::base_type base_type;
991 BOOST_STATIC_CONSTEXPR AttributeType enum_type = AttributeType::Vector;
992 BOOST_STATIC_CONSTEXPR AttributeType enum_base_type =
993 type_info<typename T::value_type>::enum_base_type;
994 BOOST_STATIC_CONSTEXPR size_t depth = 1 +
995 type_info<typename T::value_type>::depth;
998 // special case for binary data, which is a std::vector<uint8_t>
1000 struct type_info<std::vector<uint8_t>, void>
1002 typedef std::vector<uint8_t> type;
1003 typedef std::vector<uint8_t> base_type;
1004 BOOST_STATIC_CONSTEXPR AttributeType enum_type = AttributeType::Binary;
1005 BOOST_STATIC_CONSTEXPR AttributeType enum_base_type = AttributeType::Binary;
1006 BOOST_STATIC_CONSTEXPR size_t depth = 0;
1010 struct type_introspection_visitor : boost::static_visitor<>
1013 AttributeType base_type;
1016 type_introspection_visitor() : boost::static_visitor<>(),
1017 type(AttributeType::Null), base_type(AttributeType::Null), depth(0){}
1019 template <typename T>
1020 void operator()(T const& /*item*/)
1022 type = type_info<T>::enum_type;
1023 base_type = type_info<T>::enum_base_type;
1024 depth = type_info<T>::depth;
1028 AttributeType OCRepresentation::AttributeItem::type() const
1030 type_introspection_visitor vis;
1031 boost::apply_visitor(vis, m_values[m_attrName]);
1035 AttributeType OCRepresentation::AttributeItem::base_type() const
1037 type_introspection_visitor vis;
1038 boost::apply_visitor(vis, m_values[m_attrName]);
1039 return vis.base_type;
1042 size_t OCRepresentation::AttributeItem::depth() const
1044 type_introspection_visitor vis;
1045 boost::apply_visitor(vis, m_values[m_attrName]);
1049 OCRepresentation::iterator OCRepresentation::begin()
1051 return OCRepresentation::iterator(m_values.begin(), m_values);
1054 OCRepresentation::const_iterator OCRepresentation::begin() const
1056 return OCRepresentation::const_iterator(m_values.begin(), m_values);
1059 OCRepresentation::const_iterator OCRepresentation::cbegin() const
1061 return OCRepresentation::const_iterator(m_values.cbegin(), m_values);
1064 OCRepresentation::iterator OCRepresentation::end()
1066 return OCRepresentation::iterator(m_values.end(), m_values);
1069 OCRepresentation::const_iterator OCRepresentation::end() const
1071 return OCRepresentation::const_iterator(m_values.end(), m_values);
1074 OCRepresentation::const_iterator OCRepresentation::cend() const
1076 return OCRepresentation::const_iterator(m_values.cend(), m_values);
1079 size_t OCRepresentation::size() const
1081 return m_values.size();
1084 bool OCRepresentation::empty() const
1086 return m_values.empty();
1089 bool OCRepresentation::iterator::operator==(const OCRepresentation::iterator& rhs) const
1091 return m_iterator == rhs.m_iterator;
1094 bool OCRepresentation::iterator::operator!=(const OCRepresentation::iterator& rhs) const
1096 return m_iterator != rhs.m_iterator;
1099 bool OCRepresentation::const_iterator::operator==(
1100 const OCRepresentation::const_iterator& rhs) const
1102 return m_iterator == rhs.m_iterator;
1105 bool OCRepresentation::const_iterator::operator!=(
1106 const OCRepresentation::const_iterator& rhs) const
1108 return m_iterator != rhs.m_iterator;
1111 OCRepresentation::iterator::reference OCRepresentation::iterator::operator*()
1116 OCRepresentation::const_iterator::const_reference
1117 OCRepresentation::const_iterator::operator*() const
1122 OCRepresentation::iterator::pointer OCRepresentation::iterator::operator->()
1127 OCRepresentation::const_iterator::const_pointer
1128 OCRepresentation::const_iterator::operator->() const
1133 OCRepresentation::iterator& OCRepresentation::iterator::operator++()
1136 if (m_iterator != m_item.m_values.end())
1138 m_item.m_attrName = m_iterator->first;
1142 m_item.m_attrName = "";
1147 OCRepresentation::const_iterator& OCRepresentation::const_iterator::operator++()
1150 if (m_iterator != m_item.m_values.end())
1152 m_item.m_attrName = m_iterator->first;
1156 m_item.m_attrName = "";
1161 OCRepresentation::iterator OCRepresentation::iterator::operator++(int)
1163 OCRepresentation::iterator itr(*this);
1168 OCRepresentation::const_iterator OCRepresentation::const_iterator::operator++(int)
1170 OCRepresentation::const_iterator itr(*this);
1175 struct to_string_visitor : boost::static_visitor<>
1178 template <typename T>
1179 void operator()(T const& item)
1181 str = boost::lexical_cast<std::string>(item);
1184 template <typename T>
1185 void operator()(std::vector<T> const& item)
1187 to_string_visitor vis;
1188 std::ostringstream stream;
1191 for(const auto& i : item)
1194 stream << vis.str << " ";
1202 void to_string_visitor::operator()(bool const& item)
1204 str = item ? "true" : "false";
1208 void to_string_visitor::operator()(std::string const& item)
1214 void to_string_visitor::operator()(NullType const& /*item*/)
1220 void to_string_visitor::operator()(OCRepresentation const& /*item*/)
1222 str = "OC::OCRepresentation";
1225 std::string OCRepresentation::getValueToString(const std::string& key) const
1227 auto x = m_values.find(key);
1228 if (x != m_values.end())
1230 to_string_visitor vis;
1231 boost::apply_visitor(vis, x->second);
1232 return std::move(vis.str);
1238 std::string OCRepresentation::AttributeItem::getValueToString() const
1240 to_string_visitor vis;
1241 boost::apply_visitor(vis, m_values[m_attrName]);
1242 return std::move(vis.str);
1245 std::ostream& operator<<(std::ostream& os, const OCRepresentation::AttributeItem& ai)
1247 os << ai.getValueToString();