1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  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,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef ARY_STORE_S_BASE_HXX
25 #define ARY_STORE_S_BASE_HXX
26 
27 // USED SERVICES
28 #include <deque>
29 #include <cosv/tpl/tpltools.hxx>
30 
31 
32 
33 
34 namespace ary
35 {
36 namespace stg
37 {
38 
39 
40 /** The basic storage container of the repository.
41 
42     @collab Storage
43     Implements Storage. Not used elsewhere.
44 
45     @tpl ENTITY
46     The type of *it, where it is of type c_iter, has to be ENTITY * const.
47 */
48 template <class ENTITY>
49 class Base
50 {
51   public:
52     // LIFECYCLE
53     typedef std::deque< ENTITY* >               impl_type;
54     typedef typename impl_type::const_iterator  c_iter;
55 
56 
57     /** @param i_nrOfReservedItems
58         The number of actual items to reserve, including the item
59         at index [0] that is always empty and unused.
60     */
61                         Base(
62                             uintt               i_nrOfReservedItems );
63                         ~Base();
64 
65     // OPERATORS
66     ENTITY *            operator[](
67                             uintt               i_index ) const;
68     // OPERATIONS
69     uintt               Add_Entity(             /// @return the index of the new element.
70                             DYN ENTITY &        pass_newEntity );
71     DYN ENTITY *        Set_Entity(             /// @return the previous value.
72                             uintt               i_index,
73                             DYN ENTITY &        pass_newEntity );
74     // INQUIRY
75     uintt               Size() const;           /// Incl. reserved size.
76     uintt               ReservedSize() const;   /// Incl. zero for element at [0].
77 
78     c_iter              Begin() const;          /// @return location of index 1, because 0 is always empty.
79     c_iter              BeginUnreserved() const;
80     c_iter              End() const;
81 
82   private:
83     // DATA
84     impl_type               aData;
85     uintt                   nReservedSize;
86 };
87 
88 
89 
90 // IMPLEMENTATION
91 
92 template <class ENTITY>
Base(uintt i_nrOfReservedItems)93 Base<ENTITY>::Base(uintt i_nrOfReservedItems)
94     :   aData(i_nrOfReservedItems, 0),
95         nReservedSize(i_nrOfReservedItems)
96 {
97 }
98 
99 template <class ENTITY>
~Base()100 Base<ENTITY>::~Base()
101 {
102     csv::erase_container_of_heap_ptrs(aData);
103 }
104 
105 
106 template <class ENTITY>
107 ENTITY *
operator [](uintt i_index) const108 Base<ENTITY>::operator[](uintt i_index) const
109 {
110     if (i_index < aData.size())
111         return aData[i_index];
112     return 0;
113 }
114 
115 template <class ENTITY>
116 uintt
Add_Entity(DYN ENTITY & pass_newEntity)117 Base<ENTITY>::Add_Entity(DYN ENTITY & pass_newEntity)
118 {
119     aData.push_back(&pass_newEntity);
120     return aData.size() - 1;
121 }
122 
123 template <class ENTITY>
124 DYN ENTITY *
Set_Entity(uintt i_index,DYN ENTITY & pass_newEntity)125 Base<ENTITY>::Set_Entity( uintt         i_index,
126                           DYN ENTITY &  pass_newEntity )
127 {
128     csv_assert(i_index != 0 AND i_index < aData.size());
129 
130     Dyn<ENTITY>
131         ret(aData[i_index]);
132     aData[i_index] = &pass_newEntity;
133     return ret.Release();
134 }
135 
136 template <class ENTITY>
137 uintt
Size() const138 Base<ENTITY>::Size() const
139 {
140     return aData.size();
141 }
142 
143 template <class ENTITY>
144 uintt
ReservedSize() const145 Base<ENTITY>::ReservedSize() const
146 {
147     return nReservedSize;
148 }
149 
150 template <class ENTITY>
151 typename Base<ENTITY>::c_iter
Begin() const152 Base<ENTITY>::Begin() const
153 {
154     return aData.begin() + 1;
155 }
156 
157 template <class ENTITY>
158 typename Base<ENTITY>::c_iter
BeginUnreserved() const159 Base<ENTITY>::BeginUnreserved() const
160 {
161     return aData.begin() + nReservedSize;
162 }
163 
164 template <class ENTITY>
165 typename Base<ENTITY>::c_iter
End() const166 Base<ENTITY>::End() const
167 {
168     return aData.end();
169 }
170 
171 
172 
173 
174 }   // namespace stg
175 }   // namespace ary
176 #endif
177