ecto
spore.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/shared_ptr.hpp>
31 #include <boost/function/function1.hpp>
32 
33 #include <ecto/util.hpp> //name_of
34 #include <ecto/tendril.hpp>
35 #include <stdexcept>
36 #include <string>
37 
38 namespace ecto
39 {
44  template<typename T>
45  struct spore
46  {
48  typedef T value_type;
49  typedef T& reference_type;
50  typedef T* pointer_type;
51  typedef const T* const_pointer_type;
55  spore() { }
56 
62  tendril_(t)
63  {
64  if(!t)
65  BOOST_THROW_EXCEPTION(except::NullTendril()
66  << except::diag_msg("creating sport with type")
67  << except::spore_typename(name_of<T>()));
68 
69  t->enforce_type<T>();
70  }
71 
78  spore<T>& set_callback(typename boost::function1<void, T> cb)
79  {
80  get()->set_callback(cb);
81  return *this;
82  }
83 
85  {
86  get()->notify();
87  return *this;
88  }
89 
90  spore<T>& set_doc(const std::string& doc)
91  {
92  get()->set_doc(doc);
93  return *this;
94  }
95 
96  spore<T>& set_default_val(const T& val)
97  {
98  get()->set_default_val(val);
99  return *this;
100  }
101 
102  bool dirty() const
103  {
104  return get()->dirty();
105  }
106 
107  void dirty(bool d)
108  {
109  return get()->dirty(d);
110  }
111 
112  bool user_supplied() const
113  {
114  return get()->user_supplied();
115  }
116 
117  bool has_default() const
118  {
119  return get()->has_default();
120  }
121 
123  {
124  get()->required(b);
125  return *this;
126  }
127 
128  bool required() const
129  {
130  return get()->required();
131  }
132 
133  pointer_type operator->()
134  {
135  tendril_ptr _p = get();
136  return &(_p->get<T>());
137  }
138 
139  const_pointer_type operator->() const
140  {
141  tendril_cptr _p = get();
142  return &(_p->get<const T>());
143  }
144 
145  reference_type operator*()
146  {
147  tendril_ptr _p = get();
148  return _p->get<T>();
149  }
150 
151  const_pointer_type operator*() const
152  {
153  tendril_cptr _p = get();
154  return _p->get<const T>();
155  }
156 
157  typedef tendril_ptr this_type::*unspecified_bool_type;
158 
159  operator unspecified_bool_type() const // never throws
160  {
161  return tendril_ ? &this_type::tendril_ : 0;
162  }
163 
164  private:
165 
170  inline tendril_ptr get()
171  {
172  if (!tendril_)
173  BOOST_THROW_EXCEPTION(except::NullTendril());
174  return tendril_;
175  }
176 
181  inline tendril_cptr get() const
182  {
183  if (!tendril_)
184  BOOST_THROW_EXCEPTION(except::NullTendril()
185  << except::diag_msg("access via spore")
186  << except::spore_typename(name_of<T>()));
187 
188  return tendril_;
189  }
191  };
192 }
The spore is a typed handle for tendrils, making holding onto tendrils a bit easier.
Definition: spore.hpp:45
T & reference_type
Definition: spore.hpp:49
spore< T > & required(bool b)
Definition: spore.hpp:122
tendril_ptr tendril_
Definition: spore.hpp:190
spore< T > this_type
Definition: spore.hpp:47
const T * const_pointer_type
Definition: spore.hpp:51
const_pointer_type operator*() const
Definition: spore.hpp:151
boost::shared_ptr< const tendril > tendril_cptr
Definition: forward.hpp:36
Definition: parameters.hpp:11
spore< T > & set_doc(const std::string &doc)
Definition: spore.hpp:90
spore(tendril_ptr t)
Definition: spore.hpp:61
T value_type
Definition: spore.hpp:48
bool has_default() const
Definition: spore.hpp:117
boost::shared_ptr< tendril > tendril_ptr
Definition: forward.hpp:34
spore()
Definition: spore.hpp:55
const_pointer_type operator->() const
Definition: spore.hpp:139
bool dirty() const
Definition: spore.hpp:102
bool user_supplied() const
Definition: spore.hpp:112
void dirty(bool d)
Definition: spore.hpp:107
spore< T > & notify()
Definition: spore.hpp:84
pointer_type operator->()
Definition: spore.hpp:133
spore< T > & set_default_val(const T &val)
Definition: spore.hpp:96
reference_type operator*()
Definition: spore.hpp:145
bool required() const
Definition: spore.hpp:128
spore< T > & set_callback(typename boost::function1< void, T > cb)
Definition: spore.hpp:78
T * pointer_type
Definition: spore.hpp:50
tendril_ptr this_type::* unspecified_bool_type
Definition: spore.hpp:157