dmlite 0.6
extensible.h
Go to the documentation of this file.
1/// @file include/dmlite/cpp/utils/extensible.h
2/// @brief Extensible types (hold metadata).
3/// @author Alejandro Álvarez Ayllón <aalvarez@cern.ch>
4#ifndef DMLITE_CPP_UTILS_EXTENSIBLE_H
5#define DMLITE_CPP_UTILS_EXTENSIBLE_H
6
7#include <stdint.h>
8#include <boost/any.hpp>
9#include <boost/property_tree/ptree.hpp>
10#include <dmlite/common/errno.h>
12#include <map>
13#include <stdexcept>
14#include <string>
15#include <vector>
16
17namespace dmlite {
18
19 /// Helpful typedef for KeyValue containers
20 class Extensible {
21 private:
22 typedef std::pair<std::string, boost::any> EntryType_;
23 typedef std::vector<EntryType_> DictType_;
25
26 void populate(const boost::property_tree::ptree& root);
27
28 public:
29 /// Converts an any to a boolean, casting if needed.
30 static bool anyToBoolean (const boost::any& any);
31 /// Converts an any to an unsigned, casting if needed.
32 static unsigned anyToUnsigned(const boost::any& any);
33 /// Converts an any to a long, casting if needed.
34 static long anyToLong (const boost::any& any);
35 /// Converts an any to a double, casting if needed.
36 static double anyToDouble (const boost::any& any);
37 /// Converts an any to a string, casting if needed.
38 static std::string anyToString (const boost::any& any);
39 /// Converts an any to a int64_t
40 static int64_t anyToS64 (const boost::any& any);
41 /// Converts an any to a uint64_t
42 static uint64_t anyToU64 (const boost::any& any);
43
44 /// Returns true if there is a field name "key".
45 bool hasField(const std::string& key) const;
46
47 /// Returns a reference to the value associated with "key".
48 /// Will throw DmException(DM_INVALID_VALUE,...) when not found.
49 const boost::any& operator [] (const std::string& key) const ;
50
51 /// Returns a modifiable reference to the value associated with "key".
52 /// Will create the entry if it does not exist.
53 boost::any& operator [] (const std::string& key);
54
55 // Comparison operators. Containers may need them.
56 bool operator == (const Extensible&) const;
57 bool operator != (const Extensible&) const;
58 bool operator > (const Extensible&) const;
59 bool operator < (const Extensible&) const;
60
61 /// Number of elements inside this Extensible.
62 unsigned long size() const;
63
64 /// Removes all the content.
65 void clear();
66
67 /// Copies the content from another Extensible
68 /// Note: This will call clear first!
69 void copy(const Extensible& s);
70
71 /// Removes an entry.
72 void erase(const std::string&);
73
74 /// Serializes to JSON. In principle, it only supports POD.
75 std::string serialize(void) const;
76
77 /// Deserializes from a JSON string.
78 void deserialize(const std::string& serial) ;
79
80 /// Get all the keys available
81 std::vector<std::string> getKeys(void) const ;
82
83 /// Gets a boolean. May be able to perform some conversions.
84 bool getBool(const std::string& key, bool defaultValue = false) const ;
85
86 /// Gets an integer. May be able to perform some conversions.
87 long getLong(const std::string& key, long defaultValue = 0) const ;
88
89 /// Gets an unsigned integer. May be able to perform some conversions.
90 unsigned long getUnsigned(const std::string& key, unsigned long defaultValue = 0) const ;
91
92 /// Gets a float. May be able to perform some conversions.
93 double getDouble(const std::string& key, double defaultValue = 0) const ;
94
95 /// Gets a signed 64 bits type
96 int64_t getS64(const std::string& key, int64_t defaultValue = 0) const ;
97
98 /// Gets an unsigned 64 bits type
99 uint64_t getU64(const std::string& key, uint64_t defaultValue = 0) const ;
100
101 /// Gets a string. May perform some conversions.
102 std::string getString(const std::string& key, const std::string& defaultValue = "") const ;
103
104 /// Gets a nested dictionary.
105 Extensible getExtensible(const std::string& key,
106 const Extensible& defaultValue = Extensible()) const ;
107
108 /// Gets an array.
109 std::vector<boost::any> getVector(const std::string& key,
110 const std::vector<boost::any>& defaultValue = std::vector<boost::any>()) const ;
111
112 /// Iterators
113 typedef DictType_::const_iterator const_iterator;
114
115 const_iterator begin() const { return dictionary_.begin(); }
116 const_iterator end() const { return dictionary_.end(); }
117
118 };
119
120};
121
122#endif // DMLITE_CPP_UTILS_TYPES_H
Helpful typedef for KeyValue containers.
Definition: extensible.h:20
long getLong(const std::string &key, long defaultValue=0) const
Gets an integer. May be able to perform some conversions.
const_iterator end() const
Definition: extensible.h:116
std::vector< std::string > getKeys(void) const
Get all the keys available.
std::string serialize(void) const
Serializes to JSON. In principle, it only supports POD.
double getDouble(const std::string &key, double defaultValue=0) const
Gets a float. May be able to perform some conversions.
bool operator>(const Extensible &) const
bool operator==(const Extensible &) const
static unsigned anyToUnsigned(const boost::any &any)
Converts an any to an unsigned, casting if needed.
void erase(const std::string &)
Removes an entry.
static int64_t anyToS64(const boost::any &any)
Converts an any to a int64_t.
bool getBool(const std::string &key, bool defaultValue=false) const
Gets a boolean. May be able to perform some conversions.
bool operator<(const Extensible &) const
DictType_ dictionary_
Definition: extensible.h:24
bool operator!=(const Extensible &) const
void copy(const Extensible &s)
static bool anyToBoolean(const boost::any &any)
Converts an any to a boolean, casting if needed.
void populate(const boost::property_tree::ptree &root)
unsigned long size() const
Number of elements inside this Extensible.
uint64_t getU64(const std::string &key, uint64_t defaultValue=0) const
Gets an unsigned 64 bits type.
static double anyToDouble(const boost::any &any)
Converts an any to a double, casting if needed.
Extensible getExtensible(const std::string &key, const Extensible &defaultValue=Extensible()) const
Gets a nested dictionary.
unsigned long getUnsigned(const std::string &key, unsigned long defaultValue=0) const
Gets an unsigned integer. May be able to perform some conversions.
const_iterator begin() const
Definition: extensible.h:115
std::vector< boost::any > getVector(const std::string &key, const std::vector< boost::any > &defaultValue=std::vector< boost::any >()) const
Gets an array.
const boost::any & operator[](const std::string &key) const
void clear()
Removes all the content.
static long anyToLong(const boost::any &any)
Converts an any to a long, casting if needed.
int64_t getS64(const std::string &key, int64_t defaultValue=0) const
Gets a signed 64 bits type.
bool hasField(const std::string &key) const
Returns true if there is a field name "key".
std::pair< std::string, boost::any > EntryType_
Definition: extensible.h:22
void deserialize(const std::string &serial)
Deserializes from a JSON string.
std::vector< EntryType_ > DictType_
Definition: extensible.h:23
static uint64_t anyToU64(const boost::any &any)
Converts an any to a uint64_t.
DictType_::const_iterator const_iterator
Iterators.
Definition: extensible.h:113
std::string getString(const std::string &key, const std::string &defaultValue="") const
Gets a string. May perform some conversions.
static std::string anyToString(const boost::any &any)
Converts an any to a string, casting if needed.
Error codes.
Exceptions used by the API.
Namespace for the dmlite C++ API.
Definition: authn.h:16