Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
advice.cpp
Go to the documentation of this file.
1/*
2 Advice - generic loosely coupled interaction guided by symbolic pattern
3
4 Copyright (C)
5 2010, 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
85#include "lib/error.hpp"
86#include "lib/nocopy.hpp"
87#include "lib/del-stash.hpp"
88#include "lib/depend.hpp"
89#include "lib/symbol.hpp"
90#include "lib/sync.hpp"
91#include "lib/util.hpp"
92#include "include/logging.h"
93#include "common/advice.hpp"
95
96
97using lib::Literal;
98using lib::DelStash;
99using util::unConst;
100
101typedef void (DeleterFunc)(void*);
102
103
104namespace lumiera{
105namespace advice {
106
107 namespace { // ======= implementation of the AdviceSystem ============
108
116 : public lib::Sync<>
118 {
119
122
123 public:
125 : index_()
126 {
127 TRACE (library, "Initialising Advice Index tables.");
128 }
129
131 {
132 TRACE (library, "Shutting down Advice system.");
133 }
134
135
136
137 /* == Advice data storage management == */
138
146 void*
147 allocateBuffer(size_t siz)
148 {
149 try { return new char[siz]; }
150
151 catch(std::bad_alloc&)
152 {
153 throw error::Fatal("Unable to store Advice due to memory exhaustion");
154 }
155 ERROR_LOG_AND_IGNORE (memory, "Storing a piece of Advice")
156 throw error::Fatal("Unable to store Advice data");
157 }
158
159 void
160 releaseBuffer (void* buff, size_t)
161 {
162 delete[] (char*)buff;
163 }
164
165 void
167 {
168 Lock sync{this};
169 adviceDataRegistry_.manage (entry, how_to_delete);
170 }
171
172 private:
173 void
174 discardEntry (PointOfAdvice* storedProvision)
175 {
176 if (storedProvision)
177 {
178 adviceDataRegistry_.kill (storedProvision);
179 } }
180
181
182
183 public:
184 /* == forward additions and retractions to the index == */
185
186 void
188 HashVal previous_bindingKey)
189 {
190 Lock sync{this};
191 index_.modifyRequest(previous_bindingKey, req);
192 }
193
194 void
196 {
197 Lock sync{this};
198 index_.addRequest (req);
199 }
200
201 void
203 {
204 try
205 {
206 Lock sync{this};
207 index_.removeRequest (req);
208 }
209
210 catch(...)
211 {
212 Literal errID = lumiera_error();
213 WARN (library, "Problems on deregistration of advice request: %s", errID.c());
214 }
215 }
216
217
218 void
219 publishProvision (PointOfAdvice* newProvision, const PointOfAdvice* previousProvision)
220 {
221 Lock sync{this};
222
223 if (not previousProvision and newProvision)
224 index_.addProvision (*newProvision);
225 else
226 if (previousProvision and newProvision)
227 index_.modifyProvision (*previousProvision, *newProvision);
228 else
229 if (previousProvision and not newProvision)
230 index_.removeProvision (*previousProvision);
231
232 discardEntry (unConst(previousProvision));
233 }
234
235 void
236 discardSolutions (const PointOfAdvice* existingProvision)
237 {
238 Lock sync{this};
239
240 if (existingProvision)
241 index_.removeProvision (*existingProvision);
242
243 discardEntry (unConst(existingProvision));
244 }
245 };
246
247
250
251
252 } //(End) AdviceSystem implementation
253
254
255
256
257
258
259 /* ====== AdviceLink : access point for Provisions and Requests ====== */
260
261
270 void*
272 {
273 return aSys().allocateBuffer(siz);
274 }
275
276
277 void
278 AdviceLink::releaseBuffer (void* buff, size_t siz)
279 {
280 aSys().releaseBuffer(buff, siz);
281 }
282
283
291 void
293 {
294 aSys().manageAdviceData (entry,how_to_delete);
295 }
296
297
298
299
312 void
314 {
315 const PointOfAdvice* previousProvision (getSolution());
316 this->setSolution (newProvision);
317
318 aSys().publishProvision (newProvision, previousProvision);
319 }
320
321
330 void
332 {
333 const PointOfAdvice* existingProvision (getSolution());
334 this->setSolution ( NULL );
335 aSys().discardSolutions (existingProvision);
336 }
337
338
339 void
340 AdviceLink::publishRequestBindingChange(HashVal previous_bindingKey)
341 {
342 aSys().publishRequestBindingChange (*this, previous_bindingKey);
343 }
344
345
346 void
348 {
349 aSys().registerRequest (*this);
350 }
351
352
353 void
355 {
356 aSys().deregisterRequest (*this);
357 }
358
359
360
361
362
363}} // namespace lib::advice
void() DeleterFunc(void *)
Definition advice.cpp:101
Expecting Advice and giving Advice: a cross-cutting collaboration of loosely coupled participants.
Manage a collection of deleter functions.
Definition del-stash.hpp:58
void kill(TY *obj)
void manage(TY *obj)
Access point to singletons and other kinds of dependencies designated by type.
Definition depend.hpp:281
Inline string literal.
Definition symbol.hpp:78
constexpr const char * c() const
Definition symbol.hpp:93
scoped guard to control the actual locking.
Definition sync.hpp:228
Facility for monitor object based locking.
Definition sync.hpp:210
Index datastructure for organising advice solutions.
Definition index.hpp:142
void modifyRequest(HashVal oKey, POA &entry)
Definition index.hpp:351
void addRequest(POA &entry)
Definition index.hpp:337
void modifyProvision(POA const &oldRef, POA &newEntry)
Definition index.hpp:383
void removeProvision(POA const &refEntry)
Definition index.hpp:402
void addProvision(POA &entry)
Definition index.hpp:375
void removeRequest(POA const &refEntry)
Definition index.hpp:367
Basic (abstracted) view of an advice collaboration partner, as used internally by the AdviceSystem to...
Definition advice.hpp:123
void setSolution(PointOfAdvice *solution=0)
Definition advice.hpp:152
const PointOfAdvice * getSolution() const
Definition advice.hpp:158
the system-wide service to support the implementation of advice collaborations.
Definition advice.cpp:118
void manageAdviceData(PointOfAdvice *entry, DeleterFunc *how_to_delete)
Definition advice.cpp:166
void publishProvision(PointOfAdvice *newProvision, const PointOfAdvice *previousProvision)
Definition advice.cpp:219
void publishRequestBindingChange(PointOfAdvice &req, HashVal previous_bindingKey)
Definition advice.cpp:187
void * allocateBuffer(size_t siz)
low-level allocation of storage to hold advice data
Definition advice.cpp:147
void discardSolutions(const PointOfAdvice *existingProvision)
Definition advice.cpp:236
void discardEntry(PointOfAdvice *storedProvision)
Definition advice.cpp:174
Any copy and copy construction prohibited.
Definition nocopy.hpp:38
Collecting and finally triggering deleter functions.
Singleton services and Dependency Injection.
lumiera_err lumiera_error(void)
Get and clear current error state.
Lumiera error handling (C++ interface).
#define ERROR_LOG_AND_IGNORE(_FLAG_, _OP_DESCR_)
convenience shortcut for a sequence of catch blocks just logging and consuming an error.
Definition error.hpp:267
Implementation datastructure for use by the Advice system.
return NULL
Definition llist.h:586
This header is for including and configuring NoBug.
lib::Depend< AdviceSystem > aSys
hidden implementation-level access to the AdviceSystem
Definition advice.cpp:249
LumieraError< LERR_(FATAL), Logic > Fatal
Definition error.hpp:208
Lumiera public interface.
Definition advice.hpp:102
OBJ * unConst(const OBJ *)
shortcut to save some typing when having to define const and non-const variants of member functions
Definition util.hpp:358
Mix-Ins to allow or prohibit various degrees of copying and cloning.
Marker types to indicate a literal string and a Symbol.
Object Monitor based synchronisation.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...