libiqxmlrpc  0.12.4
 All Classes Namespaces Files Functions Typedefs Enumerations
method.h
1 // Libiqxmlrpc - an object-oriented XML-RPC solution.
2 // Copyright (C) 2011 Anton Dedov
3 
4 #ifndef _iqxmlrpc_method_h_
5 #define _iqxmlrpc_method_h_
6 
7 #include "except.h"
8 #include "inet_addr.h"
9 #include "value.h"
10 
11 #include <boost/utility.hpp>
12 #include <boost/scoped_ptr.hpp>
13 
14 #include <map>
15 #include <string>
16 
17 namespace iqxmlrpc
18 {
19 class Server;
20 class Interceptor;
21 class Method;
23 
25 typedef std::vector<Value> Param_list;
26 
28 typedef void (*Method_function)(Method*, const Param_list&, Value&);
29 
31 class LIBIQXMLRPC_API Server_feedback {
32  Server* server_;
33 
34 public:
35  // Do not use objects constructed with default ctor!
37  server_(0) {}
38 
40  server_(s) {}
41 
42  void set_exit_flag();
43  void log_message( const std::string& );
44 };
45 
48 class LIBIQXMLRPC_API Method {
49 public:
50  struct Data {
51  std::string method_name;
52  iqnet::Inet_addr peer_addr;
53  Server_feedback server_face;
54  };
55 
56 private:
57  friend class Method_dispatcher_base;
58  Data data_;
59  std::string authname_;
60 
61 public:
62  virtual ~Method() {}
63 
66  void process_execution(Interceptor*, const Param_list& params, Value& response);
67 
68  const std::string& name() const { return data_.method_name; }
69  const iqnet::Inet_addr& peer_addr() const { return data_.peer_addr; }
70  Server_feedback& server() { return data_.server_face; }
71 
72  bool authenticated() const { return !authname_.empty(); }
73  const std::string& authname() const { return authname_; }
74  void authname(const std::string& n) { authname_ = n; }
75 
76 private:
78  virtual void execute( const Param_list& params, Value& response ) = 0;
79 };
80 
81 #ifdef _MSC_VER
82 #pragma warning(push)
83 #pragma warning(disable: 4251)
84 #pragma warning(disable: 4275)
85 #endif
86 
88 
99 class LIBIQXMLRPC_API Interceptor: boost::noncopyable {
100 public:
101  virtual ~Interceptor() {}
102 
103  void nest(Interceptor* ic)
104  {
105  nested.reset(ic);
106  }
107 
109 
113  virtual void process(Method*, const Param_list&, Value&) = 0;
114 
115 protected:
117 
120  void yield(Method* m, const Param_list& params, Value& result)
121  {
122  m->process_execution(nested.get(), params, result);
123  }
124 
125 private:
126  boost::scoped_ptr<Interceptor> nested;
127 };
128 
129 #ifdef _MSC_VER
130 #pragma warning(pop)
131 #endif
132 
135 class LIBIQXMLRPC_API Method_function_adapter: public Method {
136 public:
138  function(f) {}
139 
140 private:
141  void execute(const Param_list& params, Value& result)
142  {
143  function(this, params, result);
144  }
145 
146  Method_function function;
147 };
148 
150 
154 class LIBIQXMLRPC_API Method_factory_base {
155 public:
156  virtual ~Method_factory_base() {}
157 
158  virtual Method* create() = 0;
159 };
160 
161 
163 template <class T>
165 public:
166  T* create() { return new T(); }
167 };
168 
169 
171 template <>
173 public:
175  function(fn) {}
176 
177  Method* create() { return new Method_function_adapter(function); }
178 
179 private:
180  Method_function function;
181 };
182 
183 
185 class LIBIQXMLRPC_API Method_dispatcher_base {
186 public:
187  virtual ~Method_dispatcher_base() {}
188 
189  Method* create_method(const Method::Data& data)
190  {
191  Method *method = do_create_method(data.method_name);
192  if (method)
193  method->data_ = data;
194 
195  return method;
196  }
197 
198  void get_methods_list(Array& retval) const
199  {
200  do_get_methods_list(retval);
201  }
202 
203 private:
204  virtual Method*
205  do_create_method(const std::string&) = 0;
206 
207  virtual void
208  do_get_methods_list(Array&) const = 0;
209 };
210 
211 } // namespace iqxmlrpc
212 
213 #endif
214 // vim:ts=2:sw=2:et