Lumiera  0.pre.03
»edityourfreedom«
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 using lumiera::query::LERR_(CAPABILITY_QUERY);
57 
58 
59 namespace lumiera{
60 namespace query {
61 
62 
63 
64 
67  : defsRegistry_(new DefsRegistry)
68  {
69  INFO (session, "Configure technical defaults of the session.");
70 
71  }
72 
73 
74 
78 
79 
80  void
82  {
83  defsRegistry_.reset(new DefsRegistry);
84  }
85 
86 
87 
88  template<class TAR>
89  P<TAR>
90  DefsManager::search (const Query<TAR>& capabilities)
91  {
92  P<TAR> res;
93  QueryHandler<TAR>& typeHandler = ConfigResolver::instance();
94  for (DefsRegistry::Iter<TAR> i = defsRegistry_->candidates(capabilities);
95  bool(*i) ; ++i )
96  {
97  res = *i;
98  typeHandler.resolve (res, capabilities);
99  if (res)
100  return res;
101  }
102  return res; // "no solution found"
103  }
104 
105 
106  template<class TAR>
107  P<TAR>
108  DefsManager::create (const Query<TAR>& capabilities)
109  {
110  P<TAR> res;
111  QueryHandler<TAR>& typeHandler = ConfigResolver::instance();
112  typeHandler.resolve (res, capabilities);
113  if (res)
114  defsRegistry_->put (res, capabilities);
115  return res;
116  }
117 
118 
119  template<class TAR>
120  bool
121  DefsManager::define (const P<TAR>& defaultObj, const Query<TAR>& capabilities)
122  {
123  P<TAR> candidate (defaultObj);
124  QueryHandler<TAR>& typeHandler = ConfigResolver::instance();
125  typeHandler.resolve (candidate, capabilities);
126  if (!candidate)
127  return false;
128  else
129  return defsRegistry_->put (candidate, capabilities);
130  }
131 
132 
133  template<class TAR>
134  bool
135  DefsManager::forget (const P<TAR>& defaultObj)
136  {
137  return defsRegistry_->forget (defaultObj);
138  }
139 
140 
141  template<class TAR>
142  P<TAR>
144  {
145  P<TAR> res (search (capabilities));
146  if (res)
147  return res;
148  else
149  res = create (capabilities); // not yet known as default, create new
150 
151  if (!res)
152  throw lumiera::error::Config (_Fmt("The following Query could not be resolved: %s.")
153  % capabilities.rebuild().asKey()
154  , LUMIERA_ERROR_CAPABILITY_QUERY );
155  else
156  return res;
157  }
158 
159 }} // namespace lumiera::query
160 #endif /* LUMIERA_QUERY_DEFS_MANAGER_IMPL_H */
used for enumerating solutions
Builder rebuild() const
Definition: query.hpp:581
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 Proc-Layer code can use this QueryHandler to retrieve instances of th...
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.
unique_ptr< impl::DefsRegistry > defsRegistry_
lib::P< TAR > search(Query< TAR > const &)
search through the registered defaults, never create anything.
Management of defaults and default rules.
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
#define LERR_(_NAME_)
Definition: error.hpp:58
Lumiera error handling (C++ interface).
user-visible Interface to the ConfigRules subsystem.
Namespace of Session and user visible high-level objects.
Lumiera public interface.
Interface for accessing rule based configuration.
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition: trait.hpp:76
LumieraError< LERR_(CONFIG), Invalid > Config
Definition: error.hpp:217
Generic interface to express a query for specifically typed result elements exposing some capabilitie...
Definition: query.hpp:279