Wallaroo  0.8
part.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_PART_H_
34 #define WALLAROO_PART_H_
35 
36 #include <string>
37 #include <sstream>
38 #include "exceptions.h"
39 #include "cxx0x.h"
40 #include "dependency.h"
41 #include "deserializable_value.h"
42 
43 namespace wallaroo
44 {
45 
46 // forward declaration:
47 class Plugin;
48 
57 class RegToken
58 {
59 public:
60  Part* GetPart() const { return part; }
61 private:
62  friend class Part;
63  RegToken( Part* d ) : part( d ) {}
64  Part* part;
65 };
66 
76 class Part
77 {
78 public:
79  // we need to make Part virtual, to use dynamic_cast
80  virtual ~Part() {}
81 
86  void Wire( const std::string& dependency, const cxx0x::shared_ptr< Part >& part )
87  {
88  Dependencies::iterator i = dependencies.find( dependency );
89  if ( i == dependencies.end() ) throw ElementNotFound( dependency );
90  ( i -> second ) -> Link( part );
91  }
92 
99  // NOTE: we pass value as const reference to allow the effective specialization of string
100  template < typename T >
101  void SetAttribute( const std::string& attribute, const T& value )
102  {
103  std::ostringstream stream;
104  if ( !( stream << std::boolalpha << value ) ) throw WrongType();
105  SetStringAttribute( attribute, stream.str() );
106  }
107 
111  bool MultiplicitiesOk() const
112  {
113  for (
114  Dependencies::const_iterator i = dependencies.begin();
115  i != dependencies.end();
116  ++i
117  )
118  if ( ! i -> second -> WiringOk() )
119  return false;
120  return true;
121  }
122 
130  virtual void Init() {};
131 
132 protected:
134  {
135  return RegToken( this );
136  }
137 private:
138  // this method should only be invoked by Class
139  // to add the reference counter for the shared library.
140  template < class T1, class T2 > friend class Class;
141  void Source( const cxx0x::shared_ptr< Plugin >& p )
142  {
143  plugin = p;
144  }
145 
146  // this method should only be invoked by the dependencies of this part
147  // to register itself into the dependencies table.
148  template < typename T, typename P, template < typename X > class Ownership >
149  friend class Collaborator;
150  void Register( const std::string& id, Dependency* c )
151  {
152  dependencies[ id ] = c;
153  }
154 
155  // this method should only be invoked by the attributes of this part
156  // to register itself into the attributes table.
157  template < class T > friend class Attribute;
158  void Register( const std::string& id, DeserializableValue* attribute )
159  {
160  attributes[ id ] = attribute;
161  }
162 
163  // set attribute to a value represented as string.
164  // throws ElementNotFound if the attribute doesn't exist.
165  // throws WrongType if @c value is not a valid representation for the type of the attribute
166  void SetStringAttribute( const std::string& attribute, const std::string& value )
167  {
168  Attributes::iterator i = attributes.find( attribute );
169  if ( i == attributes.end() ) throw ElementNotFound( attribute );
170  ( i -> second ) -> Value( value );
171  }
172 
173  typedef cxx0x::unordered_map< std::string, Dependency* > Dependencies;
174  Dependencies dependencies;
175 
176  typedef cxx0x::unordered_map< std::string, DeserializableValue* > Attributes;
177  Attributes attributes;
178 
179  cxx0x::shared_ptr< Plugin > plugin; // optional shared ptr to plugin, to release the shared library when is no more used
180 };
181 
182 
188 template <>
189 inline void Part::SetAttribute( const std::string& attribute, const std::string& value )
190 {
191  // Optimization: with strings we don't need conversion
192  SetStringAttribute( attribute, value );
193 }
194 
195 } // namespace
196 
197 #endif
void SetAttribute(const std::string &attribute, const T &value)
Definition: part.h:101
void Wire(const std::string &dependency, const cxx0x::shared_ptr< Part > &part)
Definition: part.h:86
Part * GetPart() const
Definition: part.h:60
bool MultiplicitiesOk() const
Definition: part.h:111
Definition: attribute.h:93
virtual void Init()
Definition: part.h:130
Definition: part.h:76
RegToken RegistrationToken()
Definition: part.h:133
Definition: exceptions.h:128
Definition: part.h:57
virtual ~Part()
Definition: part.h:80
Definition: class.h:54
Definition: attribute.h:45
Definition: deserializable_value.h:47
Definition: exceptions.h:110
Definition: dependency.h:49
Definition: collaborator.h:165