93c255febc993da747769d069ad0312b859a7aea
[platform/upstream/iotivity.git] / service / resource-container / src / BundleResource.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "BundleResource.h"
22
23 #include <list>
24 #include <string.h>
25
26 #include "InternalTypes.h"
27
28 namespace OIC
29 {
30     namespace Service
31     {
32         BundleResource::BundleResource()
33         {
34             m_pNotiReceiver = nullptr;
35         }
36
37         BundleResource::~BundleResource()
38         {
39             m_pNotiReceiver = nullptr;
40         }
41
42         void BundleResource::registerObserver(NotificationReceiver *pNotiReceiver)
43         {
44             m_pNotiReceiver = pNotiReceiver;
45         }
46
47         std::list< std::string > BundleResource::getAttributeNames()
48         {
49             std::list< std::string > ret;
50             for (RCSResourceAttributes::iterator it = m_resourceAttributes.begin();
51                  it != m_resourceAttributes.end(); ++it)
52             {
53                 ret.push_back(it->key());
54             }
55             return ret;
56         }
57
58         RCSResourceAttributes &BundleResource::getAttributes()
59         {
60             return m_resourceAttributes;
61         }
62
63         void BundleResource::setAttributes(RCSResourceAttributes &attrs)
64         {
65             for (RCSResourceAttributes::iterator it = attrs.begin(); it != attrs.end(); ++it)
66             {
67                 OC_LOG_V(INFO, CONTAINER_TAG, "set attribute \(%s)'",
68                          std::string(it->key() + "\', with " + it->value().toString()).c_str());
69
70                 m_resourceAttributes[it->key()] = it->value();
71             }
72         }
73
74         void BundleResource::setAttribute(const std::string &key,
75                                           RCSResourceAttributes::Value &&value, bool notify)
76         {
77             OC_LOG_V(INFO, CONTAINER_TAG, "set attribute \(%s)'", std::string(key + "\', with " +
78                      value.toString()).c_str());
79
80             m_resourceAttributes[key] = value;
81
82             if (notify && m_pNotiReceiver)
83                 m_pNotiReceiver->onNotificationReceived(m_uri);
84         }
85
86         void BundleResource::setAttribute(const std::string &key, RCSResourceAttributes::Value &&value)
87         {
88             setAttribute(key, std::move(value), true);
89         }
90
91         RCSResourceAttributes::Value BundleResource::getAttribute(const std::string &key)
92         {
93             OC_LOG_V(INFO, CONTAINER_TAG, "get attribute \'(%s)" , std::string(key + "\'").c_str());
94
95             return m_resourceAttributes.at(key);
96         }
97     }
98 }