summaryrefslogtreecommitdiff
path: root/ext/librethinkdbxx/src/cursor.h
blob: 60ae1817760f50c01616370a61fbc4104735bf61 (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
#pragma once

#include "connection.h"

namespace RethinkDB {

// The response from the server, as returned by run.
// The response is either a single datum or a stream:
//  * If it is a stream, the cursor represents each element of the stream.
//    - Batches are fetched from the server as needed.
//  * If it is a single datum, is_single() returns true.
//    - If it is an array, the cursor represents each element of that array
//    - Otherwise, to_datum() returns the datum and iteration throws an exception.
// The cursor can only be iterated over once, it discards data that has already been read.
class CursorPrivate;
class Cursor {
public:
    Cursor() = delete;
    ~Cursor();

    Cursor(Cursor&&);                   // movable
    Cursor& operator=(Cursor&&);
    Cursor(const Cursor&) = delete;     // not copyable
    Cursor& operator=(const Cursor&) = delete;

    // Returned by begin() and end()
    class iterator {
    public:
        iterator(Cursor*);
        iterator& operator++ ();
        Datum& operator* ();
        bool operator!= (const iterator&) const;

    private:
        Cursor *cursor;
    };

    // Consume the next element
    Datum& next(double wait = FOREVER) const;

    // Peek at the next element
    Datum& peek(double wait = FOREVER) const;

    // Call f on every element of the Cursor
    void each(std::function<void(Datum&&)> f, double wait = FOREVER) const;

    // Consume and return all elements
    Array&& to_array() &&;

    // If is_single(), returns the single datum. Otherwise returns to_array().
    Datum to_datum() &&;
    Datum to_datum() const &;

    // Efficiently consume and return all elements
    Array to_array() const &;

    // Close the cursor
    void close() const;

    // Returns false if there are no more elements
    bool has_next(double wait = FOREVER) const;

    // Returns false if the cursor is a stream
    bool is_single() const;

    iterator begin();
    iterator end();

private:
    explicit Cursor(CursorPrivate *dd);
    std::unique_ptr<CursorPrivate> d;

    friend class Connection;
};

}