Wallaroo  0.8
configuration.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_CONFIGURATION_H_
34 #define WALLAROO_CONFIGURATION_H_
35 
36 #include <string>
37 #include <sstream>
38 #include <fstream>
39 #include <vector>
40 #include "wallaroo/catalog.h"
42 #include "detail/grammar.h"
43 
44 namespace wallaroo
45 {
46 
101 {
102 public:
108  explicit Configuration( const std::string& file )
109  {
110  std::ifstream f( file.c_str() );
111  if ( !f ) throw WrongFile( file + " not found" );
112  detail::Grammar< Configuration > g( f, *this );
113  g.Parse();
114  }
115 
120  explicit Configuration( std::istream& s )
121  {
122  detail::Grammar< Configuration > g( s, *this );
123  g.Parse();
124  }
125 
129  void LoadPlugins()
130  {
131  for ( std::size_t i = 0; i < libraries.size(); ++i )
132  {
133  wallaroo::Plugin::Load( libraries[ i ] + Plugin::Suffix() );
134  }
135  }
136 
143  void Fill( Catalog& catalog )
144  {
145  for ( std::size_t i = 0; i < objects.size(); ++i )
146  {
147  const Object& o = objects[ i ];
148  catalog.Create( o.instance, o.type );
149  }
150  for ( std::size_t i = 0; i < attributes.size(); ++i )
151  {
152  const Attribute& a = attributes[ i ];
153  set_attribute( a.attribute ).of( catalog[ a.object ] ).to( a.value );
154  }
155  for ( std::size_t i = 0; i < dependencies.size(); ++i )
156  {
157  const Dependency& d = dependencies[ i ];
158  use( catalog[ d.value ] ).as( d.dependency ).of( catalog[ d.object ] );
159  }
160  }
161 
162 private:
163 
164  struct Object
165  {
166  Object( const std::string& i, const std::string& c ) :
167  instance( i ), type( c ) {}
168  std::string instance;
169  std::string type;
170  };
171 
172  struct Attribute
173  {
174  Attribute( const std::string& obj, const std::string& att, const std::string& v ) :
175  object( obj ), attribute( att ), value( v ) {}
176  std::string object;
177  std::string attribute;
178  std::string value;
179  };
180 
181  struct Dependency
182  {
183  Dependency( const std::string& obj, const std::string& dep, const std::string& v ) :
184  object( obj ), dependency( dep ), value( v ) {}
185  std::string object;
186  std::string dependency;
187  std::string value;
188  };
189 
190  // Grammar callbacks:
192  void Load( const std::string& library )
193  {
194  libraries.push_back( library );
195  }
196  void AssignAttribute( const std::string& lvalue, const std::string& attId, const std::string& attValue )
197  {
198  attributes.push_back( Attribute( lvalue, attId, attValue ) );
199  }
200  void Create( const std::string& lvalue, const std::string& rvalue )
201  {
202  objects.push_back( Object( lvalue, rvalue ) );
203  }
204  void AssignDep( const std::string& lvalue, const std::string& dep, const std::string& rvalue )
205  {
206  dependencies.push_back( Dependency( lvalue, dep, rvalue ) );
207  }
208 
209  std::vector< std::string > libraries;
210  std::vector< Object > objects;
211  std::vector< Attribute > attributes;
212  std::vector< Dependency > dependencies;
213 };
214 
215 
216 } // namespace
217 
218 #endif // WALLAROO_CONFIGURATION_H_
219 
Definition: catalog.h:65
Definition: attribute.h:93
static std::string Suffix()
Definition: dynamic_loader.h:86
void LoadPlugins()
Definition: configuration.h:129
SetOfExpression of(const detail::PartShell &part)
Definition: catalog.h:345
Configuration(std::istream &s)
Definition: configuration.h:120
UseAsExpression as(const std::string &attribute)
Definition: catalog.h:287
UseExpression use(const detail::PartShell &destClass)
Definition: catalog.h:300
void to(const T &value)
Definition: catalog.h:329
Configuration(const std::string &file)
Definition: configuration.h:108
void of(const detail::PartShell &srcClass)
Definition: catalog.h:257
void Parse()
Definition: grammar.h:97
Definition: attribute.h:45
Definition: exceptions.h:176
Definition: configuration.h:100
SetExpression set_attribute(const std::string &attribute)
Definition: catalog.h:364
void Fill(Catalog &catalog)
Definition: configuration.h:143
static cxx0x::shared_ptr< Plugin > Load(const std::string &fileName)
Definition: dynamic_loader.h:69
detail::PartShell Create(const std::string &id, const std::string &className, const P1 &p1, const P2 &p2)
Definition: catalog.h:130
Definition: grammar.h:87