DEV-31980: Extend LLEventDispatcher to handle const as well as non-const

methods. Introduce LLAppViewerListener based on LLDispatchListener,
instantiate a static one in llappviewer.cpp. Initial implementation only
supports ["op"] == "requestQuit".
meow-7.2.2
Nat Goodspeed 2009-06-24 01:54:57 +00:00
parent ad1ecb126b
commit da46eb9e7e
5 changed files with 187 additions and 16 deletions

View File

@ -23,6 +23,40 @@
#include "llevents.h"
class LLSD;
/*==========================================================================*|
class LLEventDispatcher;
namespace LLEventDetail
{
/// For a given call to add(), decide whether we're being passed an
/// unbound member function pointer or a plain callable.
/// Default case.
template <typename CALLABLE>
struct AddCallable
{
void operator()(LLEventDispatcher& disp, const std::string& name,
const CALLABLE& callable, const LLSD& required);
};
/// Unbound member function pointer
template <class CLASS>
struct AddCallable<void (CLASS::*)(const LLSD&)>
{
typedef void (CLASS::*Method)(const LLSD&);
void operator()(LLEventDispatcher& disp, const std::string& name,
Method method, const LLSD& required);
};
/// Unbound const member function pointer
template <class CLASS>
struct AddCallable<void (CLASS::*)(const LLSD&) const>
{
typedef void (CLASS::*Method)(const LLSD&) const;
void operator()(LLEventDispatcher& disp, const std::string& name,
Method method, const LLSD& required);
};
}
|*==========================================================================*/
/**
* Given an LLSD map, examine a string-valued key and call a corresponding
@ -39,28 +73,36 @@ public:
/// Accept any C++ callable, typically a boost::bind() expression
typedef boost::function<void(const LLSD&)> Callable;
/// Register a @a callable by @a name. The optional @a required parameter
/// is used to validate the structure of each incoming event (see
/// llsd_matches()).
/**
* Register a @a callable by @a name. The optional @a required parameter
* is used to validate the structure of each incoming event (see
* llsd_matches()).
*/
void add(const std::string& name, const Callable& callable, const LLSD& required=LLSD());
/*==========================================================================*|
{
LLEventDetail::AddCallable<CALLABLE>()(*this, name, callable, required);
}
|*==========================================================================*/
/// Special case: a subclass of this class can register a @a method
/// without explicitly specifying the <tt>boost::bind()</tt> expression.
/// The optional @a required parameter is used to validate the structure
/// of each incoming event (see llsd_matches()).
/**
* Special case: a subclass of this class can pass an unbound member
* function pointer without explicitly specifying the
* <tt>boost::bind()</tt> expression.
*/
template <class CLASS>
void add(const std::string& name, void (CLASS::*method)(const LLSD&),
const LLSD& required=LLSD())
{
CLASS* downcast = dynamic_cast<CLASS*>(this);
if (! downcast)
{
addFail(name, typeid(CLASS).name());
}
else
{
add(name, boost::bind(method, downcast, _1), required);
}
addMethod<CLASS>(name, method, required);
}
/// Overload for both const and non-const methods
template <class CLASS>
void add(const std::string& name, void (CLASS::*method)(const LLSD&) const,
const LLSD& required=LLSD())
{
addMethod<CLASS>(name, method, required);
}
/// Unregister a callable
@ -78,6 +120,19 @@ public:
void operator()(const LLSD& event) const;
private:
template <class CLASS, typename METHOD>
void addMethod(const std::string& name, const METHOD& method, const LLSD& required)
{
CLASS* downcast = dynamic_cast<CLASS*>(this);
if (! downcast)
{
addFail(name, typeid(CLASS).name());
}
else
{
add(name, boost::bind(method, downcast, _1), required);
}
}
void addFail(const std::string& name, const std::string& classname) const;
/// try to dispatch, return @c true if success
bool attemptCall(const std::string& name, const LLSD& event) const;
@ -87,6 +142,50 @@ private:
DispatchMap mDispatch;
};
/*==========================================================================*|
/// Have to implement these template specialization methods after
/// LLEventDispatcher so they can use its methods
template <typename CALLABLE>
void LLEventDetail::AddCallable<CALLABLE>::operator()(
LLEventDispatcher& disp, const std::string& name, const CALLABLE& callable, const LLSD& required)
{
disp.addImpl(name, callable, required);
}
template <class CLASS>
void LLEventDetail::AddCallable<void (CLASS::*)(const LLSD&)>::operator()(
LLEventDispatcher& disp, const std::string& name, const Method& method, const LLSD& required)
{
CLASS* downcast = dynamic_cast<CLASS*>(&disp);
if (! downcast)
{
disp.addFail(name, typeid(CLASS).name());
}
else
{
disp.addImpl(name, boost::bind(method, downcast, _1), required);
}
}
/// Have to overload for both const and non-const methods
template <class CLASS>
void LLEventDetail::AddCallable<void (CLASS::*)(const LLSD&) const>::operator()(
LLEventDispatcher& disp, const std::string& name, const Method& method, const LLSD& required)
{
// I am severely bummed that I have, as yet, found no way short of a
// macro to avoid replicating the (admittedly brief) body of this overload.
CLASS* downcast = dynamic_cast<CLASS*>(&disp);
if (! downcast)
{
disp.addFail(name, typeid(CLASS).name());
}
else
{
disp.addImpl(name, boost::bind(method, downcast, _1), required);
}
}
|*==========================================================================*/
/**
* Bundle an LLEventPump and a listener with an LLEventDispatcher. A class
* that contains (or derives from) LLDispatchListener need only specify the

View File

@ -73,6 +73,7 @@ set(viewer_SOURCE_FILES
llagentpilot.cpp
llanimstatelabels.cpp
llappviewer.cpp
llappviewerlistener.cpp
llassetuploadresponders.cpp
llassetuploadqueue.cpp
llaudiosourcevo.cpp
@ -468,6 +469,7 @@ set(viewer_HEADER_FILES
llanimstatelabels.h
llappearance.h
llappviewer.h
llappviewerlistener.h
llassetuploadresponders.h
llassetuploadqueue.h
llaudiosourcevo.h

View File

@ -177,7 +177,10 @@
//----------------------------------------------------------------------------
// llviewernetwork.h
#include "llviewernetwork.h"
// define a self-registering event API object
#include "llappviewerlistener.h"
static LLAppViewerListener sAppViewerListener("LLAppViewer", LLAppViewer::instance());
////// Windows-specific includes to the bottom - nasty defines in these pollute the preprocessor
//

View File

@ -0,0 +1,33 @@
/**
* @file llappviewerlistener.cpp
* @author Nat Goodspeed
* @date 2009-06-23
* @brief Implementation for llappviewerlistener.
*
* $LicenseInfo:firstyear=2009&license=viewergpl$
* Copyright (c) 2009, Linden Research, Inc.
* $/LicenseInfo$
*/
// Precompiled header
#include "llviewerprecompiledheaders.h"
// associated header
#include "llappviewerlistener.h"
// STL headers
// std headers
// external library headers
// other Linden headers
#include "llappviewer.h"
LLAppViewerListener::LLAppViewerListener(const std::string& pumpname, LLAppViewer* llappviewer):
LLDispatchListener(pumpname, "op"),
mAppViewer(llappviewer)
{
// add() every method we want to be able to invoke via this event API.
add("requestQuit", &LLAppViewerListener::requestQuit);
}
void LLAppViewerListener::requestQuit(const LLSD& event) const
{
mAppViewer->requestQuit();
}

View File

@ -0,0 +1,34 @@
/**
* @file llappviewerlistener.h
* @author Nat Goodspeed
* @date 2009-06-18
* @brief Wrap subset of LLAppViewer API in event API
*
* $LicenseInfo:firstyear=2009&license=viewergpl$
* Copyright (c) 2009, Linden Research, Inc.
* $/LicenseInfo$
*/
#if ! defined(LL_LLAPPVIEWERLISTENER_H)
#define LL_LLAPPVIEWERLISTENER_H
#include "lleventdispatcher.h"
class LLAppViewer;
class LLSD;
/// Listen on an LLEventPump with specified name for LLAppViewer request events.
class LLAppViewerListener: public LLDispatchListener
{
public:
/// Specify the pump name on which to listen, and bind the LLAppViewer
/// instance to use (e.g. LLAppViewer::instance()).
LLAppViewerListener(const std::string& pumpname, LLAppViewer* llappviewer);
private:
void requestQuit(const LLSD& event) const;
LLAppViewer* mAppViewer;
};
#endif /* ! defined(LL_LLAPPVIEWERLISTENER_H) */