Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / include / OCRepresentation.h
index 2e64fcd..84e88e5 100644 (file)
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
-/// @file OCRepresentation.h
-
-/// @brief  This file contains the declaration of classes and its members
-///         related to OCRepresentation
+/**
+ * @file
+ *
+ * This file contains the declaration of classes and its members related
+ * to OCRepresentation.
+ */
 
 #ifndef __OCREPRESENTATION_H
 #define __OCREPRESENTATION_H
 
 #include <OCException.h>
 
-namespace cereal
-{
-    class access;
-}
-
 namespace OC
 {
 
@@ -60,24 +57,18 @@ namespace OC
         DefaultChild
     };
 
-    // The consumer requires resource info to be printed in 2 different ways, both with the "oc":[]
-    // and without.  This enum is used to differentiate between the two situations.  When the
-    // serialize is called with Include OC, we encode OC, otherwise we skip it and return just the
-    // contents of the array.
-    enum class OCInfoFormat
-    {
-        IncludeOC,
-        ExcludeOC
-    };
-
     class MessageContainer
     {
         public:
-            void setJSONRepresentation(const std::string& payload);
+            void setPayload(const OCPayload* rep);
+
+            void setPayload(const OCDevicePayload* rep);
+
+            void setPayload(const OCPlatformPayload* rep);
 
-            void setJSONRepresentation(const unsigned char* payload);
+            void setPayload(const OCRepPayload* rep);
 
-            std::string getJSONRepresentation(OCInfoFormat f) const;
+            OCRepPayload* getPayload() const;
 
             const std::vector<OCRepresentation>& representations() const;
 
@@ -98,8 +89,27 @@ namespace OC
     class OCRepresentation
     {
         public:
-            OCRepresentation();
-            std::string getJSONRepresentation() const;
+            friend bool operator==(const OC::OCRepresentation&, const OC::OCRepresentation&);
+            // Note: Implementation of all constructors and destructors
+            // are all placed in the same location due to a crash that
+            // was observed in Android, where merely constructing/destructing
+            // an OCRepresentation object was enough to cause an invalid 'free'.
+            // It is believed that this is a result of incompatible compiler
+            // options between the gradle JNI and armeabi scons build, however
+            // this fix will work in the meantime.
+            OCRepresentation(): m_interfaceType(InterfaceType::None){}
+
+            OCRepresentation(OCRepresentation&&) = default;
+
+            OCRepresentation(const OCRepresentation&) = default;
+
+            OCRepresentation& operator=(const OCRepresentation&) = default;
+
+            OCRepresentation& operator=(OCRepresentation&&) = default;
+
+            virtual ~OCRepresentation(){}
+
+            OCRepPayload* getPayload() const;
 
             void addChild(const OCRepresentation&);
 
@@ -109,6 +119,8 @@ namespace OC
 
             void setChildren(const std::vector<OCRepresentation>& children);
 
+            void setUri(const char* uri);
+
             void setUri(const std::string& uri);
 
             std::string getUri() const;
@@ -117,11 +129,15 @@ namespace OC
 
             void setResourceTypes(const std::vector<std::string>& resourceTypes);
 
+            void addResourceType(const std::string& str);
+
             const std::vector<std::string>& getResourceInterfaces() const;
 
             void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
 
-            bool empty() const;
+            void addResourceInterface(const std::string& str);
+
+            bool emptyData() const;
 
             int numberOfAttributes() const;
 
@@ -133,6 +149,14 @@ namespace OC
                 m_values[str] = val;
             }
 
+            /**
+             *  Retrieve the attribute value associated with the supplied name
+             *
+             *  @param str Name of the attribute
+             *  @param val Value of the attribute
+             *  @return The getValue method returns true if the attribute was
+             *        found in the representation.  Otherwise it returns false.
+             */
             template <typename T>
             bool getValue(const std::string& str, T& val) const
             {
@@ -150,6 +174,14 @@ namespace OC
                 }
             }
 
