PageRenderTime 73ms CodeModel.GetById 28ms 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
Possible License(s): LGPL-2.1
  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. // Precompiled header
  29. #include "linden_common.h"
  30. // associated header
  31. #include "lleventfilter.h"
  32. // STL headers
  33. // std headers
  34. // external library headers
  35. #include <boost/bind.hpp>
  36. // other Linden headers
  37. #include "llerror.h" // LL_ERRS
  38. #include "llsdutil.h" // llsd_matches()
  39. LLEventFilter::LLEventFilter(LLEventPump& source, const std::string& name, bool tweak):
  40. LLEventStream(name, tweak)
  41. {
  42. source.listen(getName(), boost::bind(&LLEventFilter::post, this, _1));
  43. }
  44. LLEventMatching::LLEventMatching(const LLSD& pattern):
  45. LLEventFilter("matching"),
  46. mPattern(pattern)
  47. {
  48. }
  49. LLEventMatching::LLEventMatching(LLEventPump& source, const LLSD& pattern):
  50. LLEventFilter(source, "matching"),
  51. mPattern(pattern)
  52. {
  53. }
  54. bool LLEventMatching::post(const LLSD& event)
  55. {
  56. if (! llsd_matches(mPattern, event).empty())
  57. return false;
  58. return LLEventStream::post(event);
  59. }
  60. LLEventTimeoutBase::LLEventTimeoutBase():
  61. LLEventFilter("timeout")
  62. {
  63. }
  64. LLEventTimeoutBase::LLEventTimeoutBase(LLEventPump& source):
  65. LLEventFilter(source, "timeout")
  66. {
  67. }
  68. void LLEventTimeoutBase::actionAfter(F32 seconds, const Action& action)
  69. {
  70. setCountdown(seconds);
  71. mAction = action;
  72. if (! mMainloop.connected())
  73. {
  74. LLEventPump& mainloop(LLEventPumps::instance().obtain("mainloop"));
  75. mMainloop = mainloop.listen(getName(), boost::bind(&LLEventTimeoutBase::tick, this, _1));
  76. }
  77. }
  78. class ErrorAfter
  79. {
  80. public:
  81. ErrorAfter(const std::string& message): mMessage(message) {}
  82. void operator()()
  83. {
  84. LL_ERRS("LLEventTimeout") << mMessage << LL_ENDL;
  85. }
  86. private:
  87. std::string mMessage;
  88. };
  89. void LLEventTimeoutBase::errorAfter(F32 seconds, const std::string& message)
  90. {
  91. actionAfter(seconds, ErrorAfter(message));
  92. }
  93. class EventAfter
  94. {
  95. public:
  96. EventAfter(LLEventPump& pump, const LLSD& event):
  97. mPump(pump),
  98. mEvent(event)
  99. {}
  100. void operator()()
  101. {
  102. mPump.post(mEvent);
  103. }
  104. private:
  105. LLEventPump& mPump;
  106. LLSD mEvent;
  107. };
  108. void LLEventTimeoutBase::eventAfter(F32 seconds, const LLSD& event)
  109. {
  110. actionAfter(seconds, EventAfter(*this, event));
  111. }
  112. bool LLEventTimeoutBase::post(const LLSD& event)
  113. {
  114. cancel();
  115. return LLEventStream::post(event);
  116. }
  117. void LLEventTimeoutBase::cancel()
  118. {
  119. mMainloop.disconnect();
  120. }
  121. bool LLEventTimeoutBase::tick(const LLSD&)
  122. {
  123. if (countdownElapsed())
  124. {
  125. cancel();
  126. mAction();
  127. }
  128. return false; // show event to other listeners
  129. }
  130. LLEventTimeout::LLEventTimeout() {}
  131. LLEventTimeout::LLEventTimeout(LLEventPump& source):
  132. LLEventTimeoutBase(source)
  133. {
  134. }
  135. void LLEventTimeout::setCountdown(F32 seconds)
  136. {
  137. mTimer.setTimerExpirySec(seconds);
  138. }
  139. bool LLEventTimeout::countdownElapsed() const
  140. {
  141. return mTimer.hasExpired();
  142. }