Lumiera  0.pre.03
»edit your freedom«
fake-configrules.hpp
Go to the documentation of this file.
1 /*
2  FAKE-CONFIGRULES.hpp - dummy implementation of the config rules system
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 
14 
37 #ifndef MOBJECT_SESSION_QUERY_FAKECONFIGRULES_H
38 #define MOBJECT_SESSION_QUERY_FAKECONFIGRULES_H
39 
42 #include "lib/depend-inject.hpp"
43 #include "lib/query-util.hpp"
44 #include "lib/util.hpp"
45 #include "lib/p.hpp"
46 
47 #include <boost/any.hpp>
48 #include <string>
49 #include <map>
50 
51 
52 
53 namespace steam {
54 namespace mobject {
55 namespace session {
56  namespace query {
57 
58  namespace asset = steam::asset;
59 
60  using asset::Pipe;
61  using asset::ProcPatt;
62  using asset::PProcPatt;
65  using lib::P;
66 
67  using lumiera::Query;
68  using lumiera::QueryKey;
69  using lumiera::query::isFakeBypass;
70 
71  using util::contains;
72  using util::isnil;
73 
74  using boost::any;
75  using boost::any_cast;
76 
77 
78 
79 
80 
81  namespace { // internal details
82 
84  template<class TY>
85  struct WrapReturn { typedef P<TY> Wrapper; };
86 
87  template<>
88  struct WrapReturn<ProcPatt> { typedef PProcPatt Wrapper; };
89 
90 
94  template<typename TY>
95  inline bool
96  is_defaults_query (Query<TY> const& querySpec)
97  {
98  return querySpec.usesPredicate ("default");
99  }
100 
101  template<typename WRA>
102  inline bool
103  exists(WRA const& solution)
104  {
105  return bool(solution);
106  }
107 
108  } // details (end)
109 
110 
111 
121  class MockTable
122  : public steam::ConfigResolver
123  {
124  typedef std::map<QueryKey,any> Tab;
125 
126  Tab answer_;
127  bool isInit_;
128 
129  protected:
130  MockTable ();
131  virtual void reset();
132  any const& fetch_from_table_for (QueryKey const& query);
133 
134  // special cases....
135  template<class TY>
136  bool detect_case (typename WrapReturn<TY>::Wrapper&, Query<TY> const&);
137  bool fabricate_matching_new_Pipe (Query<Pipe> const& q, string const& pipeID, string const& streamID);
138  bool fabricate_just_new_Pipe (Query<Pipe> const& q);
139  bool fabricate_ProcPatt_on_demand (Query<const ProcPatt> const& q);
140  bool fabricate_Timeline_on_demand (Query<asset::Timeline> const& q);
141  bool fabricate_Sequence_on_demand (Query<asset::Sequence> const& q);
142 
143  template<class TY>
144  bool set_new_mock_solution (Query<TY> const& q, typename WrapReturn<TY>::Wrapper& candidate);
145 
146 
147  private:
148  void fill_mock_table ();
149 
151  template<class STRU>
152  any&
153  item (string const& querySpec)
154  {
155  return answer_[Query<STRU>(querySpec)];
156  }
157  };
158 
159 
165  template<class TY, class BASE>
166  class LookupPreconfigured : public BASE
167  {
168  typedef typename WrapReturn<TY>::Wrapper Ret;
169 
170 
172  virtual bool
173  resolve (Ret& solution, Query<TY> const& q)
174  {
175  any const& entry = this->fetch_from_table_for(q);
176  if (!isnil (entry))
177  {
178  Ret const& candidate (any_cast<Ret const&> (entry));
179  if (! solution
180  ||(solution && solution == candidate) // simulates a real unification
181  )
182  return exists (solution = candidate);
183  }
184  return try_special_case(solution, q);
185  }
186 
187  private:
188  bool
189  try_special_case (Ret& solution, Query<TY> const& q)
190  {
191  if (solution && isFakeBypass(q)) // backdoor for tests
192  return exists (solution);
193 
194  if (is_defaults_query (q))
195  {
196  Query<TY> defaultsQuery = q.rebuild().removeTerm("default");
197  return exists (solution = Session::current->defaults (defaultsQuery));
198  } // may lead to recursion
199 
200  if (this->detect_case (solution, q))
201  return resolve (solution, q);
202 
203  return exists (solution = Ret()); // fail: return default-constructed empty smart ptr
204  }
205  };
206 
207 
209  template<class TY>
210  inline bool
211  MockTable::detect_case (typename WrapReturn<TY>::Wrapper&, Query<TY> const&)
212  {
213  return false;
214  }
215  template<>
216  inline bool
217  MockTable::detect_case (WrapReturn<Pipe>::Wrapper& candidate, Query<Pipe> const& q)
218  {
219  if (q.usesPredicate ("make"))
220  // used by tests to force fabrication of a new "solution"
221  return fabricate_just_new_Pipe (q);
222 
223  const string pipeID = q.extractID("pipe");
224  const string streamID = q.extractID("stream");
225 
226  if (candidate && pipeID == candidate->getPipeID())
227  return set_new_mock_solution (q, candidate); // "learn" this solution to be "valid"
228 
229  if (!isnil(pipeID) && !isnil(streamID))
230  return fabricate_matching_new_Pipe (q, pipeID, streamID);
231 
232  if (!candidate && (!isnil(streamID) || !isnil(pipeID)))
233  return fabricate_just_new_Pipe (q);
234 
235  return false;
236  }
237 
238  template<>
239  inline bool
240  MockTable::detect_case (WrapReturn<const ProcPatt>::Wrapper& candidate, Query<const ProcPatt> const& q)
241  {
242  const string streamID = q.extractID("stream");
243 
244  if (!candidate && !isnil(streamID))
245  return fabricate_ProcPatt_on_demand (q);
246  return false;
247  }
248 
249  template<>
250  inline bool
251  MockTable::detect_case (WrapReturn<asset::Timeline>::Wrapper& candidate, Query<asset::Timeline> const& q)
252  {
253  if (!candidate)
254  return fabricate_Timeline_on_demand (q);
255  return bool(candidate);
256  }
257 
258  template<>
259  inline bool
260  MockTable::detect_case (WrapReturn<asset::Sequence>::Wrapper& candidate, Query<asset::Sequence> const& q)
261  {
262  if (!candidate)
263  return fabricate_Sequence_on_demand (q);
264  return bool(candidate);
265  }
266 
267 
268 
269 
270 
271 
278  : public InstantiateChained < steam::InterfaceTypes
279  , LookupPreconfigured // building block used for each of the types
280  , MockTable // for implementing the base class (interface)
281  >
282  {
283  protected:
284  MockConfigRules ();
285  friend class lib::DependencyFactory<MockConfigRules>;
286  friend class lib::DependInject<ConfigResolver>::Local<MockConfigRules>;
287  public:
288  virtual ~MockConfigRules() {}
289 
290 
291  /* === implementation of additional functions on the ConfigRules interface goes here === */
292  };
293 
294 
295 
296  } // namespace query
297 }}} // namespace steam::mobject::session
298 #endif
Utilities to support working with predicate queries.
"Processing Pattern" is a structural Asset representing information how to build some part of the ren...
Definition: procpatt.hpp:52
AnyPair entry(Query< TY > const &query, typename WrapReturn< TY >::Wrapper &obj)
helper to simplify creating mock table entries, wrapped correctly
Per type specific configuration of instances created as service dependencies.
Customised refcounting smart pointer.
static session::SessManager & current
access point to the current Session
Definition: session.hpp:120
Steam-Layer implementation namespace root.
Definition of the concrete frontend for rule based configuration within the session.
Namespace of Session and user visible high-level objects.
Definition: sequence.hpp:65
bool is_defaults_query(Query< TY > const &querySpec)
helper detecting if a query actually intended to retrieve a "default" object.
structural asset corresponding to the part of the model forming a processing pipe for generating medi...
Definition: pipe.hpp:70
Facade: Dummy Implementation of the query interface.
The (current) Session holds all the user visible content to be edited and manipulated within the Lumi...
Definition: session.hpp:100
This framework allows to (re)configure the lib::Depend front-end for dependency-injection.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Build a single inheritance chain of template instantiations.
Definition: generator.hpp:120
Wrapper for indexing and ordering.
Definition: query.hpp:388
virtual bool resolve(Ret &solution, Query< TY > const &q)
(dummy) implementation of the QueryHandler interface
bool detect_case(typename WrapReturn< TY >::Wrapper &, Query< TY > const &)
Hook for treating very special cases for individual types only.
building block providing the mock implementation for a single type.
Helper to abstract creation and lifecycle of a dependency.
Definition: depend.hpp:125
Primary Interface to the current Session.
any & item(string const &querySpec)
shortcut for simply accessing a table entry
the actual table holding preconfigured answers packaged as boost::any objects.
user-visible Interface to the ConfigRules subsystem.
The asset subsystem of the Steam-Layer.
Definition: wrapperptr.hpp:35
Generic interface to express a query for specifically typed result elements exposing some capabilitie...
Definition: query.hpp:270
string extractID(Symbol predicate) const
convenience shortcut to extract a desired name-ID.
Definition: query.hpp:586
bool contains(SEQ const &cont, typename SEQ::const_reference val)
shortcut for brute-force containment test in any sequential container
Definition: util.hpp:255
string resolve(fsys::path iniSpec)
use the general mechanism for resolving a search path to get the absolute path of the setup...
Definition: basic-setup.cpp:56