Main MRPT website > C++ reference
MRPT logo
CMHPropertiesValuesList.h
Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                       http://www.mrpt.org/                                |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef  CMHPropertiesValuesList_H
00029 #define  CMHPropertiesValuesList_H
00030 
00031 #include <mrpt/utils/CSerializable.h>
00032 #include <mrpt/utils/CMemoryChunk.h>
00033 #include <mrpt/system/os.h>
00034 
00035 /*---------------------------------------------------------------
00036         Class
00037   ---------------------------------------------------------------*/
00038 namespace mrpt
00039 {
00040     namespace utils
00041     {
00042         using namespace mrpt;
00043 
00044         // This must be added to any CSerializable derived class:
00045                 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE( CMHPropertiesValuesList, mrpt::utils::CSerializable )
00046 
00047 
00048         /** Internal triplet for each property in utils::CMHPropertiesValuesList */
00049         struct BASE_IMPEXP  TPropertyValueIDTriplet
00050         {
00051             TPropertyValueIDTriplet() : name(), value(NULL),ID(0)
00052             {}
00053 
00054             std::string                 name;
00055             CSerializablePtr    value;
00056             int64_t                             ID;
00057         };
00058 
00059         /** An arbitrary list of "annotations", or named attributes, each being an instance of any CSerializable object (Multi-hypotheses version).
00060          *   For each named annotation (or attribute), several values may exist, each associated to a given hypothesis ID.
00061          * A non multi-hypotheses version exists in CPropertiesValuesList.
00062          * \sa CSerializable, CPropertiesValuesList
00063          * \ingroup mrpt_base_grp
00064          */
00065         class BASE_IMPEXP CMHPropertiesValuesList : public mrpt::utils::CSerializable
00066         {
00067             // This must be added to any CSerializable derived class:
00068             DEFINE_SERIALIZABLE( CMHPropertiesValuesList )
00069 
00070                 private:
00071                         std::vector<TPropertyValueIDTriplet>    m_properties;
00072 
00073         public:
00074             /** Default constructor
00075               */
00076             CMHPropertiesValuesList();
00077 
00078             /** Copy constructor
00079               */
00080             CMHPropertiesValuesList( const CMHPropertiesValuesList& o );
00081 
00082             /** Copy operator
00083               */
00084             CMHPropertiesValuesList & operator =( const CMHPropertiesValuesList& o );
00085 
00086             /** Destructor
00087               */
00088             virtual ~CMHPropertiesValuesList();
00089 
00090             /** Clears the list and frees all object's memory.
00091               */
00092             void  clear();
00093 
00094             /** Returns the value of the property (case insensitive) for some given hypothesis ID, or a NULL smart pointer if it does not exist.
00095               */
00096             CSerializablePtr  get(const char *propertyName, const int64_t & hypothesis_ID ) const;
00097 
00098             /** Returns the value of the property (case insensitive) for some given hypothesis ID checking its class in runtime, or a NULL smart pointer if it does not exist.
00099               */
00100                         template <typename T>
00101             typename T::SmartPtr getAs(const char *propertyName, const int64_t & hypothesis_ID, bool allowNullPointer = true) const
00102                         {
00103                                 MRPT_START
00104                                 CSerializablePtr obj = get(propertyName,hypothesis_ID);
00105                                 if (!obj) 
00106                                 {
00107                                         if (allowNullPointer)
00108                                                         return typename T::SmartPtr();
00109                                         else    THROW_EXCEPTION("Null pointer")
00110                                 }
00111                                 const mrpt::utils::TRuntimeClassId*     class_ID = T::classinfo;
00112                                 ASSERT_( class_ID == obj->GetRuntimeClass() );
00113                                 return typename T::SmartPtr( obj );
00114                                 MRPT_END
00115                         }
00116 
00117                         
00118                         /** Returns the value of the property (case insensitive) for the first hypothesis ID found, or NULL if it does not exist.
00119               */
00120             CSerializablePtr  getAnyHypothesis(const char *propertyName) const;
00121 
00122             /** Sets/change the value of the property (case insensitive) for the given hypothesis ID, making a copy of the object (or setting it to NULL if it is the passed value)
00123               * \sa setMemoryReference
00124               */
00125             void  set(const char *propertyName, const CSerializablePtr &obj, const int64_t & hypothesis_ID);
00126 
00127             /** Sets/change the value of the property (case insensitive) for the given hypothesis ID, directly replacing the pointer instead of making a copy of the object.
00128               * \sa set
00129               */
00130             void  setMemoryReference(const char *propertyName, const CSerializablePtr& obj, const int64_t & hypothesis_ID);
00131 
00132             /** Remove a given property, if it exists.
00133               */
00134             void  remove(const char *propertyName, const int64_t & hypothesis_ID);
00135 
00136             /** Remove all the properties for the given hypothesis.
00137               */
00138             void  removeAll(const int64_t & hypothesis_ID);
00139 
00140             /** Sets/change the value of a property (case insensitive) for the given hypothesis ID, from an elemental data type.
00141               */
00142             template <class T>
00143             void  setElemental(const char *propertyName, const T &data, const int64_t & hypothesis_ID)
00144             {
00145                 MRPT_START
00146 
00147                 CMemoryChunkPtr memChunk = CMemoryChunkPtr( new CMemoryChunk() );
00148                                 memChunk->setAllocBlockSize(10);
00149                 (*memChunk) << data;
00150 
00151                 for (std::vector<TPropertyValueIDTriplet>::iterator it=m_properties.begin();it!=m_properties.end();++it)
00152                 {
00153                     if ( it->ID == hypothesis_ID && !system::os::_strcmpi(propertyName,it->name.c_str()) )
00154                     {
00155                         // Delete current contents &
00156                         // Copy new value:
00157                         it->value = memChunk;
00158                         return;
00159                     }
00160                 }
00161 
00162                 // Insert as new:
00163                 TPropertyValueIDTriplet newPair;
00164                 newPair.name = std::string(propertyName);
00165                 newPair.value = memChunk;
00166                 newPair.ID    = hypothesis_ID;
00167                 m_properties.push_back(newPair);
00168 
00169                 MRPT_END_WITH_CLEAN_UP( \
00170                     printf("Exception while setting annotation '%s'",propertyName); \
00171                     );
00172             }
00173 
00174             /** Gets the value of a property (case insensitive) for the given hypothesis ID, retrieves it as an elemental data type (types must coincide, basic size check is performed).
00175               * \return false if the property does not exist for the given hypothesis.
00176               */
00177             template <class T>
00178             bool getElemental(const char *propertyName, T &out_data, const int64_t & hypothesis_ID, bool raiseExceptionIfNotFound = false) const
00179             {
00180                 MRPT_START
00181                 for (std::vector<TPropertyValueIDTriplet>::const_iterator it=m_properties.begin();it!=m_properties.end();++it)
00182                 {
00183                     if (!system::os::_strcmpi(propertyName,it->name.c_str()) && it->ID == hypothesis_ID )
00184                     {
00185                         CMemoryChunkPtr memChunk = CMemoryChunkPtr(it->value);
00186                         ASSERT_(memChunk)
00187                         if (memChunk->getTotalBytesCount()!=sizeof(out_data)) THROW_EXCEPTION("Data sizes do not match.");
00188                         out_data = *static_cast<T*>( memChunk->getRawBufferData() );
00189                         return true;
00190                     }
00191                 }
00192                 // Not found:
00193                 if (raiseExceptionIfNotFound)
00194                     THROW_EXCEPTION_CUSTOM_MSG1("Property '%s' not found", propertyName );
00195                 return false;
00196                 MRPT_END
00197             }
00198 
00199             /** Returns the name of all properties in the list
00200               */
00201             std::vector<std::string>  getPropertyNames() const;
00202 
00203 
00204                         typedef std::vector<TPropertyValueIDTriplet>::iterator iterator;
00205                         typedef std::vector<TPropertyValueIDTriplet>::const_iterator const_iterator;
00206 
00207                         iterator begin() { return m_properties.begin(); }
00208                         const_iterator begin() const { return m_properties.begin(); }
00209                         iterator end() { return m_properties.end(); }
00210                         const_iterator end() const { return m_properties.end(); }
00211 
00212                         size_t size() const { return m_properties.size(); }
00213 
00214         }; // End of class def.
00215 
00216 
00217         } // End of namespace
00218 } // end of namespace
00219 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011