Lumiera  0.pre.03
»edityourfreedom«
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
49 #include "proc/mobject/session.hpp"
50 #include "proc/config-resolver.hpp"
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 proc {
63 namespace mobject {
64 namespace session {
65  namespace query {
66 
67  namespace asset = proc::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;
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 proc::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);
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
221  {
222  return false;
223  }
224  template<>
225  inline bool
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
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
261  {
262  if (!candidate)
263  return fabricate_Timeline_on_demand (q);
264  return bool(candidate);
265  }
266 
267  template<>
268  inline bool
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 < proc::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 ();
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 proc::mobject::session
307 #endif
bool fabricate_Timeline_on_demand(Query< asset::Timeline > const &q)
special case: fabricate new Timeline, maybe using specific sub-objects as hinted by the IDs given wit...
The (current) Session holds all the user visible content to be edited and manipulated within the Lumi...
Definition: session.hpp:107
Utilities to support working with predicate queries.
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition: trait.hpp:76
lib::P< const asset::ProcPatt > PProcPatt
The asset subsystem of the Proc-Layer.
Definition: id-scheme.hpp:64
bool fabricate_just_new_Pipe(Query< Pipe > const &q)
special case: create a new pipe for a specific stream ID
Per type specific configuration of instances created as service dependencies.
bool detect_case(typename WrapReturn< TY >::Wrapper &, Query< TY > const &)
Hook for treating very special cases for individual types only.
static session::SessManager & current
access point to the current Session
Definition: session.hpp:127
Customised refcounting smart pointer.
bool fabricate_ProcPatt_on_demand(Query< const ProcPatt > const &q)
special case: create/retrieve new processing pattern for given stream ID...
Definition of the concrete frontend for rule based configuration within the session.
virtual void reset()
clear the contents of the mock solution table.
This framework allows to (re)configure the lib::Depend front-end for dependency-injection.
bool contains(MAP &map, typename MAP::key_type const &key)
shortcut for containment test on a map
Definition: util.hpp:205
bool isFakeBypass(lumiera::QueryKey const &q)
Builder rebuild() const
Definition: query.hpp:581
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
bool try_special_case(Ret &solution, Query< TY > const &q)
MockConfigRules()
to be used only by the singleton factory
building block providing the mock implementation for a single type.
string extractID(Symbol predicate) const
convenience shortcut to extract a desired name-ID.
Definition: query.hpp:595
any & item(string const &querySpec)
shortcut for simply accessing a table entry
any const & fetch_from_table_for(QueryKey const &query)
this is the (preliminary/mock) implementation handling queries for objects of a specific type and wit...
Wrapper for indexing and ordering.
Definition: query.hpp:397
bool isnil(lib::time::Duration const &dur)
Definition: timevalue.hpp:642
the actual table holding preconfigured answers packaged as boost::any objects.
user-visible Interface to the ConfigRules subsystem.
Namespace of Session and user visible high-level objects.
bool fabricate_matching_new_Pipe(Query< Pipe > const &q, string const &pipeID, string const &streamID)
special case: create a new pipe with matching pipe and stream IDs on the fly when referred...
structural asset corresponding to the part of the model forming a processing pipe for generating medi...
Definition: pipe.hpp:79
Helper to abstract creation and lifecycle of a dependency.
Definition: depend.hpp:120
Facade: Dummy Implementation of the query interface.
Primary Interface to the current Session.
virtual bool resolve(Ret &solution, Query< TY > const &q)
(dummy) implementation of the QueryHandler interface
Proc-Layer implementation namespace root.
Definition: id-scheme.hpp:63
Build a single inheritance chain of template instantiations.
Definition: generator.hpp:129
"Processing Pattern" is a structural Asset representing information how to build some part of the ren...
Definition: procpatt.hpp:61
bool set_new_mock_solution(Query< TY > const &q, typename WrapReturn< TY >::Wrapper &candidate)
for entering "valid" solutions on-the-fly from tests
bool fabricate_Sequence_on_demand(Query< asset::Sequence > const &q)
special case: fabricate new Timeline, maybe using ID specs from the query...
bool usesPredicate(Symbol predicate) const
Definition: query.hpp:603
Generic interface to express a query for specifically typed result elements exposing some capabilitie...
Definition: query.hpp:279
void fill_mock_table()
hard coded answers to configuration queries.
friend class Local