implement observable collections. 55/110055/3
authorLukasz Stanislawski <l.stanislaws@samsung.com>
Thu, 12 Jan 2017 15:05:13 +0000 (16:05 +0100)
committerLukasz Stanislawski <l.stanislaws@samsung.com>
Wed, 18 Jan 2017 12:43:10 +0000 (04:43 -0800)
Add utility classes that will be used for further code refactoring.
The purpose of observable collection is to create generic model that
can read and observed by presenters.

Change-Id: Ie84585d1ed911038948db62a9fd8dc5a3fa24db3

clock/inc/Model/AlarmList.h [new file with mode: 0644]
clock/inc/Utils/ObservableList.h [new file with mode: 0644]

diff --git a/clock/inc/Model/AlarmList.h b/clock/inc/Model/AlarmList.h
new file mode 100644 (file)
index 0000000..6781f80
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+* Copyright 2016  Samsung Electronics Co., Ltd
+*
+* Licensed under the Flora License, Version 1.1 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://floralicense.org/license/
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+
+#ifndef _UTILS_ALARM_LIST_H_
+#define _UTILS_ALARM_LIST_H_
+
+#include <list>
+
+#include "Utils/ObservableList.h"
+#include "Utils/Serialization.h"
+#include "Model/Alarm.h"
+#include "Utils/Log.h"
+
+namespace model {
+       /**
+        * @brief AlarmList is an observable list of alarms with limited size.
+        */
+       class AlarmList : public utils::ObservableList<model::Alarm>, utils::ISerializable
+       {
+               public:
+                       /**
+                        * @brief AlarmList iterator
+                        */
+                       typedef utils::ObservableList<model::Alarm>::Iterator Iterator;
+
+                       /**
+                        * @brief AlarmList constructor
+                        *
+                        * @param[in] max maximum size of the list
+                        */
+                       AlarmList(unsigned int max) : max_size_(max) {}
+
+                       /**
+                        * @brief Deserializing constructor
+                        */
+                       AlarmList(unsigned int max, utils::IReader &reader) : max_size_(max)
+                       {
+                               utils::Deserialize(reader, list_);
+                       }
+
+                       /**
+                        * @brief Add element to the end of the list
+                        *
+                        * @return Iterator to last element
+                        */
+                       Iterator Add(model::Alarm &alarm)
+                       {
+                               if (Size() >= max_size_)
+                                       return End();
+                               return utils::ObservableList<Alarm>::Add(alarm);
+                       }
+
+                       /**
+                        * @brief Gets maximum size of the list
+                        */
+                       size_t MaxSize() const
+                       {
+                               return max_size_;
+                       }
+
+                       void Serialize(utils::IWriter &w) const
+                       {
+                               utils::Serialize(w, list_);
+                       }
+               private:
+                       unsigned int max_size_;
+       };
+}
+
+#endif
diff --git a/clock/inc/Utils/ObservableList.h b/clock/inc/Utils/ObservableList.h
new file mode 100644 (file)
index 0000000..bc0ce0e
--- /dev/null
@@ -0,0 +1,131 @@
+/*
+* Copyright 2016  Samsung Electronics Co., Ltd
+*
+* Licensed under the Flora License, Version 1.1 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://floralicense.org/license/
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+
+#ifndef _UTILS_OBSERVABLE_LIST_H_
+#define _UTILS_OBSERVABLE_LIST_H_
+
+#include <list>
+
+#include "Utils/Signal.h"
+
+namespace utils {
+       /**
+        * @brief Class implements observable list
+        */
+       template<typename T>
+       class ObservableList : protected std::list<T>
+       {
+               public:
+                       /**
+                        * @brief ObservableList iterator
+                        */
+                       typedef typename std::list<T>::iterator Iterator;
+
+                       /**
+                        * @brief Constructs observable list
+                        */
+                       ObservableList() {}
+
+                       /**
+                        * @brief Constructs observable list from std list.
+                        *
+                        * @param[in] list std list.
+                        */
+                       ObservableList(std::list<T> &list) : std::list<T>(list) {}
+
+                       /**
+                        * @brief Constructs observable list with predefined size.
+                        *
+                        * @param[in] size size of the list
+                        */
+                       ObservableList(int size) : std::list<T>(size) {}
+
+                       /**
+                        * @brief Add element to the end of the list
+                        *
+                        * @param[in] obj reference to object.
+                        */
+                       inline Iterator Add(T &obj)
+                       {
+                               push_back(obj);
+                               auto it = --end();
+                               OnAdded(it);
+                               return it;
+                       }
+
+                       /**
+                        * @brief Remove element on specified position in the list.
+                        *
+                        * @param[in] it iterator to element.
+                        */
+                       void Remove(Iterator it)
+                       {
+                               OnRemoved(it);
+                               erase(it);
+                       }
+
+                       /**
+                        * @brief Replaces element on specified position in the list.
+                        *
+                        * @param[in] position iterator to element.
+                        * @param[in] obj iterator to object.
+                        */
+                       void Replace(const Iterator position, T &obj)
+                       {
+                               *position = obj;
+                               OnReplaced(position);
+                       }
+
+                       /**
+                        * @brief Returns interator to first element of the list.
+                        */
+                       inline Iterator Begin() { return begin(); }
+
+                       /**
+                        * @brief Returns interator to first element of the list.
+                        */
+                       inline Iterator End() { return end(); }
+
+                       /**
+                        * @brief Signal emitted when new alarm has been added.
+                        */
+                       utils::Signal<void(Iterator)> OnAdded;
+
+                       /**
+                        * @brief Signal emitted when new alarm has been removed.
+                        */
+                       utils::Signal<void(Iterator)> OnRemoved;
+
+                       /**
+                        * @brief Signal emitted when new alarm has been replaced with other
+                        * one.
+                        */
+                       utils::Signal<void(Iterator)> OnReplaced;
+
+                       /**
+                        * @brief Returns size of the list.
+                        *
+                        * @return size of the list.
+                        */
+                       size_t Size() const
+                       {
+                               return size();
+                       }
+       };
+};
+
+#endif