gatelib  2.1
g_cont_vect_base.h
1 #pragma once
2 
3 #include "g_cont_base_cont.h"
4 #include "private/g_cont_vect_priv.h"
5 
6 namespace g
7 {
8 namespace cont
9 {
10 
11 G_EXC_DEFINE(IndexOutOfBoundsException,ContException);
12 G_EXC_DEFINE(ReducingCapacityException,ContException);
13 
14 template<class T, class REF, class REF_C , class IT , class IT_C > class vect_base : public base_cont<T,REF,REF_C,IT,IT_C>
15 {
16 private:
17  vect_base ( const vect_base& ) {} //prohibited copy constructor
18 public:
19  template <class> friend class priv::vect_positioner;
20  template <class> friend struct priv::vect_content;
21 
22  typedef REF Ref_t;
23  typedef REF_C RefConst_t;
24  typedef IT It_t;
25  typedef IT_C ItConst_t;
26 
27 protected:
28  vect_base ( AllocationPolicyAbstract* , int alloc_delta_right_bits );
29 
30 public:
31  virtual ~vect_base ( ) {}
32 
33  virtual void pushAfter ( const Ref_t& item , const It_t& where );
34  virtual void pushBefore ( const Ref_t& item , const It_t& where );
35 
36  Ref_t& operator [] ( int i );
37  RefConst_t& operator [] ( int i ) const;
38 
39  virtual int getLbound ( ) const { return mContent.lbound; }
40  virtual int getUbound ( ) const { return mContent.ubound; }
41 
42  virtual void setEmpty ( ) { mContent.empty(); }
43  virtual void eraseMemory ( ) { mContent.erase(); }
44 
45  virtual Ref_t remove ( const It_t& where ) { return mContent.remove (this->mTestIterator(where,true)); }
46 
47  void setSize ( int lbound , int ubound ) { mContent.setSize ( lbound , ubound ); }
48  void setSize ( size_t size ) { setSize ( 0 , (int)size - 1 ); }
49 
50  void reSize ( int lbound , int ubound ) { mContent.resize ( lbound , ubound ); }
51  void reSize ( size_t size ) { reSize ( 0 , (int)size-1 ); }
52 
53  void setCapacity ( size_t );
54 
55  size_t getCapacity ( ) const { return mContent.capacity; }
56 
57 protected:
58  priv::vect_content<REF> mContent;
59  priv::vect_positioner<REF> mPositioner;
60 };
61 
62 template<class T, class REF, class REF_C , class IT , class IT_C > vect_base<T,REF,REF_C,IT,IT_C>::vect_base ( AllocationPolicyAbstract* aAllocPolicyP , int aAllocDeltaRBits ):
63  base_cont<T,REF,REF_C,IT,IT_C> ( aAllocPolicyP , &mPositioner ),
64  mContent ( aAllocPolicyP , aAllocDeltaRBits ) ,
65  mPositioner ( &mContent ) { }
66 
67 template<class T, class REF, class REF_C , class IT , class IT_C > void vect_base<T,REF,REF_C,IT,IT_C>::setCapacity ( size_t aCapacity )
68 {
69  G_EXC_SET_CONTEXT ( "vect_base<T,REF,REF_C,IT,IT_C>::setCapacity ( size_t aCapacity )" );
70 
71  if ( aCapacity >= this->getSize ( ) )
72  {
73  mContent.changeCapacity ( aCapacity );
74  }
75  else
76  {
77  G_EXC_RAISE_MSG ( ReducingCapacityException ,"Capacity can't be < of the size" );
78  }
79 }
80 
81 template<class T, class REF, class REF_C , class IT , class IT_C > typename vect_base<T,REF,REF_C,IT,IT_C>::Ref_t& vect_base<T,REF,REF_C,IT,IT_C>::operator [] ( int aI )
82 {
83  G_EXC_SET_CONTEXT ( "typename vect_base<T,REF,REF_C,IT,IT_C>::Ref_t& vect_base<T,REF,REF_C,IT,IT_C>::operator [] ( int aI )" );
84 
85  if ( this->isAnIndex ( aI ) )
86  {
87  return *(mContent.data + aI - getLbound());
88  }
89  else
90  {
91  G_EXC_RAISE_MSG ( IndexOutOfBoundsException , "Index out of bounds!" );
92  return *(mContent.data); //unreachable avoid warning
93  }
94 }
95 
96 template<class T, class REF, class REF_C , class IT , class IT_C > typename vect_base<T,REF,REF_C,IT,IT_C>::RefConst_t& vect_base<T,REF,REF_C,IT,IT_C>::operator [] ( int aI ) const
97 {
98  G_EXC_SET_CONTEXT ( "typename vect_base<T,REF,REF_C,IT,IT_C>::RefConst_t& vect_base<T,REF,REF_C,IT,IT_C>::operator [] ( int aI ) const" );
99 
100  if ( this->isAnIndex ( aI ) )
101  {
102  return *(mContent.data + aI - getLbound());
103  }
104  else
105  {
106  G_EXC_RAISE_MSG ( IndexOutOfBoundsException , "Index out of bounds!" );
107  }
108 }
109 
110 template<class T, class REF, class REF_C , class IT , class IT_C > void vect_base<T,REF,REF_C,IT,IT_C>::pushAfter ( const Ref_t& aItem , const It_t& aWhere )
111 {
112  if ( this->getSize() > 0 )
113  {
114  mContent.pushAfter ( this->mTestIterator(aWhere) , aItem );
115  }
116  else
117  {
118  mContent.replaceContent ( 0 , 0 , &aItem );
119  }
120 }
121 
122 template<class T, class REF, class REF_C , class IT , class IT_C > void vect_base<T,REF,REF_C,IT,IT_C>::pushBefore ( const Ref_t& aItem , const It_t& aWhere )
123 {
124  if ( this->getSize() > 0 )
125  {
126  mContent.pushAfter ( this->mTestIterator(aWhere) - 1 , aItem );
127  }
128  else
129  {
130  mContent.replaceContent ( 0 , 0 , &aItem );
131  }
132 }
133 
134 }//namespace cont
135 }//namespace g
136 
Definition: g_cont_base_cont.h:19
Definition: g_cont_AllocationPolicyAbstract.h:16
#define G_EXC_DEFINE(aexctypename, abaseexctypename)
Definition: g_exception_macros.h:39
Definition: g.mthread.ThreadSimpleEvent.h:5
#define G_EXC_RAISE_MSG(aexctype, amessage)
Raises an exception specifying the message.
Definition: g_exception_macros.h:58
Definition: g_cont_vect_base.h:14
#define G_EXC_SET_CONTEXT(acontextstr)
Sets the method context.
Definition: g_exception_macros.h:52
Definition: g_cont_vect_priv.h:24
Definition: g_cont_vect_priv.h:237