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)
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
42 #ifndef ENGINE_NODEWIRING_CONFIG_H
43 #define ENGINE_NODEWIRING_CONFIG_H
44 
45 
46 #include "lib/meta/configflags.hpp"
47 #include "lib/meta/util.hpp"
48 #include "lib/util.hpp"
49 
50 #include <functional>
51 #include <bitset>
52 #include <map>
53 
54 
55 namespace steam {
56 namespace engine {
57 namespace config {
58 
59  using util::contains;
60  using lib::meta::FlagInfo;
61 
62  using lib::meta::CONFIG_FLAGS_MAX;
63 
64  typedef size_t IxID;
65 
66 
101  template< template<class CONF> class Factory
102  , typename FUNC
103  , typename PAR
104  >
106  {
107  typedef std::function<FUNC> FacFunction;
108 
109  template<class FAC>
111  : private FAC,
112  public FacFunction
113  {
114  FactoryHolder(PAR p)
115  : FAC(p),
116  FacFunction (static_cast<FAC&> (*this))
117  { }
118  };
119 
120 
122  typedef std::map<IxID, PFunc> ConfigTable;
123 
124  ConfigTable possibleConfig_;
125 
126 
130  {
131  PAR ctor_param_;
132  ConfigTable& factories_;
133 
134  FactoryTableBuilder (ConfigTable& tab, PAR p)
135  : ctor_param_(p),
136  factories_(tab) { }
137 
138 
139  /* === visitation interface === */
140 
141  typedef void Ret;
142 
143  template<class CONF>
144  void
145  visit (IxID code)
146  {
147  PFunc pFactory (new FactoryHolder<Factory<CONF>> (ctor_param_));
148  factories_[code] = pFactory;
149  }
150 
151  void done() {}
152  };
153 
154 
155  public:
156  template<class CONFS>
157  ConfigSelector(CONFS const&, PAR factory_ctor_param)
158  {
159  FactoryTableBuilder buildTable(this->possibleConfig_,
160  factory_ctor_param );
161  // store a new Factory instance
162  // for each possible Flag-Configuration
163  FlagInfo<CONFS>::accept (buildTable);
164  }
165 
166  FacFunction&
167  operator[] (IxID configFlags)
168  {
169  if (contains (possibleConfig_, configFlags))
170  return *possibleConfig_[configFlags];
171  else
172  throw lumiera::error::Invalid("ConfigSelector: No preconfigured factory for config-bits="
173  +std::bitset<CONFIG_FLAGS_MAX>(configFlags).to_string());
174  }
175  };
176 
177 
178 
179 
180  using lib::meta::Yes_t;
181  using lib::meta::No_t;
182 
203  template<template<class> class _CandidateTemplate_>
205  {
206 
207  template<class X>
208  class Test
209  {
210  typedef _CandidateTemplate_<X> Instance;
211 
212  template<class U>
213  static Yes_t check(typename U::is_defined *);
214  template<class U>
215  static No_t check(...);
216 
217  public:
218  static const bool value = (sizeof(Yes_t)==sizeof(check<Instance>(0)));
219  };
220  };
221 
222 
223 
224 }}} // namespace steam::engine::config
225 #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:190
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:95
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