irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

irods_lookup_table.hpp
Go to the documentation of this file.
1 #ifndef PLUGIN_TABLE_HPP
2 #define PLUGIN_TABLE_HPP
3 
4 // =-=-=-=-=-=-=-
5 // boost includes
6 #include <boost/any.hpp>
7 
8 // =-=-=-=-=-=-=-
9 #include "rodsErrorTable.h"
10 #include "irods_hash.hpp"
11 #include "irods_error.hpp"
12 #include "irods_stacktrace.hpp"
13 
14 #include "rodsLog.h"
15 
16 namespace irods {
17 
18 // =-=-=-=-=-=-=-
19 // class to manage tables of plugins. employing a class in order to use
20 // RAII for adding entries to the table now that it is not a static array
21  template < typename ValueType,
22  typename KeyType = std::string,
23  typename HashType = irods_string_hash >
24  class lookup_table {
25  protected:
26  typedef HASH_TYPE< KeyType, ValueType, HashType > irods_hash_map;
27 
29 
30  public:
31  typedef typename irods_hash_map::iterator iterator;
32  typedef typename irods_hash_map::const_iterator const_iterator;
34  virtual ~lookup_table() {}
35  ValueType& operator[]( KeyType _k ) {
36  return table_[ _k ];
37  }
38  int size() const {
39  return table_.size();
40  }
41  bool has_entry( KeyType _k ) const {
42  return !( table_.end() == table_.find( _k ) );
43  }
44  size_t erase( KeyType _k ) {
45  return table_.erase( _k );
46  }
47  void clear() {
48  table_.clear();
49  }
50  bool empty() const {
51  return table_.empty();
52  }
54  return table_.begin();
55  }
57  return table_.end();
58  }
60  return table_.cbegin();
61  }
63  return table_.cend();
64  }
65  iterator find( KeyType _k ) {
66  return table_.find( _k );
67  }
68 
69  // =-=-=-=-=-=-=-
70  // accessor function
71  error get( const std::string& _key, ValueType& _val ) {
72  if ( !has_entry( _key ) ) {
73  return ERROR( KEY_NOT_FOUND, "key not found" );
74  }
75 
76  _val = table_[ _key ];
77 
78  return SUCCESS();
79  }
80 
81  // =-=-=-=-=-=-=-
82  // mutator function
83  error set( const std::string& _key, const ValueType& _val ) {
84  table_[ _key ] = _val;
85  return SUCCESS();
86  } // set
87 
88  }; // class lookup_table
89 
90 
91 // =-=-=-=-=-=-=-
92 // partial specialization created to support templating the get/set
93 // functions which need to manage exception handling etc from
94 // a boost::any_cast
95  template< typename KeyType, typename HashType >
96  class lookup_table < boost::any, KeyType, HashType > {
97  protected:
98  typedef HASH_TYPE< KeyType, boost::any, HashType > irods_hash_map;
100 
101  public:
102  typedef typename irods_hash_map::iterator iterator;
104  virtual ~lookup_table() {}
105  boost::any& operator[]( KeyType _k ) {
106  return table_[ _k ];
107  }
108  int size() {
109  return table_.size();
110  }
111  bool has_entry( KeyType _k ) {
112  return !( table_.end() == table_.find( _k ) );
113  }
114  size_t erase( KeyType _k ) {
115  return table_.erase( _k );
116  }
117  void clear() {
118  table_.clear();
119  }
120  bool empty() {
121  return table_.empty();
122  }
124  return table_.begin();
125  }
127  return table_.end();
128  }
129  iterator find( KeyType _k ) {
130  return table_.find( _k );
131  }
132 
133  // =-=-=-=-=-=-=-
134  // get a property from the table if it exists. catch the exception in the case where
135  // the template types may not match and return success/fail
136  template< typename T >
137  error get( const std::string& _key, T& _val ) {
138  // =-=-=-=-=-=-=-
139  // check params
140  if ( _key.empty() ) {
141  return ERROR( KEY_NOT_FOUND, "the key is empty" );
142  }
143 
144  if ( !has_entry( _key ) ) {
145  std::stringstream msg;
146  msg << "failed to find key [";
147  msg << _key;
148  msg << "] in table.";
149  return ERROR( KEY_NOT_FOUND, msg.str() );
150  }
151 
152  // =-=-=-=-=-=-=-
153  // attempt to any_cast property value to given type. catch exception and log
154  // failure then exit
155  try {
156  _val = boost::any_cast< T >( table_[ _key ] );
157  return SUCCESS();
158  }
159  catch ( const boost::bad_any_cast& ) {
160  std::stringstream msg;
161  msg << "type and property key [";
162  msg << _key;
163  msg << "] mismatch";
164  return ERROR( KEY_TYPE_MISMATCH, msg.str() );
165  }
166 
167  // =-=-=-=-=-=-=-
168  // invalid location in the code
169  return ERROR( INVALID_LOCATION, "shouldn't get here." );
170 
171  } // get_property
172 
173  // =-=-=-=-=-=-=-
174  // set a property in the table
175  template< typename T >
176  error set( const std::string& _key, const T& _val ) {
177  // =-=-=-=-=-=-=-
178  // check params
179  if ( _key.empty() ) {
180  return ERROR( KEY_NOT_FOUND, "empty key" );
181  }
182 
183  // =-=-=-=-=-=-=-
184  // add property to map
185  table_[ _key ] = _val;
186 
187  return SUCCESS() ;
188 
189  } // set_property
190 
191  }; // class lookup_table
192 
194 
195 }; // namespace irods
196 
197 #endif // PLUGIN_TABLE_HPP
198 
199 
200 
irods::lookup_table::lookup_table
lookup_table()
Definition: irods_lookup_table.hpp:33
irods::lookup_table
Definition: irods_lookup_table.hpp:24
irods_hash.hpp
irods::lookup_table< boost::any, KeyType, HashType >::get
error get(const std::string &_key, T &_val)
Definition: irods_lookup_table.hpp:137
irods_stacktrace.hpp
irods::lookup_table< boost::any, KeyType, HashType >::operator[]
boost::any & operator[](KeyType _k)
Definition: irods_lookup_table.hpp:105
irods::lookup_table< boost::any, KeyType, HashType >::clear
void clear()
Definition: irods_lookup_table.hpp:117
irods::lookup_table::begin
iterator begin()
Definition: irods_lookup_table.hpp:53
irods::lookup_table::iterator
irods_hash_map::iterator iterator
Definition: irods_lookup_table.hpp:31
irods::lookup_table< boost::any, KeyType, HashType >::erase
size_t erase(KeyType _k)
Definition: irods_lookup_table.hpp:114
boost
Definition: rcMisc.cpp:4341
KEY_TYPE_MISMATCH
@ KEY_TYPE_MISMATCH
Definition: rodsErrorTable.h:750
irods::lookup_table::~lookup_table
virtual ~lookup_table()
Definition: irods_lookup_table.hpp:34
irods::lookup_table< boost::any, KeyType, HashType >::size
int size()
Definition: irods_lookup_table.hpp:108
irods::lookup_table::get
error get(const std::string &_key, ValueType &_val)
Definition: irods_lookup_table.hpp:71
irods::lookup_table::cend
iterator cend()
Definition: irods_lookup_table.hpp:62
irods::lookup_table::erase
size_t erase(KeyType _k)
Definition: irods_lookup_table.hpp:44
irods::lookup_table< boost::any, KeyType, HashType >::table_
irods_hash_map table_
Definition: irods_lookup_table.hpp:99
irods::lookup_table< boost::any, KeyType, HashType >::lookup_table
lookup_table()
Definition: irods_lookup_table.hpp:103
irods::lookup_table::table_
irods_hash_map table_
Definition: irods_lookup_table.hpp:28
irods::lookup_table::size
int size() const
Definition: irods_lookup_table.hpp:38
irods::lookup_table< boost::any, KeyType, HashType >::find
iterator find(KeyType _k)
Definition: irods_lookup_table.hpp:129
SUCCESS
#define SUCCESS()
Definition: irods_error.hpp:121
irods::lookup_table< boost::any, KeyType, HashType >::begin
iterator begin()
Definition: irods_lookup_table.hpp:123
irods::lookup_table::empty
bool empty() const
Definition: irods_lookup_table.hpp:50
irods
Definition: apiHandler.hpp:35
irods::lookup_table::operator[]
ValueType & operator[](KeyType _k)
Definition: irods_lookup_table.hpp:35
irods::plugin_property_map
lookup_table< boost::any > plugin_property_map
Definition: irods_lookup_table.hpp:193
irods::lookup_table::irods_hash_map
boost::unordered_map< KeyType, ValueType, HashType > irods_hash_map
Definition: irods_lookup_table.hpp:26
irods::lookup_table< boost::any, KeyType, HashType >::end
iterator end()
Definition: irods_lookup_table.hpp:126
irods::lookup_table< boost::any, KeyType, HashType >::~lookup_table
virtual ~lookup_table()
Definition: irods_lookup_table.hpp:104
KEY_NOT_FOUND
@ KEY_NOT_FOUND
Definition: rodsErrorTable.h:749
irods::lookup_table::find
iterator find(KeyType _k)
Definition: irods_lookup_table.hpp:65
irods::lookup_table< boost::any, KeyType, HashType >::empty
bool empty()
Definition: irods_lookup_table.hpp:120
irods::lookup_table< boost::any, KeyType, HashType >::iterator
irods_hash_map::iterator iterator
Definition: irods_lookup_table.hpp:102
irods::lookup_table::has_entry
bool has_entry(KeyType _k) const
Definition: irods_lookup_table.hpp:41
irods::lookup_table< boost::any, KeyType, HashType >::set
error set(const std::string &_key, const T &_val)
Definition: irods_lookup_table.hpp:176
irods::lookup_table< boost::any, KeyType, HashType >::irods_hash_map
boost::unordered_map< KeyType, boost::any, HashType > irods_hash_map
Definition: irods_lookup_table.hpp:98
rodsLog.h
irods::lookup_table::end
iterator end()
Definition: irods_lookup_table.hpp:56
ERROR
#define ERROR(code_, message_)
Definition: irods_error.hpp:117
INVALID_LOCATION
@ INVALID_LOCATION
Definition: rodsErrorTable.h:756
irods::error
Definition: irods_error.hpp:23
irods::lookup_table::cbegin
iterator cbegin()
Definition: irods_lookup_table.hpp:59
irods::lookup_table::clear
void clear()
Definition: irods_lookup_table.hpp:47
rodsErrorTable.h
irods_error.hpp
irods::lookup_table::set
error set(const std::string &_key, const ValueType &_val)
Definition: irods_lookup_table.hpp:83
irods::lookup_table< boost::any, KeyType, HashType >::has_entry
bool has_entry(KeyType _k)
Definition: irods_lookup_table.hpp:111
irods::lookup_table::const_iterator
irods_hash_map::const_iterator const_iterator
Definition: irods_lookup_table.hpp:32