Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef ARY_STORE_S_STORAGE_HXX
21 : #define ARY_STORE_S_STORAGE_HXX
22 :
23 : // USED SERVICES
24 : #include <ary/types.hxx>
25 : #include "s_iterator.hxx"
26 :
27 :
28 :
29 :
30 : namespace ary
31 : {
32 : namespace stg
33 : {
34 :
35 :
36 : /** The storage unit of one class of commomly stored repository
37 : entities.
38 : */
39 : template <class ENTITY>
40 : class Storage
41 : {
42 : public:
43 : typedef Base<ENTITY> container_type;
44 : typedef ary::TypedId<ENTITY> key_type;
45 : typedef stg::const_iterator<ENTITY> c_iter;
46 : typedef stg::iterator<ENTITY> iter;
47 :
48 : // LIFECYCLE
49 1 : virtual ~Storage() {}
50 :
51 : // OPERATORS
52 : const ENTITY & operator[](
53 : key_type i_id ) const;
54 : ENTITY & operator[](
55 : key_type i_id );
56 : const ENTITY & operator[](
57 : Rid i_index ) const;
58 : ENTITY & operator[](
59 : Rid i_index );
60 : // OPERATIONS
61 : /// Sets the id of the new entity.
62 : key_type Store_Entity(
63 : DYN ENTITY & pass_newEntity );
64 : /// Sets the id of the new entity.
65 : void Set_Reserved(
66 : uintt i_index,
67 : DYN ENTITY & pass_newEntity );
68 : /// Sets the id of the new entity.
69 : void Replace_Entity(
70 : key_type i_index,
71 : DYN ENTITY & pass_newEntity );
72 : // INQUIRY
73 : bool Exists(
74 : key_type i_id ) const;
75 : bool Exists(
76 : Rid i_index ) const;
77 :
78 : c_iter Begin() const;
79 : c_iter BeginUnreserved() const;
80 : c_iter End() const;
81 :
82 : // ACCESS
83 : iter Begin();
84 : iter BeginUnreserved();
85 : iter End();
86 :
87 : protected:
88 : Storage(
89 : uintt i_nrOfReservedItems );
90 : private:
91 : // DATA
92 : container_type aData;
93 : };
94 :
95 :
96 :
97 :
98 :
99 :
100 : // IMPLEMENTATION
101 :
102 : // Used later, so implemented first.
103 : template <class ENTITY>
104 : inline bool
105 0 : Storage<ENTITY>::Exists(Rid i_index) const
106 : {
107 0 : return 0 < i_index AND i_index < aData.Size();
108 : }
109 :
110 : template <class ENTITY>
111 : inline bool
112 0 : Storage<ENTITY>::Exists(key_type i_id) const
113 : {
114 0 : return Exists(i_id.Value());
115 : }
116 :
117 : template <class ENTITY>
118 : inline const ENTITY &
119 975646 : Storage<ENTITY>::operator[](Rid i_index) const
120 : {
121 : csv_assert(Exists(i_index));
122 975646 : return * aData[i_index];
123 : }
124 :
125 : template <class ENTITY>
126 : inline ENTITY &
127 710145 : Storage<ENTITY>::operator[](Rid i_index)
128 : {
129 : csv_assert(Exists(i_index));
130 710145 : return * aData[i_index];
131 : }
132 :
133 : template <class ENTITY>
134 : inline const ENTITY &
135 975332 : Storage<ENTITY>::operator[](key_type i_id) const
136 : {
137 975332 : return operator[](i_id.Value());
138 : }
139 :
140 : template <class ENTITY>
141 : inline ENTITY &
142 706674 : Storage<ENTITY>::operator[](key_type i_id)
143 : {
144 706674 : return operator[](i_id.Value());
145 : }
146 :
147 : template <class ENTITY>
148 : typename Storage<ENTITY>::key_type
149 24604 : Storage<ENTITY>::Store_Entity(DYN ENTITY & pass_newEntity)
150 : {
151 : csv_assert( aData.Size() >= aData.ReservedSize() );
152 : Rid
153 24604 : ret( aData.Add_Entity(pass_newEntity) );
154 24604 : pass_newEntity.Set_Id(ret);
155 24604 : return key_type(ret);
156 : }
157 :
158 : template <class ENTITY>
159 : void
160 18 : Storage<ENTITY>::Set_Reserved(uintt i_index,
161 : DYN ENTITY & pass_newEntity)
162 : {
163 : // 0 must not be used.
164 : csv_assert( i_index != 0 );
165 : // Make sure, i_index actually is the id of a reserved item.
166 : csv_assert( i_index < aData.ReservedSize() );
167 :
168 : // If there was a previous entity, it will be deleted by
169 : // the destructor of pOldEntity.
170 : Dyn<ENTITY>
171 18 : pOldEntity(aData.Set_Entity(i_index, pass_newEntity));
172 18 : pass_newEntity.Set_Id(i_index);
173 18 : }
174 :
175 : template <class ENTITY>
176 : void
177 3696 : Storage<ENTITY>::Replace_Entity( key_type i_index,
178 : DYN ENTITY & pass_newEntity )
179 : {
180 : uintt
181 3696 : nIndex = i_index.Value();
182 : // Make sure, i_index actually is the id of an existing,
183 : // non reserved entity.
184 : csv_assert( csv::in_range(aData.ReservedSize(), nIndex, aData.Size()) );
185 :
186 : // If there was a previous entity, it will be deleted by
187 : // the destructor of pOldEntity.
188 : Dyn<ENTITY>
189 3696 : pOldEntity(aData.Set_Entity(nIndex, pass_newEntity));
190 3696 : pass_newEntity.Set_Id(nIndex);
191 3696 : }
192 :
193 : template <class ENTITY>
194 : inline
195 : typename Storage<ENTITY>::c_iter
196 1 : Storage<ENTITY>::Begin() const
197 : {
198 1 : return c_iter(aData.Begin());
199 : }
200 :
201 : template <class ENTITY>
202 : inline
203 : typename Storage<ENTITY>::c_iter
204 27 : Storage<ENTITY>::BeginUnreserved() const
205 : {
206 27 : return c_iter(aData.BeginUnreserved());
207 : }
208 :
209 : template <class ENTITY>
210 : inline
211 : typename Storage<ENTITY>::c_iter
212 28 : Storage<ENTITY>::End() const
213 : {
214 28 : return c_iter(aData.End());
215 : }
216 :
217 : template <class ENTITY>
218 : inline
219 : typename Storage<ENTITY>::iter
220 2 : Storage<ENTITY>::Begin()
221 : {
222 2 : return iter(aData.Begin());
223 : }
224 :
225 : template <class ENTITY>
226 : inline
227 : typename Storage<ENTITY>::iter
228 2 : Storage<ENTITY>::BeginUnreserved()
229 : {
230 2 : return iter(aData.BeginUnreserved());
231 : }
232 :
233 : template <class ENTITY>
234 : inline
235 : typename Storage<ENTITY>::iter
236 39058 : Storage<ENTITY>::End()
237 : {
238 39058 : return iter(aData.End());
239 : }
240 :
241 : template <class ENTITY>
242 : inline
243 2 : Storage<ENTITY>::Storage(uintt i_nrOfReservedItems)
244 2 : : aData(i_nrOfReservedItems)
245 : {
246 : // Make sure Rid and uintt are the same type, because
247 : // the interface of this uses Rid, but the interface of
248 : // container_type uses uintt.
249 : csv_assert( sizeof(uintt) == sizeof(Rid) );
250 2 : }
251 :
252 :
253 :
254 :
255 : // HELPER FUNCTIONS
256 :
257 : /** @return 0, if data are not there.
258 : */
259 : template <class ENTITY>
260 : inline const ENTITY *
261 : Search( const Storage<ENTITY> & i_storage,
262 : Rid i_id )
263 : {
264 : if (NOT i_storage.Exists(i_id))
265 : return 0;
266 : return &i_storage[i_id];
267 : }
268 :
269 : /** @return 0, if data are not there.
270 : */
271 : template <class ENTITY>
272 : inline ENTITY *
273 : SearchAccess( const Storage<ENTITY> & i_storage,
274 : Rid i_id )
275 : {
276 : if (NOT i_storage.Exists(i_id))
277 : return 0;
278 : return &i_storage[i_id];
279 : }
280 :
281 :
282 :
283 :
284 : } // namespace stg
285 : } // namespace ary
286 : #endif
287 :
288 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|