1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #ifndef ARY_STORE_S_STORAGE_HXX 29 #define ARY_STORE_S_STORAGE_HXX 30 31 // USED SERVICES 32 #include <ary/types.hxx> 33 #include "s_iterator.hxx" 34 35 36 37 38 namespace ary 39 { 40 namespace stg 41 { 42 43 44 /** The storage unit of one class of commomly stored repository 45 entities. 46 */ 47 template <class ENTITY> 48 class Storage 49 { 50 public: 51 typedef Base<ENTITY> container_type; 52 typedef ary::TypedId<ENTITY> key_type; 53 typedef stg::const_iterator<ENTITY> c_iter; 54 typedef stg::iterator<ENTITY> iter; 55 56 // LIFECYCLE 57 virtual ~Storage() {} 58 59 // OPERATORS 60 const ENTITY & operator[]( 61 key_type i_id ) const; 62 ENTITY & operator[]( 63 key_type i_id ); 64 const ENTITY & operator[]( 65 Rid i_index ) const; 66 ENTITY & operator[]( 67 Rid i_index ); 68 // OPERATIONS 69 /// Sets the id of the new entity. 70 key_type Store_Entity( 71 DYN ENTITY & pass_newEntity ); 72 /// Sets the id of the new entity. 73 void Set_Reserved( 74 uintt i_index, 75 DYN ENTITY & pass_newEntity ); 76 /// Sets the id of the new entity. 77 void Replace_Entity( 78 key_type i_index, 79 DYN ENTITY & pass_newEntity ); 80 // INQUIRY 81 bool Exists( 82 key_type i_id ) const; 83 bool Exists( 84 Rid i_index ) const; 85 86 c_iter Begin() const; 87 c_iter BeginUnreserved() const; 88 c_iter End() const; 89 90 // ACCESS 91 iter Begin(); 92 iter BeginUnreserved(); 93 iter End(); 94 95 protected: 96 Storage( 97 uintt i_nrOfReservedItems ); 98 private: 99 // DATA 100 container_type aData; 101 }; 102 103 104 105 106 107 108 // IMPLEMENTATION 109 110 // Used later, so implemented first. 111 template <class ENTITY> 112 inline bool 113 Storage<ENTITY>::Exists(Rid i_index) const 114 { 115 return 0 < i_index AND i_index < aData.Size(); 116 } 117 118 template <class ENTITY> 119 inline bool 120 Storage<ENTITY>::Exists(key_type i_id) const 121 { 122 return Exists(i_id.Value()); 123 } 124 125 template <class ENTITY> 126 inline const ENTITY & 127 Storage<ENTITY>::operator[](Rid i_index) const 128 { 129 csv_assert(Exists(i_index)); 130 return * aData[i_index]; 131 } 132 133 template <class ENTITY> 134 inline ENTITY & 135 Storage<ENTITY>::operator[](Rid i_index) 136 { 137 csv_assert(Exists(i_index)); 138 return * aData[i_index]; 139 } 140 141 template <class ENTITY> 142 inline const ENTITY & 143 Storage<ENTITY>::operator[](key_type i_id) const 144 { 145 return operator[](i_id.Value()); 146 } 147 148 template <class ENTITY> 149 inline ENTITY & 150 Storage<ENTITY>::operator[](key_type i_id) 151 { 152 return operator[](i_id.Value()); 153 } 154 155 template <class ENTITY> 156 typename Storage<ENTITY>::key_type 157 Storage<ENTITY>::Store_Entity(DYN ENTITY & pass_newEntity) 158 { 159 csv_assert( aData.Size() >= aData.ReservedSize() ); 160 Rid 161 ret( aData.Add_Entity(pass_newEntity) ); 162 pass_newEntity.Set_Id(ret); 163 return key_type(ret); 164 } 165 166 template <class ENTITY> 167 void 168 Storage<ENTITY>::Set_Reserved(uintt i_index, 169 DYN ENTITY & pass_newEntity) 170 { 171 // 0 must not be used. 172 csv_assert( i_index != 0 ); 173 // Make sure, i_index actually is the id of a reserved item. 174 csv_assert( i_index < aData.ReservedSize() ); 175 176 // If there was a previous entity, it will be deleted by 177 // the destructor of pOldEntity. 178 Dyn<ENTITY> 179 pOldEntity(aData.Set_Entity(i_index, pass_newEntity)); 180 pass_newEntity.Set_Id(i_index); 181 } 182 183 template <class ENTITY> 184 void 185 Storage<ENTITY>::Replace_Entity( key_type i_index, 186 DYN ENTITY & pass_newEntity ) 187 { 188 uintt 189 nIndex = i_index.Value(); 190 // Make sure, i_index actually is the id of an existing, 191 // non reserved entity. 192 csv_assert( csv::in_range(aData.ReservedSize(), nIndex, aData.Size()) ); 193 194 // If there was a previous entity, it will be deleted by 195 // the destructor of pOldEntity. 196 Dyn<ENTITY> 197 pOldEntity(aData.Set_Entity(nIndex, pass_newEntity)); 198 pass_newEntity.Set_Id(nIndex); 199 } 200 201 template <class ENTITY> 202 inline 203 typename Storage<ENTITY>::c_iter 204 Storage<ENTITY>::Begin() const 205 { 206 return c_iter(aData.Begin()); 207 } 208 209 template <class ENTITY> 210 inline 211 typename Storage<ENTITY>::c_iter 212 Storage<ENTITY>::BeginUnreserved() const 213 { 214 return c_iter(aData.BeginUnreserved()); 215 } 216 217 template <class ENTITY> 218 inline 219 typename Storage<ENTITY>::c_iter 220 Storage<ENTITY>::End() const 221 { 222 return c_iter(aData.End()); 223 } 224 225 template <class ENTITY> 226 inline 227 typename Storage<ENTITY>::iter 228 Storage<ENTITY>::Begin() 229 { 230 return iter(aData.Begin()); 231 } 232 233 template <class ENTITY> 234 inline 235 typename Storage<ENTITY>::iter 236 Storage<ENTITY>::BeginUnreserved() 237 { 238 return iter(aData.BeginUnreserved()); 239 } 240 241 template <class ENTITY> 242 inline 243 typename Storage<ENTITY>::iter 244 Storage<ENTITY>::End() 245 { 246 return iter(aData.End()); 247 } 248 249 template <class ENTITY> 250 inline 251 Storage<ENTITY>::Storage(uintt i_nrOfReservedItems) 252 : aData(i_nrOfReservedItems) 253 { 254 // Make sure Rid and uintt are the same type, because 255 // the interface of this uses Rid, but the interface of 256 // container_type uses uintt. 257 csv_assert( sizeof(uintt) == sizeof(Rid) ); 258 } 259 260 261 262 263 // HELPER FUNCTIONS 264 265 /** @return 0, if data are not there. 266 */ 267 template <class ENTITY> 268 inline const ENTITY * 269 Search( const Storage<ENTITY> & i_storage, 270 Rid i_id ) 271 { 272 if (NOT i_storage.Exists(i_id)) 273 return 0; 274 return &i_storage[i_id]; 275 } 276 277 /** @return 0, if data are not there. 278 */ 279 template <class ENTITY> 280 inline ENTITY * 281 SearchAccess( const Storage<ENTITY> & i_storage, 282 Rid i_id ) 283 { 284 if (NOT i_storage.Exists(i_id)) 285 return 0; 286 return &i_storage[i_id]; 287 } 288 289 290 291 292 } // namespace stg 293 } // namespace ary 294 #endif 295