Lunchbox  1.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
persistentMap.h
1 
2 /* Copyright (c) 2014, Stefan.Eilemann@epfl.ch
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License version 2.1 as published
6  * by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11  * details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 
18 #ifndef LUNCHBOX_PERSISTENTMAP_H
19 #define LUNCHBOX_PERSISTENTMAP_H
20 
21 #include <lunchbox/api.h>
22 #include <lunchbox/debug.h> // className
23 #include <lunchbox/log.h> // LBTHROW
24 #include <lunchbox/types.h>
25 #include <boost/noncopyable.hpp>
26 
27 #include <boost/type_traits.hpp>
28 #include <iostream>
29 #include <set>
30 #include <stdexcept>
31 #include <string>
32 #include <vector>
33 
34 namespace lunchbox
35 {
36 namespace detail { class PersistentMap; }
37 
43 class PersistentMap : public boost::noncopyable
44 {
45 public:
59  LUNCHBOX_API PersistentMap( const std::string& uri = std::string( ));
60 
65  LUNCHBOX_API explicit PersistentMap( const URI& uri );
66 
68  LUNCHBOX_API ~PersistentMap();
69 
74  LUNCHBOX_API static bool handles( const URI& uri );
75 
85  template< class V > bool insert( const std::string& key, const V& value )
86  { return _insert( key, value, boost::has_trivial_assign< V >( )); }
87 
97  template< class V >
98  bool insert( const std::string& key, const std::vector< V >& values )
99  { return _insert( key, values, boost::has_trivial_assign< V >( )); }
100 
110  template< class V >
111  bool insert( const std::string& key, const std::set< V >& values )
112  { return insert( key, std::vector<V>( values.begin(), values.end( ))); }
113 
121  LUNCHBOX_API std::string operator [] ( const std::string& key ) const;
122 
130  template< class V > std::vector< V > getVector( const std::string& key );
131 
139  template< class V > std::set< V > getSet( const std::string& key );
140 
142  LUNCHBOX_API bool contains( const std::string& key ) const;
143 
144 private:
145  detail::PersistentMap* const _impl;
146 
147  LUNCHBOX_API bool _insert( const std::string& key, const void* data,
148  const size_t size );
149 
150 
151  // Enables map.insert( "foo", "bar" ); bar is a char[4]. The funny braces
152  // declare v as a "const ref to array of four chars", not as a "const array
153  // to four char refs". Long live Bjarne!
154  template< size_t N > bool
155  _insert( const std::string& k, char const (& v)[N], const boost::true_type&)
156  {
157  return _insert( k, (void*)v, N - 1 ); // strip '0'
158  }
159 
160  template< class V >
161  bool _insert( const std::string& k, const V& v, const boost::true_type& )
162  {
163  if( boost::is_pointer< V >::value )
164  LBTHROW( std::runtime_error( "Can't insert pointers" ));
165  return _insert( k, &v, sizeof( V ));
166  }
167 
168  template< class V >
169  bool _insert( const std::string&, const V& v, const boost::false_type& )
170  { LBTHROW( std::runtime_error( "Can't insert non-POD " + className( v ))); }
171 
172  template< class V >
173  bool _insert( const std::string& key, const std::vector< V >& values,
174  const boost::true_type& )
175  { return _insert( key, values.data(), values.size() * sizeof( V )); }
176 };
177 
178 template<> inline
179 bool PersistentMap::_insert( const std::string& k, const std::string& v,
180  const boost::false_type& )
181 {
182  return _insert( k, v.data(), v.length( ));
183 }
184 
185 template< class V > inline
186 std::vector< V > PersistentMap::getVector( const std::string& key )
187 {
188  const std::string& value = (*this)[ key ];
189  return std::vector< V >( reinterpret_cast< const V* >( value.data( )),
190  reinterpret_cast< const V* >( value.data() + value.size( )));
191 }
192 
193 template< class V > inline
194 std::set< V > PersistentMap::getSet( const std::string& key )
195 {
196  const std::string& value = (*this)[ key ];
197  return std::set< V >( reinterpret_cast< const V* >( value.data( )),
198  reinterpret_cast< const V* >( value.data() +
199  value.size( )));
200 }
201 
202 
203 // inline std::ostream& operator << ( std::ostream& os, const PersistentMap& m )
204 }
205 
206 #endif //LUNCHBOX_PERSISTENTMAP_H
std::set< V > getSet(const std::string &key)
Retrieve a value as a set for a key.
Defines export visibility macros for Lunchbox.
Basic type definitions not provided by the operating system.
static LUNCHBOX_API bool handles(const URI &uri)
LUNCHBOX_API PersistentMap(const std::string &uri=std::string())
Construct a new persistent map.
std::vector< V > getVector(const std::string &key)
Retrieve a value as a vector for a key.
std::string className(const T *object)
Print the RTTI name of the given class.
Definition: debug.h:74
LUNCHBOX_API ~PersistentMap()
Destruct the persistent map.
Unified interface to save key-value pairs in a persistent store.
Definition: persistentMap.h:43
bool insert(const std::string &key, const std::set< V > &values)
Insert or update a set of values in the database.
The URI class parses the given uri string according to the regex given in RFC3986.
Definition: uri.h:55
bool insert(const std::string &key, const V &value)
Insert or update a value in the database.
Definition: persistentMap.h:85
LUNCHBOX_API std::string operator[](const std::string &key) const
Retrieve a value for a key.
LUNCHBOX_API bool contains(const std::string &key) const
bool insert(const std::string &key, const std::vector< V > &values)
Insert or update a vector of values in the database.
Definition: persistentMap.h:98
This file contains logging classes.
#define LBTHROW(exc)
Log a std::exception if topic LOG_EXCEPTION is set before throwing exception.
Definition: log.h:220