ecto
registry.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 #pragma once
30 #include <vector>
31 #include <boost/noncopyable.hpp>
32 #include <boost/function/function0.hpp>
33 #include <boost/make_shared.hpp>
34 #include <ecto/forward.hpp>
35 #include <ecto/util.hpp>
36 #include <ecto/cell.hpp>
37 
38 namespace ecto {
39 
40  namespace py {
41  void postregistration(const std::string&, const std::string&, const std::string&);
42  }
43 
44  struct cell;
45 
46  namespace registry {
47 
48  typedef boost::shared_ptr<cell>(*factory_fn_t)();
49  typedef void (*declare_params_t)(ecto::tendrils&);
50  typedef void (*declare_io_t)(const ecto::tendrils&, ecto::tendrils&,
51  ecto::tendrils&);
52 
53  struct entry_t {
57 
58  boost::shared_ptr<cell> construct_() { return construct(); }
59  void declare_params_(ecto::tendrils& t) { declare_params(t); }
60  void declare_io_(const ecto::tendrils& p, ecto::tendrils& i, ecto::tendrils& o)
61  {
62  declare_io(p, i, o);
63  }
64  };
65 
66  template <typename ModuleTag>
67  struct module_registry : boost::noncopyable
68  {
69  typedef boost::function0<void> nullary_fn_t;
70 
71  void add(nullary_fn_t f)
72  {
73  regvec.push_back(f);
74  }
75 
76  void go()
77  {
78  for (unsigned j=0; j<regvec.size(); ++j)
79  regvec[j]();
80  }
81 
82  std::vector<nullary_fn_t> regvec;
83 
85  {
86  static module_registry instance_;
87  return instance_;
88  }
89 
90  private:
91 
93  };
94 
95  template <typename Module, typename T>
96  struct registrator
97  {
98  const char* name_;
99  const char* docstring_;
100 
101  typedef ::ecto::cell_<T> cell_t;
102 
103  static boost::shared_ptr<cell> create()
104  {
105  return boost::shared_ptr<cell>(new cell_t);
106  }
107 
108  explicit registrator(const char* name, const char* docstring)
109  : name_(name), docstring_(docstring)
110  {
111  // this fires the construction of proper python classes at import time
112  module_registry<Module>::instance().add(boost::ref(*this));
113 
114  // this registers the functions needed to do the construction above
115  entry_t e;
116  e.construct = &create;// ecto::create_cell<T>;
117  e.declare_params = (void (*)(tendrils&)) &cell_t::declare_params;
118  e.declare_io = (void (*)(const tendrils&, tendrils&, tendrils&)) &cell_t::declare_io;
119  register_factory_fn(name_of<T>(), e);
120  }
121 
122  void operator()() const
123  {
124  ecto::py::postregistration(name_, docstring_, name_of<T>());
125  }
126  const static registrator& inst;
127  };
128 
129  entry_t lookup(const std::string& name);
130  boost::shared_ptr<cell> create(const std::string& name);
131  boost::shared_ptr<cell> create_initialized(const std::string& name);
132  void register_factory_fn(const std::string& name, entry_t e);
133  }
134 }
135 
136 #define ECTO_MODULETAG(MODULE) namespace ecto { namespace tag { struct MODULE; } }
137 #define ECTO_CELL(MODULE, TYPE, NAME, DOCSTRING) \
138  ECTO_ASSERT_MODULE_NAME(MODULE) \
139  ECTO_MODULETAG(MODULE) \
140  namespace ecto{ namespace registry { \
141  template<> \
142  const ::ecto::registry::registrator< ::ecto::tag::MODULE,TYPE>& \
143  ::ecto::registry::registrator< ::ecto::tag::MODULE,TYPE>::inst \
144  (::ecto::registry::registrator< ::ecto::tag::MODULE,TYPE>(NAME, DOCSTRING)); \
145  } }
146 
147 #define ECTO_INSTANTIATE_REGISTRY(MODULE) \
148  ECTO_MODULETAG(MODULE) \
149  template struct ::ecto::registry::module_registry< ::ecto::tag::MODULE>;
150 
151 #define ECTO_REGISTER(MODULE) \
152  ::ecto::registry::module_registry< ::ecto::tag::MODULE>::instance().go();
boost::shared_ptr< cell >(* factory_fn_t)()
Definition: registry.hpp:48
Definition: registry.hpp:53
void operator()() const
Definition: registry.hpp:122
static const registrator & inst
Definition: registry.hpp:126
declare_params_t declare_params
Definition: registry.hpp:55
static boost::shared_ptr< cell > create()
Definition: registry.hpp:103
declare_io_t declare_io
Definition: registry.hpp:56
factory_fn_t construct
Definition: registry.hpp:54
void register_factory_fn(const std::string &name, entry_t e)
module_registry()
Definition: registry.hpp:92
Definition: parameters.hpp:11
boost::function0< void > nullary_fn_t
Definition: registry.hpp:69
boost::shared_ptr< cell > create_initialized(const std::string &name)
void(* declare_io_t)(const ecto::tendrils &, ecto::tendrils &, ecto::tendrils &)
Definition: registry.hpp:50
void(* declare_params_t)(ecto::tendrils &)
Definition: registry.hpp:49
const char * docstring_
Definition: registry.hpp:99
void declare_params_(ecto::tendrils &t)
Definition: registry.hpp:59
void add(nullary_fn_t f)
Definition: registry.hpp:71
void declare_io_(const ecto::tendrils &p, ecto::tendrils &i, ecto::tendrils &o)
Definition: registry.hpp:60
boost::shared_ptr< cell > construct_()
Definition: registry.hpp:58
::ecto::cell_< T > cell_t
Definition: registry.hpp:101
void postregistration(const std::string &, const std::string &, const std::string &)
Definition: registry.hpp:96
const char * name_
Definition: registry.hpp:98
entry_t lookup(const std::string &name)
Definition: registry.hpp:67
std::vector< nullary_fn_t > regvec
Definition: registry.hpp:82
boost::shared_ptr< cell > create(const std::string &name)
registrator(const char *name, const char *docstring)
Definition: registry.hpp:108
The tendrils are a collection for the ecto::tendril class, addressable by a string key...
Definition: tendrils.hpp:53
void go()
Definition: registry.hpp:76
static module_registry & instance()
Definition: registry.hpp:84