gatelib  2.1
g_cont_base_cont.h
1 #pragma once
2 
3 #include "g_cont_cont_with_positioner.h"
4 #include "g_cont_it_ref.h"
5 #include "g_cont_it.h"
6 #include "g_cont_ref.h"
7 #include "g_cont_alg.h"
8 
9 namespace g
10 {
11 namespace cont
12 {
13 template < class T , class REF , class REF_C > class it_base;
14 
15 G_EXC_DEFINE(NotAValidIteratorException,ContException);
16 G_EXC_DEFINE(EmptyContException,ContException);
17 
18 //base class for recipient of references
19 template<class T, class REF, class REF_C , class IT , class IT_C > class base_cont : public cont_with_positioner<REF>
20 {
21 public:
22  template <class,class,class> friend class it_base;
23  template <class,class,class,class,class> friend class lst_base;
24  template <class,class,class,class,class> friend class vect_base;
25 
26  typedef REF Ref_t;
27  typedef REF_C RefConst_t;
28  typedef IT It_t;
29  typedef IT_C ItConst_t;
30 
33 
34  friend class positioner_abstract<REF>;
35 
36 protected:
37  base_cont( AllocationPolicyAbstract* aAllocPolicyP , Positioner_t* positioner_p ) : cont_with_positioner<REF>(aAllocPolicyP,positioner_p){}
38 
39 public:
40  virtual ~base_cont() {}
41 
42  bool operator == ( const base_cont& other ) const;
43 
44  virtual Ref_t remove ( const It_t& ) = 0;
45  virtual void eraseMemory ( ) = 0;
46 
47  virtual void pushAfter ( const Ref_t& , const It_t& ) = 0;
48  virtual void pushBefore ( const Ref_t& , const It_t& ) = 0;
49 
50  virtual void pushHead ( const Ref_t& aItem ) { this->pushBefore(aItem, It_t(*this, head)); }
51  virtual void pushTail ( const Ref_t& aItem ) { this->pushAfter(aItem, It_t(*this, tail)); }
52 
53  virtual Ref_t popHead() { return this->remove(It_t(*this, head)); }
54  virtual Ref_t popTail() { return this->remove(It_t(*this, tail)); }
55 
56  virtual RefConst_t getHead() const { return ItConst_t(*this,head).underlying(); }
57  virtual Ref_t getHead() { return It_t(*this,head).underlying(); }
58 
59  virtual RefConst_t getTail() const { return ItConst_t(*this,tail).underlying(); }
60  virtual Ref_t getTail() { return It_t(*this,tail).underlying(); }
61 
62  virtual It_t getIterator ( IterFrom_t from = head ) { return It_t(*this,from); }
63  virtual ItConst_t getIterator ( IterFrom_t from = head ) const { return ItConst_t(*this,from); }
64 
65  virtual void setContent ( base_cont& other );
66 
67  bool contains ( const RefConst_t& value ) const { return search(value).isIn(); }
68  It_t search ( const Ref_t& item ) { return alg::search ( getIterator(head) , item ); }
69  ItConst_t search ( const RefConst_t& item ) const { return alg::search ( getIterator(head) , item ); }
70  bool tryRemove ( const Ref_t& item ) { return alg::try_remove(*this,item); }
71  size_t removeAll ( const Ref_t& item ) { return alg::remove_all(*this,item); }
72 
73 private:
74  inline int mDeref ( const It_t& where ) const { return where.mIndex; }
75  inline int mTestIterator( const It_t& , bool exc_if_empty = false ) const;
76  inline Ref_t* mSequentialGet ( int i ) const;
77 };
78 
79 template<class T, class REF, class REF_C , class IT , class IT_C > bool base_cont<T,REF,REF_C,IT,IT_C>::operator == ( const base_cont<T,REF,REF_C,IT,IT_C>& aOther ) const
80 {
81  ItConst_t my_it(*this);
82 
83  if ( this->getSize() != aOther.getSize() )
84  {
85  return false;
86  }
87 
88  for ( ItConst_t oth_it ( aOther ) ; oth_it.isIn ( ) ; oth_it++ , my_it++ )
89  {
90  if(my_it.underlying() != oth_it.underlying())
91  {
92  return false;
93  }
94  }
95 
96  return true;
97 }
98 
99 template<class T, class REF, class REF_C , class IT , class IT_C >
100  void base_cont<T,REF,REF_C,IT,IT_C>::setContent ( base_cont<T,REF,REF_C,IT,IT_C>& aOther )
101 {
102  this->setEmpty();
103 
104  for ( It_t it(aOther) ; it.isIn() ; it++ )
105  {
106  pushTail ( it.underlying() );
107  }
108 }
109 
110 //Check the iterator is valid and throw the exception in case of the iterator is not belongin to THIS recipient
111 template<class T, class REF, class REF_C , class IT , class IT_C > int base_cont<T,REF,REF_C,IT,IT_C>::mTestIterator( const It_t& aWhere , bool aRaiseExcIfEmpty ) const
112 {
113  G_EXC_SET_CONTEXT ( "int base_cont<T,REF,REF_C,IT,IT_C>::mTestIterator( const It_t& aWhere , bool aRaiseExcIfEmpty ) const" );
114 
115  //Iterator not belonging to the object
116  if (!aWhere.isMine(*this))
117  {
118  G_EXC_RAISE_MSG ( NotAValidIteratorException , "Iterator is not belonging to this ref_base_cont." );
119  }
120 
121  if ( aWhere.isIn () )
122  {
123  return mDeref ( aWhere );
124  }
125  else if (this->getSize() > 0)
126  {
127  G_EXC_RAISE_MSG(NotAValidIteratorException , "Iterator pointer no longer valid(Probably content has changed or reallocated).");
128  }
129  else if(aRaiseExcIfEmpty)
130  {
131  G_EXC_RAISE_MSG ( EmptyContException , "Item not found!" );
132  }
133 
134  return G_CONT_INDEX_NOT_VALID;
135 }
136 
137 //Used by list, iterates the positioner i-times
138 template<class T, class REF, class REF_C , class IT , class IT_C > REF* base_cont<T,REF,REF_C,IT,IT_C>::mSequentialGet(int aI) const
139 {
140  G_EXC_SET_CONTEXT ( "REF* base_cont<T,REF,REF_C,IT,IT_C>::mSequentialGet(int aI) const" );
141 
142  if ( aI < 0 )
143  {
144  G_EXC_RAISE_CONT ( "Index < 0" );
145  return (REF*)NULL;
146  }
147  else
148  {
149  int index = this->mPositionerP->first();
150 
151  if ( index != G_CONT_INDEX_NOT_VALID )
152  {
153  this->mPositionerP->forward(index,aI);
154  }
155 
156  if ( index == G_CONT_INDEX_NOT_VALID )
157  {
158  G_EXC_RAISE_CONT( "Index out of bounds!" );
159  }
160 
161  return this->mPositionerP->getPtr ( index );
162  }
163 }
164 
165 } //namespace cont
166 } //namespace g
167 
Definition: g_cont_base_cont.h:19
Definition: g_cont_lst_base.h:12
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_base_cont.h:13
Definition: g_cont_cont_with_positioner.h:10
Definition: g_cont_vect_base.h:14
#define G_EXC_SET_CONTEXT(acontextstr)
Sets the method context.
Definition: g_exception_macros.h:52