Ignore:
Timestamp:
Mar 30, 2011, 3:41:11 PM (10 years ago)
Author:
tbretz
Message:
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:
1 edited

Legend:

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

    r10219 r10270  
    99class Converter
    1010{
     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
    1122private:
    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
     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;
    1634
    1735    template<class T>
    18         void EvalImp(int i, std::stringstream &line, std::vector<char> &v, const T &val);
     36        void GetBinImp(std::vector<char> &v, const T &val) const;
    1937    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);
     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;
    2342
    2443    template<class T>
    25         std::string Get(const char *&data);
     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
    2665
    2766public:
    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);
     67    Converter(std::ostream &out, const std::string &fmt, bool strict=true);
     68    Converter(const std::string &fmt, bool strict=true);
    3069
    31     /// Returns whether the conversion was successfull
    32     bool GetRc() const { return rc; }
     70    /// @returns whether the interpreted format was valid but empty ("")
     71    bool empty() const { return fList.size()==1 && fList.back().first.second==0; }
    3372
    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(); }
     73    /// @returns whether the compilation was successfull
     74    bool valid() const { return !fList.empty() && fList.back().first.second==0; }
    3875
    39     /// Return the size of the data memory
    40     int  Size() const { return data.size(); }
     76    /// @returns true if the compilation failed
     77    bool operator!() const { return !valid(); }
    4178
    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()); }
     79    const FormatList &GetList() const { return fList; }
    4480
     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);
    4583
     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/*
    4694    // Returns the number of entries in Converter:vec
    4795    int N() const { return vec.size(); }
     
    58106    template <class T>
    59107        T At(int i) const { return boost::any_cast<T>(vec[i]); }
    60 
     108*/
    61109
    62110    static std::vector<std::string> Regex(const std::string &expr, const std::string &line);
Note: See TracChangeset for help on using the changeset viewer.