Fix RemoteSVG UTC failed due to the proxy block (2)
[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) 2021 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 namespace Toolkit
29 {
30 namespace Internal
31 {
32 /**
33  * @brief RoundRobinContainerView is a view to a container that allows iterating through the elements cyclically.
34  */
35 template<typename T>
36 class RoundRobinContainerView
37 {
38 public:
39   using ContainerType = std::vector<T>;
40
41   /**
42    * @brief Constructs a new RoundRobinControlView with the given number elements using the provided factory.
43    * @param[in] numberOfElements The number of elements in the container
44    * @param[in] factory          Factory function of functor that will be used to create instances of the elements
45    */
46   template<typename FactoryType>
47   RoundRobinContainerView(size_t numberOfElements, const FactoryType& factory)
48   : mElements(),
49     mNextIndex{}
50   {
51     mElements.reserve(numberOfElements);
52     for(unsigned i = {}; i < numberOfElements; ++i)
53     {
54       mElements.push_back(factory());
55     }
56   }
57
58   /**
59    * @brief Reset the position of the iterator returned by GetNext() to the first element.
60    */
61   void Reset()
62   {
63     mNextIndex = 0u;
64   }
65
66   /**
67    * @brief Returns the next element on the container.
68    * @return Iterator for the next element
69    */
70   typename ContainerType::iterator GetNext()
71   {
72     SetValidNextIndex();
73
74     return mElements.begin() + mNextIndex++;
75   }
76
77   /**
78    * @brief Returns the iterator to the end of the container.
79    *
80    * Can be used to compare against GetNext() to check if the container is empty.
81    *
82    * @return The container end() element
83    */
84   typename ContainerType::const_iterator End() const
85   {
86     return mElements.cend();
87   }
88
89   // default members
90   ~RoundRobinContainerView() = default;
91
92   RoundRobinContainerView(const RoundRobinContainerView&) = delete;
93   RoundRobinContainerView& operator=(const RoundRobinContainerView&) = delete;
94   RoundRobinContainerView(RoundRobinContainerView&&)                 = default;
95   RoundRobinContainerView& operator=(RoundRobinContainerView&&) = default;
96
97 private:
98   /**
99    * @brief Check the current index and reset if necessary.
100    */
101   void SetValidNextIndex()
102   {
103     if(mNextIndex >= mElements.size())
104     {
105       Reset();
106     }
107   }
108
109 private:
110   ContainerType mElements;  //< container of elements
111   size_t        mNextIndex; //< index to the next element to be viewed
112 };
113
114 } // namespace Internal
115
116 } // namespace Toolkit
117
118 } // namespace Dali
119
120 #endif // DALI_TOOLKIT_INTERNAL_ROUND_ROBIN_CONTAINER_VIEW_H