summaryrefslogtreecommitdiff
path: root/indra/llcommon/tests/listener.h
blob: 887a2c90825be62e788e15b4709bb0d96a8c831d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
 * @file   listener.h
 * @author Nat Goodspeed
 * @date   2009-03-06
 * @brief  Useful for tests of the LLEventPump family of classes
 *
 * $LicenseInfo:firstyear=2009&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#if ! defined(LL_LISTENER_H)
#define LL_LISTENER_H

#include "llsd.h"
#include "llevents.h"
#include "tests/StringVec.h"
#include <iostream>

/*****************************************************************************
*   test listener class
*****************************************************************************/
class Listener;
std::ostream& operator<<(std::ostream&, const Listener&);

/// Bear in mind that this is strictly for testing
class Listener
{
public:
    /// Every Listener is instantiated with a name
    Listener(const std::string& name):
        mName(name)
    {
//      std::cout << *this << ": ctor\n";
    }
/*==========================================================================*|
    // These methods are only useful when trying to track Listener instance
    // lifespan
    Listener(const Listener& that):
        mName(that.mName),
        mLastEvent(that.mLastEvent)
    {
        std::cout << *this << ": copy\n";
    }
    virtual ~Listener()
    {
        std::cout << *this << ": dtor\n";
    }
|*==========================================================================*/
    /// You can request the name
    std::string getName() const { return mName; }
    /// This is a typical listener method that returns 'false' when done,
    /// allowing subsequent listeners on the LLEventPump to process the
    /// incoming event.
    bool call(const LLSD& event)
    {
//      std::cout << *this << "::call(" << event << ")\n";
        mLastEvent = event;
        return false;
    }
    /// This is an alternate listener that returns 'true' when done, which
    /// stops processing of the incoming event.
    bool callstop(const LLSD& event)
    {
//      std::cout << *this << "::callstop(" << event << ")\n";
        mLastEvent = event;
        return true;
    }
    /// ListenMethod can represent either call() or callstop().
    typedef bool (Listener::*ListenMethod)(const LLSD&);
    /**
     * This helper method is only because our test code makes so many
     * repetitive listen() calls to ListenerMethods. In real code, you should
     * call LLEventPump::listen() directly so it can examine the specific
     * object you pass to boost::bind().
     */
    LLBoundListener listenTo(LLEventPump& pump,
                             ListenMethod method=&Listener::call,
                             const LLEventPump::NameList& after=LLEventPump::empty,
                             const LLEventPump::NameList& before=LLEventPump::empty)
    {
        return pump.listen(getName(), boost::bind(method, this, _1), after, before);
    }
    /// Both call() and callstop() set mLastEvent. Retrieve it.
    LLSD getLastEvent() const
    {
//      std::cout << *this << "::getLastEvent() -> " << mLastEvent << "\n";
        return mLastEvent;
    }
    /// Reset mLastEvent to a known state.
    void reset(const LLSD& to = LLSD())
    {
//      std::cout << *this << "::reset(" << to << ")\n";
        mLastEvent = to;
    }

private:
    std::string mName;
    LLSD mLastEvent;
};

std::ostream& operator<<(std::ostream& out, const Listener& listener)
{
    out << "Listener(" << listener.getName() /* << "@" << &listener */ << ')';
    return out;
}

/**
 * This class tests the relative order in which various listeners on a given
 * LLEventPump are called. Each listen() call binds a particular string, which
 * we collect for later examination. The actual event is ignored.
 */
struct Collect
{
    bool add(const std::string& bound, const LLSD& event)
    {
        result.push_back(bound);
        return false;
    }
    void clear() { result.clear(); }
    StringVec result;
};

struct Concat
{
    bool operator()(const LLSD& event)
    {
        result += event.asString();
        return false;
    }
    void clear() { result.clear(); }
    std::string result;
};

#endif /* ! defined(LL_LISTENER_H) */