1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef RYAI_STATIC_VECTOR_H
21 #define RYAI_STATIC_VECTOR_H
26 This class implements an array of entities of any derived class of a given base class
27 The entities are allocated in a single memory block.
31 // not the way to use templates .. ( bad generalisation implementation ).
34 template <class BaseClass>
38 // ctor & dtor --------------------------------------------------
39 StaticVector(): _data(0), _count(0), _size(0) {}
40 ~StaticVector() { if (_data) delete[] _data; }
42 // allocate memory and initialise objects -----------------------
43 template <class DerivedClass>
44 void init(uint count,DerivedClass *&dc)
46 nlassert(_data==NULL);
49 _data=(BaseClass*)malloc(count*sizeof(DerivedClass));
50 _size=sizeof(DerivedClass);
51 nlassert(_data!=NULL);
53 for (uint i=0;i<count;++i)
55 // std::construct(&((DerivedClass*)_data)[i], DerivedClass());
57 new (&((DerivedClass*)_data)[i]) DerivedClass();
62 // destroy array of objects and free memory ---------------------
71 // return number of objects in array or 0 if not initialised ----
77 // [] operator for accessing data -------------------------------
78 BaseClass &operator[](uint index) const
80 nlassert( index < _count );
81 return *(BaseClass *)((char *)_data+index*_size);
84 void setElm(BaseClass *obj, uint index)
86 nlassert( index < _count );
87 // BaseClass *ptr = (BaseClass *) ((char *)_data+index*_size);
88 // memcpy( ptr, obj, _size );
89 ( *(BaseClass *)((char *)_data+index*_size) ) = *obj;