ecto
tendrils.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 <boost/thread.hpp>
31 #include <ecto/tendril.hpp>
32 #include <ecto/spore.hpp>
33 #include <boost/thread.hpp>
34 #define BOOST_SIGNALS2_MAX_ARGS 3
35 #include <boost/signals2.hpp>
36 #include <string>
37 #include <sstream>
38 #include <cstring>
39 #include <map>
40 #include <stdexcept>
41 
42 namespace ecto
43 {
45  {
47  };
48 
49 
53  class ECTO_EXPORT tendrils : boost::noncopyable
54  {
55  public:
56 
57  typedef std::map<std::string, tendril_ptr> storage_type;
58 
59  typedef storage_type::iterator iterator;
60  typedef storage_type::const_iterator const_iterator;
61  typedef storage_type::value_type value_type;
62  typedef storage_type::key_type key_type;
63  typedef storage_type::size_type size_type;
64  typedef storage_type::difference_type difference_type;
65  typedef storage_type::key_compare key_compare;
66 
67  tendrils();
68 
69  iterator begin() { return storage.begin(); }
70  const_iterator begin() const { return storage.begin(); }
71  iterator end() { return storage.end(); }
72  const_iterator end() const { return storage.end(); }
73 
74  iterator find(const std::string& name) { return storage.find(name); }
75  const_iterator find(const std::string& name) const { return storage.find(name); }
76 
77  void clear() { storage.clear(); }
78 
79  size_type size() const { return storage.size(); }
80  size_type count(const std::string& name) const { return storage.count(name); }
81 
82  void erase(iterator pos) { storage.erase(pos); }
83  void erase(const key_type& k) { storage.erase(k); }
84 
85  template <typename InputIterator>
86  void
87  insert(InputIterator first, InputIterator last)
88  {
89  storage.insert(first, last);
90  }
91 
92  std::pair<iterator, bool> insert(const value_type &v)
93  {
94  return storage.insert(v);
95  }
96 
97  key_compare key_comp() const { return storage.key_comp(); }
98 
99 
106  template<typename T>
107  spore<T>
108  declare(const std::string& name)
109  {
110  tendril_ptr t(make_tendril<T>());
111  return declare(name, t);
112  }
113 
120  template<typename T>
121  spore<T>
122  declare(const std::string& name, const std::string& doc)
123  {
124  return declare<T>(name).set_doc(doc);
125  }
126 
134  template<typename T>
135  spore<T>
136  declare(const std::string& name, const std::string& doc, const T& default_val)
137  {
138  return declare<T>(name, doc).set_default_val(default_val);
139  }
140 
157  template<typename T, typename CellImpl>
158  spore<T>
159  declare(spore<T> CellImpl::* ptm, const std::string& name, const std::string& doc = "",
160  const typename spore<T>::value_type& default_val = typename spore<T>::value_type())
161  {
162  sig_t::extended_slot_type slot(spore_assign(ptm,name),_1,_2,_3);
163  static_bindings_.connect_extended(slot);
164  return declare<T>(name,doc,default_val);
165  }
166 
167  template <typename Impl>
168  void realize_potential(Impl* cookie)
169  {
170  static_bindings_(cookie,this);
171  }
172 
180  declare(const std::string& name, tendril_ptr t);
181 
188  template<typename T>
189  T&
190  get(const std::string& name) const
191  {
192  const_iterator iter = storage.find(name);
193  try
194  {
195  if (iter == end())
196  doesnt_exist(name);
197  return iter->second->get<T>();
198  } catch (except::TypeMismatch& e)
199  {
200  e << except::actualtype_hint(iter->first)
201  << except::tendril_key(name)
202  ;
203  throw;
204  }
205  }
206 
213  const tendril_ptr& operator[](const std::string& name) const;
214 
215  tendril_ptr& operator[](const std::string& name);
216 
222  void
223  print_doc(std::ostream& out, const std::string& tendrils_name) const;
224 
225  template <class Archive>
226  void serialize(Archive& ar, const unsigned int);
227 
228  private:
229 
230  void doesnt_exist(const std::string& name) const;
231  tendrils(const tendrils&);
232 
233  storage_type storage;
234  typedef boost::signals2::signal<void(void*, const tendrils*)> sig_t;
236 
237  };
238 
242  template<typename CellImpl, typename T>
244  {
245  typedef spore<T> CellImpl::* PtrToMember; // the member pointer type
246  typedef void result_type; //result type for function object
247 
248  spore_assign_impl(PtrToMember member, const std::string& key)
249  :
250  member_(member),
251  key_(key)
252  {
253  }
254 
255  void
256  operator()(const boost::signals2::connection &conn, void* vthiz, const tendrils* tdls) const
257  {
258  conn.disconnect();//this is a one time callback.
259  CellImpl* thiz = static_cast<CellImpl*>(vthiz);
260  (*thiz).*member_ = (*tdls)[key_];
261  }
262 
264  std::string key_;
265  };
266 
267 
275  template<typename CellImpl, typename T>
277  spore_assign(spore<T> CellImpl::* ptm, const std::string& name)
278  {
279  return spore_assign_impl<CellImpl, T>(ptm, name);
280  }
281 }
The spore is a typed handle for tendrils, making holding onto tendrils a bit easier.
Definition: spore.hpp:45
spore_assign_impl(PtrToMember member, const std::string &key)
Definition: tendrils.hpp:248
spore< T > declare(spore< T > CellImpl::*ptm, const std::string &name, const std::string &doc="", const typename spore< T >::value_type &default_val=typename spore< T >::value_type())
Definition: tendrils.hpp:159
Definition: tendrils.hpp:46
std::map< std::string, tendril_ptr > storage_type
Definition: tendrils.hpp:57
storage_type::key_type key_type
Definition: tendrils.hpp:62
iterator find(const std::string &name)
Definition: tendrils.hpp:74
void realize_potential(Impl *cookie)
Definition: tendrils.hpp:168
#define ECTO_EXPORT
Definition: util.hpp:49
spore< T > declare(const std::string &name, const std::string &doc, const T &default_val)
Definition: tendrils.hpp:136
Definition: parameters.hpp:11
iterator end()
Definition: tendrils.hpp:71
storage_type::iterator iterator
Definition: tendrils.hpp:59
std::pair< iterator, bool > insert(const value_type &v)
Definition: tendrils.hpp:92
void erase(const key_type &k)
Definition: tendrils.hpp:83
spore< T > CellImpl::* PtrToMember
Definition: tendrils.hpp:245
boost::signals2::signal< void(void *, const tendrils *)> sig_t
Definition: tendrils.hpp:234
size_type count(const std::string &name) const
Definition: tendrils.hpp:80
T value_type
Definition: spore.hpp:48
tendril_type
Definition: tendrils.hpp:44
PtrToMember member_
Definition: tendrils.hpp:263
sig_t static_bindings_
Definition: tendrils.hpp:235
storage_type storage
Definition: tendrils.hpp:233
storage_type::const_iterator const_iterator
Definition: tendrils.hpp:60
boost::shared_ptr< tendril > tendril_ptr
Definition: forward.hpp:34
iterator begin()
Definition: tendrils.hpp:69
void insert(InputIterator first, InputIterator last)
Definition: tendrils.hpp:87
key_compare key_comp() const
Definition: tendrils.hpp:97
const_iterator begin() const
Definition: tendrils.hpp:70
void erase(iterator pos)
Definition: tendrils.hpp:82
void operator()(const boost::signals2::connection &conn, void *vthiz, const tendrils *tdls) const
Definition: tendrils.hpp:256
Definition: tendrils.hpp:46
Definition: tendrils.hpp:46
size_type size() const
Definition: tendrils.hpp:79
spore< T > declare(const std::string &name, const std::string &doc)
Definition: tendrils.hpp:122
std::string key_
Definition: tendrils.hpp:264
spore< T > declare(const std::string &name)
Declare a tendril of a certain type, with only a name, no doc, or default values. ...
Definition: tendrils.hpp:108
storage_type::difference_type difference_type
Definition: tendrils.hpp:64
const_iterator find(const std::string &name) const
Definition: tendrils.hpp:75
storage_type::key_compare key_compare
Definition: tendrils.hpp:65
static spore_assign_impl< CellImpl, T > spore_assign(spore< T > CellImpl::*ptm, const std::string &name)
Definition: tendrils.hpp:277
storage_type::value_type value_type
Definition: tendrils.hpp:61
const_iterator end() const
Definition: tendrils.hpp:72
void clear()
Definition: tendrils.hpp:77
The tendrils are a collection for the ecto::tendril class, addressable by a string key...
Definition: tendrils.hpp:53
storage_type::size_type size_type
Definition: tendrils.hpp:63
void result_type
Definition: tendrils.hpp:246
Definition: tendrils.hpp:243