Lumiera  0.pre.03
»edit your freedom«
defs-manager-impl.hpp
Go to the documentation of this file.
1 /*
2  DEFS-MANAGER-IMPL.h - access to preconfigured default objects and definitions
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 
42 #ifndef LUMIERA_QUERY_DEFS_MANAGER_IMPL_H
43 #define LUMIERA_QUERY_DEFS_MANAGER_IMPL_H
44 
45 
48 #include "common/config-rules.hpp"
49 #include "lib/format-string.hpp"
50 #include "lib/error.hpp"
51 
52 using util::_Fmt;
53 
56 
57 
58 namespace lumiera{
59 namespace query {
60 
61 
62 
63 
66  : defsRegistry_(new DefsRegistry)
67  {
68  INFO (session, "Configure technical defaults of the session.");
69 
70  }
71 
72 
73 
77 
78 
79  void
81  {
82  defsRegistry_.reset(new DefsRegistry);
83  }
84 
85 
86 
87  template<class TAR>
88  P<TAR>
89  DefsManager::search (const Query<TAR>& capabilities)
90  {
91  P<TAR> res;
92  QueryHandler<TAR>& typeHandler = ConfigResolver::instance();
93  for (DefsRegistry::Iter<TAR> i = defsRegistry_->candidates(capabilities);
94  bool(*i) ; ++i )
95  {
96  res = *i;
97  typeHandler.resolve (res, capabilities);
98  if (res)
99  return res;
100  }
101  return res; // "no solution found"
102  }
103 
104 
105  template<class TAR>
106  P<TAR>
107  DefsManager::create (const Query<TAR>& capabilities)
108  {
109  P<TAR> res;
110  QueryHandler<TAR>& typeHandler = ConfigResolver::instance();
111  typeHandler.resolve (res, capabilities);
112  if (res)
113  defsRegistry_->put (res, capabilities);
114  return res;
115  }
116 
117 
118  template<class TAR>
119  bool
120  DefsManager::define (const P<TAR>& defaultObj, const Query<TAR>& capabilities)
121  {
122  P<TAR> candidate (defaultObj);
123  QueryHandler<TAR>& typeHandler = ConfigResolver::instance();
124  typeHandler.resolve (candidate, capabilities);
125  if (!candidate)
126  return false;
127  else
128  return defsRegistry_->put (candidate, capabilities);
129  }
130 
131 
132  template<class TAR>
133  bool
134  DefsManager::forget (const P<TAR>& defaultObj)
135  {
136  return defsRegistry_->forget (defaultObj);
137  }
138 
139 
140  template<class TAR>
141  P<TAR>
142  DefsManager::operator() (const Query<TAR>& capabilities)
143  {
144  P<TAR> res (search (capabilities));
145  if (res)
146  return res;
147  else
148  res = create (capabilities); // not yet known as default, create new
149 
150  if (!res)
151  throw error::Config (_Fmt("The following Query could not be resolved: %s.")
152  % capabilities.rebuild().asKey()
153  , LERR_(CAPABILITY_QUERY) );
154  else
155  return res;
156  }
157 
158 }} // namespace lumiera::query
159 #endif /* LUMIERA_QUERY_DEFS_MANAGER_IMPL_H */
used for enumerating solutions
bool define(lib::P< TAR > const &, Query< TAR > const &=Query< TAR >())
register the given object as default, after ensuring it fulfils the query.
the "front side" interface: the Steam-Layer code can use this QueryHandler to retrieve instances of t...
lib::P< TAR > operator()(Query< TAR > const &)
common access point: retrieve the default object fulfilling some given conditions.
Front-end for printf-style string template interpolation.
virtual bool resolve(P< TY > &solution, Query< TY > const &q)=0
try to find or create an object of type TY fulfilling the given query.
lib::P< TAR > create(Query< TAR > const &)
retrieve an object fulfilling the query and register it as default.
DefsManager() noexcept
initialise the most basic internal defaults.
A front-end for using printf-style formatting.
Namespace of Session and user visible high-level objects.
Definition: sequence.hpp:74
lib::P< TAR > search(Query< TAR > const &)
search through the registered defaults, never create anything.
Management of defaults and default rules.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
A piece of implementation code factored out into a separate header (include).
bool forget(lib::P< TAR > const &)
remove the defaults registration of the given object, if there was such
Lumiera error handling (C++ interface).
Lumiera public interface.
Definition: advice.cpp:113
Interface for accessing rule based configuration.
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition: trait.hpp:80
user-visible Interface to the ConfigRules subsystem.
Generic interface to express a query for specifically typed result elements exposing some capabilitie...
Definition: query.hpp:279