xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
XrdClPropertyList.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2014 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //-----------------------------------------------------------------------------
5 // This file is part of the XRootD software suite.
6 //
7 // XRootD is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // XRootD is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
19 //
20 // In applying this licence, CERN does not waive the privileges and immunities
21 // granted to it by virtue of its status as an Intergovernmental Organization
22 // or submit itself to any jurisdiction.
23 //------------------------------------------------------------------------------
24 
25 #ifndef __XRD_CL_PROPERTY_LIST_HH__
26 #define __XRD_CL_PROPERTY_LIST_HH__
27 
28 #include <map>
29 #include <string>
30 #include <sstream>
31 #include <algorithm>
32 
34 
35 namespace XrdCl
36 {
37  //----------------------------------------------------------------------------
39  //----------------------------------------------------------------------------
41  {
42  public:
43  typedef std::map<std::string, std::string> PropertyMap;
44 
45  //------------------------------------------------------------------------
50  //------------------------------------------------------------------------
51  template<typename Item>
52  void Set( const std::string &name, const Item &value )
53  {
54  std::ostringstream o;
55  o << value;
56  pProperties[name] = o.str();
57  }
58 
59  //------------------------------------------------------------------------
63  //------------------------------------------------------------------------
64  template<typename Item>
65  bool Get( const std::string &name, Item &item ) const
66  {
67  PropertyMap::const_iterator it;
68  it = pProperties.find( name );
69  if( it == pProperties.end() )
70  return false;
71  std::istringstream i; i.str( it->second );
72  i >> item;
73  if( i.bad() )
74  return false;
75  return true;
76  }
77 
78  //------------------------------------------------------------------------
82  //------------------------------------------------------------------------
83  template<typename Item>
84  Item Get( const std::string &name ) const
85  {
86  PropertyMap::const_iterator it;
87  it = pProperties.find( name );
88  if( it == pProperties.end() )
89  return Item();
90  std::istringstream i; i.str( it->second );
91  Item item;
92  i >> item;
93  if( i.bad() )
94  return Item();
95  return item;
96  }
97 
98  //------------------------------------------------------------------------
104  //------------------------------------------------------------------------
105  template<typename Item>
106  void Set( const std::string &name, uint32_t index, const Item &value )
107  {
108  std::ostringstream o;
109  o << name << " " << index;
110  Set( o.str(), value );
111  }
112 
113  //------------------------------------------------------------------------
117  //------------------------------------------------------------------------
118  template<typename Item>
119  bool Get( const std::string &name, uint32_t index, Item &item ) const
120  {
121  std::ostringstream o;
122  o << name << " " << index;
123  return Get( o.str(), item );
124  }
125 
126  //------------------------------------------------------------------------
130  //------------------------------------------------------------------------
131  template<typename Item>
132  Item Get( const std::string &name, uint32_t index ) const
133  {
134  std::ostringstream o;
135  o << name << " " << index;
136  return Get<Item>( o.str() );
137  }
138 
139  //------------------------------------------------------------------------
141  //------------------------------------------------------------------------
142  bool HasProperty( const std::string &name ) const
143  {
144  return pProperties.find( name ) != pProperties.end();
145  }
146 
147  //------------------------------------------------------------------------
149  //------------------------------------------------------------------------
150  bool HasProperty( const std::string &name, uint32_t index ) const
151  {
152  std::ostringstream o;
153  o << name << " " << index;
154  return HasProperty( o.str() );
155  }
156 
157  //------------------------------------------------------------------------
159  //------------------------------------------------------------------------
160  PropertyMap::const_iterator begin() const
161  {
162  return pProperties.begin();
163  }
164 
165  //------------------------------------------------------------------------
167  //------------------------------------------------------------------------
168  PropertyMap::const_iterator end() const
169  {
170  return pProperties.end();
171  }
172 
173  //------------------------------------------------------------------------
175  //------------------------------------------------------------------------
176  void Clear()
177  {
178  pProperties.clear();
179  }
180 
181  private:
183  };
184 
185  //----------------------------------------------------------------------------
186  // Specialize get for strings
187  //----------------------------------------------------------------------------
188  template<>
189  inline bool PropertyList::Get<std::string>( const std::string &name,
190  std::string &item ) const
191  {
192  PropertyMap::const_iterator it;
193  it = pProperties.find( name );
194  if( it == pProperties.end() )
195  return false;
196  item = it->second;
197  return true;
198  }
199 
200  template<>
201  inline std::string PropertyList::Get<std::string>( const std::string &name ) const
202  {
203  PropertyMap::const_iterator it;
204  it = pProperties.find( name );
205  if( it == pProperties.end() )
206  return std::string();
207  return it->second;
208  }
209 
210  //----------------------------------------------------------------------------
211  // Specialize set for XRootDStatus
212  //----------------------------------------------------------------------------
213  template<>
214  inline void PropertyList::Set<XRootDStatus>( const std::string &name,
215  const XRootDStatus &item )
216  {
217  std::ostringstream o;
218  o << item.status << ";" << item.code << ";" << item.errNo << "#";
219  o << item.GetErrorMessage();
220  Set( name, o.str() );
221  }
222 
223  //----------------------------------------------------------------------------
224  // Specialize get for XRootDStatus
225  //----------------------------------------------------------------------------
226  template<>
227  inline bool PropertyList::Get<XRootDStatus>( const std::string &name,
228  XRootDStatus &item ) const
229  {
230  std::string str, msg, tmp;
231  if( !Get( name, str ) )
232  return false;
233 
234  std::string::size_type i;
235  i = str.find( '#' );
236  if( i == std::string::npos )
237  return false;
238  item.SetErrorMessage( str.substr( i+1, str.length()-i-1 ) );
239  str.erase( i, str.length()-i );
240  std::replace( str.begin(), str.end(), ';', ' ' );
241  std::istringstream is; is.str( str );
242  is >> item.status; if( is.bad() ) return false;
243  is >> item.code; if( is.bad() ) return false;
244  is >> item.errNo; if( is.bad() ) return false;
245  return true;
246  }
247 
248  template<>
249  inline XRootDStatus PropertyList::Get<XRootDStatus>(
250  const std::string &name ) const
251  {
252  XRootDStatus st;
253  if( !Get( name, st ) )
254  return XRootDStatus();
255  return st;
256  }
257 
258  //----------------------------------------------------------------------------
259  // Specialize set for URL
260  //----------------------------------------------------------------------------
261  template<>
262  inline void PropertyList::Set<URL>( const std::string &name,
263  const URL &item )
264  {
265  Set( name, item.GetURL() );
266  }
267 
268  //----------------------------------------------------------------------------
269  // Specialize get for URL
270  //----------------------------------------------------------------------------
271  template<>
272  inline bool PropertyList::Get<URL>( const std::string &name,
273  URL &item ) const
274  {
275  std::string tmp;
276  if( !Get( name, tmp ) )
277  return false;
278 
279  item = tmp;
280  return true;
281  }
282 
283  //----------------------------------------------------------------------------
284  // Specialize set for vector<string>
285  //----------------------------------------------------------------------------
286  template<>
287  inline void PropertyList::Set<std::vector<std::string> >(
288  const std::string &name,
289  const std::vector<std::string> &item )
290  {
291  std::vector<std::string>::const_iterator it;
292  int i = 0;
293  for( it = item.begin(); it != item.end(); ++it, ++i )
294  Set( name, i, *it );
295  }
296 
297  //----------------------------------------------------------------------------
298  // Specialize get for XRootDStatus
299  //----------------------------------------------------------------------------
300  template<>
301  inline bool PropertyList::Get<std::vector<std::string> >(
302  const std::string &name,
303  std::vector<std::string> &item ) const
304  {
305  std::string tmp;
306  item.clear();
307  for( int i = 0; HasProperty( name, i ); ++i )
308  {
309  if( !Get( name, i, tmp ) )
310  return false;
311  item.push_back( tmp );
312  }
313  return true;
314  }
315 }
316 
317 #endif // __XRD_OUC_PROPERTY_LIST_HH__
Item Get(const std::string &name) const
Definition: XrdClPropertyList.hh:84
bool Get(const std::string &name, Item &item) const
Definition: XrdClPropertyList.hh:65
bool Get(const std::string &name, uint32_t index, Item &item) const
Definition: XrdClPropertyList.hh:119
void Clear()
Clear the property list.
Definition: XrdClPropertyList.hh:176
void Set(const std::string &name, uint32_t index, const Item &value)
Definition: XrdClPropertyList.hh:106
PropertyMap::const_iterator begin() const
Get the begin iterator.
Definition: XrdClPropertyList.hh:160
bool HasProperty(const std::string &name) const
Check if we now about the given name.
Definition: XrdClPropertyList.hh:142
std::map< std::string, std::string > PropertyMap
Definition: XrdClPropertyList.hh:43
Request status.
Definition: XrdClXRootDResponses.hh:218
void Set(const std::string &name, const Item &value)
Definition: XrdClPropertyList.hh:52
Item Get(const std::string &name, uint32_t index) const
Definition: XrdClPropertyList.hh:132
URL representation.
Definition: XrdClURL.hh:30
bool HasProperty(const std::string &name, uint32_t index) const
Check if we know about the given name and index.
Definition: XrdClPropertyList.hh:150
PropertyMap pProperties
Definition: XrdClPropertyList.hh:182
PropertyMap::const_iterator end() const
Get the end iterator.
Definition: XrdClPropertyList.hh:168
A key-value pair map storing both keys and values as strings.
Definition: XrdClPropertyList.hh:40