PropertyListImpl.h

Go to the documentation of this file.
00001 // @(#)root/reflex:$Id: PropertyListImpl.h 29288 2009-07-01 13:03:35Z axel $
00002 // Author: Stefan Roiser 2004
00003 
00004 // Copyright CERN, CH-1211 Geneva 23, 2004-2006, All rights reserved.
00005 //
00006 // Permission to use, copy, modify, and distribute this software for any
00007 // purpose is hereby granted without fee, provided that this copyright and
00008 // permissions notice appear in all copies and derivatives.
00009 //
00010 // This software is provided "as is" without express or implied warranty.
00011 
00012 #ifndef Reflex_PropertyListImpl
00013 #define Reflex_PropertyListImpl
00014 
00015 
00016 // Include files
00017 #include "Reflex/Kernel.h"
00018 #include "Reflex/Any.h"
00019 #include <map>
00020 #include <iostream>
00021 
00022 #ifdef _WIN32
00023 # pragma warning( push )
00024 # pragma warning( disable : 4251 )
00025 #endif
00026 
00027 namespace Reflex {
00028 /**
00029  * @class PropertyList PropertyList.h Reflex/PropertyList.h
00030  * @author Stefan Roiser
00031  * @date 24/11/2003
00032  * @ingroup Ref
00033  */
00034 class RFLX_API PropertyListImpl {
00035    friend RFLX_API std::ostream& operator <<(std::ostream& s,
00036                                              const PropertyListImpl& p);
00037 
00038 public:
00039    /** default constructor */
00040    PropertyListImpl();
00041 
00042 
00043    /** copy constructor */
00044    PropertyListImpl(const PropertyListImpl &pl);
00045 
00046 
00047    /** destructor */
00048    virtual ~PropertyListImpl();
00049 
00050 
00051    /**
00052     * AddProperty will add a key value pair to the PropertyNth lsit
00053     * @param key the key of the PropertyNth
00054     * @param value the value of the PropertyNth (as any object)
00055     * @return the property key of this property
00056     */
00057    size_t AddProperty(const std::string& key,
00058                       const Any& value);
00059 
00060 
00061    /**
00062     * AddProperty will add a property value pair to the property list
00063     * @param key the key of the property
00064     * @param value the value of the property (as any object)
00065     */
00066    void AddProperty(size_t key,
00067                     const Any& value);
00068 
00069 
00070    /**
00071     * AddProperty will add a key value pair to the PropertyNth lsit
00072     * @param key the key of the PropertyNth
00073     * @param value the value of the PropertyNth (as any object)
00074     * @return the property key of this property
00075     */
00076    size_t AddProperty(const std::string& key,
00077                       const char* value);
00078 
00079 
00080    /**
00081     * AddProperty will add a key value pair to the property list
00082     * @param key the key of the property
00083     * @param value the value of the property (as any object)
00084     */
00085    void AddProperty(size_t key,
00086                     const char* value);
00087 
00088 
00089    /**
00090     * ClearProperties will remove all properties from the list
00091     */
00092    void ClearProperties();
00093 
00094 
00095    /**
00096     * HasProperty will return true if the property list contains a key "key" and
00097     * the property for this key is valid
00098     * @param  key to look for
00099     * @return true if a valid property for key exists
00100     */
00101    bool HasProperty(const std::string& key) const;
00102 
00103 
00104    /**
00105     * HasProperty will return true if the property list contains a key "key" and
00106     * the property for this key is valid
00107     * @param  key to look for
00108     * @return true if a valid property for key exists
00109     */
00110    bool HasProperty(size_t key) const;
00111 
00112 
00113    /**
00114     * Key_Begin will return the begin iterator of the key container
00115     * @return begin iterator of key container
00116     */
00117    static StdString_Iterator Key_Begin();
00118 
00119 
00120    /**
00121     * Key_End will return the end iterator of the key container
00122     * @return end iterator of key container
00123     */
00124    static StdString_Iterator Key_End();
00125 
00126 
00127    /**
00128     * Key_RBegin will return the rbegin iterator of the key container
00129     * @return rbegin iterator of key container
00130     */
00131    static Reverse_StdString_Iterator Key_RBegin();
00132 
00133 
00134    /**
00135     * Key_REnd will return the rend iterator of the key container
00136     * @return rend iterator of key container
00137     */
00138    static Reverse_StdString_Iterator Key_REnd();
00139 
00140 
00141    /**
00142     * KeysAsString will return a space separated list of all keys
00143     * @return a list of all currently allocated keys
00144     */
00145    static std::string KeysAsString();
00146 
00147 
00148    /**
00149     * KeyAt will return the nth key allocated
00150     * @param nth key currently allocated
00151     * @return key as a string
00152     */
00153    static const std::string& KeyAt(size_t nth);
00154 
00155 
00156    /**
00157     * Key is the static getter function to return the index of a key. If allocateNew is
00158     * set to true a new key will be allocated if it doesn't exist and it's index returned.
00159     * Otherwise if the key exists the function returns it's index or 0 if no key exists.
00160     * @param key the key to look for
00161     * @param allocateNew allocate a new key if the key doesn't exist
00162     * @return key index or 0 if no key exists and allocateNew is set to false
00163     */
00164    static size_t KeyByName(const std::string& key,
00165                            bool allocateNew = false);
00166 
00167 
00168    /**
00169     * KeySize will return the number of currently allocated keys
00170     * @return number of allocated keys
00171     */
00172    static size_t KeySize();
00173 
00174 
00175    /**
00176     * propertyNumString will return the nth PropertyNth as a string if printable
00177     * @param  key the PropertyNth key
00178     * @return nth PropertyNth value as string
00179     */
00180    std::string PropertyAsString(const std::string& key) const;
00181 
00182 
00183    /**
00184     * PropertyAsString will return the property value as a string if it exists
00185     * The parameter is a property key which can be aquired with the PropertyKey method.
00186     * @param key property key to look for
00187     * @return string representation of the property
00188     */
00189    std::string PropertyAsString(size_t key) const;
00190 
00191 
00192    /**
00193     * PropertyKey will return the the key value corresponding to the parameter given
00194     * @param key the string denoting the key value to lookup
00195     * @param allocateNew if set to true a new key will be allocated if it doesn't exist
00196       if set to false and the key doesn't exist the function returns 0
00197     * @return the key value corresponding to the key param
00198     */
00199    size_t PropertyKey(const std::string& key,
00200                       bool allocateNew = false) const;
00201 
00202 
00203    /**
00204     * PropertyKeys will return all keys of this PropertyNth list
00205     * @return all PropertyNth keys
00206     */
00207    std::string PropertyKeys() const;
00208 
00209 
00210    /**
00211     * PropertyCount will return the number of properties attached
00212     * to this item
00213     * @return number of properties
00214     */
00215    size_t PropertyCount() const;
00216 
00217 
00218    /**
00219     * propertyNumValue will return the nth PropertyNth value
00220     * @param  key the PropertyNth key
00221     * @return nth PropertyNth value
00222     */
00223    Any& PropertyValue(const std::string& key) const;
00224 
00225 
00226    /**
00227     * PropertyAsString will return the property value as an Any object if it exists
00228     * The parameter is a property key which can be aquired with the PropertyKey method.
00229     * @param key property key to look for
00230     * @return Any representation of the property
00231     */
00232    Any& PropertyValue(size_t key) const;
00233 
00234 
00235    /**
00236     * RemoveProperty will remove a key value pair to the PropertyNth lsit
00237     * @param key the key of the PropertyNth
00238     */
00239    void RemoveProperty(const std::string& key);
00240 
00241 
00242    /**
00243     * RemoveProperty will remove a property value from the property list
00244     * @param key of the property identified by the property key number
00245     */
00246    void RemoveProperty(size_t key);
00247 
00248 private:
00249    /**
00250     * the Property container
00251     */
00252    typedef std::vector<Any> Properties;
00253 
00254 
00255    /** the properties of the item
00256     * @label properties
00257     * @link aggregationByValue
00258     * @clientCardinality 1
00259     * @supplierCardinality 0..1
00260     */
00261    std::vector<Any>* fProperties;
00262 
00263 };    // class PropertyListImpl
00264 
00265 /**
00266  * will put the PropertyNth (key and value) on the ostream if printable
00267  * @param s the reference to the stream
00268  * @return the stream
00269  */
00270 RFLX_API std::ostream& operator <<(std::ostream& s,
00271                                    const PropertyListImpl& p);
00272 
00273 } //namespace Reflex
00274 
00275 
00276 //-------------------------------------------------------------------------------
00277 inline Reflex::PropertyListImpl::PropertyListImpl()
00278 //-------------------------------------------------------------------------------
00279    : fProperties(0) {
00280 }
00281 
00282 
00283 //-------------------------------------------------------------------------------
00284 inline Reflex::PropertyListImpl::PropertyListImpl(const PropertyListImpl& pl)
00285 //-------------------------------------------------------------------------------
00286    : fProperties(pl.fProperties) {
00287 }
00288 
00289 
00290 //-------------------------------------------------------------------------------
00291 inline size_t
00292 Reflex::PropertyListImpl::AddProperty(const std::string& key,
00293                                       const Any& value) {
00294 //-------------------------------------------------------------------------------
00295    size_t k = PropertyKey(key, true);
00296    AddProperty(k, value);
00297    return k;
00298 }
00299 
00300 
00301 //-------------------------------------------------------------------------------
00302 inline void
00303 Reflex::PropertyListImpl::AddProperty(size_t key,
00304                                       const Any& value) {
00305 //-------------------------------------------------------------------------------
00306    if (!fProperties) {
00307       fProperties = new Properties();
00308    }
00309 
00310    if (key >= fProperties->size()) {
00311       fProperties->resize(key + 1, Dummy::Any());
00312    }
00313    (*fProperties)[key] = value;
00314 }
00315 
00316 
00317 //-------------------------------------------------------------------------------
00318 inline size_t
00319 Reflex::PropertyListImpl::AddProperty(const std::string& key,
00320                                       const char* value) {
00321 //-------------------------------------------------------------------------------
00322    return AddProperty(key, Any(value));
00323 }
00324 
00325 
00326 //-------------------------------------------------------------------------------
00327 inline void
00328 Reflex::PropertyListImpl::AddProperty(size_t key,
00329                                       const char* value) {
00330 //-------------------------------------------------------------------------------
00331    AddProperty(key, Any(value));
00332 }
00333 
00334 
00335 //-------------------------------------------------------------------------------
00336 inline void
00337 Reflex::PropertyListImpl::RemoveProperty(const std::string& key) {
00338 //-------------------------------------------------------------------------------
00339    RemoveProperty(PropertyKey(key));
00340 }
00341 
00342 
00343 //-------------------------------------------------------------------------------
00344 inline void
00345 Reflex::PropertyListImpl::RemoveProperty(size_t key) {
00346 //-------------------------------------------------------------------------------
00347    if (fProperties) {
00348       fProperties->at(key).Swap(Dummy::Any());
00349    }
00350 }
00351 
00352 
00353 #ifdef _WIN32
00354 # pragma warning( pop )
00355 #endif
00356 
00357 #endif // Reflex_PropertyListImpl

Generated on Tue Jul 5 14:25:52 2011 for ROOT_528-00b_version by  doxygen 1.5.1