about summary refs log tree commit diff
path: root/test/transient_tester.hpp
blob: 3cf891bef957855e85c4104422e7a4d4501c618d (plain) (blame)
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
//
// immer: immutable data structures for C++
// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
//
// This software is distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
//

#pragma once

#include "dada.hpp"

namespace {

template <typename VP, typename VT>
struct transient_tester
{
    VP vp;
    VT vt;
    dadaism d      = {};
    bool transient = false;

    transient_tester(VP vp)
        : vp{vp}
        , vt{vp.transient()}
    {}

    bool step()
    {
        auto s = d.next();
        if (soft_dada()) {
            auto new_transient = !transient;
            try {
                if (new_transient)
                    vt = vp.transient();
                else
                    vp = vt.persistent();
            } catch (const dada_error&) {
                return false;
            }
            transient = new_transient;
            return true;
        } else
            return false;
    }
};

template <typename VP>
transient_tester<VP, typename VP::transient_type> as_transient_tester(VP p)
{
    return {std::move(p)};
}

} // anonymous namespace