File indexing completed on 2024-06-23 05:14:15

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     utils/cached.h
0003 
0004     This file is part of Kleopatra, the KDE keymanager
0005     SPDX-FileCopyrightText: 2009 Klarälvdalens Datakonsult AB
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #pragma once
0011 
0012 #include <type_traits>
0013 
0014 namespace Kleo
0015 {
0016 
0017 template<typename T>
0018 class cached
0019 {
0020     T m_value;
0021     bool m_dirty;
0022 
0023     using CallType = const typename std::conditional<std::is_standard_layout<T>::value && std::is_trivial<T>::value, T, T &>::type;
0024 
0025 public:
0026     cached()
0027         : m_value()
0028         , m_dirty(true)
0029     {
0030     }
0031     /* implicit */ cached(const CallType value)
0032         : m_value(value)
0033         , m_dirty(false)
0034     {
0035     }
0036 
0037     operator T() const
0038     {
0039         return m_value;
0040     }
0041 
0042     cached &operator=(T value)
0043     {
0044         m_value = value;
0045         m_dirty = false;
0046         return *this;
0047     }
0048 
0049     bool dirty() const
0050     {
0051         return m_dirty;
0052     }
0053 
0054     T value() const
0055     {
0056         return m_value;
0057     }
0058 
0059     void set_dirty()
0060     {
0061         m_dirty = true;
0062     }
0063 };
0064 
0065 }