Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
transiently.hpp
Go to the documentation of this file.
1/*
2 TRANSIENTLY.hpp - temporary manipulations undone when leaving scope
3
4 Copyright (C)
5 2023, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12*/
13
14
27#ifndef LIB_TEST_TRANSIENTLY_H
28#define LIB_TEST_TRANSIENTLY_H
29
30
31#include "lib/nocopy.hpp"
32#include "lib/meta/function.hpp"
33#include "lib/ppmpl.h"
34
35#include <utility>
36
37namespace lib {
38namespace test{
39
44 template<typename TAR>
47 {
50
51 public:
52 Transiently(TAR& target)
53 : originalVal_{target}
54 , manipulated_{target}
55 { }
56
58 {
59 manipulated_ = std::move (originalVal_);
60 }
61
62 template<typename X>
63 void
65 {
66 manipulated_ = std::forward<X> (x);
67 }
68 };
69
70
72 template<>
73 class Transiently<void(void)>
75 {
76 using Manipulator = std::function<void(void)>;
77
80
81 public:
82 Transiently (Manipulator manipulation)
83 : doIt_{std::move (manipulation)}
84 , undoIt_{}
85 { }
86
88 {
89 CHECK (undoIt_, "REJECT Manipulation -- "
90 "Failed to provide a way "
91 "to undo the manipulation.");
92 undoIt_();
93 }
94
95 void
97 {
98 undoIt_ = std::move (cleanUp);
99 doIt_(); // actually perform the manipulation
100 }
101 };
102
107 template<typename FUN, typename=lib::meta::enable_if<lib::meta::has_Sig<FUN, void(void)>>>
108 Transiently (FUN&&) -> Transiently<void(void)>;
109
110}} // namespace lib::test
111
112
113
114
115/* === test helper macros === */
116
121#define TRANSIENTLY(_OO_) \
122 lib::test::Transiently PPMPL_CAT(transientlyManipulated_,__LINE__)(_OO_); PPMPL_CAT(transientlyManipulated_,__LINE__)
123
124
125#endif /*LIB_TEST_TRANSIENTLY_H*/
std::function< void(void)> Manipulator
Transiently(Manipulator manipulation)
void cleanUp(Manipulator cleanUp)
Token to capture a value and restore original when leaving scope.
Any copy and copy construction prohibited.
Definition nocopy.hpp:38
Metaprogramming tools for detecting and transforming function types.
Implementation namespace for support and library code.
STL namespace.
Test runner and basic definitions for tests.
Mix-Ins to allow or prohibit various degrees of copying and cloning.
Preprocessor metaprogramming library.