[IOT-1808]Changes in C++ APIs to remove TopicList raw pointers.
[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 "oic_malloc.h"
23
24 namespace OIC
25 {
26     namespace Service
27     {
28         NSTopicsList::NSTopicsList(::NSTopicLL *topics, bool modify)
29         {
30             ::NSTopicLL *topicsNode = nullptr;
31             topicsNode = topics;
32             m_modifiable = modify;
33
34             while (topicsNode != nullptr)
35             {
36                 m_topicsList.push_back(new NSTopic(
37                     topicsNode->topicName, (NSTopic::NSTopicState)topicsNode->state));
38                 topicsNode = topicsNode->next;
39             }
40
41         }
42         NSTopicsList::NSTopicsList(const NSTopicsList &topicsList)
43         {
44             for (auto it : topicsList.getTopicsList())
45             {
46                 m_topicsList.push_back(new NSTopic(it.getTopicName(), it.getState()));
47             }
48             m_modifiable = false;
49         }
50
51         NSTopicsList &NSTopicsList::operator=(const NSTopicsList &topicsList)
52         {
53             for (auto it : topicsList.getTopicsList())
54             {
55                 this->m_topicsList.push_back(new NSTopic(it.getTopicName(), it.getState()));
56             }
57             return *this;
58             m_modifiable = false;
59         }
60
61         NSTopicsList::~NSTopicsList()
62         {
63             for (auto it : m_topicsList)
64             {
65                 delete it;
66             }
67             m_topicsList.clear();
68         }
69
70         void NSTopicsList::addTopic(const std::string &topicName, NSTopic::NSTopicState state)
71         {
72             if(m_modifiable)
73             {
74                 m_topicsList.push_back(new NSTopic(topicName, state));
75             }
76             else
77             {
78                 //TODO: add exception code for Invalid operation
79             }
80         }
81
82         void NSTopicsList::removeTopic(const std::string &topicName)
83         {
84             if(m_modifiable)
85             {
86                 for (auto it : m_topicsList)
87                 {
88                     if (it->getTopicName().compare(topicName) == 0)
89                     {
90                         m_topicsList.remove(it);
91                     }
92                 }
93             }
94             else
95             {
96                 //TODO: add exception code for Invalid operation
97             }
98         }
99
100         std::list<NSTopic> NSTopicsList::getTopicsList() const
101         {
102             std::list<NSTopic> topicList;
103             for (auto it : m_topicsList)
104             {
105                 NSTopic topic(it->getTopicName(), it->getState());
106                 topicList.push_back(topic);
107             }
108             return topicList;
109         }
110
111         void NSTopicsList::unsetModifiability()
112         {
113             m_modifiable = false;
114         }
115     }
116 }