include/dmlite/cpp/utils/extensible.h

Go to the documentation of this file.
00001 /// @file   include/dmlite/cpp/utils/extensible.h
00002 /// @brief  Extensible types (hold metadata).
00003 /// @author Alejandro Álvarez Ayllón <aalvarez@cern.ch>
00004 #ifndef DMLITE_CPP_UTILS_EXTENSIBLE_H
00005 #define DMLITE_CPP_UTILS_EXTENSIBLE_H
00006 
00007 #include <stdint.h>
00008 #include <boost/any.hpp>
00009 #include <boost/property_tree/ptree.hpp>
00010 #include <dmlite/common/errno.h>
00011 #include <dmlite/cpp/exceptions.h>
00012 #include <map>
00013 #include <stdexcept>
00014 #include <string>
00015 #include <vector>
00016 
00017 namespace dmlite {
00018   
00019   /// Helpful typedef for KeyValue containers
00020   struct Extensible {
00021    private:
00022      typedef std::pair<std::string, boost::any> EntryType_;
00023      typedef std::vector<EntryType_> DictType_;
00024      DictType_ dictionary_;
00025      
00026      void populate(const boost::property_tree::ptree& root);
00027      
00028    public:
00029     /// Converts an any to a boolean, casting if needed.
00030     static bool        anyToBoolean (const boost::any& any);
00031     /// Converts an any to an unsigned, casting if needed.
00032     static unsigned    anyToUnsigned(const boost::any& any);
00033     /// Converts an any to a long, casting if needed.
00034     static long        anyToLong    (const boost::any& any);
00035     /// Converts an any to a double, casting if needed.
00036     static double      anyToDouble  (const boost::any& any);
00037     /// Converts an any to a string, casting if needed.
00038     static std::string anyToString  (const boost::any& any);
00039     /// Converts an any to a int64_t
00040     static int64_t      anyToS64    (const boost::any& any);
00041     /// Converts an any to a uint64_t
00042     static uint64_t     anyToU64    (const boost::any& any);
00043      
00044     /// Returns true if there is a field name "key".
00045     bool hasField(const std::string& key) const;
00046      
00047     /// Returns a reference to the value associated with "key".
00048     /// Will throw DmException(DM_INVALID_VALUE,...) when not found.
00049     const boost::any& operator [] (const std::string& key) const throw (DmException);
00050      
00051     /// Returns a modifiable reference to the value associated with "key".
00052     /// Will create the entry if it does not exist.
00053     boost::any& operator [] (const std::string& key);
00054      
00055     // Comparison operators. Containers may need them.
00056     bool operator == (const Extensible&) const;
00057     bool operator != (const Extensible&) const;
00058     bool operator >  (const Extensible&) const;
00059     bool operator <  (const Extensible&) const;
00060      
00061     /// Number of elements inside this Extensible.
00062     unsigned long size() const;
00063      
00064     /// Removes all the content.
00065     void clear();
00066      
00067     /// Copies the content from another Extensible
00068     /// Note: This will call clear first!
00069     void copy(const Extensible& s);
00070      
00071     /// Removes an entry.
00072     void erase(const std::string&);
00073      
00074     /// Serializes to JSON. In principle, it only supports POD.
00075     std::string serialize(void) const;
00076      
00077     /// Deserializes from a JSON string.
00078     void deserialize(const std::string& serial) throw (DmException);
00079      
00080     /// Get all the keys available
00081     std::vector<std::string> getKeys(void) const throw (DmException);
00082      
00083     /// Gets a boolean. May be able to perform some conversions.
00084     bool getBool(const std::string& key, bool defaultValue = false) const throw (DmException);
00085      
00086     /// Gets an integer. May be able to perform some conversions.
00087     long getLong(const std::string& key, long defaultValue = 0) const throw (DmException);
00088      
00089     /// Gets an unsigned integer. May be able to perform some conversions.
00090     unsigned long getUnsigned(const std::string& key, unsigned long defaultValue = 0) const throw (DmException);
00091      
00092     /// Gets a float. May be able to perform some conversions.
00093     double getDouble(const std::string& key, double defaultValue = 0)  const throw (DmException);
00094 
00095     /// Gets a signed 64 bits type
00096     int64_t getS64(const std::string& key, int64_t defaultValue = 0) const throw (DmException);
00097 
00098     /// Gets an unsigned 64 bits type
00099     uint64_t getU64(const std::string& key, uint64_t defaultValue = 0) const throw (DmException);
00100      
00101     /// Gets a string. May perform some conversions.
00102     std::string getString(const std::string& key, const std::string& defaultValue = "") const throw (DmException);
00103      
00104     /// Gets a nested dictionary.
00105     Extensible getExtensible(const std::string& key,
00106                              const Extensible& defaultValue = Extensible()) const throw (DmException);
00107      
00108     /// Gets an array.
00109     std::vector<boost::any> getVector(const std::string& key,
00110                                       const std::vector<boost::any>& defaultValue = std::vector<boost::any>()) const throw (DmException);
00111 
00112     /// Iterators
00113     typedef DictType_::const_iterator const_iterator;
00114 
00115     const_iterator begin() const { return dictionary_.begin(); }
00116     const_iterator end()   const { return dictionary_.end(); }
00117   };
00118 
00119 };
00120 
00121 #endif // DMLITE_CPP_UTILS_TYPES_H

Generated on 28 Apr 2014 for dmlite by  doxygen 1.4.7