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

Last change on this file since 10378 was 10364, checked in by lyard, 14 years ago
Added FactFits class
File size: 4.0 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, size_t 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 char* &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 char* &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, size_t size) const;
85 std::vector<char> GetVector(const void *d, size_t size) const;
86 std::vector<boost::any> GetAny(const void *d, size_t 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, size_t size, size_t chunk=1);
92
93 void ToFits(void* dest, const void* src, size_t size) const;
94
95 std::vector<char> ToFits(const void* src, size_t size) const;
96
97/*
98 // Returns the number of entries in Converter:vec
99 int N() const { return vec.size(); }
100
101 /// Checks if an entry with the given class type is available at index i in Converter::vec
102 template <class T>
103 bool Has(unsigned int i) const { return i<vec.size() && vec[i].type()==typeid(T); }
104
105 /// return the entry with the given class type at index i from Converter::vec (exception safe)
106 template <class T>
107 T Get(int i) const { return !Has<T>(i) ? T() : boost::any_cast<T>(vec[i]); }
108
109 /// return the entry with the given class type at index i from Converter::vec (throws exceptions)
110 template <class T>
111 T At(int i) const { return boost::any_cast<T>(vec[i]); }
112*/
113
114 static std::vector<std::string> Regex(const std::string &expr, const std::string &line);
115};
116
117#endif
Note: See TracBrowser for help on using the repository browser.