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://mrpt.sourceforge.net/                            |
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          */
00064         class BASE_IMPEXP CMHPropertiesValuesList : public mrpt::utils::CSerializable
00065         {
00066             // This must be added to any CSerializable derived class:
00067             DEFINE_SERIALIZABLE( CMHPropertiesValuesList )
00068 
00069                 private:
00070                         std::vector<TPropertyValueIDTriplet>    m_properties;
00071 
00072         public:
00073             /** Default constructor
00074               */
00075             CMHPropertiesValuesList();
00076 
00077             /** Copy constructor
00078               */
00079             CMHPropertiesValuesList( const CMHPropertiesValuesList& o );
00080 
00081             /** Copy operator
00082               */
00083             CMHPropertiesValuesList & operator =( const CMHPropertiesValuesList& o );
00084 
00085             /** Destructor
00086               */
00087             virtual ~CMHPropertiesValuesList();
00088 
00089             /** Clears the list and frees all object's memory.
00090               */
00091             void  clear();
00092 
00093             /** Returns the value of the property (case insensitive) for some given hypothesis ID, or a NULL smart pointer if it does not exist.
00094               */
00095             CSerializablePtr  get(const char *propertyName, const int64_t & hypothesis_ID ) const;
00096 
00097             /** 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.
00098               */
00099                         template <typename T>
00100             typename T::SmartPtr getAs(const char *propertyName, const int64_t & hypothesis_ID, bool allowNullPointer = true) const
00101                         {
00102                                 MRPT_START
00103                                 CSerializablePtr obj = get(propertyName,hypothesis_ID);
00104                                 if (!obj) 
00105                                 {
00106                                         if (allowNullPointer)
00107                                                         return typename T::SmartPtr();
00108                                         else    THROW_EXCEPTION("Null pointer")
00109                                 }
00110                                 const mrpt::utils::TRuntimeClassId*     class_ID = T::classinfo;
00111                                 ASSERT_( class_ID == obj->GetRuntimeClass() );
00112                                 return typename T::SmartPtr( obj );
00113                                 MRPT_END
00114                         }
00115 
00116                         
00117                         /** Returns the value of the property (case insensitive) for the first hypothesis ID found, or NULL if it does not exist.
00118               */
00119             CSerializablePtr  getAnyHypothesis(const char *propertyName) const;
00120 
00121             /** 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)
00122               * \sa setMemoryReference
00123               */
00124             void  set(const char *propertyName, const CSerializablePtr &obj, const int64_t & hypothesis_ID);
00125 
00126             /** 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.
00127               * \sa set
00128               */
00129             void  setMemoryReference(const char *propertyName, const CSerializablePtr& obj, const int64_t & hypothesis_ID);
00130 
00131             /** Remove a given property, if it exists.
00132               */
00133             void  remove(const char *propertyName, const int64_t & hypothesis_ID);
00134 
00135             /** Remove all the properties for the given hypothesis.
00136               */
00137             void  removeAll(const int64_t & hypothesis_ID);
00138 
00139             /** Sets/change the value of a property (case insensitive) for the given hypothesis ID, from an elemental data type.
00140               */
00141             template <class T>
00142             void  setElemental(const char *propertyName, const T &data, const int64_t & hypothesis_ID)
00143             {
00144                 MRPT_START;
00145 
00146                 CMemoryChunkPtr memChunk = CMemoryChunkPtr( new CMemoryChunk() );
00147                                 memChunk->setAllocBlockSize(10);
00148                 (*memChunk) << data;
00149 
00150                 for (std::vector<TPropertyValueIDTriplet>::iterator it=m_properties.begin();it!=m_properties.end();++it)
00151                 {
00152                     if ( it->ID == hypothesis_ID && !system::os::_strcmpi(propertyName,it->name.c_str()) )
00153                     {
00154                         // Delete current contents &
00155                         // Copy new value:
00156                         it->value = memChunk;
00157                         return;
00158                     }
00159                 }
00160 
00161                 // Insert as new:
00162                 TPropertyValueIDTriplet newPair;
00163                 newPair.name = std::string(propertyName);
00164                 newPair.value = memChunk;
00165                 newPair.ID    = hypothesis_ID;
00166                 m_properties.push_back(newPair);
00167 
00168                 MRPT_END_WITH_CLEAN_UP( \
00169                     printf("Exception while setting annotation '%s'",propertyName); \
00170                     );
00171             }
00172 
00173             /** 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).
00174               * \return false if the property does not exist for the given hypothesis.
00175               */
00176             template <class T>
00177             bool getElemental(const char *propertyName, T &out_data, const int64_t & hypothesis_ID, bool raiseExceptionIfNotFound = false) const
00178             {
00179                 MRPT_START
00180                 for (std::vector<TPropertyValueIDTriplet>::const_iterator it=m_properties.begin();it!=m_properties.end();++it)
00181                 {
00182                     if (!system::os::_strcmpi(propertyName,it->name.c_str()) && it->ID == hypothesis_ID )
00183                     {
00184                         CMemoryChunkPtr memChunk = CMemoryChunkPtr(it->value);
00185                         ASSERT_(memChunk)
00186                         if (memChunk->getTotalBytesCount()!=sizeof(out_data)) THROW_EXCEPTION("Data sizes do not match.");
00187                         out_data = *static_cast<T*>( memChunk->getRawBufferData() );
00188                         return true;
00189                     }
00190                 }
00191                 // Not found:
00192                 if (raiseExceptionIfNotFound)
00193                     THROW_EXCEPTION_CUSTOM_MSG1("Property '%s' not found", propertyName );
00194                 return false;
00195                 MRPT_END
00196             }
00197 
00198             /** Returns the name of all properties in the list
00199               */
00200             std::vector<std::string>  getPropertyNames() const;
00201 
00202 
00203                         typedef std::vector<TPropertyValueIDTriplet>::iterator iterator;
00204                         typedef std::vector<TPropertyValueIDTriplet>::const_iterator const_iterator;
00205 
00206                         iterator begin() { return m_properties.begin(); }
00207                         const_iterator begin() const { return m_properties.begin(); }
00208                         iterator end() { return m_properties.end(); }
00209                         const_iterator end() const { return m_properties.end(); }
00210 
00211                         size_t size() const { return m_properties.size(); }
00212 
00213         }; // End of class def.
00214 
00215 
00216         } // End of namespace
00217 } // end of namespace
00218 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN:exported at Tue Jan 25 21:56:31 UTC 2011