Scribus
Open source desktop publishing at your fingertips
actions.h
1 /*
2  * actions.h
3  *
4  *
5  * Created by Andreas Vox on 02.06.06.
6  * Copyright 2006 under GPL2. All rights reserved.
7  *
8  */
9 
10 
11 #ifndef ACTIONS_H
12 #define ACTIONS_H
13 
14 #include <map>
15 #include <string>
16 #include "digester.h"
17 
18 namespace desaxe {
19 
29 {
30 protected:
31  Action_body() : dig(NULL) {}
32  virtual ~Action_body() {}
33  virtual void begin(const Xml_string&, Xml_attr) {}
34 
35  virtual void end(const Xml_string&) {}
36  virtual void chars(const Xml_string&) {}
37  virtual void reset() {}
38 
39  Digester* dig;
40 private:
41  int refs;
42  friend class Action;
43 };
44 
45 
46 
59 class Action
60 {
61 public:
62  inline Digester* digester() { return body->dig; }
63  inline void setDigester(Digester* dig) { body->dig = dig; }
64 
65  inline void begin(const Xml_string& tag, Xml_attr attr)
66  { body->begin(tag, attr); }
67  inline void end(const Xml_string& tag) { body->end(tag); }
68  inline void chars(const Xml_string& data) { body->chars(data); }
69 
70  // Handle stuff:
71  Action(const Action& other)
72  {
73  body = other.body;
74  body->refs++;
75  }
76  virtual ~Action()
77  {
78  if (--body->refs == 0)
79  delete body;
80  }
81  Action& operator=(const Action& other)
82  {
83  if (body != other.body) {
84  if (--body->refs == 0)
85  delete body;
86  body = other.body;
87  ++body->refs;
88  }
89  return *this;
90  }
91 
92  virtual void reset()
93  {
94  body->reset();
95  }
96 
97 protected:
98  Action(Action_body* body_)
99  {
100  body = body_;
101  body->refs = 1;
102  }
103 
104  Action_body* body;
105 
106 private:
107  Action(); // not defined
108 };
109 
110 
111 class Dummy {};
112 
120 template <class Body, class Arg1=Dummy, class Arg2=Dummy, class Arg3=Dummy, class Arg4=Dummy, class Arg5=Dummy>
121 struct MakeAction : public Action
122 {
123  MakeAction() : Action(new Body()) {}
124  MakeAction(Arg1 a1) : Action(new Body(a1)) {}
125  MakeAction(Arg1 a1, Arg2 a2) : Action(new Body(a1, a2)) {}
126  MakeAction(Arg1 a1, Arg2 a2, Arg3 a3) : Action(new Body(a1, a2, a3)) {}
127  MakeAction(Arg1 a1, Arg2 a2, Arg3 a3, Arg4 a4) : Action(new Body(a1, a2, a3, a4)) {}
128  MakeAction(Arg1 a1, Arg2 a2, Arg3 a3, Arg4 a4, Arg5 a5) : Action(new Body(a1, a2, a3, a4, a5)) {}
129 };
130 
131 
132 
138 template<class Type>
140 {
141 public:
142  virtual Type* eval(Digester* dig_, const Xml_string& tag, Xml_attr attr)
143  {
144  dig = dig_;
145  begin(tag, attr);
146  Type* res;
147  res = dig->template top<Type>();
148  end(tag);
149  return res;
150  }
151 protected:
152  virtual void end(const Xml_string& ) { dig->pop(); }
153 };
154 
155 
156 
160 template<class Type>
161 class Generator : public Action
162 {
163 public:
164  Type* eval(Digester* dig, const Xml_string& tag, Xml_attr attr)
165  {
166  return static_cast<Generator_body<Type>*>(body)->eval(dig, tag, attr);
167  }
168 
169 protected:
170  Generator(Generator_body<Type>* body_) : Action(body_) {}
171 };
172 
173 
181 template <class Body, class Obj_Type, class Arg1=Dummy, class Arg2=Dummy, class Arg3=Dummy>
182 struct MakeGenerator : public Generator<Obj_Type>
183 {
184  MakeGenerator() : Generator<Obj_Type>(new Body()) {}
185  MakeGenerator(Arg1 a) : Generator<Obj_Type>(new Body(a)) {}
186  MakeGenerator(Arg1 a1, Arg2 a2) : Generator<Obj_Type>(new Body(a1, a2)) {}
187  MakeGenerator(Arg1 a1, Arg2 a2, Arg3 a3) : Generator<Obj_Type>(new Body(a1, a2, a3)) {}
188 };
189 
190 } // namespace
191 
192 #endif
Definition: actions.h:139
Definition: actions.h:161
Definition: actions.h:111
Definition: actions.h:28
Definition: actions.h:182
Definition: digester.h:100
Definition: actions.h:121
Definition: actions.h:59
Definition: actions.h:18