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>
34 #include "ocpayload.h"
36 #include "oic_malloc.h"
37 #include "oic_string.h"
41 static const char COAP[] = "coap://";
42 static const char COAPS[] = "coaps://";
43 static const char COAP_TCP[] = "coap+tcp://";
45 void MessageContainer::setPayload(const OCPayload* rep)
54 case PAYLOAD_TYPE_REPRESENTATION:
55 setPayload(reinterpret_cast<const OCRepPayload*>(rep));
57 case PAYLOAD_TYPE_DEVICE:
58 setPayload(reinterpret_cast<const OCDevicePayload*>(rep));
60 case PAYLOAD_TYPE_PLATFORM:
61 setPayload(reinterpret_cast<const OCPlatformPayload*>(rep));
64 throw OC::OCException("Invalid Payload type in setPayload");
69 void MessageContainer::setPayload(const OCDevicePayload* payload)
71 if (payload == nullptr)
77 rep[OC_RSRVD_DEVICE_ID] = (payload->sid) ?
78 std::string(payload->sid) :
80 rep[OC_RSRVD_DEVICE_NAME] = payload->deviceName ?
81 std::string(payload->deviceName) :
83 rep[OC_RSRVD_SPEC_VERSION] = payload->specVersion ?
84 std::string(payload->specVersion) :
86 for (OCStringLL *strll = payload->dataModelVersions; strll; strll = strll->next)
88 rep.addDataModelVersion(strll->value);
90 for (OCStringLL *strll = payload->types; strll; strll = strll->next)
92 rep.addResourceType(strll->value);
94 m_reps.push_back(std::move(rep));
97 void MessageContainer::setPayload(const OCPlatformPayload* payload)
99 if (payload == nullptr)
104 OCRepresentation rep;
105 rep[OC_RSRVD_PLATFORM_ID] = payload->info.platformID ?
106 std::string(payload->info.platformID) :
108 rep[OC_RSRVD_MFG_NAME] = payload->info.manufacturerName ?
109 std::string(payload->info.manufacturerName) :
111 rep[OC_RSRVD_MFG_URL] = payload->info.manufacturerUrl ?
112 std::string(payload->info.manufacturerUrl) :
114 rep[OC_RSRVD_MODEL_NUM] = payload->info.modelNumber ?
115 std::string(payload->info.modelNumber) :
117 rep[OC_RSRVD_MFG_DATE] = payload->info.dateOfManufacture ?
118 std::string(payload->info.dateOfManufacture) :
120 rep[OC_RSRVD_PLATFORM_VERSION] = payload->info.platformVersion ?
121 std::string(payload->info.platformVersion) :
123 rep[OC_RSRVD_OS_VERSION] = payload->info.operatingSystemVersion ?
124 std::string(payload->info.operatingSystemVersion) :
126 rep[OC_RSRVD_HARDWARE_VERSION] = payload->info.hardwareVersion ?
127 std::string(payload->info.hardwareVersion) :
129 rep[OC_RSRVD_FIRMWARE_VERSION] = payload->info.firmwareVersion ?
130 std::string(payload->info.firmwareVersion) :
132 rep[OC_RSRVD_SUPPORT_URL] = payload->info.supportUrl ?
133 std::string(payload->info.supportUrl) :
135 rep[OC_RSRVD_SYSTEM_TIME] = payload->info.systemTime ?
136 std::string(payload->info.systemTime) :
139 for (OCStringLL *strll = payload->rt; strll; strll = strll->next)
141 rep.addResourceType(strll->value);
143 for (OCStringLL *strll = payload->interfaces; strll; strll = strll->next)
145 rep.addResourceInterface(strll->value);
148 m_reps.push_back(std::move(rep));
151 void MessageContainer::setPayload(const OCRepPayload* payload)
153 const OCRepPayload* pl = payload;
156 OCRepresentation cur;
160 this->addRepresentation(cur);
164 OCRepPayload* MessageContainer::getPayload() const
166 OCRepPayload* root = nullptr;
167 for(const auto& r : representations())
171 root = r.getPayload();
175 OCRepPayloadAppend(root, r.getPayload());
182 const std::vector<OCRepresentation>& MessageContainer::representations() const
187 void MessageContainer::addRepresentation(const OCRepresentation& rep)
189 m_reps.push_back(rep);
195 struct get_payload_array: boost::static_visitor<>
198 void operator()(T& /*arr*/)
200 throw std::logic_error("Invalid calc_dimensions_visitor type");
204 void operator()(std::vector<T>& arr)
207 dimensions[0] = arr.size();
210 dimTotal = calcDimTotal(dimensions);
212 array = (void*)OICMalloc(dimTotal * root_size);
214 for(size_t i = 0; i < dimensions[0]; ++i)
216 copy_to_array(arr[i], array, i);
221 void operator()(std::vector<std::vector<T>>& arr)
224 dimensions[0] = arr.size();
227 for(size_t i = 0; i < arr.size(); ++i)
229 dimensions[1] = std::max(dimensions[1], arr[i].size());
231 dimTotal = calcDimTotal(dimensions);
232 array = (void*)OICCalloc(1, dimTotal * root_size);
234 for(size_t i = 0; i < dimensions[0]; ++i)
236 for(size_t j = 0; j < dimensions[1] && j < arr[i].size(); ++j)
238 copy_to_array(arr[i][j], array, i*dimensions[1] + j);
243 void operator()(std::vector<std::vector<std::vector<T>>>& arr)
246 dimensions[0] = arr.size();
249 for(size_t i = 0; i < arr.size(); ++i)
251 dimensions[1] = std::max(dimensions[1], arr[i].size());
253 for(size_t j = 0; j < arr[i].size(); ++j)
255 dimensions[2] = std::max(dimensions[2], arr[i][j].size());
259 dimTotal = calcDimTotal(dimensions);
260 array = (void*)OICCalloc(1, dimTotal * root_size);
262 for(size_t i = 0; i < dimensions[0]; ++i)
264 for(size_t j = 0; j < dimensions[1] && j < arr[i].size(); ++j)
266 for(size_t k = 0; k < dimensions[2] && k < arr[i][j].size(); ++k)
268 copy_to_array(arr[i][j][k], array,
270 dimensions[2] * dimensions[1] * i +
278 void root_size_calc()
280 root_size = sizeof(T);
284 void copy_to_array(T item, void* array, size_t pos)
286 ((T*)array)[pos] = item;
289 size_t dimensions[MAX_REP_ARRAY_DEPTH];
296 void get_payload_array::root_size_calc<int>()
298 root_size = sizeof(int64_t);
302 void get_payload_array::root_size_calc<std::string>()
304 root_size = sizeof(char*);
308 void get_payload_array::root_size_calc<OC::OCRepresentation>()
310 root_size = sizeof(OCRepPayload*);
314 void get_payload_array::copy_to_array(int item, void* array, size_t pos)
316 ((int64_t*)array)[pos] = item;
319 #if !defined(_MSC_VER)
321 void get_payload_array::copy_to_array(std::_Bit_reference br, void* array, size_t pos)
323 ((bool*)array)[pos] = static_cast<bool>(br);
328 void get_payload_array::copy_to_array(std::string item, void* array, size_t pos)
330 ((char**)array)[pos] = OICStrdup(item.c_str());
334 void get_payload_array::copy_to_array(std::string& item, void* array, size_t pos)
336 ((char**)array)[pos] = OICStrdup(item.c_str());
340 void get_payload_array::copy_to_array(const std::string& item, void* array, size_t pos)
342 ((char**)array)[pos] = OICStrdup(item.c_str());
346 void get_payload_array::copy_to_array(OCByteString item, void *array, size_t pos)
348 ((OCByteString *)array)[pos] = item;
352 void get_payload_array::copy_to_array(OCByteString &item, void *array, size_t pos)
354 ((OCByteString *)array)[pos] = item;
358 void get_payload_array::copy_to_array(const OCByteString &item, void *array, size_t pos)
360 ((OCByteString *)array)[pos] = item;
364 void get_payload_array::copy_to_array(OC::OCRepresentation item, void* array, size_t pos)
366 ((OCRepPayload**)array)[pos] = item.getPayload();
369 void OCRepresentation::getPayloadArray(OCRepPayload* payload,
370 const OCRepresentation::AttributeItem& item) const
372 get_payload_array vis{};
373 boost::apply_visitor(vis, m_values[item.attrname()]);
376 switch(item.base_type())
378 case AttributeType::Integer:
379 OCRepPayloadSetIntArrayAsOwner(payload, item.attrname().c_str(),
383 case AttributeType::Double:
384 OCRepPayloadSetDoubleArrayAsOwner(payload, item.attrname().c_str(),
388 case AttributeType::Boolean:
389 OCRepPayloadSetBoolArrayAsOwner(payload, item.attrname().c_str(),
393 case AttributeType::String:
394 OCRepPayloadSetStringArrayAsOwner(payload, item.attrname().c_str(),
398 case AttributeType::OCByteString:
399 OCRepPayloadSetByteStringArrayAsOwner(payload, item.attrname().c_str(),
400 (OCByteString *)vis.array, vis.dimensions);
402 case AttributeType::OCRepresentation:
403 OCRepPayloadSetPropObjectArrayAsOwner(payload, item.attrname().c_str(),
404 (OCRepPayload**)vis.array, vis.dimensions);
407 throw std::logic_error(std::string("GetPayloadArray: Not Implemented") +
408 std::to_string((int)item.base_type()));
412 OCRepPayload* OCRepresentation::getPayload() const
414 OCRepPayload* root = OCRepPayloadCreate();
417 throw std::bad_alloc();
420 OCRepPayloadSetUri(root, getUri().c_str());
422 for(const std::string& type : getResourceTypes())
424 OCRepPayloadAddResourceType(root, type.c_str());
427 for(const std::string& iface : getResourceInterfaces())
429 OCRepPayloadAddInterface(root, iface.c_str());
432 for(auto& val : *this)
436 case AttributeType::Null:
437 OCRepPayloadSetNull(root, val.attrname().c_str());
439 case AttributeType::Integer:
440 OCRepPayloadSetPropInt(root, val.attrname().c_str(), static_cast<int>(val));
442 case AttributeType::Double:
443 OCRepPayloadSetPropDouble(root, val.attrname().c_str(),
444 val.getValue<double>());
446 case AttributeType::Boolean:
447 OCRepPayloadSetPropBool(root, val.attrname().c_str(), val.getValue<bool>());
449 case AttributeType::String:
450 OCRepPayloadSetPropString(root, val.attrname().c_str(),
451 static_cast<std::string>(val).c_str());
453 case AttributeType::OCByteString:
454 OCRepPayloadSetPropByteString(root, val.attrname().c_str(), val.getValue<OCByteString>());
456 case AttributeType::OCRepresentation:
457 OCRepPayloadSetPropObjectAsOwner(root, val.attrname().c_str(),
458 static_cast<OCRepresentation>(val).getPayload());
460 case AttributeType::Vector:
461 getPayloadArray(root, val);
463 case AttributeType::Binary:
464 OCRepPayloadSetPropByteString(root, val.attrname().c_str(),
465 OCByteString{const_cast<uint8_t*>(val.getValue<std::vector<uint8_t>>().data()),
466 val.getValue<std::vector<uint8_t>>().size()});
469 throw std::logic_error(std::string("Getpayload: Not Implemented") +
470 std::to_string((int)val.type()));
478 size_t calcArrayDepth(const size_t dimensions[MAX_REP_ARRAY_DEPTH])
480 if (dimensions[0] == 0)
482 throw std::logic_error("invalid calcArrayDepth");
484 else if (dimensions[1] == 0)
488 else if (dimensions[2] == 0)
499 T OCRepresentation::payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl)
501 throw std::logic_error("payload_array_helper_copy: unsupported type");
504 int OCRepresentation::payload_array_helper_copy<int>(size_t index, const OCRepPayloadValue* pl)
506 return pl->arr.iArray[index];
509 double OCRepresentation::payload_array_helper_copy<double>(size_t index, const OCRepPayloadValue* pl)
511 return pl->arr.dArray[index];
514 bool OCRepresentation::payload_array_helper_copy<bool>(size_t index, const OCRepPayloadValue* pl)
516 return pl->arr.bArray[index];
519 std::string OCRepresentation::payload_array_helper_copy<std::string>(
520 size_t index, const OCRepPayloadValue* pl)
522 if (pl->arr.strArray[index])
524 return std::string(pl->arr.strArray[index]);
528 return std::string{};
533 OCByteString OCRepresentation::payload_array_helper_copy<OCByteString>(
534 size_t index, const OCRepPayloadValue *pl)
536 OCByteString result {NULL, 0};
537 if (pl->arr.ocByteStrArray[index].len)
539 result = (pl->arr.ocByteStrArray[index]);
545 OCRepresentation OCRepresentation::payload_array_helper_copy<OCRepresentation>(
546 size_t index, const OCRepPayloadValue* pl)
549 if (pl->arr.objArray[index])
551 r.setPayload(pl->arr.objArray[index]);
557 void OCRepresentation::payload_array_helper(const OCRepPayloadValue* pl, size_t depth)
561 std::vector<T> val(pl->arr.dimensions[0]);
563 for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
565 val[i] = payload_array_helper_copy<T>(i, pl);
567 this->setValue(std::string(pl->name), val);
571 std::vector<std::vector<T>> val(pl->arr.dimensions[0]);
572 for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
574 val[i].resize(pl->arr.dimensions[1]);
575 for(size_t j = 0; j < pl->arr.dimensions[1]; ++j)
577 val[i][j] = payload_array_helper_copy<T>(
578 i * pl->arr.dimensions[1] + j, pl);
581 this->setValue(std::string(pl->name), val);
585 std::vector<std::vector<std::vector<T>>> val(pl->arr.dimensions[0]);
586 for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
588 val[i].resize(pl->arr.dimensions[1]);
589 for(size_t j = 0; j < pl->arr.dimensions[1]; ++j)
591 val[i][j].resize(pl->arr.dimensions[2]);
592 for(size_t k = 0; k < pl->arr.dimensions[2]; ++k)
594 val[i][j][k] = payload_array_helper_copy<T>(
595 pl->arr.dimensions[2] * j +
596 pl->arr.dimensions[2] * pl->arr.dimensions[1] * i +
602 this->setValue(std::string(pl->name), val);
606 throw std::logic_error("Invalid depth in payload_array_helper");
610 void OCRepresentation::setPayloadArray(const OCRepPayloadValue* pl)
616 payload_array_helper<int>(pl, calcArrayDepth(pl->arr.dimensions));
618 case OCREP_PROP_DOUBLE:
619 payload_array_helper<double>(pl, calcArrayDepth(pl->arr.dimensions));
621 case OCREP_PROP_BOOL:
622 payload_array_helper<bool>(pl, calcArrayDepth(pl->arr.dimensions));
624 case OCREP_PROP_STRING:
625 payload_array_helper<std::string>(pl, calcArrayDepth(pl->arr.dimensions));
627 case OCREP_PROP_BYTE_STRING:
628 payload_array_helper<OCByteString>(pl, calcArrayDepth(pl->arr.dimensions));
630 case OCREP_PROP_OBJECT:
631 payload_array_helper<OCRepresentation>(pl, calcArrayDepth(pl->arr.dimensions));
634 throw std::logic_error("setPayload array invalid type");
639 void OCRepresentation::setPayload(const OCRepPayload* pl)
643 OCStringLL* ll = pl->types;
646 addResourceType(ll->value);
653 addResourceInterface(ll->value);
657 OCRepPayloadValue* val = pl->values;
663 case OCREP_PROP_NULL:
667 setValue<int>(val->name, val->i);
669 case OCREP_PROP_DOUBLE:
670 setValue<double>(val->name, val->d);
672 case OCREP_PROP_BOOL:
673 setValue<bool>(val->name, val->b);
675 case OCREP_PROP_STRING:
676 setValue<std::string>(val->name, val->str);
678 case OCREP_PROP_OBJECT:
680 OCRepresentation cur;
681 cur.setPayload(val->obj);
682 setValue<OCRepresentation>(val->name, cur);
685 case OCREP_PROP_ARRAY:
686 setPayloadArray(val);
688 case OCREP_PROP_BYTE_STRING:
691 (val->ocByteStr.bytes, val->ocByteStr.bytes + val->ocByteStr.len)
695 throw std::logic_error(std::string("Not Implemented!") +
696 std::to_string((int)val->type));
703 void OCRepresentation::addChild(const OCRepresentation& rep)
705 m_children.push_back(rep);
708 void OCRepresentation::clearChildren()
713 const std::vector<OCRepresentation>& OCRepresentation::getChildren() const
718 void OCRepresentation::setChildren(const std::vector<OCRepresentation>& children)
720 m_children = children;
723 void OCRepresentation::setDevAddr(const OCDevAddr m_devAddr)
725 std::ostringstream ss;
726 if (m_devAddr.flags & OC_SECURE)
730 else if (m_devAddr.adapter & OC_ADAPTER_TCP)
738 if (m_devAddr.flags & OC_IP_USE_V6)
740 ss << '[' << m_devAddr.addr << ']';
744 ss << m_devAddr.addr;
748 ss << ':' << m_devAddr.port;
753 const std::string OCRepresentation::getHost() const
758 void OCRepresentation::setUri(const char* uri)
760 m_uri = uri ? uri : "";
763 void OCRepresentation::setUri(const std::string& uri)
768 std::string OCRepresentation::getUri() const
773 const std::vector<std::string>& OCRepresentation::getResourceTypes() const
775 return m_resourceTypes;
778 void OCRepresentation::setResourceTypes(const std::vector<std::string>& resourceTypes)
780 m_resourceTypes = resourceTypes;
783 void OCRepresentation::addResourceType(const std::string& str)
785 m_resourceTypes.push_back(str);
788 const std::vector<std::string>& OCRepresentation::getResourceInterfaces() const
793 void OCRepresentation::addResourceInterface(const std::string& str)
795 m_interfaces.push_back(str);
798 void OCRepresentation::setResourceInterfaces(const std::vector<std::string>& resourceInterfaces)
800 m_interfaces = resourceInterfaces;
803 const std::vector<std::string>& OCRepresentation::getDataModelVersions() const
805 return m_dataModelVersions;
808 void OCRepresentation::addDataModelVersion(const std::string& str)
810 m_dataModelVersions.push_back(str);
813 bool OCRepresentation::hasAttribute(const std::string& str) const
815 return m_values.find(str) != m_values.end();
818 bool OCRepresentation::emptyData() const
820 // This logic is meant to determine whether based on the JSON serialization rules
821 // if this object will result in empty JSON. URI is only serialized if there is valid
822 // data, ResourceType and Interfaces are only serialized if we are a nothing, a
823 // child of a default or link item.
824 // Our values array is only printed in the if we are the child of a Batch resource,
825 // the parent in a 'default' situation, or not in a child/parent relationship.
830 else if ((m_interfaceType == InterfaceType::None
831 || m_interfaceType==InterfaceType::DefaultChild
832 || m_interfaceType==InterfaceType::LinkChild)
833 && (m_resourceTypes.size()>0 || m_interfaces.size()>0
834 || m_dataModelVersions.size()>0))
838 else if ((m_interfaceType == InterfaceType::None
839 || m_interfaceType == InterfaceType::BatchChild
840 || m_interfaceType == InterfaceType::DefaultParent)
841 && m_values.size()>0)
846 if (m_children.size() > 0)
854 int OCRepresentation::numberOfAttributes() const
856 return m_values.size();
859 bool OCRepresentation::erase(const std::string& str)
861 return m_values.erase(str);
864 void OCRepresentation::setNULL(const std::string& str)
866 m_values[str] = OC::NullType();
869 bool OCRepresentation::isNULL(const std::string& str) const
871 auto x = m_values.find(str);
873 if (m_values.end() != x)
875 return x->second.which() == AttributeValueNullIndex;
879 throw OCException(OC::Exception::INVALID_ATTRIBUTE+ str);
886 std::ostream& operator <<(std::ostream& os, const AttributeType at)
890 case AttributeType::Null:
893 case AttributeType::Integer:
896 case AttributeType::Double:
899 case AttributeType::Boolean:
902 case AttributeType::String:
905 case AttributeType::OCByteString:
906 os << "OCByteString";
908 case AttributeType::OCRepresentation:
909 os << "OCRepresentation";
911 case AttributeType::Vector:
914 case AttributeType::Binary:
921 // STL Container For OCRepresentation
924 OCRepresentation::AttributeItem::AttributeItem(const std::string& name,
925 std::map<std::string, AttributeValue>& vals):
926 m_attrName(name), m_values(vals){}
928 OCRepresentation::AttributeItem OCRepresentation::operator[](const std::string& key)
930 OCRepresentation::AttributeItem attr{key, m_values};
931 return std::move(attr);
934 const OCRepresentation::AttributeItem OCRepresentation::operator[](const std::string& key) const
936 OCRepresentation::AttributeItem attr{key, m_values};
937 return std::move(attr);
940 const std::string& OCRepresentation::AttributeItem::attrname() const
945 template<typename T, typename = void>
948 // contains the actual type
950 // contains the inner most vector-type
952 // contains the AttributeType for this item
953 BOOST_STATIC_CONSTEXPR AttributeType enum_type =
954 AttributeTypeConvert<T>::type;
955 // contains the AttributeType for this base-type
956 BOOST_STATIC_CONSTEXPR AttributeType enum_base_type =
957 AttributeTypeConvert<T>::type;
958 // depth of the vector
959 BOOST_STATIC_CONSTEXPR size_t depth = 0;
965 typename std::enable_if<
966 is_vector<T>::value &&
967 !std::is_same<uint8_t, typename T::value_type>::value
972 typedef typename type_info<typename T::value_type>::base_type base_type;
973 BOOST_STATIC_CONSTEXPR AttributeType enum_type = AttributeType::Vector;
974 BOOST_STATIC_CONSTEXPR AttributeType enum_base_type =
975 type_info<typename T::value_type>::enum_base_type;
976 BOOST_STATIC_CONSTEXPR size_t depth = 1 +
977 type_info<typename T::value_type>::depth;
980 // special case for binary data, which is a std::vector<uint8_t>
982 struct type_info<std::vector<uint8_t>, void>
984 typedef std::vector<uint8_t> type;
985 typedef std::vector<uint8_t> base_type;
986 BOOST_STATIC_CONSTEXPR AttributeType enum_type = AttributeType::Binary;
987 BOOST_STATIC_CONSTEXPR AttributeType enum_base_type = AttributeType::Binary;
988 BOOST_STATIC_CONSTEXPR size_t depth = 0;
992 struct type_introspection_visitor : boost::static_visitor<>
995 AttributeType base_type;
998 type_introspection_visitor() : boost::static_visitor<>(),
999 type(AttributeType::Null), base_type(AttributeType::Null), depth(0){}
1001 template <typename T>
1002 void operator()(T const& /*item*/)
1004 type = type_info<T>::enum_type;
1005 base_type = type_info<T>::enum_base_type;
1006 depth = type_info<T>::depth;
1010 AttributeType OCRepresentation::AttributeItem::type() const
1012 type_introspection_visitor vis;
1013 boost::apply_visitor(vis, m_values[m_attrName]);
1017 AttributeType OCRepresentation::AttributeItem::base_type() const
1019 type_introspection_visitor vis;
1020 boost::apply_visitor(vis, m_values[m_attrName]);
1021 return vis.base_type;
1024 size_t OCRepresentation::AttributeItem::depth() const
1026 type_introspection_visitor vis;
1027 boost::apply_visitor(vis, m_values[m_attrName]);
1031 OCRepresentation::iterator OCRepresentation::begin()
1033 return OCRepresentation::iterator(m_values.begin(), m_values);
1036 OCRepresentation::const_iterator OCRepresentation::begin() const
1038 return OCRepresentation::const_iterator(m_values.begin(), m_values);
1041 OCRepresentation::const_iterator OCRepresentation::cbegin() const
1043 return OCRepresentation::const_iterator(m_values.cbegin(), m_values);
1046 OCRepresentation::iterator OCRepresentation::end()
1048 return OCRepresentation::iterator(m_values.end(), m_values);
1051 OCRepresentation::const_iterator OCRepresentation::end() const
1053 return OCRepresentation::const_iterator(m_values.end(), m_values);
1056 OCRepresentation::const_iterator OCRepresentation::cend() const
1058 return OCRepresentation::const_iterator(m_values.cend(), m_values);
1061 size_t OCRepresentation::size() const
1063 return m_values.size();
1066 bool OCRepresentation::empty() const
1068 return m_values.empty();
1071 bool OCRepresentation::iterator::operator==(const OCRepresentation::iterator& rhs) const
1073 return m_iterator == rhs.m_iterator;
1076 bool OCRepresentation::iterator::operator!=(const OCRepresentation::iterator& rhs) const
1078 return m_iterator != rhs.m_iterator;
1081 bool OCRepresentation::const_iterator::operator==(
1082 const OCRepresentation::const_iterator& rhs) const
1084 return m_iterator == rhs.m_iterator;
1087 bool OCRepresentation::const_iterator::operator!=(
1088 const OCRepresentation::const_iterator& rhs) const
1090 return m_iterator != rhs.m_iterator;
1093 OCRepresentation::iterator::reference OCRepresentation::iterator::operator*()
1098 OCRepresentation::const_iterator::const_reference
1099 OCRepresentation::const_iterator::operator*() const
1104 OCRepresentation::iterator::pointer OCRepresentation::iterator::operator->()
1109 OCRepresentation::const_iterator::const_pointer
1110 OCRepresentation::const_iterator::operator->() const
1115 OCRepresentation::iterator& OCRepresentation::iterator::operator++()
1118 if (m_iterator != m_item.m_values.end())
1120 m_item.m_attrName = m_iterator->first;
1124 m_item.m_attrName = "";
1129 OCRepresentation::const_iterator& OCRepresentation::const_iterator::operator++()
1132 if (m_iterator != m_item.m_values.end())
1134 m_item.m_attrName = m_iterator->first;
1138 m_item.m_attrName = "";
1143 OCRepresentation::iterator OCRepresentation::iterator::operator++(int)
1145 OCRepresentation::iterator itr(*this);
1150 OCRepresentation::const_iterator OCRepresentation::const_iterator::operator++(int)
1152 OCRepresentation::const_iterator itr(*this);
1157 struct to_string_visitor : boost::static_visitor<>
1160 template <typename T>
1161 void operator()(T const& item)
1163 str = boost::lexical_cast<std::string>(item);
1166 template <typename T>
1167 void operator()(std::vector<T> const& item)
1169 to_string_visitor vis;
1170 std::ostringstream stream;
1173 for(const auto& i : item)
1176 stream << vis.str << " ";
1184 void to_string_visitor::operator()(bool const& item)
1186 str = item ? "true" : "false";
1190 void to_string_visitor::operator()(std::string const& item)
1196 void to_string_visitor::operator()(NullType const& /*item*/)
1202 void to_string_visitor::operator()(std::vector<uint8_t> const &item)
1204 std::ostringstream stream;
1205 for (size_t i = 0; i < item.size(); i++ )
1207 stream << "\\x" << std::hex << (int) item[i];
1213 void to_string_visitor::operator()(OCByteString const &item)
1215 std::vector<uint8_t> v(item.bytes, item.bytes + item.len);
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();