18 #ifndef LUNCHBOX_PERSISTENTMAP_H
19 #define LUNCHBOX_PERSISTENTMAP_H
22 #include <lunchbox/debug.h>
25 #include <boost/noncopyable.hpp>
27 #include <boost/type_traits.hpp>
36 namespace detail {
class PersistentMap; }
59 LUNCHBOX_API
PersistentMap(
const std::string& uri = std::string( ));
74 LUNCHBOX_API
static bool handles(
const URI& uri );
85 template<
class V >
bool insert(
const std::string& key,
const V& value )
86 {
return _insert( key, value, boost::has_trivial_assign< V >( )); }
98 bool insert(
const std::string& key,
const std::vector< V >& values )
99 {
return _insert( key, values, boost::has_trivial_assign< V >( )); }
111 bool insert(
const std::string& key,
const std::set< V >& values )
112 {
return insert( key, std::vector<V>( values.begin(), values.end( ))); }
121 LUNCHBOX_API std::string
operator [] (
const std::string& key )
const;
130 template<
class V > std::vector< V >
getVector(
const std::string& key );
139 template<
class V > std::set< V >
getSet(
const std::string& key );
142 LUNCHBOX_API
bool contains(
const std::string& key )
const;
145 detail::PersistentMap*
const _impl;
147 LUNCHBOX_API
bool _insert(
const std::string& key,
const void* data,
154 template<
size_t N >
bool
155 _insert(
const std::string& k,
char const (& v)[N],
const boost::true_type&)
157 return _insert( k, (
void*)v, N - 1 );
161 bool _insert(
const std::string& k,
const V& v,
const boost::true_type& )
163 if( boost::is_pointer< V >::value )
164 LBTHROW( std::runtime_error(
"Can't insert pointers" ));
165 return _insert( k, &v,
sizeof( 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 ))); }
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 )); }
179 bool PersistentMap::_insert(
const std::string& k,
const std::string& v,
180 const boost::false_type& )
182 return _insert( k, v.data(), v.length( ));
185 template<
class V >
inline
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( )));
193 template<
class V >
inline
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() +
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.
LUNCHBOX_API ~PersistentMap()
Destruct the persistent map.
Unified interface to save key-value pairs in a persistent store.
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.
bool insert(const std::string &key, const V &value)
Insert or update a value in the database.
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.
This file contains logging classes.
#define LBTHROW(exc)
Log a std::exception if topic LOG_EXCEPTION is set before throwing exception.