1//
2// immer: immutable data structures for C++
3// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
4//
5// This software is distributed under the Boost Software License, Version 1.0.
6// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
7//
8
9#pragma once
10
11#include "dada.hpp"
12
13namespace {
14
15template <typename VP, typename VT>
16struct transient_tester
17{
18 VP vp;
19 VT vt;
20 dadaism d = {};
21 bool transient = false;
22
23 transient_tester(VP vp)
24 : vp{vp}
25 , vt{vp.transient()}
26 {}
27
28 bool step()
29 {
30 auto s = d.next();
31 if (soft_dada()) {
32 auto new_transient = !transient;
33 try {
34 if (new_transient)
35 vt = vp.transient();
36 else
37 vp = vt.persistent();
38 } catch (const dada_error&) { return false; }
39 transient = new_transient;
40 return true;
41 } else
42 return false;
43 }
44};
45
46template <typename VP>
47transient_tester<VP, typename VP::transient_type>
48as_transient_tester(VP p)
49{
50 return { std::move(p) };
51}
52
53} // anonymous namespace
54