| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/**************************************************************************** |
| 3 |
* * |
| 4 |
* Copyright (c) 2025 Kacper Donat <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 9 |
* under the terms of the GNU Lesser General Public License as * |
| 10 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 11 |
* License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with FreeCAD. If not, see * |
| 20 |
* <https://www.gnu.org/licenses/>. * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
#ifndef FREECAD_PROPERTYOVERRIDES_H |
| 25 |
#define FREECAD_PROPERTYOVERRIDES_H |
| 26 |
|
| 27 |
#include <ranges> |
| 28 |
#include <type_traits> |
| 29 |
#include <utility> |
| 30 |
#include <vector> |
| 31 |
#include <functional> |
| 32 |
|
| 33 |
#include <FCGlobal.h> |
| 34 |
|
| 35 |
namespace App |
| 36 |
{ |
| 37 |
|
| 38 |
/** |
| 39 |
* @brief Default accessor for property-like classes. |
| 40 |
* |
| 41 |
* The `DefaultAccessor` provides a generic implementation of `get()` and `set()` methods |
| 42 |
* that call the `getValue()` and `setValue()` members of a property. This works for the |
| 43 |
* majority of `App::Property` types in FreeCAD. |
| 44 |
* |
| 45 |
* ### Usage |
| 46 |
* Normally you do not use `DefaultAccessor` directly. Instead, use `PropAccessor<P>`, |
| 47 |
* which inherits from `DefaultAccessor` unless specialized. |
| 48 |
* |
| 49 |
* @tparam P The property type, e.g. `App::PropertyBool`, `App::PropertyFloat`. |
| 50 |
*/ |
| 51 |
template<class P> |
| 52 |
struct DefaultAccessor |
| 53 |
{ |
| 54 |
/// Value type of the property, deduced from `getValue()`. |
| 55 |
using Value = std::decay_t<decltype(std::declval<const P&>().getValue())>; |
| 56 |
|
| 57 |
/// Retrieves the property's current value. |
| 58 |
static Value get(const P& p) |
| 59 |
{ |
| 60 |
return p.getValue(); |
| 61 |
} |
| 62 |
|
| 63 |
/// Assigns a new value to the property. |
| 64 |
static void set(P& p, const Value& v) |
| 65 |
{ |
| 66 |
p.setValue(v); |
| 67 |
} |
| 68 |
}; |
| 69 |
|
| 70 |
/** |
| 71 |
* @brief Customizable accessor hook for properties. |
| 72 |
* |
| 73 |
* `PropAccessor` defaults to `DefaultAccessor`. Specialize `PropAccessor<P>` if you |
| 74 |
* need custom behavior for a specific property type (e.g. suppressing recomputes, |
| 75 |
* triggering extra updates). |
| 76 |
* |
| 77 |
* @tparam P The property type to customize. |
| 78 |
*/ |
| 79 |
template<class P> |
| 80 |
struct PropAccessor: DefaultAccessor<P> |
| 81 |
{ |
| 82 |
}; |
| 83 |
|
| 84 |
template<class P> |
| 85 |
concept HasGetValue = requires(const P& cp) { cp.getValue(); }; |
| 86 |
template<class P> |
| 87 |
concept HasSetValue = requires(P& p, typename DefaultAccessor<P>::Value v) { p.setValue(v); }; |
| 88 |
template<class P> |
| 89 |
concept PropertyLike = HasGetValue<P> && HasSetValue<P>; |
| 90 |
|
| 91 |
/** |
| 92 |
* @brief RAII guard that overrides a property for the duration of a scope. |
| 93 |
* |
| 94 |
* `ScopedPropertyOverride` temporarily changes the value of a property, and restores |
| 95 |
* the old value when the guard is destroyed. This ensures properties are always restored |
| 96 |
* even if exceptions occur. |
| 97 |
* |
| 98 |
* ### Example |
| 99 |
* @code{.cpp} |
| 100 |
* App::PropertyBool& vis = ...; |
| 101 |
* { |
| 102 |
* App::ScopedPropertyOverride guard(vis, false); |
| 103 |
* // object is hidden here |
| 104 |
* } |
| 105 |
* // old visibility automatically restored |
| 106 |
* @endcode |
| 107 |
* |
| 108 |
* @tparam P The property type, e.g. `App::PropertyBool`. |
| 109 |
*/ |
| 110 |
template<PropertyLike P> |
| 111 |
class ScopedPropertyOverride |
| 112 |
{ |
| 113 |
public: |
| 114 |
using Value = PropAccessor<P>::Value; |
| 115 |
|
| 116 |
|
| 117 |
/** |
| 118 |
* @brief Constructs an override guard. |
| 119 |
* |
| 120 |
* Saves the old value and sets the property to a new one. |
| 121 |
* |
| 122 |
* @param prop Reference to the property being overridden. |
| 123 |
* @param newValue The temporary value to apply. |
| 124 |
*/ |
| 125 |
ScopedPropertyOverride(P& prop, const Value& newValue) |
| 126 |
: _prop(&prop) |
| 127 |
, _old(PropAccessor<P>::get(prop)) |
| 128 |
, _active(true) |
| 129 |
{ |
| 130 |
PropAccessor<P>::set(*_prop, newValue); |
| 131 |
} |
| 132 |
|
| 133 |
FC_DISABLE_COPY(ScopedPropertyOverride); |
| 134 |
|
| 135 |
ScopedPropertyOverride(ScopedPropertyOverride&& other) noexcept |
| 136 |
: _prop(other._prop) |
| 137 |
, _old(std::move(other._old)) |
| 138 |
, _active(other._active) |
| 139 |
{ |
| 140 |
other._prop = nullptr; |
| 141 |
other._active = false; |
| 142 |
} |
| 143 |
|
| 144 |
ScopedPropertyOverride& operator=(ScopedPropertyOverride&& other) noexcept |
| 145 |
{ |
| 146 |
if (this != &other) { |
| 147 |
restore(); |
| 148 |
_prop = other._prop; |
| 149 |
_old = std::move(other._old); |
| 150 |
_active = other._active; |
| 151 |
other._prop = nullptr; |
| 152 |
other._active = false; |
| 153 |
} |
| 154 |
return *this; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* @brief Destructor. |
| 159 |
* |
| 160 |
* Restores the original value of the property. |
| 161 |
*/ |
| 162 |
~ScopedPropertyOverride() |
| 163 |
{ |
| 164 |
restore(); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* @brief Checks if the guard is currently active. |
| 169 |
* |
| 170 |
* @return True if the override is in effect. |
| 171 |
*/ |
| 172 |
bool active() const |
| 173 |
{ |
| 174 |
return _active; |
| 175 |
} |
| 176 |
|
| 177 |
private: |
| 178 |
void restore() noexcept |
| 179 |
{ |
| 180 |
if (_prop && _active) { |
| 181 |
PropAccessor<P>::set(*_prop, _old); |
| 182 |
_active = false; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
P* _prop {nullptr}; ///< Pointer to the property being overridden. |
| 187 |
Value _old {}; ///< Saved old value. |
| 188 |
bool _active {false}; ///< Whether this guard is currently active. |
| 189 |
}; |
| 190 |
|
| 191 |
/** |
| 192 |
* @brief Helper function to create a `ScopedPropertyOverride` with type deduction. |
| 193 |
* |
| 194 |
* @tparam P The property type. |
| 195 |
* @tparam V The value type (deduced). |
| 196 |
* @param prop Property reference. |
| 197 |
* @param v New value to assign temporarily. |
| 198 |
* |
| 199 |
* @return A `ScopedPropertyOverride` guard. |
| 200 |
*/ |
| 201 |
template<PropertyLike P, class V> |
| 202 |
auto makeOverride(P& prop, V&& v) |
| 203 |
{ |
| 204 |
using Guard = ScopedPropertyOverride<P>; |
| 205 |
return Guard(prop, static_cast<Guard::Value>(std::forward<V>(v))); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* @brief RAII context for managing multiple property overrides. |
| 210 |
* |
| 211 |
* `PropertyOverrideContext` stores multiple property overrides at once. When the |
| 212 |
* context is destroyed, all properties are restored in reverse (LIFO) order. |
| 213 |
* |
| 214 |
* ### Example |
| 215 |
* @code{.cpp} |
| 216 |
* App::PropertyBool& vis = ...; |
| 217 |
* App::PropertyString& label = ...; |
| 218 |
* |
| 219 |
* App::PropertyOverrideContext overrides; |
| 220 |
* overrides.override(vis, false); |
| 221 |
* overrides.override(label, "Temp"); |
| 222 |
* // both props overridden |
| 223 |
* // ... |
| 224 |
* // restored automatically when ctx is destroyed |
| 225 |
* @endcode |
| 226 |
*/ |
| 227 |
class PropertyOverrideContext |
| 228 |
{ |
| 229 |
public: |
| 230 |
PropertyOverrideContext() = default; |
| 231 |
|
| 232 |
FC_DISABLE_COPY(PropertyOverrideContext); |
| 233 |
FC_DEFAULT_MOVE(PropertyOverrideContext); |
| 234 |
|
| 235 |
/** |
| 236 |
* @brief Destructor. |
| 237 |
* |
| 238 |
* Restores all overrides. |
| 239 |
*/ |
| 240 |
~PropertyOverrideContext() |
| 241 |
{ |
| 242 |
clear(); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* @brief Override a property within this context. |
| 247 |
* |
| 248 |
* Saves the old value and assigns a new one. The value is restored automatically |
| 249 |
* when the context is destroyed or `clear()` is called. |
| 250 |
* |
| 251 |
* @tparam P Property type. |
| 252 |
* @param prop Reference to property. |
| 253 |
* @param newValue Temporary value to assign. |
| 254 |
*/ |
| 255 |
template<PropertyLike P> |
| 256 |
void override(P& prop, PropAccessor<P>::Value newValue) |
| 257 |
{ |
| 258 |
using Access = PropAccessor<P>; |
| 259 |
|
| 260 |
_guards.emplace_back([&prop, old = PropAccessor<P>::get(prop)]() { |
| 261 |
Access::set(prop, std::move(old)); |
| 262 |
}); |
| 263 |
|
| 264 |
Access::set(prop, std::move(newValue)); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* @brief Restore all properties immediately. |
| 269 |
* |
| 270 |
* Calls the restore handlers in reverse order of creation and clears the context. |
| 271 |
*/ |
| 272 |
void clear() |
| 273 |
{ |
| 274 |
for (auto& _guard : std::views::reverse(_guards)) { |
| 275 |
_guard(); |
| 276 |
} |
| 277 |
_guards.clear(); |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* @brief Returns the number of active overrides in the context. |
| 282 |
* |
| 283 |
* @return Count of overrides. |
| 284 |
*/ |
| 285 |
std::size_t size() const |
| 286 |
{ |
| 287 |
return _guards.size(); |
| 288 |
} |
| 289 |
|
| 290 |
private: |
| 291 |
std::vector<std::function<void()>> _guards; |
| 292 |
}; |
| 293 |
} // namespace App |
| 294 |
#endif // FREECAD_PROPERTYOVERRIDES_H |