Warning, file /include/xrootd/XrdCl/XrdClPropertyList.hh was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 #ifndef __XRD_CL_PROPERTY_LIST_HH__
0026 #define __XRD_CL_PROPERTY_LIST_HH__
0027
0028 #include <map>
0029 #include <string>
0030 #include <sstream>
0031 #include <algorithm>
0032
0033 #include "XrdCl/XrdClXRootDResponses.hh"
0034
0035 namespace XrdCl
0036 {
0037
0038
0039
0040 class PropertyList
0041 {
0042 public:
0043 typedef std::map<std::string, std::string> PropertyMap;
0044
0045
0046
0047
0048
0049
0050
0051 template<typename Item>
0052 void Set( const std::string &name, const Item &value )
0053 {
0054 std::ostringstream o;
0055 o << value;
0056 pProperties[name] = o.str();
0057 }
0058
0059
0060
0061
0062
0063
0064 template<typename Item>
0065 bool Get( const std::string &name, Item &item ) const
0066 {
0067 PropertyMap::const_iterator it;
0068 it = pProperties.find( name );
0069 if( it == pProperties.end() )
0070 return false;
0071 std::istringstream i; i.str( it->second );
0072 i >> item;
0073 if( i.bad() )
0074 return false;
0075 return true;
0076 }
0077
0078
0079
0080
0081
0082
0083 template<typename Item>
0084 Item Get( const std::string &name ) const
0085 {
0086 PropertyMap::const_iterator it;
0087 it = pProperties.find( name );
0088 if( it == pProperties.end() )
0089 return Item();
0090 std::istringstream i; i.str( it->second );
0091 Item item;
0092 i >> item;
0093 if( i.bad() )
0094 return Item();
0095 return item;
0096 }
0097
0098
0099
0100
0101
0102
0103
0104
0105 template<typename Item>
0106 void Set( const std::string &name, uint32_t index, const Item &value )
0107 {
0108 std::ostringstream o;
0109 o << name << " " << index;
0110 Set( o.str(), value );
0111 }
0112
0113
0114
0115
0116
0117
0118 template<typename Item>
0119 bool Get( const std::string &name, uint32_t index, Item &item ) const
0120 {
0121 std::ostringstream o;
0122 o << name << " " << index;
0123 return Get( o.str(), item );
0124 }
0125
0126
0127
0128
0129
0130
0131 template<typename Item>
0132 Item Get( const std::string &name, uint32_t index ) const
0133 {
0134 std::ostringstream o;
0135 o << name << " " << index;
0136 return Get<Item>( o.str() );
0137 }
0138
0139
0140
0141
0142 bool HasProperty( const std::string &name ) const
0143 {
0144 return pProperties.find( name ) != pProperties.end();
0145 }
0146
0147
0148
0149
0150 bool HasProperty( const std::string &name, uint32_t index ) const
0151 {
0152 std::ostringstream o;
0153 o << name << " " << index;
0154 return HasProperty( o.str() );
0155 }
0156
0157
0158
0159
0160 PropertyMap::const_iterator begin() const
0161 {
0162 return pProperties.begin();
0163 }
0164
0165
0166
0167
0168 PropertyMap::const_iterator end() const
0169 {
0170 return pProperties.end();
0171 }
0172
0173
0174
0175
0176 void Clear()
0177 {
0178 pProperties.clear();
0179 }
0180
0181 private:
0182 PropertyMap pProperties;
0183 };
0184
0185
0186
0187
0188 template<>
0189 inline bool PropertyList::Get<std::string>( const std::string &name,
0190 std::string &item ) const
0191 {
0192 PropertyMap::const_iterator it;
0193 it = pProperties.find( name );
0194 if( it == pProperties.end() )
0195 return false;
0196 item = it->second;
0197 return true;
0198 }
0199
0200 template<>
0201 inline std::string PropertyList::Get<std::string>( const std::string &name ) const
0202 {
0203 PropertyMap::const_iterator it;
0204 it = pProperties.find( name );
0205 if( it == pProperties.end() )
0206 return std::string();
0207 return it->second;
0208 }
0209
0210
0211
0212
0213 template<>
0214 inline void PropertyList::Set<XRootDStatus>( const std::string &name,
0215 const XRootDStatus &item )
0216 {
0217 std::ostringstream o;
0218 o << item.status << ";" << item.code << ";" << item.errNo << "#";
0219 o << item.GetErrorMessage();
0220 Set( name, o.str() );
0221 }
0222
0223
0224
0225
0226 template<>
0227 inline bool PropertyList::Get<XRootDStatus>( const std::string &name,
0228 XRootDStatus &item ) const
0229 {
0230 std::string str, msg, tmp;
0231 if( !Get( name, str ) )
0232 return false;
0233
0234 std::string::size_type i;
0235 i = str.find( '#' );
0236 if( i == std::string::npos )
0237 return false;
0238 item.SetErrorMessage( str.substr( i+1, str.length()-i-1 ) );
0239 str.erase( i, str.length()-i );
0240 std::replace( str.begin(), str.end(), ';', ' ' );
0241 std::istringstream is; is.str( str );
0242 is >> item.status; if( is.bad() ) return false;
0243 is >> item.code; if( is.bad() ) return false;
0244 is >> item.errNo; if( is.bad() ) return false;
0245 return true;
0246 }
0247
0248 template<>
0249 inline XRootDStatus PropertyList::Get<XRootDStatus>(
0250 const std::string &name ) const
0251 {
0252 XRootDStatus st;
0253 if( !Get( name, st ) )
0254 return XRootDStatus();
0255 return st;
0256 }
0257
0258
0259
0260
0261 template<>
0262 inline void PropertyList::Set<URL>( const std::string &name,
0263 const URL &item )
0264 {
0265 Set( name, item.GetURL() );
0266 }
0267
0268
0269
0270
0271 template<>
0272 inline bool PropertyList::Get<URL>( const std::string &name,
0273 URL &item ) const
0274 {
0275 std::string tmp;
0276 if( !Get( name, tmp ) )
0277 return false;
0278
0279 item = tmp;
0280 return true;
0281 }
0282
0283
0284
0285
0286 template<>
0287 inline void PropertyList::Set<std::vector<std::string> >(
0288 const std::string &name,
0289 const std::vector<std::string> &item )
0290 {
0291 std::vector<std::string>::const_iterator it;
0292 int i = 0;
0293 for( it = item.begin(); it != item.end(); ++it, ++i )
0294 Set( name, i, *it );
0295 }
0296
0297
0298
0299
0300 template<>
0301 inline bool PropertyList::Get<std::vector<std::string> >(
0302 const std::string &name,
0303 std::vector<std::string> &item ) const
0304 {
0305 std::string tmp;
0306 item.clear();
0307 for( int i = 0; HasProperty( name, i ); ++i )
0308 {
0309 if( !Get( name, i, tmp ) )
0310 return false;
0311 item.push_back( tmp );
0312 }
0313 return true;
0314 }
0315 }
0316
0317 #endif