Updates for const->constexpr
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / helpers / round-robin-container-view.h
1
2 #ifndef DALI_TOOLKIT_INTERNAL_ROUND_ROBIN_CONTAINER_VIEW_H
3 #define DALI_TOOLKIT_INTERNAL_ROUND_ROBIN_CONTAINER_VIEW_H
4
5 /*
6  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 // EXTERNAL INCLUDES
23 #include <cstddef>
24 #include <vector>
25
26 namespace Dali
27 {
28
29 namespace Toolkit
30 {
31
32 namespace Internal
33 {
34
35 /**
36  * @brief RoundRobinContainerView is a view to a container that allows iterating through the elements cyclically.
37  */
38 template<typename T>
39 class RoundRobinContainerView
40 {
41 public:
42   using ContainerType = std::vector<T>;
43
44   /**
45    * @brief Constructs a new RoundRobinControlView with the given number elements using the provided factory.
46    * @param[in] numberOfElements The number of elements in the container
47    * @param[in] factory          Factory function of functor that will be used to create instances of the elements
48    */
49   template<typename FactoryType>
50   RoundRobinContainerView(size_t numberOfElements, const FactoryType& factory)
51   : mElements(),
52     mNextIndex{}
53   {
54     mElements.reserve(numberOfElements);
55     for(unsigned i = {}; i < numberOfElements; ++i)
56     {
57       mElements.push_back(factory());
58     }
59   }
60
61   /**
62    * @brief Reset the position of the iterator returned by GetNext() to the first element.
63    */
64   void Reset()
65   {
66     mNextIndex = 0u;
67   }
68
69   /**
70    * @brief Returns the next element on the container.
71    * @return Iterator for the next element
72    */
73   typename ContainerType::iterator GetNext()
74   {
75     SetValidNextIndex();
76
77     return mElements.begin() + mNextIndex++;
78   }
79
80   /**
81    * @brief Returns the iterator to the end of the container.
82    *
83    * Can be used to compare against GetNext() to check if the container is empty.
84    *
85    * @return The container end() element
86    */
87   typename ContainerType::const_iterator End() const
88   {
89     return mElements.cend();
90   }
91
92   // default members
93   ~RoundRobinContainerView() = default;
94
95   RoundRobinContainerView(const RoundRobinContainerView&)  = delete;
96   RoundRobinContainerView& operator=(const RoundRobinContainerView&)  = delete;
97   RoundRobinContainerView(RoundRobinContainerView&&) = default;
98   RoundRobinContainerView& operator=(RoundRobinContainerView&&) = default;
99
100 private:
101   /**
102    * @brief Check the current index and reset if necessary.
103    */
104   void SetValidNextIndex()
105   {
106     if(mNextIndex >= mElements.size())
107     {
108       Reset();
109     }
110   }
111
112 private:
113   ContainerType mElements; //< container of elements
114   size_t mNextIndex;       //< index to the next element to be viewed
115 };
116
117 } // namespace Internal
118
119 } // namespace Toolkit
120
121 } // namespace Dali
122
123
124 #endif // DALI_TOOLKIT_INTERNAL_ROUND_ROBIN_CONTAINER_VIEW_H