00001 /* 00002 00003 This file is part of OpenFLUID software 00004 Copyright(c) 2007, INRA - Montpellier SupAgro 00005 00006 00007 == GNU General Public License Usage == 00008 00009 OpenFLUID is free software: you can redistribute it and/or modify 00010 it under the terms of the GNU General Public License as published by 00011 the Free Software Foundation, either version 3 of the License, or 00012 (at your option) any later version. 00013 00014 OpenFLUID is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 GNU General Public License for more details. 00018 00019 You should have received a copy of the GNU General Public License 00020 along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>. 00021 00022 00023 == Other Usage == 00024 00025 Other Usage means a use of OpenFLUID that is inconsistent with the GPL 00026 license, and requires a written agreement between You and INRA. 00027 Licensees for Other Usage of OpenFLUID may use this file in accordance 00028 with the terms contained in the written agreement between You and INRA. 00029 00030 */ 00031 00032 00033 /** 00034 \file MapValue.hpp 00035 \brief Header of ... 00036 00037 \author Jean-Christophe FABRE <fabrejc@supagro.inra.fr> 00038 */ 00039 00040 00041 #ifndef __MAPVALUE_HPP__ 00042 #define __MAPVALUE_HPP__ 00043 00044 00045 #include <map> 00046 #include <boost/shared_ptr.hpp> 00047 00048 #include <openfluid/core/CompoundValue.hpp> 00049 #include <openfluid/core/DoubleValue.hpp> 00050 #include <openfluid/core/IntegerValue.hpp> 00051 #include <openfluid/core/BooleanValue.hpp> 00052 #include <openfluid/core/StringValue.hpp> 00053 #include <openfluid/core/MatrixValue.hpp> 00054 #include <openfluid/core/VectorValue.hpp> 00055 #include <openfluid/dllexport.hpp> 00056 00057 00058 namespace openfluid { namespace core { 00059 00060 /** 00061 MapValue is a container for a key => value map, where keys are strings and values can be any type derived from openfluid::core::Value.\n 00062 00063 \see Value 00064 00065 \n 00066 00067 <I>Example : declaration</I> 00068 @code 00069 // declaration of a MapValue, empty by default 00070 openfluid::core::MapValue Val1; 00071 @endcode 00072 00073 00074 <I>Example : setting the contained values</I> 00075 @code 00076 // using the generic set method (notice the new operator) 00077 Val1.set("myvalue1",new openfluid::core::DoubleValue(18.05)); 00078 00079 // using a specific set method 00080 Val1.setDoubleValue("myvalue2",openfluid::core::DoubleValue(0.005)); 00081 00082 // using a specific set method 00083 Val1.setMatrixValue("myvalue3",openfluid::core::MatrixValue(3,3,1.99)); 00084 @endcode 00085 00086 00087 <I>Example : getting the contained values</I> 00088 @code 00089 openfluid::core::DoubleValue Tmp1; 00090 double DblTmp1; 00091 00092 // using the generic get method 00093 Tmp1 = Val1.get("myvalue1").asDoubleValue(); 00094 00095 // using specific get methods 00096 Tmp1 = Val1.getDoubleValue("myvalue1"); 00097 DblTmp1 = Val1.getDouble("myvalue1"); 00098 00099 // or using the [] operator 00100 Tmp1 = Val1["myvalue1"].asDoubleValue(); 00101 @endcode 00102 00103 00104 <I>Example : testing the contained elements</I> 00105 @code 00106 // testing if a key exist 00107 Val1.isKeyExist("myvalue1"); // true in this case; 00108 00109 // testing if a key exist and the contained value type 00110 Val1.isKeyExist("myvalue2") && Val1["myvalue2"].getType() == openfluid::core::Value::BOOLEAN; // false in this case 00111 @endcode 00112 00113 00114 <I>Example : conversion from string</I> 00115 @code 00116 openfluid::core::StringValue StringVal; 00117 openfluid::core::MapValue Val2; 00118 00119 // to MapValue, using a string values separator 00120 StringVal.set("myvalue1=toto;myvalue2=12.56;myvalue3=17;myvalue3=false"); 00121 StringVal.toMapValue(";",Val2); 00122 00123 // all values are stored as strings, that can be converted to other types 00124 openfluid::core::IntegerValue TmpInt; 00125 Val2.get("myvalue3").asStringValue().toIntegerValue(TmpInt); 00126 @endcode 00127 00128 00129 <I>Example : conversion to string</I> 00130 @code 00131 std::string StdStrVal = Val1.toString(); 00132 @endcode 00133 */ 00134 class DLLEXPORT MapValue : public CompoundValue 00135 { 00136 public: 00137 typedef std::map<std::string,boost::shared_ptr<Value> > Map_t; 00138 00139 00140 private: 00141 00142 Map_t m_Value; 00143 00144 public: 00145 00146 /** 00147 Default constructor 00148 */ 00149 MapValue() : CompoundValue() {}; 00150 00151 /** 00152 Copy constructor 00153 */ 00154 MapValue(const MapValue& Val); 00155 00156 MapValue(const Map_t& Val) : CompoundValue(), m_Value(Val) {}; 00157 00158 Value& operator =(const Value& Other); 00159 00160 ~MapValue(); 00161 00162 inline Type getType() const { return Value::MAP; }; 00163 00164 Value* clone() const { return new MapValue(*this); }; 00165 00166 void writeToStream(std::ostream& OutStm) const; 00167 00168 /** 00169 Sets a new value at the given key 00170 @param[in] Key the key to add 00171 @param[in] Element the element to add, must be derived from openfluid::core::Value 00172 */ 00173 void set(const std::string& Key, Value* Element); 00174 00175 /** 00176 Sets a new double value at the given key 00177 @param[in] Key the key to add 00178 @param[in] Val the value to add 00179 */ 00180 inline void setDouble(const std::string& Key, const double& Val) 00181 { set(Key,new DoubleValue(Val)); }; 00182 00183 /** 00184 Sets a new long value at the given key 00185 @param[in] Key the key to add 00186 @param[in] Val the value to add 00187 00188 */ 00189 inline void setInteger(const std::string& Key, const long& Val) 00190 { set(Key,new IntegerValue(Val)); }; 00191 00192 /** 00193 Sets a new boolean value at the given key 00194 @param[in] Key the key to add 00195 @param[in] Val the value to add 00196 */ 00197 inline void setBoolean(const std::string& Key, const bool& Val) 00198 { set(Key,new BooleanValue(Val)); }; 00199 00200 /** 00201 Sets a new string value at the given key 00202 @param[in] Key the key to add 00203 @param[in] Val the value to add 00204 */ 00205 inline void setString(const std::string& Key, const std::string& Val) 00206 { set(Key,new StringValue(Val)); }; 00207 00208 /** 00209 Sets a new VectorValue value at the given key 00210 @param[in] Key the key to add 00211 @param[in] Val the value to add 00212 */ 00213 inline void setVectorValue(const std::string& Key, const VectorValue& Val) 00214 { set(Key,new VectorValue(Val)); }; 00215 00216 /** 00217 Sets a new MatrixValue value at the given key 00218 @param[in] Key the key to add 00219 @param[in] Val the value to add 00220 */ 00221 inline void setMatrixValue(const std::string& Key, const MatrixValue& Val) 00222 { set(Key,new MatrixValue(Val)); }; 00223 00224 /** 00225 Operator to get/set a value at a key given between [] 00226 @return the value at the given key 00227 */ 00228 Value& operator[](const std::string& Key); 00229 00230 /** 00231 Returns the value of the map at the given key 00232 @param[in] Key the key of the requested value 00233 @return the value at the given key 00234 */ 00235 Value& get(const std::string& Key); 00236 00237 /** 00238 Returns the double value of the map at the given key 00239 @param[in] Key the key of the requested value 00240 @return the value at the given key 00241 */ 00242 inline double getDouble(const std::string& Key) { return get(Key).asDoubleValue().get(); }; 00243 00244 /** 00245 Returns the long value of the map at the given key 00246 @param[in] Key the key of the requested value 00247 @return the value at the given key 00248 */ 00249 inline long getInteger(const std::string& Key) { return get(Key).asIntegerValue().get(); }; 00250 00251 /** 00252 Returns the boolean value of the map at the given key 00253 @param[in] Key the key of the requested value 00254 @return the value at the given key 00255 */ 00256 inline bool getBoolean(const std::string& Key) { return get(Key).asBooleanValue().get(); }; 00257 00258 /** 00259 Returns the string value of the map at the given key 00260 @param[in] Key the key of the requested value 00261 @return the value at the given key 00262 */ 00263 inline std::string getString(const std::string& Key) { return get(Key).asStringValue().get(); }; 00264 00265 /** 00266 Returns the VectorValue value of the map at the given key 00267 @param[in] Key the key of the requested value 00268 @return the value at the given key 00269 */ 00270 inline VectorValue getVectorValue(const std::string& Key) { return get(Key).asVectorValue(); }; 00271 00272 /** 00273 Returns the MatrixValue value of the map at the given key 00274 @param[in] Key the key of the requested value 00275 @return the value at the given key 00276 */ 00277 inline MatrixValue getMatrixValue(const std::string& Key) { return get(Key).asMatrixValue(); }; 00278 00279 /** 00280 Removes the value corresponding to the given key 00281 @param[in] Key the key to remove 00282 */ 00283 bool remove(const std::string& Key); 00284 00285 /** 00286 Returns the size of the map 00287 @return size of the map 00288 */ 00289 inline unsigned long getSize() const { return m_Value.size(); }; 00290 00291 /** 00292 Returns the size of the map 00293 @return size of the map 00294 */ 00295 unsigned long size() const { return m_Value.size(); }; 00296 00297 /** 00298 Checks if the given key exists 00299 @param[in] Key the key to check 00300 @return true if the given key is present 00301 */ 00302 inline bool isKeyExist(const std::string& Key) const { return (m_Value.find(Key) != m_Value.end()); }; 00303 00304 /** 00305 Clears the map by removing all values 00306 */ 00307 void clear(); 00308 00309 }; 00310 00311 00312 } } // namespaces 00313 00314 00315 // ===================================================================== 00316 // ===================================================================== 00317 00318 00319 #endif /* __MAPVALUE_HPP__ */