New APIs : hasAttribute, numberOfAttributes, erase in OCRepresentation.
authorSashi Penta <sashi.kumar.penta@intel.com>
Fri, 26 Sep 2014 22:04:08 +0000 (15:04 -0700)
committerSashi Penta <sashi.kumar.penta@intel.com>
Fri, 26 Sep 2014 22:07:03 +0000 (15:07 -0700)
Change-Id: Ia6df616bdd73d7e73a34563896dd3c5d941b9a8b

examples/garageclient.cpp
include/OCApi.h

index 08b98f0..813c06b 100644 (file)
@@ -51,8 +51,35 @@ Garage myGarage;
 
 void printRepresentation(const OCRepresentation& rep)
 {
-        rep.getValue("state", myGarage.m_state);
-        rep.getValue("name", myGarage.m_name);
+
+        // Check if attribute "name" exists, and then getValue
+        if(rep.hasAttribute("name"))
+        {
+            myGarage.m_name = rep.getValue<std::string>("name");
+        }
+
+        // You can directly try to get the value. this function
+        // return false if there is no attribute "state"
+        if(!rep.getValue("state", myGarage.m_state))
+        {
+            std::cout << "Attribute state doesn't exist in the representation\n";
+        }
+
+
+        OCRepresentation rep2 = rep;
+
+        std::cout << "Number of attributes in rep2: "
+                  << rep2.numberOfAttributes() << std::endl;
+
+        if(rep2.erase("name"))
+        {
+            std::cout << "attribute: name, was removed successfully from rep2.\n";
+        }
+
+        std::cout << "Number of attributes in rep2: "
+                  << rep2.numberOfAttributes() << std::endl;
+
+
         rep.getValue("light", myGarage.m_lightRep);
 
         myGarage.m_lightRep.getValue("states", myGarage.m_lightStates);
index 34a57af..a34d064 100644 (file)
@@ -141,6 +141,11 @@ namespace OC
         public:
         OCRepresentation() {}
 
+        bool erase(const std::string& str)
+        {
+            return m_attributeMap.erase(str) != 0;
+        }
+
         std::string getUri(void) const
         {
             return m_uri;
@@ -152,6 +157,19 @@ namespace OC
         template <typename T>
         bool getValue(const std::string& str, T& val) const;
 
+        template <typename T>
+        T getValue(const std::string& str) const;
+
+        bool hasAttribute(const std::string& str) const
+        {
+            return m_attributeMap.find(str) != m_attributeMap.end();
+        }
+
+        int numberOfAttributes() const
+        {
+            return m_attributeMap.size();
+        }
+
         void setUri(std::string uri)
         {
             m_uri = uri;
@@ -525,6 +543,22 @@ namespace OC
         m_attributeMap[str] = getJSON(val);
     }
 
+    template <typename T>
+    T OCRepresentation::getValue(const std::string& str) const
+    {
+        T val;
+
+        auto x = m_attributeMap.find(str);
+
+        if(m_attributeMap.end() != x)
+        {
+            AttributeValue v = val;
+            parseJSON(v, x->second);
+            val = boost::get<T>(v);
+        }
+
+        return val;
+    }
 
     template <typename T>
     bool OCRepresentation::getValue(const std::string& str, T& val) const