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

Last change on this file since 10285 was 10270, checked in by tbretz, 14 years ago
Change the interface. Now it first compiles the format string into a binary representation and any conversion can then be done later based on this.
File size: 3.8 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{
11public:
12 typedef std::pair<const std::type_info *, int> Type;
13 typedef std::pair<int, int> Offset;
14 typedef std::pair<Type, Offset> Format;
15 typedef std::vector<Format> FormatList;
16
17 struct O { };
18 struct W { };
19
20 static std::string Clean(std::string s);
21
22private:
23 std::ostream &wout; /// ostream to which output is redirected
24
25 const std::string fFormat; /// Original format string
26 const FormatList fList; /// Compiled format description
27
28 template <class T>
29 T Get(std::stringstream &line) const;
30
31 bool GetBool(std::stringstream &line) const;
32 std::string GetString(std::stringstream &line) const;
33 std::string GetStringEol(std::stringstream &line) const;
34
35 template<class T>
36 void GetBinImp(std::vector<char> &v, const T &val) const;
37 template<class T>
38 void GetBinImp(std::vector<boost::any> &v, const T &val) const;
39
40 void GetBinString(std::vector<char> &v, const std::string &val) const;
41 void GetBinString(std::vector<boost::any> &v, const std::string &val) const;
42
43 template<class T>
44 std::string GetString(const char *&data) const;
45
46 template<class T>
47 static Type GetType();
48 template<class T>
49 static Type GetVoid();
50
51 template <class T>
52 std::vector<T> Get(const std::string &str) const;
53 template <class T>
54 T Get(const void *d, int size) const;
55
56
57
58 template<class T>
59 void Add(std::string &str, const char* &ptr) const;
60 void AddString(std::string &str, const std::string &ptr) const;
61 template<class T>
62 void Add(std::vector<boost::any> &vec, const char* &ptr) const;
63 void AddString(std::vector<boost::any> &vec, const std::string &ptr) const;
64
65
66public:
67 Converter(std::ostream &out, const std::string &fmt, bool strict=true);
68 Converter(const std::string &fmt, bool strict=true);
69
70 /// @returns whether the interpreted format was valid but empty ("")
71 bool empty() const { return fList.size()==1 && fList.back().first.second==0; }
72
73 /// @returns whether the compilation was successfull
74 bool valid() const { return !fList.empty() && fList.back().first.second==0; }
75
76 /// @returns true if the compilation failed
77 bool operator!() const { return !valid(); }
78
79 const FormatList &GetList() const { return fList; }
80
81 static FormatList Compile(std::ostream &out, const std::string &fmt, bool strict=false);
82 static FormatList Compile(const std::string &fmt, bool strict=false);
83
84 std::string GetString(const void *d, int size) const;
85 std::vector<char> GetVector(const void *d, int size) const;
86 std::vector<boost::any> GetAny(const void *d, int size) const;
87
88 std::vector<boost::any> GetAny(const std::string &str) const;
89 std::vector<char> GetVector(const std::string &str) const;
90
91 static std::string GetHex(const void *d, int size);
92
93/*
94 // Returns the number of entries in Converter:vec
95 int N() const { return vec.size(); }
96
97 /// Checks if an entry with the given class type is available at index i in Converter::vec
98 template <class T>
99 bool Has(unsigned int i) const { return i<vec.size() && vec[i].type()==typeid(T); }
100
101 /// return the entry with the given class type at index i from Converter::vec (exception safe)
102 template <class T>
103 T Get(int i) const { return !Has<T>(i) ? T() : boost::any_cast<T>(vec[i]); }
104
105 /// return the entry with the given class type at index i from Converter::vec (throws exceptions)
106 template <class T>
107 T At(int i) const { return boost::any_cast<T>(vec[i]); }
108*/
109
110 static std::vector<std::string> Regex(const std::string &expr, const std::string &line);
111};
112
113#endif
Note: See TracBrowser for help on using the repository browser.