Ignore:
Timestamp:
Mar 3, 2011, 12:47:46 PM (10 years ago)
Author:
tbretz
Message:
Added conversion to a vector of boost::any, added more format options for this case and added special treatment for quoted strings and bools.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/Converter.h

    r10183 r10219  
    55#include <ostream>
    66
     7#include <boost/any.hpp>
     8
    79class Converter
    810{
    911private:
    10     bool rc;
    11     std::ostream &wout;
    12     std::vector<char> data;
    13 
    14     template<class V>
    15         void Eval(int i, std::stringstream &line, std::vector<char> &vec) const;
     12    bool rc;                        /// Flag for the success of the conversion
     13    std::ostream &wout;             /// ostream to which output is redirected
     14    std::vector<char> data;         /// data storage
     15    std::vector<boost::any> vec;    /// storage for typed data
    1616
    1717    template<class T>
    18         std::string Get(const char *&data) const;
     18        void EvalImp(int i, std::stringstream &line, std::vector<char> &v, const T &val);
     19    template<class T>
     20        void Eval(int i, std::stringstream &line, std::vector<char> &v);
     21        void EvalBool(int i, std::stringstream &line, std::vector<char> &v);
     22        void EvalString(int i, std::stringstream &line, std::vector<char> &v);
     23
     24    template<class T>
     25        std::string Get(const char *&data);
    1926
    2027public:
     
    2229    Converter(std::ostream &out, const std::string &fmt, const void *d, int size);
    2330
     31    /// Returns whether the conversion was successfull
     32    bool GetRc() const { return rc; }
     33
     34    /// const Pointer to the data memory
    2435    const char *Ptr() const { return &*data.begin(); }
     36    /// non-const Pointer to the data memory (for convinience using Dim)
     37    char *Ptr() { return &*data.begin(); }
     38
     39    /// Return the size of the data memory
     40    int  Size() const { return data.size(); }
     41
     42    /// Return the data memory converted into a string (not necessarily \0-terminated)
    2543    const std::string Str() const { return std::string(&*data.begin(), data.size()); }
    26     char *Ptr() { return &*data.begin(); }
    27     int   Size() const { return data.size(); }
    28     bool  GetRc() const { return rc; }
    2944
    3045
     46    // Returns the number of entries in Converter:vec
     47    int N() const { return vec.size(); }
     48
     49    /// Checks if an entry with the given class type is available at index i in Converter::vec
     50    template <class T>
     51        bool Has(unsigned int i) const { return i<vec.size() && vec[i].type()==typeid(T); }
     52
     53    /// return the entry with the given class type at index i from Converter::vec (exception safe)
     54    template <class T>
     55        T Get(int i) const { return !Has<T>(i) ? T() : boost::any_cast<T>(vec[i]); }
     56
     57    /// return the entry with the given class type at index i from Converter::vec (throws exceptions)
     58    template <class T>
     59        T At(int i) const { return boost::any_cast<T>(vec[i]); }
     60
     61
     62    static std::vector<std::string> Regex(const std::string &expr, const std::string &line);
    3163};
    3264
Note: See TracChangeset for help on using the changeset viewer.