Wallaroo  0.8
catalog.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_CATALOG_H_
34 #define WALLAROO_CATALOG_H_
35 
36 #include <string>
37 #include <typeinfo>
38 #include <cassert>
39 #include "detail/partshell.h"
40 #include "cxx0x.h"
41 #include "part.h"
42 #include "class.h"
43 
44 namespace wallaroo
45 {
46 
47 // forward declarations:
48 class Context;
49 class UseAsExpression;
50 class UseExpression;
51 
65 class Catalog
66 {
67 public:
68 
71  Catalog() {}
72 
83  detail::PartShell operator [] ( const std::string& id ) const
84  {
85  Parts::const_iterator i = parts.find( id );
86  if ( i == parts.end() ) throw ElementNotFound( id );
87  return detail::PartShell( i -> second );
88  }
89 
95  void Add( const std::string& id, const cxx0x::shared_ptr< Part >& dev )
96  {
97  std::pair< Parts::iterator, bool > result =
98  parts.insert( std::make_pair( id, dev ) );
99  if ( ! result.second ) throw DuplicatedElement( id );
100  }
101 
107  void Remove( const std::string& id )
108  {
109  if ( parts.erase( id ) != 1 ) throw ElementNotFound( id );
110  }
111 
113  void Clear() { parts.clear(); }
114 
118  std::size_t Size() const { return parts.size(); }
119 
129  template < class P1, class P2 >
130  detail::PartShell Create( const std::string& id, const std::string& className, const P1& p1, const P2& p2 )
131  {
132  typedef Class< P1, P2 > C;
133  C c = C::ForName( className );
134  cxx0x::shared_ptr< Part > obj = c.NewInstance( p1, p2 );
135  if ( obj.get() == NULL ) throw ElementNotFound( className );
136  Add( id, obj );
137  return detail::PartShell( obj );
138  }
139 
148  template < class P >
149  detail::PartShell Create( const std::string& id, const std::string& className, const P& p )
150  {
151  typedef Class< P, void > C;
152  C c = C::ForName( className );
153  cxx0x::shared_ptr< Part > obj = c.NewInstance( p );
154  if ( obj.get() == NULL ) throw ElementNotFound( className );
155  Add( id, obj );
156  return detail::PartShell( obj );
157  }
158 
166  detail::PartShell Create( const std::string& id, const std::string& className )
167  {
168  typedef Class< void, void > C;
169  C c = C::ForName( className );
170  cxx0x::shared_ptr< Part > obj = c.NewInstance();
171  if ( obj.get() == NULL ) throw ElementNotFound( className );
172  Add( id, obj );
173  return detail::PartShell( obj );
174  }
175 
180  bool IsWiringOk() const
181  {
182  return FindWrongMultiplicity().empty();
183  }
184 
189  void CheckWiring() const
190  {
191  const std::string wrongPart = FindWrongMultiplicity();
192  if ( !wrongPart.empty() ) throw WiringError( wrongPart );
193  }
194 
202  void Init()
203  {
204  for ( Parts::const_iterator i = parts.begin(); i != parts.end(); ++i )
205  i -> second -> Init();
206  }
207 
208 private:
209 
210  // copy ctor and assignment operator disabled
211  Catalog( const Catalog& );
212  Catalog& operator = ( const Catalog& );
213 
214  // returns the name of the first parts with wrong multiplicity
215  // or the empty string if the test has success
216  std::string FindWrongMultiplicity() const
217  {
218  for( Parts::const_iterator i = parts.begin(); i != parts.end(); ++i )
219  {
220  if ( ! i -> second -> MultiplicitiesOk() )
221  return( i -> first );
222  }
223  return std::string();
224  }
225 
226  typedef cxx0x::unordered_map< std::string, cxx0x::shared_ptr< Part > > Parts;
227  Parts parts;
228 
229  friend class Context;
230  friend class UseAsExpression;
231  friend class SetExpression;
232  friend UseExpression use( const std::string& destClass );
233  friend bool IsWiringOk();
234  friend void CheckWiring();
235 
236 
237  static Catalog*& Current()
238  {
239  static Catalog* current = NULL;
240  return current;
241  }
242 };
243 
244 
245 // This is a helper class that provides the result of the use().as() function
246 // useful to concatenate use().as() with of().
248 {
249 public:
250  UseAsExpression( detail::PartShell& _destClass, const std::string& _attribute ) :
251  destClass( _destClass ),
252  attribute( _attribute )
253  {
254  }
255  // throw ElementNotFound If @c attribute does not exist in @c srcClass.
256  // throw WrongType If @c destClass has not a type compatible with the dependency.
257  void of( const detail::PartShell& srcClass )
258  {
259  // perform the final assignment:
260  srcClass.Wire( attribute, destClass );
261  }
262  // throw CatalogNotSpecified if the current catalog has not been selected with wallaroo_within
263  // throw ElementNotFound If @c attribute does not exist in this part or
264  // if @c srcClass key is not found in the current catalog.
265  // throw WrongType If @c destClass has not a type compatible with the dependency.
266  void of( const std::string& srcClass )
267  {
268  // default container case
269  Catalog* current = Catalog::Current();
270  if ( ! current ) throw CatalogNotSpecified();
271  of( ( *current )[ srcClass ] );
272  }
273 private:
274  const detail::PartShell destClass;
275  const std::string attribute;
276 };
277 
278 // This is a helper class that provides the result of the use() function
279 // useful to concatenate use() with as().
281 {
282 public:
283  explicit UseExpression( const detail::PartShell& _destClass )
284  : destClass( _destClass )
285  {
286  }
287  UseAsExpression as( const std::string& attribute )
288  {
289  return UseAsExpression( destClass, attribute );
290  }
291 private:
292  detail::PartShell destClass;
293 };
294 
295 
300 inline UseExpression use( const detail::PartShell& destClass )
301 {
302  return UseExpression( destClass );
303 }
304 
310 inline UseExpression use( const std::string& destClass )
311 {
312  // default container case
313  Catalog* current = Catalog::Current();
314  if ( ! current ) throw CatalogNotSpecified();
315  return use( ( *current )[ destClass ] );
316 }
317 
318 
319 // This is a helper class that provides the result of the set_attribute().of() function
320 // useful to concatenate set_attribute().of() with to().
322 {
323 public:
324  SetOfExpression( const detail::PartShell& _part, const std::string& _attribute ) :
325  part( _part ), attribute( _attribute ) {}
326  // throw ElementNotFound If attribute does not exist in this part.
327  // throw WrongType If @c value has not a type compatible with the attribute.
328  template < typename T >
329  void to( const T& value )
330  {
331  // perform the final assignment:
332  part.SetAttribute( attribute, value );
333  }
334 private:
335  const detail::PartShell part;
336  const std::string attribute;
337 };
338 
339 // This is a helper class that provides the result of the set_attribute() function
340 // useful to concatenate set_attribute() with of().
342 {
343 public:
344  explicit SetExpression( const std::string& att ) : attribute( att ) {}
345  SetOfExpression of( const detail::PartShell& part ) { return SetOfExpression( part, attribute ); }
346  // throw CatalogNotSpecified if the current catalog has not been selected including
347  // this function in a wallaroo_within section
348  SetOfExpression of( const std::string& part )
349  {
350  // default container case
351  Catalog* current = Catalog::Current( );
352  if ( !current ) throw CatalogNotSpecified( );
353  return SetOfExpression( ( *current )[ part ], attribute );
354  }
355 private:
356  const std::string attribute;
357 };
358 
364 inline SetExpression set_attribute( const std::string& attribute ) { return SetExpression( attribute ); }
365 
366 
367 // Helper class that changes the current catalog on the ctor and
368 // restores the previous on the dtor
369 class Context
370 {
371 public:
372  Context( Catalog& c ) :
373  firstTime( true )
374  {
375  previous = Catalog::Current();
376  Catalog::Current() = &c;
377  }
379  {
380  Catalog::Current() = previous;
381  }
382  bool FirstTime() const
383  {
384  return firstTime;
385  }
386  void Terminate()
387  {
388  firstTime = false;
389  }
390 private:
391  bool firstTime;
392  Catalog* previous;
393 };
394 
395 
419 #define wallaroo_within( C ) \
420  for ( wallaroo::Context context( C ); context.FirstTime(); context.Terminate() )
421 
423 
424 } // namespace
425 
426 #endif
UseExpression(const detail::PartShell &_destClass)
Definition: catalog.h:283
Definition: catalog.h:65
void Clear()
Definition: catalog.h:113
Definition: partshell.h:51
void Add(const std::string &id, const cxx0x::shared_ptr< Part > &dev)
Definition: catalog.h:95
void Remove(const std::string &id)
Definition: catalog.h:107
Definition: catalog.h:321
Definition: catalog.h:247
UseAsExpression(detail::PartShell &_destClass, const std::string &_attribute)
Definition: catalog.h:250
SetOfExpression of(const detail::PartShell &part)
Definition: catalog.h:345
bool FirstTime() const
Definition: catalog.h:382
Definition: catalog.h:341
bool IsWiringOk() const
Definition: catalog.h:180
Definition: exceptions.h:152
friend UseExpression use(const std::string &destClass)
Definition: catalog.h:310
Definition: catalog.h:369
void CheckWiring() const
Definition: catalog.h:189
detail::PartShell operator[](const std::string &id) const
Definition: catalog.h:83
Definition: exceptions.h:128
void Init()
Definition: catalog.h:202
Definition: exceptions.h:92
UseAsExpression as(const std::string &attribute)
Definition: catalog.h:287
UseExpression use(const detail::PartShell &destClass)
Definition: catalog.h:300
~Context()
Definition: catalog.h:378
detail::PartShell Create(const std::string &id, const std::string &className, const P &p)
Definition: catalog.h:149
void of(const std::string &srcClass)
Definition: catalog.h:266
void to(const T &value)
Definition: catalog.h:329
Catalog()
Definition: catalog.h:71
Definition: class.h:113
void Terminate()
Definition: catalog.h:386
detail::PartShell Create(const std::string &id, const std::string &className)
Definition: catalog.h:166
SetOfExpression of(const std::string &part)
Definition: catalog.h:348
Definition: catalog.h:280
Definition: class.h:54
SetOfExpression(const detail::PartShell &_part, const std::string &_attribute)
Definition: catalog.h:324
void of(const detail::PartShell &srcClass)
Definition: catalog.h:257
void SetAttribute(const std::string &attribute, const T &value) const
Definition: partshell.h:68
std::size_t Size() const
Definition: catalog.h:118
Definition: attribute.h:45
Definition: exceptions.h:193
SetExpression set_attribute(const std::string &attribute)
Definition: catalog.h:364
SetExpression(const std::string &att)
Definition: catalog.h:344
Definition: class.h:172
detail::PartShell Create(const std::string &id, const std::string &className, const P1 &p1, const P2 &p2)
Definition: catalog.h:130
void Wire(const std::string &collaboratorName, const PartShell &destination) const
Definition: partshell.h:62
Context(Catalog &c)
Definition: catalog.h:372