MercuryDPM  0.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BaseHandler.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2014, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
27 
28 #ifndef BASEHANDLER_H
29 #define BASEHANDLER_H
30 
31 #include <iostream>
32 #include <vector>
33 
34 
37 template <class T>
39 {
40 public:
43  {
44  clear();
45  #ifdef CONSTUCTOR_OUTPUT
46  std::cout << "BaseHandler::BaseHandler() finished" << std::endl;
47  #endif
48  }
49 
53  {
54  clear();
55  for (typename std::vector<T*>::const_iterator it=BH.begin();it!=BH.end();it++)
56  {
57  addObject((*it)->copy());
58  }
59  #ifdef CONSTUCTOR_OUTPUT
60  std::cout << "BaseHandler::BaseHandler(const BaseHandler &BH) finished" << std::endl;
61  #endif
62  }
63 
66  // BaseHandler operator = (const BaseHandler<T>& rhs)
67  // {
68  // if (this != &rhs)
69  // {
70  // clear();
71  // for (typename std::vector<T*>::const_iterator it=rhs.begin();it!=rhs.end();it++)
72  // {
73  // addObject((*it)->copy());
74  // }
75  // }
76  // #ifdef OPERATOR_OUTPUT
77  // std::cout << "BaseHandler BaseHandler::operator = (const BoundaryHandler& rhs) finished" << std::endl;
78  // #endif
79  // return *this;
80  // }
81 
83  virtual ~BaseHandler()
84  {
85  clear();
86  #ifdef DESTRUCTOR_OUTPUT
87  std::cout << "BaseHandler::~BaseHandler() finished" << std::endl;
88  #endif
89  }
90 
93  void copyAndAddObject(const T& O)
94  {
95  addObject(O.copy());
96  }
97 
100  void copyAndAddObject(const T* O)
101  {
102  addObject(O->copy());
103  }
104 
106  virtual void addObject(T* O)
107  {
108  objects_.push_back(O);
109  //Set the index of the particle
110  getLastObject()->set_Index(getNumberOfObjects()-1);
111  //set the non changing particle identifier
112  getLastObject()->set_Id(nextId_);
113  //Update Id for next particle
114  nextId_++;
115  //Update the maximum number of Particles
117  }
118 
122  virtual void removeObject(unsigned const int id)
123  {
124  if (id>=getNumberOfObjects())
125  {
126  std::cerr<<"In: void BaseHandler::removeObject(const unsigned int id) const"<<std::endl;
127  std::cerr<<"No Object exist with index "<<id<<" number of objects is "<<getNumberOfObjects()<<std::endl;
128  throw;
129  }
130 
131  //Physically remove Object
132  delete objects_[id];
133 
134  //If the Object was the last Object nothing has to be done, otherwise some additional work is neccesary
135  if (getObject(id) != getLastObject())
136  {
137  //Copy the pointer to the last Object to position id
138  objects_[id] = getLastObject();
139  objects_[id]->moveInHandler(id);
140  }
141  //Remove the (now double) reference to that last Object
142  objects_.pop_back();
143  }
144 
145 
148  {
149  if (getNumberOfObjects()==0)
150  {
151  std::cerr<<"In: void BaseHandler::removeLastObject const"<<std::endl;
152  std::cerr<<"No Object exist in this BaseHandler"<<std::endl;
153  throw;
154  }
155  //Physically removes Object
156  delete objects_.back();
157  //Remove the (now double) reference to that last Object
158  objects_.pop_back();
159  }
160 
162  void clear()
163  {
164  objects_.clear();
165  nextId_=0;
166  maxObjects_=0;
167  }
168 
171  virtual void readObject(std::istream& is)=0;
172 
176  T* getObject(const unsigned int id) const
177  {
178  if (id>=getNumberOfObjects())
179  {
180  std::cerr<<"In: Object* BaseHandler::getObject(const unsigned int id) const"<<std::endl;
181  std::cerr<<"No Object exist with index "<<id<<" number of objects is "<<getNumberOfObjects()<<std::endl;
182  throw;
183  }
184  else
185  {
186  return objects_[id];
187  }
188  }
189 
192  T* getLastObject() const
193  {
194  return objects_.back();
195  }
196 
199  unsigned int getNumberOfObjects() const
200  {
201  return objects_.size();
202  }
203 
206  unsigned int getStorageCapacity() const
207  {
208  return objects_.capacity();
209  }
210 
213  void setStorageCapacity(const unsigned int N)
214  {
215  objects_.reserve(N);
216  }
217 
220  const typename std::vector<T*>::const_iterator begin() const
221  {
222  return objects_.begin();
223  }
224 
227  const typename std::vector<T*>::iterator begin()
228  {
229  return objects_.begin();
230  }
233  const typename std::vector<T*>::const_iterator end() const
234  {
235  return objects_.end();
236  }
237 
240  const typename std::vector<T*>::iterator end()
241  {
242  return objects_.end();
243  }
244 
245 private:
247  std::vector<T*> objects_;
248 
250  unsigned int maxObjects_;
251 
253  int nextId_;
254 };
255 
256 #endif
257 
virtual void addObject(T *O)
Adds a new Object to the BaseHandler.
Definition: BaseHandler.h:106
virtual void readObject(std::istream &is)=0
Reads Object into the BaseHandler from restart data.
void copyAndAddObject(const T &O)
Creates a copy of a Object and adds it to the BaseHandler.
Definition: BaseHandler.h:93
T * getObject(const unsigned int id) const
Gets a pointer to the Object at the specified index in the BaseHandler.
Definition: BaseHandler.h:176
void setStorageCapacity(const unsigned int N)
Sets the storage capacity of this BaseHandler.
Definition: BaseHandler.h:213
void copyAndAddObject(const T *O)
Creates a copy of a Object and adds it to the BaseHandler.
Definition: BaseHandler.h:100
T * getLastObject() const
Gets a pointer to the last Object in this BaseHandler.
Definition: BaseHandler.h:192
const std::vector< T * >::const_iterator begin() const
Gets the begin of the const_iterator over all Object in this BaseHandler.
Definition: BaseHandler.h:220
BaseHandler()
Default BaseHandler constructor, it simply creates an empty BaseHandler.
Definition: BaseHandler.h:42
void removeLastObject()
Removes the last Object from the BaseHandler.
Definition: BaseHandler.h:147
const std::vector< T * >::iterator end()
Gets the end of the iterator over all BaseBoundary in this BaseHandler.
Definition: BaseHandler.h:240
const std::vector< T * >::const_iterator end() const
Gets the end of the const_iterator over all BaseBoundary in this BaseHandler.
Definition: BaseHandler.h:233
std::vector< T * > objects_
The actual list of Object pointers.
Definition: BaseHandler.h:247
Container to store all Object.
Definition: BaseHandler.h:38
unsigned int getNumberOfObjects() const
Gets the number of Object in this BaseHandler.
Definition: BaseHandler.h:199
const std::vector< T * >::iterator begin()
Gets the begin of the iterator over all BaseBoundary in this BaseHandler.
Definition: BaseHandler.h:227
BaseHandler(const BaseHandler< T > &BH)
Copy constructor, it copies the BaseHandler and all Object it contains.
Definition: BaseHandler.h:52
int nextId_
identifier for next object created
Definition: BaseHandler.h:253
unsigned int getStorageCapacity() const
Gets the storage capacity of this BaseHandler.
Definition: BaseHandler.h:206
virtual void removeObject(unsigned const int id)
Removes a Object from the BaseHandler.
Definition: BaseHandler.h:122
unsigned int maxObjects_
An integer to keep track of the largest number of objects ever stored in this BaseHandler.
Definition: BaseHandler.h:250
virtual ~BaseHandler()
Assignment operator.
Definition: BaseHandler.h:83
void clear()
Empties the whole BaseHandler by removing all Object.
Definition: BaseHandler.h:162