source: trunk/FACT++/src/Converter.h @ 10219

Last change on this file since 10219 was 10219, checked in by tbretz, 10 years ago
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 size: 2.3 KB
Line 
1#ifndef FACT_Converter
2#define FACT_Converter
3
4#include <vector>
5#include <ostream>
6
7#include <boost/any.hpp>
8
9class Converter
10{
11private:
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
16
17    template<class T>
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);
26
27public:
28    Converter(std::ostream &out, const std::string &fmt, const std::string &str);
29    Converter(std::ostream &out, const std::string &fmt, const void *d, int size);
30
31    /// Returns whether the conversion was successfull
32    bool GetRc() const { return rc; }
33
34    /// const Pointer to the data memory
35    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)
43    const std::string Str() const { return std::string(&*data.begin(), data.size()); }
44
45
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);
63};
64
65#endif
Note: See TracBrowser for help on using the repository browser.