libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
temporal.hh
Go to the documentation of this file.
1/*
2 * libkeepass - KeePass key database importer/exporter
3 * Copyright (C) 2014 Christian Kindahl
4 * Copyright (C) 2024 Dominik Krümpelmann
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
25#pragma once
26#include <ctime>
27
28namespace keepass {
29
35template <typename T> class temporal {
36private:
37 T value_;
38 std::time_t time_ = 0;
39
40public:
42 temporal() = default;
43
49 temporal(const T& value, std::time_t time) : value_(value), time_(time) {}
50
55 temporal(const temporal<T>& other) {
56 value_ = other.value_;
57 time_ = other.time_;
58 }
59
64 temporal(temporal<T>&& other) noexcept {
65 value_ = std::move(other.value_);
66 time_ = std::move(other.time_);
67 }
68
73 const T& value() const { return value_; }
74
79 std::time_t time() const { return time_; }
80
85 void set_time(std::time_t time) { time_ = time; }
86
91 void Set(const T& val) {
92 value_ = val;
93 time_ = std::time(nullptr);
94 }
95
102 value_ = value;
103 time_ = std::time(nullptr);
104 return *this;
105 }
106
113 value_ = other.value_;
114 time_ = other.time_;
115 return *this;
116 }
117
123 temporal<T>& operator=(temporal<T>&& other) noexcept {
124 value_ = std::move(other.value_);
125 time_ = std::move(other.time_);
126 return *this;
127 }
128
133 explicit operator const T&() const { return value_; }
134
139 const T* operator->() const { return &value_; }
140
145 const T& operator*() const { return value_; }
146};
147
148} // namespace keepass
Template class for keeping track of when a variable is modified.
Definition temporal.hh:35
const T * operator->() const
Arrow operator forwarding to the stored value.
Definition temporal.hh:139
temporal< T > & operator=(const temporal< T > &other)
Copy-assigns from another temporal wrapper.
Definition temporal.hh:112
temporal(const temporal< T > &other)
Copy-constructs from another temporal wrapper.
Definition temporal.hh:55
void set_time(std::time_t time)
Sets the modification timestamp without changing the value.
Definition temporal.hh:85
temporal(temporal< T > &&other) noexcept
Move-constructs from another temporal wrapper.
Definition temporal.hh:64
temporal()=default
Default-constructs an empty temporal wrapper.
temporal< T > & operator=(const T &value)
Assigns a plain value, updating the timestamp to now.
Definition temporal.hh:101
const T & value() const
Returns a const reference to the stored value.
Definition temporal.hh:73
temporal(const T &value, std::time_t time)
Constructs a temporal wrapper with an explicit timestamp.
Definition temporal.hh:49
void Set(const T &val)
Replaces the stored value and updates the timestamp to now.
Definition temporal.hh:91
const T & operator*() const
Dereference operator forwarding to the stored value.
Definition temporal.hh:145
temporal< T > & operator=(temporal< T > &&other) noexcept
Move-assigns from another temporal wrapper.
Definition temporal.hh:123
std::time_t time() const
Returns the modification timestamp.
Definition temporal.hh:79