Lumiera  0.pre.03
»edit your freedom«
nodewiring-config.hpp
Go to the documentation of this file.
1 /*
2  NODEWIRING-CONFIG.hpp - Helper for representing and selecting the wiring case
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
51 #ifndef ENGINE_NODEWIRING_CONFIG_H
52 #define ENGINE_NODEWIRING_CONFIG_H
53 
54 
55 #include "lib/meta/configflags.hpp"
56 #include "lib/meta/util.hpp"
57 #include "lib/util.hpp"
58 
59 #include <functional>
60 #include <bitset>
61 #include <map>
62 
63 
64 namespace steam {
65 namespace engine {
66 namespace config {
67 
68  using util::contains;
69  using lib::meta::FlagInfo;
70 
71  using lib::meta::CONFIG_FLAGS_MAX;
72 
73  typedef size_t IxID;
74 
75 
110  template< template<class CONF> class Factory
111  , typename FUNC
112  , typename PAR
113  >
115  {
116  typedef std::function<FUNC> FacFunction;
117 
118  template<class FAC>
120  : private FAC,
121  public FacFunction
122  {
123  FactoryHolder(PAR p)
124  : FAC(p),
125  FacFunction (static_cast<FAC&> (*this))
126  { }
127  };
128 
129 
131  typedef std::map<IxID, PFunc> ConfigTable;
132 
133  ConfigTable possibleConfig_;
134 
135 
139  {
140  PAR ctor_param_;
141  ConfigTable& factories_;
142 
143  FactoryTableBuilder (ConfigTable& tab, PAR p)
144  : ctor_param_(p),
145  factories_(tab) { }
146 
147 
148  /* === visitation interface === */
149 
150  typedef void Ret;
151 
152  template<class CONF>
153  void
154  visit (IxID code)
155  {
156  PFunc pFactory (new FactoryHolder<Factory<CONF>> (ctor_param_));
157  factories_[code] = pFactory;
158  }
159 
160  void done() {}
161  };
162 
163 
164  public:
165  template<class CONFS>
166  ConfigSelector(CONFS const&, PAR factory_ctor_param)
167  {
168  FactoryTableBuilder buildTable(this->possibleConfig_,
169  factory_ctor_param );
170  // store a new Factory instance
171  // for each possible Flag-Configuration
172  FlagInfo<CONFS>::accept (buildTable);
173  }
174 
175  FacFunction&
176  operator[] (IxID configFlags)
177  {
178  if (contains (possibleConfig_, configFlags))
179  return *possibleConfig_[configFlags];
180  else
181  throw lumiera::error::Invalid("ConfigSelector: No preconfigured factory for config-bits="
182  +std::bitset<CONFIG_FLAGS_MAX>(configFlags).to_string());
183  }
184  };
185 
186 
187 
188 
189  using lib::meta::Yes_t;
190  using lib::meta::No_t;
191 
212  template<template<class> class _CandidateTemplate_>
214  {
215 
216  template<class X>
217  class Test
218  {
219  typedef _CandidateTemplate_<X> Instance;
220 
221  template<class U>
222  static Yes_t check(typename U::is_defined *);
223  template<class U>
224  static No_t check(...);
225 
226  public:
227  static const bool value = (sizeof(Yes_t)==sizeof(check<Instance>(0)));
228  };
229  };
230 
231 
232 
233 }}} // namespace steam::engine::config
234 #endif
Helper template for semi-automatic detection if instantiation is possible.
Helper for calculating values and for invoking runtime code based on a given FlagTuple.
Under some circumstances it is necessary to assemble functionality out of elementary building blocks...
Simple and lightweight helpers for metaprogramming and type detection.
Steam-Layer implementation namespace root.
FacFunction & operator[](IxID configFlags)
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
char Yes_t
helper types to detect the overload resolution chosen by the compiler
Definition: meta/util.hpp:104
Helper for fabricating ProcNode Wiring configurations.
ConfigTable possibleConfig_
Table of factories.
bool contains(SEQ const &cont, typename SEQ::const_reference val)
shortcut for brute-force containment test in any sequential container
Definition: util.hpp:255