openModeller  Version 1.5.0
PreMultiContainer.hh
Go to the documentation of this file.
1 
27 #ifndef PRE_MULTICONTAINER_HH
28  #define PRE_MULTICONTAINER_HH
29 
30  #include <openmodeller/Log.hh>
32 
33  #include <time.h>
34 
35  #include <vector>
36  #include <map>
37  #include <typeinfo>
38 
39  //adapted from Terralib PDI (Digital Image Processing) to OM Pre-analysis ( Missae & Emiliano - DPI/INPE )
40  //Multi-container node interface.
41  //author Emiliano F. Castejon <castejon@dpi.inpe.br>
42  template< typename PreMultiContainerKeyT >
44  public :
45 
46  //Default Constructor.
48 
49  //Default Destructor.
50  virtual ~MCNodeInterface() {};
51 
58  const = 0;
59 
64  virtual const std::string& getObjTypeInfo() const = 0;
65 
66  private :
67 
68  //Alternative constructor.
71 
78  };
79 
80  //adapted from Terralib PDI (Digital Image Processing) to OM Pre-analysis ( Missae & Emiliano - DPI/INPE )
81  // Multi-container node class.
82  //author Emiliano F. Castejon <castejon@dpi.inpe.br>
83  template< typename ObjectT, typename PreMultiContainerKeyT >
84  class MCNode : public MCNodeInterface< PreMultiContainerKeyT > {
85  public :
86 
87  //Default Constructor.
88  MCNode()
89  {
90  obj_ptr_ = 0;
91  };
92 
93  //Default Destructor.
95  {
96  if( obj_ptr_ ) {
97  delete obj_ptr_;
98  }
99  };
100 
107  {
110 
111  if( obj_ptr_ ) {
112  new_node_ptr->obj_ptr_ = new ObjectT;
113  ( *( new_node_ptr->obj_ptr_ ) ) = ( *obj_ptr_ );
114 
115  new_node_ptr->obj_type_str_ = obj_type_str_;
116  }
117 
118  return new_node_ptr;
119  };
120 
125  void setObjPtr( ObjectT* ptr )
126  {
127  if( !ptr ) {
128  std::string msg = "MCNode::setObjPtr: Invalid pointer.\n";
129  Log::instance()->error( msg.c_str() );
130  throw InvalidParameterException( msg );
131  }
132 
133  if( obj_ptr_ ) {
134  delete obj_ptr_;
135  }
136 
137  obj_ptr_ = ptr;
138  obj_type_str_ = std::string( typeid( *ptr ).name() );
139  };
140 
145  ObjectT* getObjPtr() const
146  {
147  return obj_ptr_;
148  };
149 
154  const std::string& getObjTypeInfo() const
155  {
156  return obj_type_str_;
157  };
158 
159  protected :
160 
161 
162  //The internal object pointer.
163  ObjectT* obj_ptr_;
164 
165  //The internal object type.
166  std::string obj_type_str_;
167 
168  };
169 
170  //adapted from Terralib PDI (Digital Image Processing) to OM Pre-analysis ( Missae & Emiliano - DPI/INPE )
171  //A container class to store multiple types os object copies.
172  //author Emiliano F. Castejon <castejon@dpi.inpe.br>
173  //note: This is a thread-safe class.
174  //note: Do not use this class with polymorphic types !!
175  template< typename PreMultiContainerKeyT >
177 
178  public :
179 
180  //brief Default Constructor
182 
183  //Alternative Constructor
184  //external: External reference.
187 
188  //Default Destructor
190 
198  bool operator==(
199  const PreMultiContainer< PreMultiContainerKeyT >& ext_instance ) const;
200 
208  bool operator!=(
209  const PreMultiContainer< PreMultiContainerKeyT >& ext_instance ) const;
210 
218  const PreMultiContainer< PreMultiContainerKeyT >& ext_instance );
219 
220  //Clear all contents.
221  void clear();
222 
229  template< typename ObjectT >
230  void store( const PreMultiContainerKeyT& obj_key,
231  const ObjectT& obj_reference );
232 
240  template< typename ObjectT >
241  bool retrieve( const PreMultiContainerKeyT& obj_key,
242  ObjectT& obj_reference ) const;
243 
250  template< typename ObjectT >
251  void multiRetrieve( std::vector< std::pair< PreMultiContainerKeyT,
252  ObjectT > >& objs_vector ) const;
253 
261  template< typename ObjectT >
262  bool isStored( const PreMultiContainerKeyT& obj_key ) const;
263 
269  void remove( const PreMultiContainerKeyT& obj_key );
270 
271  protected :
272 
273  //Internal container type definition.
274  typedef typename std::map< PreMultiContainerKeyT,
276 
277  //The nodes container instance.
279 
280  //The last update time.
282 
288  void update_time();
289 
290 };
291 
292 template< typename PreMultiContainerKeyT >
294 {
295  last_up_time_ = 0;
296 }
297 
298 template< typename PreMultiContainerKeyT >
301 {
302  last_up_time_ = 0;
303 
304  operator=( external );
305 }
306 
307 template< typename PreMultiContainerKeyT >
309 {
310  clear();
311 }
312 
313 template< typename PreMultiContainerKeyT >
315 {
316 
317  typename IntContainerT::iterator it = container_instance_.begin();
318  typename IntContainerT::iterator it_end = container_instance_.end();
319 
320  while( it != it_end ) {
321  delete (it->second);
322 
323  ++it;
324  }
325 
326  container_instance_.clear();
327 
328 }
329 
330 template< typename PreMultiContainerKeyT >
332  const PreMultiContainer< PreMultiContainerKeyT >& ext_instance ) const
333 {
334  if( last_up_time_ == ext_instance.last_up_time_ ) {
335  return true;
336  } else {
337  return false;
338  }
339 }
340 
341 template< typename PreMultiContainerKeyT >
343  const PreMultiContainer< PreMultiContainerKeyT >& ext_instance )
344  const
345 {
346  if( last_up_time_ == ext_instance.last_up_time_ ) {
347  return false;
348  } else {
349  return true;
350  }
351 }
352 
353 template< typename PreMultiContainerKeyT >
356  const PreMultiContainer< PreMultiContainerKeyT >& ext_instance )
357 {
358  if( ( &ext_instance ) != this ) {
359 
360  //Clearing the current objects
361 
362  typename IntContainerT::iterator my_container_it =
363  container_instance_.begin();
364  typename IntContainerT::iterator my_container_it_end =
365  container_instance_.end();
366 
367  while( my_container_it != my_container_it_end ) {
368  delete (my_container_it->second);
369 
370  ++my_container_it;
371  }
372 
373  container_instance_.clear();
374 
375  //Cloning external objects
376 
377  typename IntContainerT::const_iterator container_it =
378  ext_instance.container_instance_.begin();
379  typename IntContainerT::const_iterator container_it_end =
380  ext_instance.container_instance_.end();
381 
382  while( container_it != container_it_end ) {
383  container_instance_[ container_it->first ] =
384  container_it->second->clone();
385 
386  ++container_it;
387  }
388 
389  last_up_time_ = ext_instance.last_up_time_;
390 
391 
392  }
393 
394  return *this;
395 }
396 
397 template< typename PreMultiContainerKeyT >
399 {
400  last_up_time_ = time( 0 );
401 }
402 
403 template< typename PreMultiContainerKeyT >
404 template< typename ObjectT >
406  const PreMultiContainerKeyT& obj_key, const ObjectT& obj_reference )
407 {
408 
409  //Creating a new node
410 
411  ObjectT* newobjptr = new ObjectT;
412  ( *newobjptr ) = obj_reference;
413 
416  newnodeptr->setObjPtr( newobjptr );
417 
418  typename IntContainerT::iterator container_it =
419  container_instance_.find( obj_key );
420 
421  //If a old node with the same key exists, it will be deleted
422 
423  if( container_it == container_instance_.end() ) {
424  container_instance_[ obj_key ] = newnodeptr;
425  } else {
426  delete (container_it->second);
427 
428  container_it->second = newnodeptr;
429  }
430 
431  update_time();
432 
433 }
434 
435 template< typename PreMultiContainerKeyT >
436 template< typename ObjectT >
438  const PreMultiContainerKeyT& obj_key, ObjectT& obj_reference ) const
439 {
440 
441  typename IntContainerT::const_iterator container_it =
442  container_instance_.find( obj_key );
443 
444  if( container_it == container_instance_.end() ) {
445 
446  return false;
447  } else {
448  if( typeid( ObjectT ).name() ==
449  container_it->second->getObjTypeInfo() ) {
450 
451  obj_reference =
453  container_it->second )->getObjPtr() ) );
454 
455  return true;
456  } else {
457 
458  return false;
459  }
460  }
461 }
462 
463 template< typename PreMultiContainerKeyT >
464 template< typename ObjectT >
466  std::vector< std::pair< PreMultiContainerKeyT,
467  ObjectT > >& objs_vector ) const
468 {
469  objs_vector.clear();
470 
471  typename IntContainerT::const_iterator container_it =
472  container_instance_.begin();
473  typename IntContainerT::const_iterator container_it_end =
474  container_instance_.end();
475 
476  std::pair< PreMultiContainerKeyT, ObjectT > temp_pair;
477 
478  while( container_it != container_it_end ) {
479  if( typeid( ObjectT ).name() ==
480  container_it->second->getObjTypeInfo() ) {
481 
482  temp_pair.first = container_it->first;
483  temp_pair.second =
485  container_it->second )->getObjPtr() ) );
486 
487  objs_vector.push_back( temp_pair );
488  }
489 
490  ++container_it;
491  }
492 
493 }
494 
495 template< typename PreMultiContainerKeyT >
496 template< typename ObjectT >
498  const PreMultiContainerKeyT& obj_key ) const
499 {
500 
501  typename IntContainerT::const_iterator container_it =
502  container_instance_.find( obj_key );
503 
504  if( container_it == container_instance_.end() ) {
505 
506  return false;
507  } else {
508  if( typeid( ObjectT ).name() ==
509  container_it->second->getObjTypeInfo() ) {
510 
511  return true;
512  } else {
513 
514  return false;
515  }
516  }
517 }
518 
519 
520 template< typename PreMultiContainerKeyT >
522  const PreMultiContainerKeyT& obj_key )
523 {
524 
525  typename IntContainerT::iterator container_it =
526  container_instance_.find( obj_key );
527 
528  //If a old node with the same key exists, it will be deleted
529 
530  if( container_it != container_instance_.end() ) {
531  delete (container_it->second);
532 
533  container_instance_.erase( container_it );
534  }
535 
536  update_time();
537 
538 }
539 
540 #endif
541 
IntContainerT container_instance_
bool isStored(const PreMultiContainerKeyT &obj_key) const
std::string obj_type_str_
void multiRetrieve(std::vector< std::pair< PreMultiContainerKeyT, ObjectT > > &objs_vector) const
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
ObjectT * getObjPtr() const
const std::string & getObjTypeInfo() const
void error(const char *format,...)
'Error' level.
Definition: Log.cpp:290
const PreMultiContainer< PreMultiContainerKeyT > & operator=(const PreMultiContainer< PreMultiContainerKeyT > &ext_instance)
void remove(const PreMultiContainerKeyT &obj_key)
bool retrieve(const PreMultiContainerKeyT &obj_key, ObjectT &obj_reference) const
void store(const PreMultiContainerKeyT &obj_key, const ObjectT &obj_reference)
void setObjPtr(ObjectT *ptr)
bool operator==(const PreMultiContainer< PreMultiContainerKeyT > &ext_instance) const
virtual MCNodeInterface< PreMultiContainerKeyT > * clone() const =0
std::map< PreMultiContainerKeyT, MCNodeInterface< PreMultiContainerKeyT > * > IntContainerT
virtual const std::string & getObjTypeInfo() const =0
MCNodeInterface< PreMultiContainerKeyT > * clone() const
MCNodeInterface(const MCNodeInterface< PreMultiContainerKeyT > &)
ObjectT * obj_ptr_
const MCNodeInterface< PreMultiContainerKeyT > & operator=(const MCNodeInterface< PreMultiContainerKeyT > &)
virtual ~MCNodeInterface()
bool operator!=(const PreMultiContainer< PreMultiContainerKeyT > &ext_instance) const