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"
42 static const char COAP[] = "coap://";
43 static const char COAPS[] = "coaps://";
44 static const char COAP_TCP[] = "coap+tcp://";
46 void MessageContainer::setPayload(const OCPayload* rep)
55 case PAYLOAD_TYPE_REPRESENTATION:
56 setPayload(reinterpret_cast<const OCRepPayload*>(rep));
58 case PAYLOAD_TYPE_DEVICE:
59 setPayload(reinterpret_cast<const OCDevicePayload*>(rep));
61 case PAYLOAD_TYPE_PLATFORM:
62 setPayload(reinterpret_cast<const OCPlatformPayload*>(rep));
65 throw OC::OCException("Invalid Payload type in setPayload");
70 void MessageContainer::setPayload(const OCDevicePayload* payload)
72 if (payload == nullptr)
78 rep[OC_RSRVD_DEVICE_ID] = (payload->sid) ?
79 std::string(payload->sid) :
81 rep[OC_RSRVD_DEVICE_NAME] = payload->deviceName ?
82 std::string(payload->deviceName) :
84 rep[OC_RSRVD_SPEC_VERSION] = payload->specVersion ?
85 std::string(payload->specVersion) :
87 for (OCStringLL *strll = payload->dataModelVersions; strll; strll = strll->next)
89 rep.addDataModelVersion(strll->value);
91 for (OCStringLL *strll = payload->types; strll; strll = strll->next)
93 rep.addResourceType(strll->value);
95 m_reps.push_back(std::move(rep));
98 void MessageContainer::setPayload(const OCPlatformPayload* payload)
100 if (payload == nullptr)
105 OCRepresentation rep;
106 rep[OC_RSRVD_PLATFORM_ID] = payload->info.platformID ?
107 std::string(payload->info.platformID) :
109 rep[OC_RSRVD_MFG_NAME] = payload->info.manufacturerName ?
110 std::string(payload->info.manufacturerName) :
112 rep[OC_RSRVD_MFG_URL] = payload->info.manufacturerUrl ?
113 std::string(payload->info.manufacturerUrl) :
115 rep[OC_RSRVD_MODEL_NUM] = payload->info.modelNumber ?
116 std::string(payload->info.modelNumber) :
118 rep[OC_RSRVD_MFG_DATE] = payload->info.dateOfManufacture ?
119 std::string(payload->info.dateOfManufacture) :
121 rep[OC_RSRVD_PLATFORM_VERSION] = payload->info.platformVersion ?
122 std::string(payload->info.platformVersion) :
124 rep[OC_RSRVD_OS_VERSION] = payload->info.operatingSystemVersion ?
125 std::string(payload->info.operatingSystemVersion) :
127 rep[OC_RSRVD_HARDWARE_VERSION] = payload->info.hardwareVersion ?
128 std::string(payload->info.hardwareVersion) :
130 rep[OC_RSRVD_FIRMWARE_VERSION] = payload->info.firmwareVersion ?
131 std::string(payload->info.firmwareVersion) :
133 rep[OC_RSRVD_SUPPORT_URL] = payload->info.supportUrl ?
134 std::string(payload->info.supportUrl) :
136 rep[OC_RSRVD_SYSTEM_TIME] = payload->info.systemTime ?
137 std::string(payload->info.systemTime) :
140 for (OCStringLL *strll = payload->rt; strll; strll = strll->next)
142 rep.addResourceType(strll->value);
144 for (OCStringLL *strll = payload->interfaces; strll; strll = strll->next)
146 rep.addResourceInterface(strll->value);
149 m_reps.push_back(std::move(rep));
152 void MessageContainer::setPayload(const OCRepPayload* payload)
154 const OCRepPayload* pl = payload;
157 OCRepresentation cur;
161 this->addRepresentation(cur);
165 OCRepPayload* MessageContainer::getPayload() const
167 OCRepPayload* root = nullptr;
168 for(const auto& r : representations())
172 root = r.getPayload();
176 OCRepPayloadAppend(root, r.getPayload());
183 const std::vector<OCRepresentation>& MessageContainer::representations() const
188 void MessageContainer::addRepresentation(const OCRepresentation& rep)
190 m_reps.push_back(rep);
196 struct get_payload_array: boost::static_visitor<>
199 void operator()(T& /*arr*/)
201 throw std::logic_error("Invalid calc_dimensions_visitor type");
205 void operator()(std::vector<T>& arr)
208 dimensions[0] = arr.size();
211 dimTotal = calcDimTotal(dimensions);
213 array = (void*)OICMalloc(dimTotal * root_size);
215 for(size_t i = 0; i < dimensions[0]; ++i)
217 copy_to_array(arr[i], array, i);
222 void operator()(std::vector<std::vector<T>>& arr)
225 dimensions[0] = arr.size();
228 for(size_t i = 0; i < arr.size(); ++i)
230 dimensions[1] = std::max(dimensions[1], arr[i].size());
232 dimTotal = calcDimTotal(dimensions);
233 array = (void*)OICCalloc(1, dimTotal * root_size);
235 for(size_t i = 0; i < dimensions[0]; ++i)
237 for(size_t j = 0; j < dimensions[1] && j < arr[i].size(); ++j)
239 copy_to_array(arr[i][j], array, i*dimensions[1] + j);
244 void operator()(std::vector<std::vector<std::vector<T>>>& arr)
247 dimensions[0] = arr.size();
250 for(size_t i = 0; i < arr.size(); ++i)
252 dimensions[1] = std::max(dimensions[1], arr[i].size());
254 for(size_t j = 0; j < arr[i].size(); ++j)
256 dimensions[2] = std::max(dimensions[2], arr[i][j].size());
260 dimTotal = calcDimTotal(dimensions);
261 array = (void*)OICCalloc(1, dimTotal * root_size);
263 for(size_t i = 0; i < dimensions[0]; ++i)
265 for(size_t j = 0; j < dimensions[1] && j < arr[i].size(); ++j)
267 for(size_t k = 0; k < dimensions[2] && k < arr[i][j].size(); ++k)
269 copy_to_array(arr[i][j][k], array,
271 dimensions[2] * dimensions[1] * i +
279 void root_size_calc()
281 root_size = sizeof(T);
285 void copy_to_array(T item, void* array, size_t pos)
287 ((T*)array)[pos] = item;
290 size_t dimensions[MAX_REP_ARRAY_DEPTH];
297 void get_payload_array::root_size_calc<int>()
299 root_size = sizeof(int64_t);
303 void get_payload_array::root_size_calc<std::string>()
305 root_size = sizeof(char*);
309 void get_payload_array::root_size_calc<OC::OCRepresentation>()
311 root_size = sizeof(OCRepPayload*);
315 void get_payload_array::copy_to_array(int item, void* array, size_t pos)
317 ((int64_t*)array)[pos] = item;
320 #if !defined(_MSC_VER)
322 void get_payload_array::copy_to_array(std::_Bit_reference br, void* array, size_t pos)
324 ((bool*)array)[pos] = static_cast<bool>(br);
329 void get_payload_array::copy_to_array(std::string item, void* array, size_t pos)
331 ((char**)array)[pos] = OICStrdup(item.c_str());
335 void get_payload_array::copy_to_array(std::string& item, void* array, size_t pos)
337 ((char**)array)[pos] = OICStrdup(item.c_str());
341 void get_payload_array::copy_to_array(const std::string& item, void* array, size_t pos)
343 ((char**)array)[pos] = OICStrdup(item.c_str());
347 void get_payload_array::copy_to_array(OCByteString item, void *array, size_t pos)
349 ((OCByteString *)array)[pos] = item;
353 void get_payload_array::copy_to_array(OCByteString &item, void *array, size_t pos)
355 ((OCByteString *)array)[pos] = item;
359 void get_payload_array::copy_to_array(const OCByteString &item, void *array, size_t pos)
361 ((OCByteString *)array)[pos] = item;
365 void get_payload_array::copy_to_array(OC::OCRepresentation item, void* array, size_t pos)
367 ((OCRepPayload**)array)[pos] = item.getPayload();
370 void OCRepresentation::getPayloadArray(OCRepPayload* payload,
371 const OCRepresentation::AttributeItem& item) const
373 get_payload_array vis{};
374 boost::apply_visitor(vis, m_values[item.attrname()]);
377 switch(item.base_type())
379 case AttributeType::Integer:
380 OCRepPayloadSetIntArrayAsOwner(payload, item.attrname().c_str(),
384 case AttributeType::Double:
385 OCRepPayloadSetDoubleArrayAsOwner(payload, item.attrname().c_str(),
389 case AttributeType::Boolean:
390 OCRepPayloadSetBoolArrayAsOwner(payload, item.attrname().c_str(),
394 case AttributeType::String:
395 OCRepPayloadSetStringArrayAsOwner(payload, item.attrname().c_str(),
399 case AttributeType::OCByteString:
400 OCRepPayloadSetByteStringArrayAsOwner(payload, item.attrname().c_str(),
401 (OCByteString *)vis.array, vis.dimensions);
403 case AttributeType::OCRepresentation:
404 OCRepPayloadSetPropObjectArrayAsOwner(payload, item.attrname().c_str(),
405 (OCRepPayload**)vis.array, vis.dimensions);
408 throw std::logic_error(std::string("GetPayloadArray: Not Implemented") +
409 std::to_string((int)item.base_type()));
413 OCRepPayload* OCRepresentation::getPayload() const
415 OCRepPayload* root = OCRepPayloadCreate();
418 throw std::bad_alloc();
421 OCRepPayloadSetUri(root, getUri().c_str());
423 for(const std::string& type : getResourceTypes())
425 OCRepPayloadAddResourceType(root, type.c_str());
428 for(const std::string& iface : getResourceInterfaces())
430 OCRepPayloadAddInterface(root, iface.c_str());
433 for(auto& val : *this)
437 case AttributeType::Null:
438 OCRepPayloadSetNull(root, val.attrname().c_str());
440 case AttributeType::Integer:
441 OCRepPayloadSetPropInt(root, val.attrname().c_str(), static_cast<int>(val));
443 case AttributeType::Double:
444 OCRepPayloadSetPropDouble(root, val.attrname().c_str(),
445 val.getValue<double>());
447 case AttributeType::Boolean:
448 OCRepPayloadSetPropBool(root, val.attrname().c_str(), val.getValue<bool>());
450 case AttributeType::String:
451 OCRepPayloadSetPropString(root, val.attrname().c_str(),
452 static_cast<std::string>(val).c_str());
454 case AttributeType::OCByteString:
455 OCRepPayloadSetPropByteString(root, val.attrname().c_str(), val.getValue<OCByteString>());
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{};
534 OCByteString OCRepresentation::payload_array_helper_copy<OCByteString>(
535 size_t index, const OCRepPayloadValue *pl)
537 OCByteString result {NULL, 0};
538 if (pl->arr.ocByteStrArray[index].len)
540 result = (pl->arr.ocByteStrArray[index]);
546 OCRepresentation OCRepresentation::payload_array_helper_copy<OCRepresentation>(
547 size_t index, const OCRepPayloadValue* pl)
550 if (pl->arr.objArray[index])
552 r.setPayload(pl->arr.objArray[index]);
558 void OCRepresentation::payload_array_helper(const OCRepPayloadValue* pl, size_t depth)
562 std::vector<T> val(pl->arr.dimensions[0]);
564 for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
566 val[i] = payload_array_helper_copy<T>(i, pl);
568 this->setValue(std::string(pl->name), val);
572 std::vector<std::vector<T>> val(pl->arr.dimensions[0]);
573 for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
575 val[i].resize(pl->arr.dimensions[1]);
576 for(size_t j = 0; j < pl->arr.dimensions[1]; ++j)
578 val[i][j] = payload_array_helper_copy<T>(
579 i * pl->arr.dimensions[1] + j, pl);
582 this->setValue(std::string(pl->name), val);
586 std::vector<std::vector<std::vector<T>>> val(pl->arr.dimensions[0]);
587 for(size_t i = 0; i < pl->arr.dimensions[0]; ++i)
589 val[i].resize(pl->arr.dimensions[1]);
590 for(size_t j = 0; j < pl->arr.dimensions[1]; ++j)
592 val[i][j].resize(pl->arr.dimensions[2]);
593 for(size_t k = 0; k < pl->arr.dimensions[2]; ++k)
595 val[i][j][k] = payload_array_helper_copy<T>(
596 pl->arr.dimensions[2] * j +
597 pl->arr.dimensions[2] * pl->arr.dimensions[1] * i +
603 this->setValue(std::string(pl->name), val);
607 throw std::logic_error("Invalid depth in payload_array_helper");
611 void OCRepresentation::setPayloadArray(const OCRepPayloadValue* pl)
617 payload_array_helper<int>(pl, calcArrayDepth(pl->arr.dimensions));
619 case OCREP_PROP_DOUBLE:
620 payload_array_helper<double>(pl, calcArrayDepth(pl->arr.dimensions));
622 case OCREP_PROP_BOOL:
623 payload_array_helper<bool>(pl, calcArrayDepth(pl->arr.dimensions));
625 case OCREP_PROP_STRING:
626 payload_array_helper<std::string>(pl, calcArrayDepth(pl->arr.dimensions));
628 case OCREP_PROP_BYTE_STRING:
629 payload_array_helper<OCByteString>(pl, calcArrayDepth(pl->arr.dimensions));
631 case OCREP_PROP_OBJECT:
632 payload_array_helper<OCRepresentation>(pl, calcArrayDepth(pl->arr.dimensions));
635 throw std::logic_error("setPayload array invalid type");
640 void OCRepresentation::setPayload(const OCRepPayload* pl)
644 OCStringLL* ll = pl->types;
647 addResourceType(ll->value);
654 addResourceInterface(ll->value);
658 OCRepPayloadValue* val = pl->values;
664 case OCREP_PROP_NULL:
668 setValue<int>(val->name, val->i);
670 case OCREP_PROP_DOUBLE:
671 setValue<double>(val->name, val->d);
673 case OCREP_PROP_BOOL:
674 setValue<bool>(val->name, val->b);
676 case OCREP_PROP_STRING:
677 setValue<std::string>(val->name, val->str);
679 case OCREP_PROP_OBJECT:
681 OCRepresentation cur;
682 cur.setPayload(val->obj);
683 setValue<OCRepresentation>(val->name, cur);
686 case OCREP_PROP_ARRAY:
687 setPayloadArray(val);
689 case OCREP_PROP_BYTE_STRING:
692 (val->ocByteStr.bytes, val->ocByteStr.bytes + val->ocByteStr.len)
696 throw std::logic_error(std::string("Not Implemented!") +
697 std::to_string((int)val->type));
704 void OCRepresentation::addChild(const OCRepresentation& rep)
706 m_children.push_back(rep);
709 void OCRepresentation::clearChildren()
714 const std::vector<OCRepresentation>& OCRepresentation::getChildren() const
719 void OCRepresentation::setChildren(const std::vector<OCRepresentation>& children)
721 m_children = children;
724 void OCRepresentation::setDevAddr(const OCDevAddr m_devAddr)
726 std::ostringstream ss;
727 if (m_devAddr.flags & OC_SECURE)
731 else if (m_devAddr.adapter & OC_ADAPTER_TCP)
739 if (m_devAddr.flags & OC_IP_USE_V6)
741 char addressEncoded[128] = {0};
743 OCStackResult result = OCEncodeAddressForRFC6874(addressEncoded,
744 sizeof(addressEncoded),
746 if (OC_STACK_OK != result)
748 throw OC::OCException("Invalid address in setDevAddr");
750 ss << '[' << addressEncoded << ']';
754 ss << m_devAddr.addr;
758 ss << ':' << m_devAddr.port;
763 const std::string OCRepresentation::getHost() const
768 void OCRepresentation::setUri(const char* uri)
770 m_uri = uri ? uri : "";
773 void OCRepresentation::setUri(const std::string& uri)
778 std::string OCRepresentation::getUri() const
783 const std::vector<std::string>& OCRepresentation::getResourceTypes() const
785 return m_resourceTypes;
788 void OCRepresentation::setResourceTypes(const std::vector<std::string>& resourceTypes)
790 m_resourceTypes = resourceTypes;
793 void OCRepresentation::addResourceType(const std::string& str)
795 m_resourceTypes.push_back(str);
798 const std::vector<std::string>& OCRepresentation::getResourceInterfaces() const
803 void OCRepresentation::addResourceInterface(const std::string& str)
805 m_interfaces.push_back(str);
808 void OCRepresentation::setResourceInterfaces(const std::vector<std::string>& resourceInterfaces)
810 m_interfaces = resourceInterfaces;
813 const std::vector<std::string>& OCRepresentation::getDataModelVersions() const
815 return m_dataModelVersions;
818 void OCRepresentation::addDataModelVersion(const std::string& str)
820 m_dataModelVersions.push_back(str);
823 bool OCRepresentation::hasAttribute(const std::string& str) const
825 return m_values.find(str) != m_values.end();
828 bool OCRepresentation::emptyData() const
830 // This logic is meant to determine whether based on the JSON serialization rules
831 // if this object will result in empty JSON. URI is only serialized if there is valid
832 // data, ResourceType and Interfaces are only serialized if we are a nothing, a
833 // child of a default or link item.
834 // Our values array is only printed in the if we are the child of a Batch resource,
835 // the parent in a 'default' situation, or not in a child/parent relationship.
840 else if ((m_interfaceType == InterfaceType::None
841 || m_interfaceType==InterfaceType::DefaultChild
842 || m_interfaceType==InterfaceType::LinkChild)
843 && (m_resourceTypes.size()>0 || m_interfaces.size()>0
844 || m_dataModelVersions.size()>0))
848 else if ((m_interfaceType == InterfaceType::None
849 || m_interfaceType == InterfaceType::BatchChild
850 || m_interfaceType == InterfaceType::DefaultParent)
851 && m_values.size()>0)
856 if (m_children.size() > 0)
864 int OCRepresentation::numberOfAttributes() const
866 return m_values.size();
869 bool OCRepresentation::erase(const std::string& str)
871 return m_values.erase(str);
874 void OCRepresentation::setNULL(const std::string& str)
876 m_values[str] = OC::NullType();
879 bool OCRepresentation::isNULL(const std::string& str) const
881 auto x = m_values.find(str);
883 if (m_values.end() != x)
885 return x->second.which() == AttributeValueNullIndex;
889 throw OCException(OC::Exception::INVALID_ATTRIBUTE+ str);
896 std::ostream& operator <<(std::ostream& os, const AttributeType at)
900 case AttributeType::Null:
903 case AttributeType::Integer:
906 case AttributeType::Double:
909 case AttributeType::Boolean:
912 case AttributeType::String:
915 case AttributeType::OCByteString:
916 os << "OCByteString";
918 case AttributeType::OCRepresentation:
919 os << "OCRepresentation";
921 case AttributeType::Vector:
924 case AttributeType::Binary:
931 // STL Container For OCRepresentation
934 OCRepresentation::AttributeItem::AttributeItem(const std::string& name,
935 std::map<std::string, AttributeValue>& vals):
936 m_attrName(name), m_values(vals){}
938 OCRepresentation::AttributeItem OCRepresentation::operator[](const std::string& key)
940 OCRepresentation::AttributeItem attr{key, m_values};
941 return std::move(attr);
944 const OCRepresentation::AttributeItem OCRepresentation::operator[](const std::string& key) const
946 OCRepresentation::AttributeItem attr{key, m_values};
947 return std::move(attr);
950 const std::string& OCRepresentation::AttributeItem::attrname() const
955 template<typename T, typename = void>
958 // contains the actual type
960 // contains the inner most vector-type
962 // contains the AttributeType for this item
963 BOOST_STATIC_CONSTEXPR AttributeType enum_type =
964 AttributeTypeConvert<T>::type;
965 // contains the AttributeType for this base-type
966 BOOST_STATIC_CONSTEXPR AttributeType enum_base_type =
967 AttributeTypeConvert<T>::type;
968 // depth of the vector
969 BOOST_STATIC_CONSTEXPR size_t depth = 0;
975 typename std::enable_if<
976 is_vector<T>::value &&
977 !std::is_same<uint8_t, typename T::value_type>::value
982 typedef typename type_info<typename T::value_type>::base_type base_type;
983 BOOST_STATIC_CONSTEXPR AttributeType enum_type = AttributeType::Vector;
984 BOOST_STATIC_CONSTEXPR AttributeType enum_base_type =
985 type_info<typename T::value_type>::enum_base_type;
986 BOOST_STATIC_CONSTEXPR size_t depth = 1 +
987 type_info<typename T::value_type>::depth;
990 // special case for binary data, which is a std::vector<uint8_t>
992 struct type_info<std::vector<uint8_t>, void>
994 typedef std::vector<uint8_t> type;
995 typedef std::vector<uint8_t> base_type;
996 BOOST_STATIC_CONSTEXPR AttributeType enum_type = AttributeType::Binary;
997 BOOST_STATIC_CONSTEXPR AttributeType enum_base_type = AttributeType::Binary;
998 BOOST_STATIC_CONSTEXPR size_t depth = 0;
1002 struct type_introspection_visitor : boost::static_visitor<>
1005 AttributeType base_type;
1008 type_introspection_visitor() : boost::static_visitor<>(),
1009 type(AttributeType::Null), base_type(AttributeType::Null), depth(0){}
1011 template <typename T>
1012 void operator()(T const& /*item*/)
1014 type = type_info<T>::enum_type;
1015 base_type = type_info<T>::enum_base_type;
1016 depth = type_info<T>::depth;
1020 AttributeType OCRepresentation::AttributeItem::type() const
1022 type_introspection_visitor vis;
1023 boost::apply_visitor(vis, m_values[m_attrName]);
1027 AttributeType OCRepresentation::AttributeItem::base_type() const
1029 type_introspection_visitor vis;
1030 boost::apply_visitor(vis, m_values[m_attrName]);
1031 return vis.base_type;
1034 size_t OCRepresentation::AttributeItem::depth() const
1036 type_introspection_visitor vis;
1037 boost::apply_visitor(vis, m_values[m_attrName]);
1041 OCRepresentation::iterator OCRepresentation::begin()
1043 return OCRepresentation::iterator(m_values.begin(), m_values);
1046 OCRepresentation::const_iterator OCRepresentation::begin() const
1048 return OCRepresentation::const_iterator(m_values.begin(), m_values);
1051 OCRepresentation::const_iterator OCRepresentation::cbegin() const
1053 return OCRepresentation::const_iterator(m_values.cbegin(), m_values);
1056 OCRepresentation::iterator OCRepresentation::end()
1058 return OCRepresentation::iterator(m_values.end(), m_values);
1061 OCRepresentation::const_iterator OCRepresentation::end() const
1063 return OCRepresentation::const_iterator(m_values.end(), m_values);
1066 OCRepresentation::const_iterator OCRepresentation::cend() const
1068 return OCRepresentation::const_iterator(m_values.cend(), m_values);
1071 size_t OCRepresentation::size() const
1073 return m_values.size();
1076 bool OCRepresentation::empty() const
1078 return m_values.empty();
1081 bool OCRepresentation::iterator::operator==(const OCRepresentation::iterator& rhs) const
1083 return m_iterator == rhs.m_iterator;
1086 bool OCRepresentation::iterator::operator!=(const OCRepresentation::iterator& rhs) const
1088 return m_iterator != rhs.m_iterator;
1091 bool OCRepresentation::const_iterator::operator==(
1092 const OCRepresentation::const_iterator& rhs) const
1094 return m_iterator == rhs.m_iterator;
1097 bool OCRepresentation::const_iterator::operator!=(
1098 const OCRepresentation::const_iterator& rhs) const
1100 return m_iterator != rhs.m_iterator;
1103 OCRepresentation::iterator::reference OCRepresentation::iterator::operator*()
1108 OCRepresentation::const_iterator::const_reference
1109 OCRepresentation::const_iterator::operator*() const
1114 OCRepresentation::iterator::pointer OCRepresentation::iterator::operator->()
1119 OCRepresentation::const_iterator::const_pointer
1120 OCRepresentation::const_iterator::operator->() const
1125 OCRepresentation::iterator& OCRepresentation::iterator::operator++()
1128 if (m_iterator != m_item.m_values.end())
1130 m_item.m_attrName = m_iterator->first;
1134 m_item.m_attrName = "";
1139 OCRepresentation::const_iterator& OCRepresentation::const_iterator::operator++()
1142 if (m_iterator != m_item.m_values.end())
1144 m_item.m_attrName = m_iterator->first;
1148 m_item.m_attrName = "";
1153 OCRepresentation::iterator OCRepresentation::iterator::operator++(int)
1155 OCRepresentation::iterator itr(*this);
1160 OCRepresentation::const_iterator OCRepresentation::const_iterator::operator++(int)
1162 OCRepresentation::const_iterator itr(*this);
1167 struct to_string_visitor : boost::static_visitor<>
1170 template <typename T>
1171 void operator()(T const& item)
1173 str = boost::lexical_cast<std::string>(item);
1176 template <typename T>
1177 void operator()(std::vector<T> const& item)
1179 to_string_visitor vis;
1180 std::ostringstream stream;
1183 for(const auto& i : item)
1186 stream << vis.str << " ";
1194 void to_string_visitor::operator()(bool const& item)
1196 str = item ? "true" : "false";
1200 void to_string_visitor::operator()(std::string const& item)
1206 void to_string_visitor::operator()(NullType const& /*item*/)
1212 void to_string_visitor::operator()(std::vector<uint8_t> const &item)
1214 std::ostringstream stream;
1215 for (size_t i = 0; i < item.size(); i++ )
1217 stream << "\\x" << std::hex << (int) item[i];
1223 void to_string_visitor::operator()(OCByteString const &item)
1225 std::vector<uint8_t> v(item.bytes, item.bytes + item.len);
1230 void to_string_visitor::operator()(OCRepresentation const& /*item*/)
1232 str = "OC::OCRepresentation";
1235 std::string OCRepresentation::getValueToString(const std::string& key) const
1237 auto x = m_values.find(key);
1238 if (x != m_values.end())
1240 to_string_visitor vis;
1241 boost::apply_visitor(vis, x->second);
1242 return std::move(vis.str);
1248 std::string OCRepresentation::AttributeItem::getValueToString() const
1250 to_string_visitor vis;
1251 boost::apply_visitor(vis, m_values[m_attrName]);
1252 return std::move(vis.str);
1255 std::ostream& operator<<(std::ostream& os, const OCRepresentation::AttributeItem& ai)
1257 os << ai.getValueToString();