PageRenderTime 17ms CodeModel.GetById 2ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/lleventfilter.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 166 lines | 109 code | 24 blank | 33 comment | 3 complexity | 6a0eaa096f485f8803789657176909e2 MD5 | raw file
  1/**
  2 * @file   lleventfilter.cpp
  3 * @author Nat Goodspeed
  4 * @date   2009-03-05
  5 * @brief  Implementation for lleventfilter.
  6 * 
  7 * $LicenseInfo:firstyear=2009&license=viewerlgpl$
  8 * Second Life Viewer Source Code
  9 * Copyright (C) 2010, Linden Research, Inc.
 10 * 
 11 * This library is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU Lesser General Public
 13 * License as published by the Free Software Foundation;
 14 * version 2.1 of the License only.
 15 * 
 16 * This library is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19 * Lesser General Public License for more details.
 20 * 
 21 * You should have received a copy of the GNU Lesser General Public
 22 * License along with this library; if not, write to the Free Software
 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 24 * 
 25 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 26 * $/LicenseInfo$
 27 */
 28
 29// Precompiled header
 30#include "linden_common.h"
 31// associated header
 32#include "lleventfilter.h"
 33// STL headers
 34// std headers
 35// external library headers
 36#include <boost/bind.hpp>
 37// other Linden headers
 38#include "llerror.h"                // LL_ERRS
 39#include "llsdutil.h"               // llsd_matches()
 40
 41LLEventFilter::LLEventFilter(LLEventPump& source, const std::string& name, bool tweak):
 42    LLEventStream(name, tweak)
 43{
 44    source.listen(getName(), boost::bind(&LLEventFilter::post, this, _1));
 45}
 46
 47LLEventMatching::LLEventMatching(const LLSD& pattern):
 48    LLEventFilter("matching"),
 49    mPattern(pattern)
 50{
 51}
 52
 53LLEventMatching::LLEventMatching(LLEventPump& source, const LLSD& pattern):
 54    LLEventFilter(source, "matching"),
 55    mPattern(pattern)
 56{
 57}
 58
 59bool LLEventMatching::post(const LLSD& event)
 60{
 61    if (! llsd_matches(mPattern, event).empty())
 62        return false;
 63
 64    return LLEventStream::post(event);
 65}
 66
 67LLEventTimeoutBase::LLEventTimeoutBase():
 68    LLEventFilter("timeout")
 69{
 70}
 71
 72LLEventTimeoutBase::LLEventTimeoutBase(LLEventPump& source):
 73    LLEventFilter(source, "timeout")
 74{
 75}
 76
 77void LLEventTimeoutBase::actionAfter(F32 seconds, const Action& action)
 78{
 79    setCountdown(seconds);
 80    mAction = action;
 81    if (! mMainloop.connected())
 82    {
 83        LLEventPump& mainloop(LLEventPumps::instance().obtain("mainloop"));
 84        mMainloop = mainloop.listen(getName(), boost::bind(&LLEventTimeoutBase::tick, this, _1));
 85    }
 86}
 87
 88class ErrorAfter
 89{
 90public:
 91    ErrorAfter(const std::string& message): mMessage(message) {}
 92
 93    void operator()()
 94    {
 95        LL_ERRS("LLEventTimeout") << mMessage << LL_ENDL;
 96    }
 97
 98private:
 99    std::string mMessage;
100};
101
102void LLEventTimeoutBase::errorAfter(F32 seconds, const std::string& message)
103{
104    actionAfter(seconds, ErrorAfter(message));
105}
106
107class EventAfter
108{
109public:
110    EventAfter(LLEventPump& pump, const LLSD& event):
111        mPump(pump),
112        mEvent(event)
113    {}
114
115    void operator()()
116    {
117        mPump.post(mEvent);
118    }
119
120private:
121    LLEventPump& mPump;
122    LLSD mEvent;
123};
124
125void LLEventTimeoutBase::eventAfter(F32 seconds, const LLSD& event)
126{
127    actionAfter(seconds, EventAfter(*this, event));
128}
129
130bool LLEventTimeoutBase::post(const LLSD& event)
131{
132    cancel();
133    return LLEventStream::post(event);
134}
135
136void LLEventTimeoutBase::cancel()
137{
138    mMainloop.disconnect();
139}
140
141bool LLEventTimeoutBase::tick(const LLSD&)
142{
143    if (countdownElapsed())
144    {
145        cancel();
146        mAction();
147    }
148    return false;                   // show event to other listeners
149}
150
151LLEventTimeout::LLEventTimeout() {}
152
153LLEventTimeout::LLEventTimeout(LLEventPump& source):
154    LLEventTimeoutBase(source)
155{
156}
157
158void LLEventTimeout::setCountdown(F32 seconds)
159{
160    mTimer.setTimerExpirySec(seconds);
161}
162
163bool LLEventTimeout::countdownElapsed() const
164{
165    return mTimer.hasExpired();
166}