00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00037
00038 namespace mrpt
00039 {
00040 namespace utils
00041 {
00042 using namespace mrpt;
00043
00044
00045 DEFINE_SERIALIZABLE_PRE( CMHPropertiesValuesList )
00046
00047
00048
00049 struct MRPTDLLIMPEXP TPropertyValueIDTriplet
00050 {
00051 TPropertyValueIDTriplet() : name(), value(NULL),ID(0)
00052 {}
00053
00054 std::string name;
00055 CSerializablePtr value;
00056 int64_t ID;
00057 };
00058
00064 class MRPTDLLIMPEXP CMHPropertiesValuesList : public mrpt::utils::CSerializable
00065 {
00066
00067 DEFINE_SERIALIZABLE( CMHPropertiesValuesList )
00068
00069 private:
00070 std::vector<TPropertyValueIDTriplet> m_properties;
00071
00072 public:
00075 CMHPropertiesValuesList();
00076
00079 CMHPropertiesValuesList( const CMHPropertiesValuesList& o );
00080
00083 CMHPropertiesValuesList & operator =( const CMHPropertiesValuesList& o );
00084
00087 virtual ~CMHPropertiesValuesList();
00088
00091 void clear();
00092
00095 CSerializablePtr get(const char *propertyName, const int64_t & hypothesis_ID ) const;
00096
00099 template <typename T>
00100 typename T::SmartPtr getAs(const char *propertyName, const int64_t & hypothesis_ID, bool allowNullPointer = true) const
00101 {
00102 MRPT_TRY_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_TRY_END
00114 }
00115
00116
00119 CSerializablePtr getAnyHypothesis(const char *propertyName) const;
00120
00124 void set(const char *propertyName, const CSerializablePtr &obj, const int64_t & hypothesis_ID);
00125
00129 void setMemoryReference(const char *propertyName, const CSerializablePtr& obj, const int64_t & hypothesis_ID);
00130
00133 void remove(const char *propertyName, const int64_t & hypothesis_ID);
00134
00137 void removeAll(const int64_t & hypothesis_ID);
00138
00141 template <class T>
00142 void setElemental(const char *propertyName, const T &data, const int64_t & hypothesis_ID)
00143 {
00144 MRPT_TRY_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
00155
00156 it->value = memChunk;
00157 return;
00158 }
00159 }
00160
00161
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_TRY_END_WITH_CLEAN_UP( \
00169 printf("Exception while setting annotation '%s'",propertyName); \
00170 );
00171 }
00172
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_TRY_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
00192 if (raiseExceptionIfNotFound)
00193 THROW_EXCEPTION_CUSTOM_MSG1("Property '%s' not found", propertyName );
00194 return false;
00195 MRPT_TRY_END
00196 }
00197
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 };
00214
00215
00216 }
00217 }
00218 #endif