Go to the documentation of this file.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_CUSTOM_BASE( CMHPropertiesValuesList, mrpt::utils::CSerializable )
00046
00047
00048
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
00060
00061
00062
00063
00064
00065 class BASE_IMPEXP CMHPropertiesValuesList : public mrpt::utils::CSerializable
00066 {
00067
00068 DEFINE_SERIALIZABLE( CMHPropertiesValuesList )
00069
00070 private:
00071 std::vector<TPropertyValueIDTriplet> m_properties;
00072
00073 public:
00074
00075
00076 CMHPropertiesValuesList();
00077
00078
00079
00080 CMHPropertiesValuesList( const CMHPropertiesValuesList& o );
00081
00082
00083
00084 CMHPropertiesValuesList & operator =( const CMHPropertiesValuesList& o );
00085
00086
00087
00088 virtual ~CMHPropertiesValuesList();
00089
00090
00091
00092 void clear();
00093
00094
00095
00096 CSerializablePtr get(const char *propertyName, const int64_t & hypothesis_ID ) const;
00097
00098
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
00119
00120 CSerializablePtr getAnyHypothesis(const char *propertyName) const;
00121
00122
00123
00124
00125 void set(const char *propertyName, const CSerializablePtr &obj, const int64_t & hypothesis_ID);
00126
00127
00128
00129
00130 void setMemoryReference(const char *propertyName, const CSerializablePtr& obj, const int64_t & hypothesis_ID);
00131
00132
00133
00134 void remove(const char *propertyName, const int64_t & hypothesis_ID);
00135
00136
00137
00138 void removeAll(const int64_t & hypothesis_ID);
00139
00140
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
00156
00157 it->value = memChunk;
00158 return;
00159 }
00160 }
00161
00162
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
00175
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
00193 if (raiseExceptionIfNotFound)
00194 THROW_EXCEPTION_CUSTOM_MSG1("Property '%s' not found", propertyName );
00195 return false;
00196 MRPT_END
00197 }
00198
00199
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 };
00215
00216
00217 }
00218 }
00219 #endif