+            /**
+             *  Return the attribute value associated with the supplied name
+             *
+             *  @param str Name of the attribute
+             *  @return When the representation contains the attribute, the
+             *       the associated value is returned.  Otherwise, getValue
+             *       returns the default contructed value for the type.
+             */
             template <typename T>
             T getValue(const std::string& str) const
             {
@@ -162,15 +194,211 @@ namespace OC
                 return val;
             }
 
+          /**
+            *  Retrieve the attributevalue structure associated with the supplied name
+            *
+            *  @param str Name of the attribute
+            *  @param attrValue Attribute Value structure
+            *  @return The getAttributeValue method returns true if the attribute was
+            *        found in the representation.  Otherwise it returns false.
+            */
+            bool getAttributeValue(const std::string& str, AttributeValue& attrValue) const
+            {
+                auto x = m_values.find(str);
+
+                if (x != m_values.end())
+                {
+                    attrValue = x->second;
+                    return true;
+                }
+                else
+                {
+                    return false;
+                }
+            }
+
+            std::string getValueToString(const std::string& key) const;
             bool hasAttribute(const std::string& str) const;
 
             void setNULL(const std::string& str);
 
             bool isNULL(const std::string& str) const;
+
+            // STL Container stuff
+        public:
+            class iterator;
+            class const_iterator;
+            // Shim class to allow iterating and indexing of the OCRepresentation
+            // object.
+            class AttributeItem
+            {
+                friend class OCRepresentation;
+                friend class iterator;
+                friend class const_iterator;
+                public:
+                    const std::string& attrname() const;
+                    AttributeType type() const;
+                    AttributeType base_type() const;
+                    size_t depth() const;
+                    template<typename T>
+                    T getValue() const
+                    {
+                        return boost::get<T>(m_values[m_attrName]);
+                    }
+
+                    std::string getValueToString() const;
+
+                    template<typename T>
+                    AttributeItem& operator=(T&& rhs)
+                    {
+                        m_values[m_attrName] = std::forward<T>(rhs);
+                        return *this;
+                    }
+
+                    AttributeItem& operator=(std::nullptr_t /*rhs*/)
+                    {
+                        NullType t;
+                        m_values[m_attrName] = t;
+                        return *this;
+                    }
+
+                    // Enable-if required to prevent conversions to alternate types.  This prevents
+                    // ambigious conversions in the case where conversions can include a number of
+                    // types, such as the string constructor.
+                    template<typename T, typename std::enable_if<
+                     std::is_same<T, int>::value ||
+                     std::is_same<T, double>::value ||
+                     std::is_same<T, bool>::value ||
+                     std::is_same<T, std::string>::value ||
+                     std::is_same<T, OCRepresentation>::value ||
+                     std::is_same<T, std::vector<int>>::value ||
+                     std::is_same<T, std::vector<std::vector<int>>>::value ||
+                     std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
+                     std::is_same<T, std::vector<double>>::value ||
+                     std::is_same<T, std::vector<std::vector<double>>>::value ||
+                     std::is_same<T, std::vector<std::vector<std::vector<double>>>>::value ||
+                     std::is_same<T, std::vector<bool>>::value ||
+                     std::is_same<T, std::vector<std::vector<bool>>>::value ||
+                     std::is_same<T, std::vector<std::vector<std::vector<bool>>>>::value ||
+                     std::is_same<T, std::vector<std::string>>::value ||
+                     std::is_same<T, std::vector<std::vector<std::string>>>::value ||
+                     std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
+                     std::is_same<T, std::vector<OCRepresentation>>::value ||
+                     std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
+                     std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value
+                     , int>::type = 0// enable_if
+                    >
+                    operator T() const
+                    {
+                        return this->getValue<T>();
+                    }
+
+                    template<typename T, typename std::enable_if<
+                        std::is_same<T, std::nullptr_t>::value
+                        , int>::type = 0
+                    >
+                    operator T() const
+                    {
+                        this->getValue<NullType>();
+                        return nullptr;
+                    }
+
+                private:
+                    AttributeItem(const std::string& name,
+                            std::map<std::string, AttributeValue>& vals);
+                    AttributeItem(const AttributeItem&) = default;
+                    std::string m_attrName;
+                    std::map<std::string, AttributeValue>& m_values;
+            };
+
+            // Iterator to allow iteration via STL containers/methods
+            class iterator
+            {
+                friend class OCRepresentation;
+                public:
+                    typedef iterator self_type;
+                    typedef AttributeItem value_type;
+                    typedef value_type& reference;
+                    typedef value_type* pointer;
+                    typedef std::forward_iterator_tag iterator_category;
+                    typedef int difference_type;
+
+                    iterator(const iterator&) = default;
+                    ~iterator() = default;
+
+                    bool operator ==(const iterator&) const;
+                    bool operator !=(const iterator&) const;
+
+                    iterator& operator++();
+                    iterator operator++(int);
+
+                    reference operator*();
+                    pointer operator->();
+                private:
+                    iterator(std::map<std::string, AttributeValue>::iterator&& itr,
+                            std::map<std::string, AttributeValue>& vals)
+                        : m_iterator(std::move(itr)),
+                        m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
+                    std::map<std::string, AttributeValue>::iterator m_iterator;
+                    AttributeItem m_item;
+            };
+
+            class const_iterator
+            {
+                friend class OCRepresentation;
+                public:
+                    typedef iterator self_type;
+                    typedef const AttributeItem value_type;
+                    typedef value_type& const_reference;
+                    typedef value_type* const_pointer;
+                    typedef std::forward_iterator_tag iterator_category;
+                    typedef int difference_type;
+
+                    const_iterator(const iterator& rhs)
+                        :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
+                    const_iterator(const const_iterator&) = default;
+                    ~const_iterator() = default;
+
+                    bool operator ==(const const_iterator&) const;
+                    bool operator !=(const const_iterator&) const;
+
+                    const_iterator& operator++();
+                    const_iterator operator++(int);
+
+                    const_reference operator*() const;
+                    const_pointer operator->() const;
+                private:
+                    const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
+                            std::map<std::string, AttributeValue>& vals)
+                        : m_iterator(std::move(itr)),
+                        m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
+                    std::map<std::string, AttributeValue>::const_iterator m_iterator;
+                    AttributeItem m_item;
+            };
+
+            iterator begin();
+            const_iterator begin() const;
+            const_iterator cbegin() const;
+            iterator end();
+            const_iterator end() const;
+            const_iterator cend() const;
+            size_t size() const;
+            bool empty() const;
+
+            AttributeItem operator[](const std::string& key);
+            const AttributeItem operator[](const std::string& key) const;
         private:
             friend class OCResourceResponse;
