| TerraLib 4.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

E:/Projetos_Primeiro_Semestre_2012/TerraView/terralib/src/terralib/kernel/TeMultiContainer.h
Go to the documentation of this file.

    00001 /************************************************************************************
    00002 TerraLib - a library for developing GIS applications.
    00003 Copyright � 2001-2007 INPE and Tecgraf/PUC-Rio.
    00004
    00005 This code is part of the TerraLib library.
    00006 This library is free software; you can redistribute it and/or
    00007 modify it under the terms of the GNU Lesser General Public
    00008 License as published by the Free Software Foundation; either
    00009 version 2.1 of the License, or (at your option) any later version.
    00010
    00011 You should have received a copy of the GNU Lesser General Public
    00012 License along with this library.
    00013
    00014 The authors reassure the license terms regarding the warranties.
    00015 They specifically disclaim any warranties, including, but not limited to,
    00016 the implied warranties of merchantability and fitness for a particular purpose.
    00017 The library provided hereunder is on an "as is" basis, and the authors have no
    00018 obligation to provide maintenance, support, updates, enhancements, or modifications.
    00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
    00020 indirect, special, incidental, or consequential damages arising out of the use
    00021 of this library and its documentation.
    00022 *************************************************************************************/
    00023
    00029 #ifndef TEMULTICONTAINER_H
    00030   #define TEMULTICONTAINER_H
    00031
    00032   #include "TeSharedPtr.h"
    00033   #include "TeMutex.h"
    00034   #include "TeAgnostic.h"
    00035
    00036   #include <vector>
    00037   #include <typeinfo>
    00038
    00044   template< typename TeMultiContainerKeyT >
    00045   class TeMCNodeInterface {
    00046     public :
    00047
    00051       TeMCNodeInterface() {};
    00052
    00056       virtual ~TeMCNodeInterface() {};
    00057
    00063       virtual TeMCNodeInterface< TeMultiContainerKeyT >* clone()
    00064         const = 0;
    00065
    00070       virtual const std::string& getObjTypeInfo() const = 0;
    00071
    00078       virtual bool operator==( const TeMCNodeInterface<
    00079         TeMultiContainerKeyT >& external ) const = 0;
    00080
    00085       virtual void* getObjVoidPtr() const = 0;
    00086
    00087     private :
    00088
    00092       TeMCNodeInterface(
    00093         const TeMCNodeInterface< TeMultiContainerKeyT >& ) {};
    00094
    00101       const TeMCNodeInterface< TeMultiContainerKeyT >& operator=(
    00102         const TeMCNodeInterface< TeMultiContainerKeyT >& ) {};
    00103   };
    00104
    00105
    00111   template< typename ObjectT, typename TeMultiContainerKeyT >
    00112   class TeMCNode : public TeMCNodeInterface< TeMultiContainerKeyT > {
    00113     public :
    00114
    00118       TeMCNode()
    00119       {
    00120         obj_ptr_ = 0;
    00121       };
    00122
    00126       ~TeMCNode()
    00127       {
    00128         if( obj_ptr_ ) {
    00129           delete obj_ptr_;
    00130         }
    00131       };
    00132
    00138       TeMCNodeInterface< TeMultiContainerKeyT >* clone() const
    00139       {
    00140         TeMCNode< ObjectT, TeMultiContainerKeyT >* new_node_ptr =
    00141           new TeMCNode< ObjectT, TeMultiContainerKeyT >;
    00142
    00143         if( obj_ptr_ ) {
    00144           new_node_ptr->obj_ptr_ = new ObjectT;
    00145           ( *( new_node_ptr->obj_ptr_ ) ) = ( *obj_ptr_ );
    00146
    00147           new_node_ptr->obj_type_str_ = obj_type_str_;
    00148         }
    00149
    00150         return new_node_ptr;
    00151       };
    00152
    00157       void setObjPtr( ObjectT* ptr )
    00158       {
    00159         TEAGN_DEBUG_CONDITION( ptr, "Invalid pointer" )
    00160
    00161         if( obj_ptr_ ) {
    00162           delete obj_ptr_;
    00163         }
    00164
    00165         obj_ptr_ = ptr;
    00166         obj_type_str_ = std::string( typeid( *ptr ).name() );
    00167       };
    00168
    00173       ObjectT* getObjPtr() const
    00174       {
    00175         return obj_ptr_;
    00176       };
    00177
    00182       const std::string& getObjTypeInfo() const
    00183       {
    00184         return obj_type_str_;
    00185       };
    00186
    00187       bool operator==( const TeMCNodeInterface<
    00188         TeMultiContainerKeyT >& external ) const
    00189       {
    00190         if( obj_type_str_ == external.getObjTypeInfo() )
    00191         {
    00192           ObjectT const* externalObjPtr = (ObjectT const*)(
    00193             external.getObjVoidPtr() );
    00194
    00195           if( (*externalObjPtr) == (*obj_ptr_) )
    00196           {
    00197             return true;
    00198           }
    00199           else
    00200           {
    00201             return false;
    00202           }
    00203         }
    00204         else
    00205         {
    00206           return false;
    00207         }
    00208       };
    00209
    00210     protected :
    00211
    00215       ObjectT* obj_ptr_;
    00216
    00220       std::string obj_type_str_;
    00221
    00222       void* getObjVoidPtr() const
    00223       {
    00224         return (void*)obj_ptr_;
    00225       }
    00226   };
    00227
    00235   template< typename TeMultiContainerKeyT >
    00236   class TeMultiContainer {
    00237
    00238     public :
    00239
    00242       typedef TeSharedPtr< TeMultiContainer< TeMultiContainerKeyT > > pointer;
    00243
    00246       typedef const TeSharedPtr< TeMultiContainer< TeMultiContainerKeyT > > const_pointer;
    00247
    00251       TeMultiContainer();
    00252
    00257       TeMultiContainer(
    00258         const TeMultiContainer< TeMultiContainerKeyT >& external );
    00259
    00263       ~TeMultiContainer();
    00264
    00271       bool operator==(
    00272         const TeMultiContainer< TeMultiContainerKeyT >& external ) const;
    00273
    00281       bool operator!=(
    00282         const TeMultiContainer< TeMultiContainerKeyT >& ext_instance ) const;
    00283
    00290       const TeMultiContainer< TeMultiContainerKeyT >& operator=(
    00291         const TeMultiContainer< TeMultiContainerKeyT >& ext_instance );
    00292
    00297       void clear();
    00298
    00305       template< typename ObjectT >
    00306       void store( const TeMultiContainerKeyT& obj_key,
    00307                          const ObjectT& obj_reference );
    00308
    00316       template< typename ObjectT >
    00317       bool retrive( const TeMultiContainerKeyT& obj_key,
    00318         ObjectT& obj_reference ) const;
    00319
    00326       template< typename ObjectT >
    00327       void multiRetrive( std::vector< std::pair< TeMultiContainerKeyT,
    00328         ObjectT > >& objs_vector ) const;
    00329
    00337       template< typename ObjectT >
    00338       bool isStored( const TeMultiContainerKeyT& obj_key ) const;
    00339
    00345       void remove( const TeMultiContainerKeyT& obj_key );
    00346
    00347     protected :
    00348
    00350       typedef typename std::map< TeMultiContainerKeyT,
    00351         TeMCNodeInterface< TeMultiContainerKeyT >* > IntContainerT;
    00352
    00354       IntContainerT container_instance_;
    00355
    00359       mutable TeMutex lock_instance_;
    00360 };
    00361
    00362
    00363 template< typename TeMultiContainerKeyT >
    00364 TeMultiContainer< TeMultiContainerKeyT >::TeMultiContainer()
    00365 {
    00366 }
    00367
    00368
    00369 template< typename TeMultiContainerKeyT >
    00370 TeMultiContainer< TeMultiContainerKeyT >::TeMultiContainer(
    00371   const TeMultiContainer< TeMultiContainerKeyT >& external )
    00372 {
    00373   operator=( external );
    00374 }
    00375
    00376
    00377 template< typename TeMultiContainerKeyT >
    00378 TeMultiContainer< TeMultiContainerKeyT >::~TeMultiContainer()
    00379 {
    00380   clear();
    00381 }
    00382
    00383
    00384 template< typename TeMultiContainerKeyT >
    00385 void TeMultiContainer< TeMultiContainerKeyT >::clear()
    00386 {
    00387   lock_instance_.lock();
    00388
    00389   typename IntContainerT::iterator it = container_instance_.begin();
    00390   typename IntContainerT::iterator it_end = container_instance_.end();
    00391
    00392   while( it != it_end ) {
    00393     delete (it->second);
    00394
    00395     ++it;
    00396   }
    00397
    00398   container_instance_.clear();
    00399
    00400   lock_instance_.unLock();
    00401 }
    00402
    00403
    00404 template< typename TeMultiContainerKeyT >
    00405 bool TeMultiContainer< TeMultiContainerKeyT >::operator==(
    00406   const TeMultiContainer< TeMultiContainerKeyT >& external ) const
    00407 {
    00408   if( container_instance_.size() == external.container_instance_.size() )
    00409   {
    00410     typename IntContainerT::const_iterator myIt =
    00411       container_instance_.begin();
    00412     typename IntContainerT::const_iterator myItEnd =
    00413       container_instance_.end();
    00414     typename IntContainerT::const_iterator extIt;
    00415     typename IntContainerT::const_iterator extItEnd =
    00416       external.container_instance_.end() ;
    00417
    00418     while( myIt != myItEnd )
    00419     {
    00420       extIt = external.container_instance_.find( myIt->first );
    00421
    00422       if( extIt == extItEnd )
    00423       {
    00424         return false;
    00425       }
    00426
    00427       if( ! myIt->second->operator==( *(extIt->second ) ) )
    00428       {
    00429         return false;
    00430       }
    00431
    00432       ++myIt;
    00433     }
    00434
    00435     return true;
    00436   }
    00437   else
    00438   {
    00439     return false;
    00440   }
    00441 }
    00442
    00443
    00444 template< typename TeMultiContainerKeyT >
    00445 bool TeMultiContainer< TeMultiContainerKeyT >::operator!=(
    00446   const TeMultiContainer< TeMultiContainerKeyT >& ext_instance )
    00447   const
    00448 {
    00449   return ( ! operator==( ext_instance ) );
    00450 }
    00451
    00452
    00453 template< typename TeMultiContainerKeyT >
    00454 const TeMultiContainer< TeMultiContainerKeyT >&
    00455 TeMultiContainer< TeMultiContainerKeyT >::operator=(
    00456   const TeMultiContainer< TeMultiContainerKeyT >& ext_instance )
    00457 {
    00458   if( ( &ext_instance ) != this ) {
    00459     lock_instance_.lock();
    00460
    00461     /* Clearing the current objects */
    00462
    00463     typename IntContainerT::iterator my_container_it =
    00464       container_instance_.begin();
    00465     typename IntContainerT::iterator my_container_it_end =
    00466       container_instance_.end();
    00467
    00468     while( my_container_it != my_container_it_end ) {
    00469       delete (my_container_it->second);
    00470
    00471       ++my_container_it;
    00472     }
    00473
    00474     container_instance_.clear();
    00475
    00476     /* Cloning external objects */
    00477
    00478     ext_instance.lock_instance_.lock();
    00479
    00480     typename IntContainerT::const_iterator container_it =
    00481       ext_instance.container_instance_.begin();
    00482     typename IntContainerT::const_iterator container_it_end =
    00483       ext_instance.container_instance_.end();
    00484
    00485     while( container_it != container_it_end ) {
    00486       container_instance_[ container_it->first ] =
    00487         container_it->second->clone();
    00488
    00489       ++container_it;
    00490     }
    00491
    00492     ext_instance.lock_instance_.unLock();
    00493
    00494     lock_instance_.unLock();
    00495   }
    00496
    00497   return *this;
    00498 }
    00499
    00500 template< typename TeMultiContainerKeyT >
    00501 template< typename ObjectT >
    00502 void TeMultiContainer< TeMultiContainerKeyT >::store(
    00503   const TeMultiContainerKeyT& obj_key, const ObjectT& obj_reference )
    00504 {
    00505   lock_instance_.lock();
    00506
    00507   /* Creating a new node */
    00508
    00509   ObjectT* newobjptr = new ObjectT;
    00510   ( *newobjptr ) = obj_reference;
    00511
    00512   TeMCNode< ObjectT, TeMultiContainerKeyT >* newnodeptr =
    00513     new TeMCNode< ObjectT, TeMultiContainerKeyT >;
    00514   newnodeptr->setObjPtr( newobjptr );
    00515
    00516   typename IntContainerT::iterator container_it =
    00517     container_instance_.find( obj_key );
    00518
    00519   /* If a old node with the same key exists, it will be deleted */
    00520
    00521   if( container_it == container_instance_.end() ) {
    00522     container_instance_[ obj_key ] = newnodeptr;
    00523   } else {
    00524     delete (container_it->second);
    00525
    00526     container_it->second = newnodeptr;
    00527   }
    00528
    00529   lock_instance_.unLock();
    00530 }
    00531
    00532
    00533 template< typename TeMultiContainerKeyT >
    00534 template< typename ObjectT >
    00535 bool TeMultiContainer< TeMultiContainerKeyT >::retrive(
    00536   const TeMultiContainerKeyT& obj_key, ObjectT& obj_reference ) const
    00537 {
    00538   lock_instance_.lock();
    00539
    00540   typename IntContainerT::const_iterator container_it =
    00541     container_instance_.find( obj_key );
    00542
    00543   if( container_it == container_instance_.end() ) {
    00544     lock_instance_.unLock();
    00545
    00546     return false;
    00547   } else {
    00548     if( typeid( ObjectT ).name() ==
    00549       container_it->second->getObjTypeInfo() ) {
    00550
    00551       obj_reference =
    00552         ( *( ( (TeMCNode< ObjectT, TeMultiContainerKeyT >* )
    00553         container_it->second )->getObjPtr() ) );
    00554
    00555       lock_instance_.unLock();
    00556
    00557       return true;
    00558     } else {
    00559       lock_instance_.unLock();
    00560
    00561       return false;
    00562     }
    00563   }
    00564 }
    00565
    00566
    00567 template< typename TeMultiContainerKeyT >
    00568 template< typename ObjectT >
    00569 void TeMultiContainer< TeMultiContainerKeyT >::multiRetrive(
    00570   std::vector< std::pair< TeMultiContainerKeyT,
    00571   ObjectT > >& objs_vector ) const
    00572 {
    00573   objs_vector.clear();
    00574
    00575   lock_instance_.lock();
    00576
    00577   typename IntContainerT::const_iterator container_it =
    00578     container_instance_.begin();
    00579   typename IntContainerT::const_iterator container_it_end =
    00580     container_instance_.end();
    00581
    00582   std::pair< TeMultiContainerKeyT, ObjectT > temp_pair;
    00583
    00584   while( container_it != container_it_end ) {
    00585     if( typeid( ObjectT ).name() ==
    00586       container_it->second->getObjTypeInfo() ) {
    00587
    00588       temp_pair.first = container_it->first;
    00589       temp_pair.second =
    00590         ( *( ( ( TeMCNode< ObjectT, TeMultiContainerKeyT >* )
    00591         container_it->second )->getObjPtr() ) );
    00592
    00593       objs_vector.push_back( temp_pair );
    00594     }
    00595
    00596     ++container_it;
    00597   }
    00598
    00599   lock_instance_.unLock();
    00600 }
    00601
    00602
    00603 template< typename TeMultiContainerKeyT >
    00604 template< typename ObjectT >
    00605 bool TeMultiContainer< TeMultiContainerKeyT >::isStored(
    00606   const TeMultiContainerKeyT& obj_key ) const
    00607 {
    00608   lock_instance_.lock();
    00609
    00610   typename IntContainerT::const_iterator container_it =
    00611     container_instance_.find( obj_key );
    00612
    00613   if( container_it == container_instance_.end() ) {
    00614     lock_instance_.unLock();
    00615
    00616     return false;
    00617   } else {
    00618     if( typeid( ObjectT ).name() ==
    00619       container_it->second->getObjTypeInfo() ) {
    00620
    00621       lock_instance_.unLock();
    00622
    00623       return true;
    00624     } else {
    00625       lock_instance_.unLock();
    00626
    00627       return false;
    00628     }
    00629   }
    00630 }
    00631
    00632
    00633 template< typename TeMultiContainerKeyT >
    00634 void TeMultiContainer< TeMultiContainerKeyT >::remove(
    00635   const TeMultiContainerKeyT& obj_key )
    00636 {
    00637   lock_instance_.lock();
    00638
    00639   typename IntContainerT::iterator container_it =
    00640     container_instance_.find( obj_key );
    00641
    00642   /* If a old node with the same key exists, it will be deleted */
    00643
    00644   if( container_it != container_instance_.end() ) {
    00645     delete (container_it->second);
    00646
    00647     container_instance_.erase( container_it );
    00648   }
    00649
    00650   lock_instance_.unLock();
    00651 }
    00652
    00657 #endif
    00658

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines