summaryrefslogtreecommitdiff
path: root/indra/llcommon/resultset.h
blob: 10d84c038f55c8e5c24b585d1b5685a80ff87170 (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
/**
 * @file   resultset.h
 * @author Nat Goodspeed
 * @date   2024-09-03
 * @brief  ResultSet is an abstract base class to allow scripted access to
 *         potentially large collections representable as LLSD arrays.
 *
 * $LicenseInfo:firstyear=2024&license=viewerlgpl$
 * Copyright (c) 2024, Linden Research, Inc.
 * $/LicenseInfo$
 */

#if ! defined(LL_RESULTSET_H)
#define LL_RESULTSET_H

#include "llinttracker.h"
#include "llsd.h"
#include <iosfwd>                   // std::ostream
#include <utility>                  // std::pair
#include <vector>

namespace LL
{

// This abstract base class defines an interface by which a large collection
// of items representable as an LLSD array can be retrieved in slices. It isa
// LLIntTracker so we can pass its unique int key to a consuming script via
// LLSD.
struct ResultSet: public LLIntTracker<ResultSet>
{
    // Get the length of the result set. Indexes are 0-relative.
    virtual int getLength() const = 0;
    // Get conventional LLSD { key, length } pair.
    LLSD getKeyLength() const;
    // Retrieve LLSD corresponding to a single entry from the result set,
    // once we're sure the index is valid.
    virtual LLSD getSingle(int index) const = 0;
    // Retrieve LLSD corresponding to a "slice" of the result set: a
    // contiguous sub-array starting at index. The returned LLSD array might
    // be shorter than count entries if count > MAX_ITEM_LIMIT, or if the
    // specified slice contains the end of the result set.
    LLSD getSlice(int index, int count) const;
    // Like getSlice(), but also return adjusted start position.
    std::pair<LLSD, int> getSliceStart(int index, int count) const;

    /*---------------- the rest is solely for debug logging ----------------*/
    std::string mName;

    ResultSet(const std::string& name);
    virtual ~ResultSet();
};

// VectorResultSet is for the simple case of a ResultSet managing a single
// std::vector<T>.
template <typename T>
struct VectorResultSet: public ResultSet
{
    using super = VectorResultSet<T>;

    VectorResultSet(const std::string& name): ResultSet(name) {}
    int getLength() const override { return narrow(mVector.size()); }
    LLSD getSingle(int index) const override { return getSingleFrom(mVector[index]); }
    virtual LLSD getSingleFrom(const T&) const = 0;

    std::vector<T> mVector;
};

} // namespace LL

std::ostream& operator<<(std::ostream& out, const LL::ResultSet& self);

#endif /* ! defined(LL_RESULTSET_H) */