-            friend class cereal::access;
-
+            friend class MessageContainer;
+
+            template<typename T>
+            void payload_array_helper(const OCRepPayloadValue* pl, size_t depth);
+            template<typename T>
+            T payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl);
+            void setPayload(const OCRepPayload* payload);
+            void setPayloadArray(const OCRepPayloadValue* pl);
+            void getPayloadArray(OCRepPayload* payload,
+                    const OCRepresentation::AttributeItem& item) const;
             // the root node has a slightly different JSON version
             // based on the interface type configured in ResourceResponse.
             // This allows ResourceResponse to set it, so that the save function
@@ -195,35 +423,22 @@ namespace OC
                     m_interfaces(interfaces)
                     {}*/
                 private:
-                    friend class cereal::access;
-                    template <class Archive>
-                    void save(Archive& ar) const;
-
-                    template<class Archive>
-                    void load(Archive& ar);
-
                     std::vector<std::string>& m_types;
                     std::vector<std::string>& m_interfaces;
             };
-            template<class Archive, class Val>
-            static void optional_load(Archive& ar, Val&& v);
-
-            template<class Archive>
-            void save(Archive& ar) const;
-
-            template<class Archive>
-            void load(Archive& ar);
-
         private:
             std::string m_uri;
             std::vector<OCRepresentation> m_children;
-            std::map<std::string, AttributeValue> m_values;
+            mutable std::map<std::string, AttributeValue> m_values;
             std::vector<std::string> m_resourceTypes;
             std::vector<std::string> m_interfaces;
 
             InterfaceType m_interfaceType;
     };
+
+    std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
 } // namespace OC
 
 
 #endif //__OCREPRESENTATION_H
+