PageRenderTime 23ms CodeModel.GetById 15ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/tests/listener.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 156 lines | 77 code | 8 blank | 71 comment | 4 complexity | 6fec4ef56bde5cba4dc6f042b5cb3946 MD5 | raw file
  1/**
  2 * @file   listener.h
  3 * @author Nat Goodspeed
  4 * @date   2009-03-06
  5 * @brief  Useful for tests of the LLEventPump family of classes
  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#if ! defined(LL_LISTENER_H)
 30#define LL_LISTENER_H
 31
 32#include "llsd.h"
 33#include <iostream>
 34
 35/*****************************************************************************
 36*   test listener class
 37*****************************************************************************/
 38class Listener;
 39std::ostream& operator<<(std::ostream&, const Listener&);
 40
 41/// Bear in mind that this is strictly for testing
 42class Listener
 43{
 44public:
 45    /// Every Listener is instantiated with a name
 46    Listener(const std::string& name):
 47        mName(name)
 48    {
 49//      std::cout << *this << ": ctor\n";
 50    }
 51/*==========================================================================*|
 52    // These methods are only useful when trying to track Listener instance
 53    // lifespan
 54    Listener(const Listener& that):
 55        mName(that.mName),
 56        mLastEvent(that.mLastEvent)
 57    {
 58        std::cout << *this << ": copy\n";
 59    }
 60    virtual ~Listener()
 61    {
 62        std::cout << *this << ": dtor\n";
 63    }
 64|*==========================================================================*/
 65    /// You can request the name
 66    std::string getName() const { return mName; }
 67    /// This is a typical listener method that returns 'false' when done,
 68    /// allowing subsequent listeners on the LLEventPump to process the
 69    /// incoming event.
 70    bool call(const LLSD& event)
 71    {
 72//      std::cout << *this << "::call(" << event << ")\n";
 73        mLastEvent = event;
 74        return false;
 75    }
 76    /// This is an alternate listener that returns 'true' when done, which
 77    /// stops processing of the incoming event.
 78    bool callstop(const LLSD& event)
 79    {
 80//      std::cout << *this << "::callstop(" << event << ")\n";
 81        mLastEvent = event;
 82        return true;
 83    }
 84    /// ListenMethod can represent either call() or callstop().
 85    typedef bool (Listener::*ListenMethod)(const LLSD&);
 86    /**
 87     * This helper method is only because our test code makes so many
 88     * repetitive listen() calls to ListenerMethods. In real code, you should
 89     * call LLEventPump::listen() directly so it can examine the specific
 90     * object you pass to boost::bind().
 91     */
 92    LLBoundListener listenTo(LLEventPump& pump,
 93                             ListenMethod method=&Listener::call,
 94                             const LLEventPump::NameList& after=LLEventPump::empty,
 95                             const LLEventPump::NameList& before=LLEventPump::empty)
 96    {
 97        return pump.listen(getName(), boost::bind(method, this, _1), after, before);
 98    }
 99    /// Both call() and callstop() set mLastEvent. Retrieve it.
100    LLSD getLastEvent() const
101    {
102//      std::cout << *this << "::getLastEvent() -> " << mLastEvent << "\n";
103        return mLastEvent;
104    }
105    /// Reset mLastEvent to a known state.
106    void reset(const LLSD& to = LLSD())
107    {
108//      std::cout << *this << "::reset(" << to << ")\n";
109        mLastEvent = to;
110    }
111
112private:
113    std::string mName;
114    LLSD mLastEvent;
115};
116
117std::ostream& operator<<(std::ostream& out, const Listener& listener)
118{
119    out << "Listener(" << listener.getName() /* << "@" << &listener */ << ')';
120    return out;
121}
122
123/**
124 * This class tests the relative order in which various listeners on a given
125 * LLEventPump are called. Each listen() call binds a particular string, which
126 * we collect for later examination. The actual event is ignored.
127 */
128struct Collect
129{
130    bool add(const std::string& bound, const LLSD& event)
131    {
132        result.push_back(bound);
133        return false;
134    }
135    void clear() { result.clear(); }
136    typedef std::vector<std::string> StringList;
137    StringList result;
138};
139
140std::ostream& operator<<(std::ostream& out, const Collect::StringList& strings)
141{
142    out << '(';
143    Collect::StringList::const_iterator begin(strings.begin()), end(strings.end());
144    if (begin != end)
145    {
146        out << '"' << *begin << '"';
147        while (++begin != end)
148        {
149            out << ", \"" << *begin << '"';
150        }
151    }
152    out << ')';
153    return out;
154}
155
156#endif /* ! defined(LL_LISTENER_H) */