Add infos into target window
[ryzomcore.git] / ryzom / server / src / ai_share / static_vector.h
blob43ea9b137be339f3727c087fa02ac4c7e0022d1b
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
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.
8 //
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>
35 class StaticVector
37 public:
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);
47 _count=count;
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());
56 #undef new
57 new (&((DerivedClass*)_data)[i]) DerivedClass();
58 #define new NL_NEW
62 // destroy array of objects and free memory ---------------------
63 void release()
65 _count=0;
66 _size=0;
67 if (_data)
68 delete[] _data;
71 // return number of objects in array or 0 if not initialised ----
72 uint count() const
74 return _count;
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;
92 private:
93 BaseClass* _data;
94 uint _count;
95 uint _size;
100 #endif