Wallaroo  0.8
collaborator.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * wallaroo - A library for configurable creation and wiring of C++ classes.
3  * Copyright (C) 2012 Daniele Pallastrelli
4  *
5  * This file is part of wallaroo.
6  * For more information, see http://wallaroolib.sourceforge.net/
7  *
8  * Boost Software License - Version 1.0 - August 17th, 2003
9  *
10  * Permission is hereby granted, free of charge, to any person or organization
11  * obtaining a copy of the software and accompanying documentation covered by
12  * this license (the "Software") to use, reproduce, display, distribute,
13  * execute, and transmit the Software, and to prepare derivative works of the
14  * Software, and to permit third-parties to whom the Software is furnished to
15  * do so, all subject to the following:
16  *
17  * The copyright notices in the Software and this entire statement, including
18  * the above license grant, this restriction and the following disclaimer,
19  * must be included in all copies of the Software, in whole or in part, and
20  * all derivative works of the Software, unless such copies or derivative
21  * works are solely in the form of machine-executable object code generated by
22  * a source language processor.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
27  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
28  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
29  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30  * DEALINGS IN THE SOFTWARE.
31  ******************************************************************************/
32 
33 #ifndef WALLAROO_COLLABORATOR_H_
34 #define WALLAROO_COLLABORATOR_H_
35 
36 #include <string>
37 #include <typeinfo>
38 #include <vector>
39 #include "cxx0x.h"
40 #include "dependency.h"
41 #include "part.h"
42 #include "exceptions.h"
43 
44 namespace wallaroo
45 {
46 
47 
48  namespace detail
49  {
50 
51  // overloaded utility function to get a shared_ptr from one of shared_ptr, weak_ptr
52 
53  template < typename T >
54  cxx0x::shared_ptr< T > GetShared( const cxx0x::shared_ptr< T >& s ) { return s; }
55 
56  template < typename T >
57  cxx0x::shared_ptr< T > GetShared( const cxx0x::weak_ptr< T >& s )
58  {
59  if ( cxx0x::shared_ptr< T > result = s.lock() ) return result;
60  throw DeletedPartError();
61  }
62 
63  // overloaded utility function to check in uniform way both shared_ptr and weak_ptr
64 
65  template < typename T >
66  bool NotEmpty( const cxx0x::shared_ptr< T >& p ) { return static_cast<bool>( p ); }
67 
68  template < typename T >
69  bool NotEmpty( const cxx0x::weak_ptr< T >& p ) { return !p.expired(); }
70 
71  }
72 
73 
76 struct optional
77 {
78  template < typename T > static bool WiringOk( const T& ) { return true; }
79 };
82 struct mandatory
83 {
84  template < typename T > static bool WiringOk( const cxx0x::weak_ptr< T >& t ) { return !t.expired(); }
85  template < typename T > static bool WiringOk( const cxx0x::shared_ptr< T >& t ) { return static_cast< bool >( t ); }
86 };
91 template <
92  std::size_t MIN = 0,
93  std::size_t MAX = 0,
94  template < typename E, typename Allocator = std::allocator< E > > class C = std::vector
95 >
97 {
98  template < typename T >
99  static bool WiringOk( const T* t )
100  {
101  const std::size_t s = t -> size();
102  return ( s >= MIN && ( MAX == 0 || s <= MAX ) );
103  }
104  template < typename T > struct Container { typedef C< T > Type; };
105 };
106 // template specializations to avoid warning about "unsigned is always >= 0":
107 template < std::size_t MAX, template < typename E, typename Allocator = std::allocator< E > > class C >
108 struct bounded_collection< 0, MAX, C >
109 {
110  template < typename T >
111  static bool WiringOk( const T* t ) { return ( t -> size() <= MAX ); }
112  template < typename T > struct Container { typedef C< T > Type; };
113 };
114 template < std::size_t MIN, template < typename E, typename Allocator = std::allocator< E > > class C >
115 struct bounded_collection< MIN, 0, C >
116 {
117  template < typename T >
118  static bool WiringOk( const T* t ) { return ( t -> size() >= MIN ); }
119  template < typename T > struct Container { typedef C< T > Type; };
120 };
121 template < template < typename E, typename Allocator = std::allocator< E > > class C >
122 struct bounded_collection< 0, 0, C >
123 {
124  template < typename T >
125  static bool WiringOk( const T* ) { return true; }
126  template < typename T > struct Container { typedef C< T > Type; };
127 };
132 template < template < typename E, typename Allocator = std::allocator< E > > class C = std::vector >
134 {
135  template < typename T > static bool WiringOk( const T* ) { return true; }
136  template < typename T > struct Container { typedef C< T > Type; };
137 };
138 
139 
140 
141 
160 template <
161  typename T,
162  typename P = mandatory,
163  template < typename X > class Ownership = cxx0x::weak_ptr
164 >
165 class Collaborator : public Dependency
166 {
167 public:
168 
169  typedef Ownership< T > Ptr;
170  typedef cxx0x::shared_ptr< T > SharedPtr;
171 
176  Collaborator( const std::string& name, const RegToken& token )
177  {
178  Part* owner = token.GetPart();
179  owner -> Register( name, this );
180  }
181 
186  void Link( const cxx0x::shared_ptr< Part >& dev )
187  {
188  if ( cxx0x::shared_ptr< T > _dev = cxx0x::dynamic_pointer_cast< T >( dev ) )
189  part = _dev;
190  else // bad type!
191  throw WrongType();
192  }
193 
197  SharedPtr operator -> () { return detail::GetShared( part ); }
198 
202  const SharedPtr operator -> () const { return detail::GetShared( part ); }
203 
207  operator SharedPtr() { return detail::GetShared( part ); }
208 
212  operator const SharedPtr() const { return detail::GetShared( part ); }
213 
218  operator bool() const { return detail::NotEmpty( part ); }
219 
224  virtual bool WiringOk() const { return P::WiringOk( part ); }
225 
226 private:
227  Ptr part;
228 
229  // copy ctor and assignment operator disabled
230  Collaborator( const Collaborator& );
231  Collaborator& operator = ( const Collaborator& );
232 };
233 
234 
235 // partial specialization for the collection case
236 template <
237  typename T,
238  std::size_t MIN,
239  std::size_t MAX,
240  template < typename E, typename Allocator = std::allocator< E > > class C,
241  template < typename X > class Ownership
242 >
243 class Collaborator< T, bounded_collection< MIN, MAX, C >, Ownership > :
244  public Dependency,
245  public bounded_collection< MIN, MAX, C >::template Container< Ownership< T > >::Type
246 {
247 private:
248  typedef Ownership< T > Ptr;
249  typedef typename bounded_collection< MIN, MAX, C >::template Container< Ptr >::Type Cont;
250 
251 public:
252 
257  Collaborator( const std::string& name, const RegToken& token )
258  {
259  Part* owner = token.GetPart();
260  owner -> Register( name, this );
261  }
262 
267  void Link( const cxx0x::shared_ptr< Part >& part )
268  {
269  if ( cxx0x::shared_ptr< T > obj = cxx0x::dynamic_pointer_cast< T >( part ) )
270  Cont::push_back( obj );
271  else
272  throw WrongType(); // bad type!
273  }
274 
279  virtual bool WiringOk() const
280  {
282  }
283 
284 private:
285  // copy ctor and assignment operator disabled
286  Collaborator( const Collaborator& );
287  Collaborator& operator = ( const Collaborator& );
288 };
289 
290 // partial specialization for the collection case
291 template <
292  typename T,
293  template < typename E, typename Allocator = std::allocator< E > > class C,
294  template < typename X > class Ownership
295 >
296 class Collaborator< T, collection< C >, Ownership > :
297  public Collaborator < T, bounded_collection< 0, 0, C >, Ownership >
298 {
299 public:
300  Collaborator( const std::string& name, const RegToken& token ) :
301  Collaborator < T, bounded_collection< 0, 0, C >, Ownership >( name, token )
302  {}
303 };
304 
305 }
306 
307 #endif
Ownership< T > Ptr
Definition: collaborator.h:169
Collaborator(const std::string &name, const RegToken &token)
Definition: collaborator.h:176
Definition: collaborator.h:133
Collaborator(const std::string &name, const RegToken &token)
Definition: collaborator.h:300
Part * GetPart() const
Definition: part.h:60
virtual bool WiringOk() const
Definition: collaborator.h:279
bool NotEmpty(const cxx0x::shared_ptr< T > &p)
Definition: collaborator.h:66
static bool WiringOk(const T *t)
Definition: collaborator.h:118
cxx0x::shared_ptr< T > GetShared(const cxx0x::shared_ptr< T > &s)
Definition: collaborator.h:54
C< T > Type
Definition: collaborator.h:104
static bool WiringOk(const cxx0x::weak_ptr< T > &t)
Definition: collaborator.h:84
Definition: part.h:76
static bool WiringOk(const cxx0x::shared_ptr< T > &t)
Definition: collaborator.h:85
static bool WiringOk(const T *)
Definition: collaborator.h:135
C< T > Type
Definition: collaborator.h:119
Definition: part.h:57
static bool WiringOk(const T *t)
Definition: collaborator.h:111
C< T > Type
Definition: collaborator.h:112
Definition: collaborator.h:104
Definition: collaborator.h:136
Definition: exceptions.h:74
static bool WiringOk(const T &)
Definition: collaborator.h:78
Definition: collaborator.h:82
C< T > Type
Definition: collaborator.h:136
cxx0x::shared_ptr< T > SharedPtr
Definition: collaborator.h:170
void Link(const cxx0x::shared_ptr< Part > &dev)
Definition: collaborator.h:186
Definition: attribute.h:45
static bool WiringOk(const T *)
Definition: collaborator.h:125
void Link(const cxx0x::shared_ptr< Part > &part)
Definition: collaborator.h:267
Definition: exceptions.h:110
static bool WiringOk(const T *t)
Definition: collaborator.h:99
virtual bool WiringOk() const
Definition: collaborator.h:224
SharedPtr operator->()
Definition: collaborator.h:197
Definition: dependency.h:49
C< T > Type
Definition: collaborator.h:126
Definition: collaborator.h:76
Definition: collaborator.h:96
Definition: collaborator.h:165
Collaborator(const std::string &name, const RegToken &token)
Definition: collaborator.h:257