autojson  0.1
A JSON parser base on the automaton provided by json.org
 All Classes Files Functions Typedefs Enumerations Enumerator Pages
Reactor.hh
1 /*
2  autojson: A JSON parser base on the automaton provided by json.org
3  Copyright (C) 2014 Wan Wai Ho
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  as published by the Free Software Foundation version 2
8  of the License.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  02110-1301, USA.
19 */
20 
21 #ifndef REACTOR_HH_INCLUDED
22 #define REACTOR_HH_INCLUDED
23 
24 #include "JSON_checker.h"
25 #include "LexicalCast.hh"
26 
27 #include <string>
28 
29 namespace ajs {
30 
31 class ParseState;
32 
35 class Reactor
36 {
37 public:
38  virtual ParseState On(ParseState& s, JSON_event event, const char *data, std::size_t len) = 0;
39  virtual Reactor* Clone() const = 0;
40 };
41 
42 struct ParseState
43 {
44  Reactor *reactor;
45  void *dest;
46 };
47 
48 template <typename DestType, typename T>
49 class SaveToMember : public Reactor
50 {
51 public :
52  SaveToMember(T DestType::*member) : m_member(member)
53  {
54  }
55 
56  ParseState On(ParseState& s, JSON_event event, const char *data, std::size_t len) override
57  {
58  DestType *dest = reinterpret_cast<DestType*>(s.dest);
59  (dest->*m_member) = lexical_cast<T>(data, len);
60  return s;
61  }
62 
63  SaveToMember* Clone() const override
64  {
65  return new SaveToMember(m_member);
66  }
67 
68 private :
69  T DestType::*m_member;
70 };
71 
72 } // end of namespace
73 
74 #endif
Main header of the C interface of autojson.
Definition: Reactor.hh:49
JSON_event
Definition: JSON_checker.h:21
Definition: Reactor.hh:42
Definition: Reactor.hh:35