Lumiera  0.pre.03
»edit your freedom«
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 
37 namespace lib {
38 namespace test{
39 
44  template<typename TAR>
47  {
48  TAR originalVal_;
49  TAR& manipulated_;
50 
51  public:
52  Transiently(TAR& target)
53  : originalVal_{target}
54  , manipulated_{target}
55  { }
56 
57  ~Transiently()
58  {
59  manipulated_ = std::move (originalVal_);
60  }
61 
62  template<typename X>
63  void
64  operator= (X&& x)
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 
78  Manipulator doIt_;
79  Manipulator undoIt_;
80 
81  public:
82  Transiently (Manipulator manipulation)
83  : doIt_{std::move (manipulation)}
84  , undoIt_{}
85  { }
86 
87  ~Transiently()
88  {
89  CHECK (undoIt_, "REJECT Manipulation -- "
90  "Failed to provide a way "
91  "to undo the manipulation.");
92  undoIt_();
93  }
94 
95  void
96  cleanUp (Manipulator cleanUp)
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)>>>
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*/
Definition: run.hpp:40
Any copy and copy construction prohibited.
Definition: nocopy.hpp:37
Implementation namespace for support and library code.
Variation where manipulation is done by λ
Definition: transiently.hpp:73
Mix-Ins to allow or prohibit various degrees of copying and cloning.
Metaprogramming tools for transforming functor types.
Preprocessor metaprogramming library.
Token to capture a value and restore original when leaving scope.
Definition: transiently.hpp:45