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