wxMaxima
Loading...
Searching...
No Matches
CachedValue.h
Go to the documentation of this file.
1// -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2//
3// Copyright (C) 2020 Kuba Ober <kuba@bertec.com>
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15//
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19//
20// SPDX-License-Identifier: GPL-2.0+
21
22#ifndef WXMAXIMA_CACHEDVALUE_H
23#define WXMAXIMA_CACHEDVALUE_H
24
30#include <wx/debug.h>
31#include <wx/log.h>
32#include <limits>
33#include <type_traits>
34
43template <typename T, typename std::enable_if<std::is_integral<T>::value, bool>::type = true>
45{
46 static constexpr T invalid = std::numeric_limits<T>::max();
47 mutable T m_value = invalid;
48
49public:
50 constexpr CachedInteger() = default;
51 constexpr CachedInteger(const CachedInteger &) = default;
52 constexpr CachedInteger &operator=(const CachedInteger &) = default;
53 constexpr bool IsValid() const { return m_value != invalid; }
54 constexpr bool IsInvalid() const { return m_value == invalid; }
55 constexpr void Invalidate() const { m_value = invalid; }
56 operator T() const { return Get(); }
57 T Get() const
58 {
59 wxASSERT_MSG(m_value != invalid, "Attempted to use an invalid cached value");
60 return (m_value != invalid) ? m_value : T{};
61 }
62 void SetCached(T newValue) const
63 {
64 wxASSERT_MSG(newValue != invalid, "Attempted to set an out-of-range cached value.");
65 m_value = newValue;
66 }
67};
68
69#endif
A cached integer value.
Definition: CachedValue.h:45