replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / cpp-wrapper / common / NSTopicsList.cpp
1 //******************************************************************
2 //
3 // Copyright 2016 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 "NSTopicsList.h"
22 #include "NSException.h"
23 #include "oic_malloc.h"
24
25 namespace OIC
26 {
27     namespace Service
28     {
29         NSTopicsList::NSTopicsList(::NSTopicLL *topics, bool modify)
30         {
31             ::NSTopicLL *topicsNode = nullptr;
32             topicsNode = topics;
33             m_modifiable = modify;
34
35             while (topicsNode != nullptr)
36             {
37                 m_topicsList.push_back(new NSTopic(
38                     topicsNode->topicName, (NSTopic::NSTopicState)topicsNode->state));
39                 topicsNode = topicsNode->next;
40             }
41
42         }
43         NSTopicsList::NSTopicsList(const NSTopicsList &topicsList)
44         {
45             for (auto it : topicsList.getTopicsList())
46             {
47                 m_topicsList.push_back(new NSTopic(it.getTopicName(), it.getState()));
48             }
49             m_modifiable = false;
50         }
51
52         NSTopicsList &NSTopicsList::operator=(const NSTopicsList &topicsList)
53         {
54             for (auto it : topicsList.getTopicsList())
55             {
56                 this->m_topicsList.push_back(new NSTopic(it.getTopicName(), it.getState()));
57             }
58             return *this;
59             m_modifiable = false;
60         }
61
62         NSTopicsList::~NSTopicsList()
63         {
64             for (auto it : m_topicsList)
65             {
66                 delete it;
67             }
68             m_topicsList.clear();
69         }
70
71         void NSTopicsList::addTopic(const std::string &topicName, NSTopic::NSTopicState state)
72         {
73             if(m_modifiable)
74             {
75                 m_topicsList.push_back(new NSTopic(topicName, state));
76             }
77             else
78             {
79                 throw NSException("Invalid Operation. Method not supported as the object state is invalid");
80             }
81         }
82
83         void NSTopicsList::removeTopic(const std::string &topicName)
84         {
85             if(m_modifiable)
86             {
87                 for (auto it : m_topicsList)
88                 {
89                     if (it->getTopicName().compare(topicName) == 0)
90                     {
91                         m_topicsList.remove(it);
92                         break;
93                     }
94                 }
95             }
96             else
97             {
98                 throw NSException("Invalid Operation. Method not supported as the object state is invalid");
99             }
100         }
101
102         std::list<NSTopic> NSTopicsList::getTopicsList() const
103         {
104             std::list<NSTopic> topicList;
105             for (auto it : m_topicsList)
106             {
107                 NSTopic topic(it->getTopicName(), it->getState());
108                 topicList.push_back(topic);
109             }
110             return topicList;
111         }
112        
113         //Below method restricts the application from illegally modifying Topics when
114         //Provider is in Invalid state. By calling the API, the service prevents and protects
115         //the integrity of TopicsList updation when the associated object is Invalid
116         //The default value of the variable is 'false' in the provider side. Also, the state is irreversible.
117         void NSTopicsList::unsetModifiability()
118         {
119             m_modifiable = false;
120         }
121     }
122 }