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 <boost/any.hpp>
00008 #include <boost/property_tree/ptree.hpp>
00009 #include <dmlite/common/errno.h>
00010 #include <dmlite/cpp/exceptions.h>
00011 #include <map>
00012 #include <stdexcept>
00013 #include <string>
00014 #include <vector>
00015 
00016 namespace dmlite {
00017   
00018   /// Helpful typedef for KeyValue containers
00019   struct Extensible {
00020    private:
00021      typedef std::map<std::string, boost::any> DictType_;
00022      DictType_ dictionary_;
00023      
00024      void populate(const boost::property_tree::ptree& root);
00025      
00026    public:
00027     /// Converts an any to a boolean, casting if needed.
00028     static bool        anyToBoolean (const boost::any& any);
00029     /// Converts an any to an unsigned, casting if needed.
00030     static unsigned    anyToUnsigned(const boost::any& any);
00031     /// Converts an any to a long, casting if needed.
00032     static long        anyToLong    (const boost::any& any);
00033     /// Converts an any to a double, casting if needed.
00034     static double      anyToDouble  (const boost::any& any);
00035     /// Converts an any to a string, casting if needed.
00036     static std::string anyToString  (const boost::any& any);    
00037      
00038      /// Returns true if there is a field name "key".
00039      bool hasField(const std::string& key) const;
00040      
00041      /// Returns a reference to the value associated with "key".
00042      /// Will throw DmException(DM_INVALID_VALUE,...) when not found.
00043      const boost::any& operator [] (const std::string& key) const throw (DmException);
00044      
00045      /// Returns a modifiable reference to the value associated with "key".
00046      /// Will create the entry if it does not exist.
00047      boost::any& operator [] (const std::string& key);
00048      
00049      // Comparison operators. Containers may need them.
00050      bool operator == (const Extensible&) const;
00051      bool operator != (const Extensible&) const;
00052      bool operator >  (const Extensible&) const;
00053      bool operator <  (const Extensible&) const;
00054      
00055      /// Number of elements inside this Extensible.
00056      unsigned long size() const;
00057      
00058      /// Removes all the content.
00059      void clear();
00060      
00061      /// Copies the content from another Extensible
00062      void copy(const Extensible& s);
00063      
00064      /// Removes an entry.
00065      void erase(const std::string&);
00066      
00067      /// Serializes to JSON. In principle, it only supports POD.
00068      std::string serialize(void) const;
00069      
00070      /// Deserializes from a JSON string.
00071      void deserialize(const std::string& serial) throw (DmException);
00072      
00073      /// Get all the keys available
00074      std::vector<std::string> getKeys(void) const throw (DmException);
00075      
00076      /// Gets a boolean. May be able to perform some conversions.
00077      bool getBool(const std::string& key) const throw (DmException);
00078      
00079      /// Gets an integer. May be able to perform some conversions.
00080      long getLong(const std::string& key) const throw (DmException);
00081      
00082      /// Gets an unsigned integer. May be able to perform some conversions.
00083      unsigned long getUnsigned(const std::string& key) const throw (DmException);
00084      
00085      /// Gets a float. May be able to perform some conversions.
00086      double getDouble(const std::string& key)  const throw (DmException);
00087      
00088      /// Gets a string. May perform some conversions.
00089      std::string getString(const std::string& key) const throw (DmException);
00090      
00091      /// Gets a nested dictionary.
00092      Extensible getExtensible(const std::string& key) const throw (DmException);
00093      
00094      /// Gets an array.
00095      std::vector<boost::any> getVector(const std::string& key) const throw (DmException);
00096   };
00097 
00098 };
00099 
00100 #endif // DMLITE_CPP_UTILS_TYPES_H

Generated on 3 Mar 2013 for dmlite by  doxygen 1.4.7