SL-11216: Introduce generic LLStoreListener<T> to capture event data.
LLStoreListener is an adapter initialized with a reference to an LLEventPump on which to listen, a reference to a variable into which to store received data, and an optional llsd::drill() path to extract desired data from each event received on the subject LLEventPump. In effect, LLStoreListener is like a miniature LLEventAPI whose only operation is to store to its destination variable.meow-7.2.2
parent
a2379d6871
commit
8bb8d7e5b7
|
|
@ -32,6 +32,7 @@
|
|||
#include "llevents.h"
|
||||
#include "stdtypes.h"
|
||||
#include "lltimer.h"
|
||||
#include "llsdutil.h"
|
||||
#include <boost/function.hpp>
|
||||
|
||||
/**
|
||||
|
|
@ -376,4 +377,54 @@ private:
|
|||
std::size_t mBatchSize;
|
||||
};
|
||||
|
||||
/**
|
||||
* LLStoreListener self-registers on the LLEventPump of interest, and
|
||||
* unregisters on destruction. As long as it exists, a particular element is
|
||||
* extracted from every event that comes through the upstream LLEventPump and
|
||||
* stored into the target variable.
|
||||
*
|
||||
* This is implemented as a subclass of LLEventFilter, though strictly
|
||||
* speaking it isn't really a "filter" at all: it never passes incoming events
|
||||
* to its own listeners, if any.
|
||||
*
|
||||
* TBD: A variant based on output iterators that stores and then increments
|
||||
* the iterator. Useful with boost::coroutine2!
|
||||
*/
|
||||
template <typename T>
|
||||
class LLStoreListener: public LLEventFilter
|
||||
{
|
||||
public:
|
||||
// pass target and optional path to element
|
||||
LLStoreListener(T& target, const LLSD& path=LLSD(), bool consume=false):
|
||||
LLEventFilter("store"),
|
||||
mTarget(target),
|
||||
mPath(path),
|
||||
mConsume(consume)
|
||||
{}
|
||||
// construct and connect
|
||||
LLStoreListener(LLEventPump& source, T& target, const LLSD& path=LLSD(), bool consume=false):
|
||||
LLEventFilter(source, "store"),
|
||||
mTarget(target),
|
||||
mPath(path),
|
||||
mConsume(consume)
|
||||
{}
|
||||
|
||||
// Calling post() with an LLSD event extracts the element indicated by
|
||||
// path, then stores it to mTarget.
|
||||
virtual bool post(const LLSD& event)
|
||||
{
|
||||
// Extract the element specified by 'mPath' from 'event'. To perform a
|
||||
// generic type-appropriate store through mTarget, construct an
|
||||
// LLSDParam<T> and store that, thus engaging LLSDParam's custom
|
||||
// conversions.
|
||||
mTarget = LLSDParam<T>(llsd::drill(event, mPath));
|
||||
return mConsume;
|
||||
}
|
||||
|
||||
private:
|
||||
T& mTarget;
|
||||
const LLSD mPath;
|
||||
const bool mConsume;
|
||||
};
|
||||
|
||||
#endif /* ! defined(LL_LLEVENTFILTER_H) */
|
||||
|
|
|
|||
Loading…
Reference in New Issue