ecpp.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003-2005 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 
30 #ifndef TNT_ECPP_H
31 #define TNT_ECPP_H
32 
33 #include <tnt/component.h>
34 #include <tnt/componentfactory.h>
35 #include <tnt/scope.h>
36 #include <tnt/sessionscope.h>
37 #include <tnt/httprequest.h>
38 #include <tnt/http.h>
39 #include <map>
40 #include <set>
41 
42 namespace tnt
43 {
44  class Urlmapper;
45  class Comploader;
46  class EcppSubComponent;
47 
49  // Subcompident
50  //
51  struct Subcompident : public tnt::Compident
52  {
53  std::string subname;
54 
55  Subcompident(const tnt::Compident& ci, const std::string& sub)
56  : tnt::Compident(ci),
57  subname(sub)
58  { }
59  Subcompident(const std::string& lib, const std::string& comp, const std::string& sub)
60  : tnt::Compident(lib, comp),
61  subname(sub)
62  { }
63 
64  explicit Subcompident(const std::string& ident);
65  std::string toString() const;
66  };
67 
69  // EcppComponent
70  //
71  class EcppComponent : public Component
72  {
73  friend class EcppSubComponent;
74 
75  typedef std::map<std::string, EcppSubComponent*> subcomps_type;
76  typedef std::set<Compident> compnotfound_type;
77 
78  Compident _myident;
79  const Urlmapper& _rootmapper;
80  Comploader& _loader;
81  subcomps_type _subcomps;
82 
83  virtual subcomps_type& getSubcomps() { return _subcomps; }
84  virtual const subcomps_type& getSubcomps() const { return _subcomps; }
85 
86  protected:
87  virtual ~EcppComponent();
88 
89  void registerSubComp(const std::string& name, EcppSubComponent* comp);
90 
91  Component& fetchComp(const std::string& url) const;
92  Component& fetchComp(const Compident& ci) const;
93  Component& fetchComp(const Subcompident& ci) const;
94  Component* createComp(const Compident& ci) const;
95  Component* createComp(const std::string& url) const
96  { return createComp(Compident(url)); }
97 
99  template <typename compident_type,
100  typename parameter1_type,
101  typename parameter2_type>
102  unsigned callComp(const compident_type& ci, HttpRequest& request,
103  parameter1_type& p1, parameter2_type& p2)
104  { return fetchComp(ci).call(request, p1, p2); }
105 
106  template <typename compident_type,
107  typename parameter_type>
108  unsigned callComp(const compident_type& ci, HttpRequest& request,
109  parameter_type& p1)
110  { return fetchComp(ci).call(request, p1); }
111 
112  template <typename compident_type>
113  unsigned callComp(const compident_type& ci, HttpRequest& request)
114  { return fetchComp(ci).call(request); }
115 
117  template <typename compident_type,
118  typename parameter1_type>
119  std::string scallComp(const compident_type& ci, HttpRequest& request,
120  parameter1_type& p1)
121  { return fetchComp(ci).scall(request, p1); }
122 
123  template <typename compident_type>
124  std::string scallComp(const compident_type& ci, HttpRequest& request)
125  { return fetchComp(ci).scall(request); }
126 
127  const char* getData(const HttpRequest& request, const char* def) const;
128 
129  public:
130  EcppComponent(const Compident& ci, const Urlmapper& um, Comploader& cl);
131 
132  const Compident& getCompident() const { return _myident; }
133 
134  EcppSubComponent& fetchSubComp(const std::string& sub) const;
135 
137  template <typename parameter1_type,
138  typename parameter2_type>
139  unsigned callSubComp(const std::string& sub, HttpRequest& request,
140  parameter1_type& p1, parameter2_type& p2) const;
141 
142  template <typename parameter1_type>
143  unsigned callSubComp(const std::string& sub, HttpRequest& request,
144  parameter1_type& p1) const;
145 
147  template <typename parameter1_type>
148  std::string scallSubComp(const std::string& sub, HttpRequest& request,
149  parameter1_type& p1) const;
150  };
151 
153  // EcppSubComponent
154  //
156  {
157  EcppComponent& _main;
158  std::string _subcompname;
159 
160  virtual subcomps_type& getSubcomps()
161  { return _main.getSubcomps(); }
162  virtual const subcomps_type& getSubcomps() const
163  { return _main.getSubcomps(); }
164 
165  public:
166  EcppSubComponent(EcppComponent& p, const std::string& name)
167  : EcppComponent(p._myident, p._rootmapper, p._loader),
168  _main(p),
169  _subcompname(name)
170  {
171  p.registerSubComp(name, this);
172  }
173 
174  virtual void drop();
176  { return Subcompident(_main.getCompident(), _subcompname); }
177 
178  EcppComponent& getMainComponent() const { return _main; }
179  };
180 
182  // inline methods
183  //
185  {
186  return dynamic_cast<EcppComponent&>(
187  fetchComp( static_cast<const Compident&>(ci) )
188  )
189  .fetchSubComp(ci.subname);
190  }
191 
192  template <typename parameter1_type,
193  typename parameter2_type>
195  const std::string& sub,
196  HttpRequest& request,
197  parameter1_type& p1,
198  parameter2_type& p2) const
199  { return fetchSubComp(sub).call(request, p1, p2); }
200 
201  template <typename parameter1_type>
203  const std::string& sub,
204  HttpRequest& request,
205  parameter1_type& p1) const
206  { return fetchSubComp(sub).call(request, p1); }
207 
209  template <typename parameter1_type>
211  const std::string& sub,
212  HttpRequest& request,
213  parameter1_type& p1) const
214  { return fetchSubComp(sub).scall(request, p1); }
215 
216  template <typename ComponentType>
218  {
219  public:
220  explicit EcppComponentFactoryImpl(const std::string& componentName)
221  : ComponentFactory(componentName)
222  { }
223 
224  virtual Component* doCreate(const tnt::Compident& ci,
225  const tnt::Urlmapper& um, tnt::Comploader& cl)
226  {
227  return new ComponentType(ci, um, cl);
228  }
229  };
230 }
231 
232 #endif // TNT_ECPP_H
233