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) 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 
23 
46 #ifndef MOBJECT_SESSION_QUERY_FAKECONFIGRULES_H
47 #define MOBJECT_SESSION_QUERY_FAKECONFIGRULES_H
48 
51 #include "lib/depend-inject.hpp"
52 #include "lib/query-util.hpp"
53 #include "lib/util.hpp"
54 #include "lib/p.hpp"
55 
56 #include <boost/any.hpp>
57 #include <string>
58 #include <map>
59 
60 
61 
62 namespace steam {
63 namespace mobject {
64 namespace session {
65  namespace query {
66 
67  namespace asset = steam::asset;
68 
69  using asset::Pipe;
70  using asset::ProcPatt;
71  using asset::PProcPatt;
74  using lib::P;
75 
76  using lumiera::Query;
77  using lumiera::QueryKey;
78  using lumiera::query::isFakeBypass;
79 
80  using util::contains;
81  using util::isnil;
82 
83  using boost::any;
84  using boost::any_cast;
85 
86 
87 
88 
89 
90  namespace { // internal details
91 
93  template<class TY>
94  struct WrapReturn { typedef P<TY> Wrapper; };
95 
96  template<>
97  struct WrapReturn<ProcPatt> { typedef PProcPatt Wrapper; };
98 
99 
103  template<typename TY>
104  inline bool
105  is_defaults_query (Query<TY> const& querySpec)
106  {
107  return querySpec.usesPredicate ("default");
108  }
109 
110  template<typename WRA>
111  inline bool
112  exists(WRA const& solution)
113  {
114  return bool(solution);
115  }
116 
117  } // details (end)
118 
119 
120 
130  class MockTable
131  : public steam::ConfigResolver
132  {
133  typedef std::map<QueryKey,any> Tab;
134 
135  Tab answer_;
136  bool isInit_;
137 
138  protected:
139  MockTable ();
140  virtual void reset();
141  any const& fetch_from_table_for (QueryKey const& query);
142 
143  // special cases....
144  template<class TY>
145  bool detect_case (typename WrapReturn<TY>::Wrapper&, Query<TY> const&);
146  bool fabricate_matching_new_Pipe (Query<Pipe> const& q, string const& pipeID, string const& streamID);
147  bool fabricate_just_new_Pipe (Query<Pipe> const& q);
148  bool fabricate_ProcPatt_on_demand (Query<const ProcPatt> const& q);
149  bool fabricate_Timeline_on_demand (Query<asset::Timeline> const& q);
150  bool fabricate_Sequence_on_demand (Query<asset::Sequence> const& q);
151 
152  template<class TY>
153  bool set_new_mock_solution (Query<TY> const& q, typename WrapReturn<TY>::Wrapper& candidate);
154 
155 
156  private:
157  void fill_mock_table ();
158 
160  template<class STRU>
161  any&
162  item (string const& querySpec)
163  {
164  return answer_[Query<STRU>(querySpec)];
165  }
166  };
167 
168 
174  template<class TY, class BASE>
175  class LookupPreconfigured : public BASE
176  {
177  typedef typename WrapReturn<TY>::Wrapper Ret;
178 
179 
181  virtual bool
182  resolve (Ret& solution, Query<TY> const& q)
183  {
184  any const& entry = this->fetch_from_table_for(q);
185  if (!isnil (entry))
186  {
187  Ret const& candidate (any_cast<Ret const&> (entry));
188  if (! solution
189  ||(solution && solution == candidate) // simulates a real unification
190  )
191  return exists (solution = candidate);
192  }
193  return try_special_case(solution, q);
194  }
195 
196  private:
197  bool
198  try_special_case (Ret& solution, Query<TY> const& q)
199  {
200  if (solution && isFakeBypass(q)) // backdoor for tests
201  return exists (solution);
202 
203  if (is_defaults_query (q))
204  {
205  Query<TY> defaultsQuery = q.rebuild().removeTerm("default");
206  return exists (solution = Session::current->defaults (defaultsQuery));
207  } // may lead to recursion
208 
209  if (this->detect_case (solution, q))
210  return resolve (solution, q);
211 
212  return exists (solution = Ret()); // fail: return default-constructed empty smart ptr
213  }
214  };
215 
216 
218  template<class TY>
219  inline bool
220  MockTable::detect_case (typename WrapReturn<TY>::Wrapper&, Query<TY> const&)
221  {
222  return false;
223  }
224  template<>
225  inline bool
226  MockTable::detect_case (WrapReturn<Pipe>::Wrapper& candidate, Query<Pipe> const& q)
227  {
228  if (q.usesPredicate ("make"))
229  // used by tests to force fabrication of a new "solution"
230  return fabricate_just_new_Pipe (q);
231 
232  const string pipeID = q.extractID("pipe");
233  const string streamID = q.extractID("stream");
234 
235  if (candidate && pipeID == candidate->getPipeID())
236  return set_new_mock_solution (q, candidate); // "learn" this solution to be "valid"
237 
238  if (!isnil(pipeID) && !isnil(streamID))
239  return fabricate_matching_new_Pipe (q, pipeID, streamID);
240 
241  if (!candidate && (!isnil(streamID) || !isnil(pipeID)))
242  return fabricate_just_new_Pipe (q);
243 
244  return false;
245  }
246 
247  template<>
248  inline bool
249  MockTable::detect_case (WrapReturn<const ProcPatt>::Wrapper& candidate, Query<const ProcPatt> const& q)
250  {
251  const string streamID = q.extractID("stream");
252 
253  if (!candidate && !isnil(streamID))
254  return fabricate_ProcPatt_on_demand (q);
255  return false;
256  }
257 
258  template<>
259  inline bool
260  MockTable::detect_case (WrapReturn<asset::Timeline>::Wrapper& candidate, Query<asset::Timeline> const& q)
261  {
262  if (!candidate)
263  return fabricate_Timeline_on_demand (q);
264  return bool(candidate);
265  }
266 
267  template<>
268  inline bool
269  MockTable::detect_case (WrapReturn<asset::Sequence>::Wrapper& candidate, Query<asset::Sequence> const& q)
270  {
271  if (!candidate)
272  return fabricate_Sequence_on_demand (q);
273  return bool(candidate);
274  }
275 
276 
277 
278 
279 
280 
287  : public InstantiateChained < steam::InterfaceTypes
288  , LookupPreconfigured // building block used for each of the types
289  , MockTable // for implementing the base class (interface)
290  >
291  {
292  protected:
293  MockConfigRules ();
294  friend class lib::DependencyFactory<MockConfigRules>;
295  friend class lib::DependInject<ConfigResolver>::Local<MockConfigRules>;
296  public:
297  virtual ~MockConfigRules() {}
298 
299 
300  /* === implementation of additional functions on the ConfigRules interface goes here === */
301  };
302 
303 
304 
305  } // namespace query
306 }}} // namespace steam::mobject::session
307 #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:61
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:129
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:74
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:79
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:109
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:129
Wrapper for indexing and ordering.
Definition: query.hpp:397
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:134
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:44
Generic interface to express a query for specifically typed result elements exposing some capabilitie...
Definition: query.hpp:279
string extractID(Symbol predicate) const
convenience shortcut to extract a desired name-ID.
Definition: query.hpp:595
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:65