123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- #ifndef _PCRECPPARG_H
- #define _PCRECPPARG_H
- #include <stdlib.h> // for NULL
- #include <string>
- #include <pcre.h>
- namespace pcrecpp {
- class StringPiece;
- template <class T>
- class _RE_MatchObject {
- public:
- static inline bool Parse(const char* str, int n, void* dest) {
- if (dest == NULL) return true;
- T* object = reinterpret_cast<T*>(dest);
- return object->ParseFrom(str, n);
- }
- };
- class PCRECPP_EXP_DEFN Arg {
- public:
-
- Arg();
-
- Arg(void*);
- typedef bool (*Parser)(const char* str, int n, void* dest);
- #define PCRE_MAKE_PARSER(type,name) \
- Arg(type* p) : arg_(p), parser_(name) { } \
- Arg(type* p, Parser parser) : arg_(p), parser_(parser) { }
- PCRE_MAKE_PARSER(char, parse_char);
- PCRE_MAKE_PARSER(unsigned char, parse_uchar);
- PCRE_MAKE_PARSER(short, parse_short);
- PCRE_MAKE_PARSER(unsigned short, parse_ushort);
- PCRE_MAKE_PARSER(int, parse_int);
- PCRE_MAKE_PARSER(unsigned int, parse_uint);
- PCRE_MAKE_PARSER(long, parse_long);
- PCRE_MAKE_PARSER(unsigned long, parse_ulong);
- #if @pcre_have_long_long@
- PCRE_MAKE_PARSER(long long, parse_longlong);
- #endif
- #if @pcre_have_ulong_long@
- PCRE_MAKE_PARSER(unsigned long long, parse_ulonglong);
- #endif
- PCRE_MAKE_PARSER(float, parse_float);
- PCRE_MAKE_PARSER(double, parse_double);
- PCRE_MAKE_PARSER(std::string, parse_string);
- PCRE_MAKE_PARSER(StringPiece, parse_stringpiece);
- #undef PCRE_MAKE_PARSER
-
- template <class T> Arg(T*, Parser parser);
-
- template <class T> Arg(T* p)
- : arg_(p), parser_(_RE_MatchObject<T>::Parse) {
- }
-
- bool Parse(const char* str, int n) const;
- private:
- void* arg_;
- Parser parser_;
- static bool parse_null (const char* str, int n, void* dest);
- static bool parse_char (const char* str, int n, void* dest);
- static bool parse_uchar (const char* str, int n, void* dest);
- static bool parse_float (const char* str, int n, void* dest);
- static bool parse_double (const char* str, int n, void* dest);
- static bool parse_string (const char* str, int n, void* dest);
- static bool parse_stringpiece (const char* str, int n, void* dest);
- #define PCRE_DECLARE_INTEGER_PARSER(name) \
- private: \
- static bool parse_ ## name(const char* str, int n, void* dest); \
- static bool parse_ ## name ## _radix( \
- const char* str, int n, void* dest, int radix); \
- public: \
- static bool parse_ ## name ## _hex(const char* str, int n, void* dest); \
- static bool parse_ ## name ## _octal(const char* str, int n, void* dest); \
- static bool parse_ ## name ## _cradix(const char* str, int n, void* dest)
- PCRE_DECLARE_INTEGER_PARSER(short);
- PCRE_DECLARE_INTEGER_PARSER(ushort);
- PCRE_DECLARE_INTEGER_PARSER(int);
- PCRE_DECLARE_INTEGER_PARSER(uint);
- PCRE_DECLARE_INTEGER_PARSER(long);
- PCRE_DECLARE_INTEGER_PARSER(ulong);
- PCRE_DECLARE_INTEGER_PARSER(longlong);
- PCRE_DECLARE_INTEGER_PARSER(ulonglong);
- #undef PCRE_DECLARE_INTEGER_PARSER
- };
- inline Arg::Arg() : arg_(NULL), parser_(parse_null) { }
- inline Arg::Arg(void* p) : arg_(p), parser_(parse_null) { }
- inline bool Arg::Parse(const char* str, int n) const {
- return (*parser_)(str, n, arg_);
- }
- #define MAKE_INTEGER_PARSER(type, name) \
- inline Arg Hex(type* ptr) { \
- return Arg(ptr, Arg::parse_ ## name ## _hex); } \
- inline Arg Octal(type* ptr) { \
- return Arg(ptr, Arg::parse_ ## name ## _octal); } \
- inline Arg CRadix(type* ptr) { \
- return Arg(ptr, Arg::parse_ ## name ## _cradix); }
- MAKE_INTEGER_PARSER(short, short)
- MAKE_INTEGER_PARSER(unsigned short, ushort)
- MAKE_INTEGER_PARSER(int, int)
- MAKE_INTEGER_PARSER(unsigned int, uint)
- MAKE_INTEGER_PARSER(long, long)
- MAKE_INTEGER_PARSER(unsigned long, ulong)
- #if @pcre_have_long_long@
- MAKE_INTEGER_PARSER(long long, longlong)
- #endif
- #if @pcre_have_ulong_long@
- MAKE_INTEGER_PARSER(unsigned long long, ulonglong)
- #endif
- #undef PCRE_IS_SET
- #undef PCRE_SET_OR_CLEAR
- #undef MAKE_INTEGER_PARSER
- }
- #endif
|