libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
security.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
27namespace keepass {
28
41template <typename T> class protect {
42private:
43 T value_;
44 bool protected_ = false;
45
46public:
48 protect() = default;
49
55 protect(const T& val, bool prot) : value_(val), protected_(prot) {}
56
62 protect(T&& val, bool prot) : value_(std::move(val)), protected_(prot) {}
63
68 protect(const protect<T>& other) {
69 value_ = other.value_;
70 protected_ = other.protected_;
71 }
72
77 protect(protect<T>&& other) noexcept {
78 value_ = std::move(other.value_);
79 protected_ = other.protected_;
80 }
81
86 bool is_protected() const { return protected_; }
87
92 void set_protected(bool prot) { protected_ = prot; }
93
98 const T& value() const { return value_; }
99
104 void set_value(const T& val) { value_ = val; }
105
111 protect<T>& operator=(const protect<T>&) = default;
112
118 protect<T>& operator=(protect<T>&& other) noexcept {
119 value_ = std::move(other.value_);
120 protected_ = other.protected_;
121 return *this;
122 }
123
128 explicit operator const T&() const { return value_; }
129
134 const T* operator->() const { return &value_; }
135
140 const T& operator*() const { return value_; }
141
147 bool operator==(const protect<T>& other) const {
148 return value_ == other.value_ && protected_ == other.protected_;
149 }
150
156 bool operator!=(const protect<T>& other) const { return !(*this == other); }
157};
158
159} // namespace keepass
Wrapper that marks a value as sensitive and tracks the protection flag of a KeePass field.
Definition security.hh:41
bool operator!=(const protect< T > &other) const
Inequality comparison of two protected wrappers.
Definition security.hh:156
const T * operator->() const
Arrow operator forwarding to the stored value.
Definition security.hh:134
protect< T > & operator=(protect< T > &&other) noexcept
Move-assigns from another protected wrapper.
Definition security.hh:118
protect()=default
Default-constructs an empty protected wrapper.
protect(protect< T > &&other) noexcept
Move-constructs from another protected wrapper.
Definition security.hh:77
protect(const T &val, bool prot)
Constructs a protected wrapper with an explicit protection flag.
Definition security.hh:55
const T & operator*() const
Dereference operator forwarding to the stored value.
Definition security.hh:140
const T & value() const
Returns a const reference to the stored value.
Definition security.hh:98
bool operator==(const protect< T > &other) const
Equality comparison of two protected wrappers.
Definition security.hh:147
void set_value(const T &val)
Replaces the stored value.
Definition security.hh:104
bool is_protected() const
Returns whether the value is marked as sensitive.
Definition security.hh:86
protect< T > & operator=(const protect< T > &)=default
Copy-assigns from another protected wrapper.
protect(const protect< T > &other)
Copy-constructs from another protected wrapper.
Definition security.hh:68
protect(T &&val, bool prot)
Constructs a protected wrapper by moving a value in.
Definition security.hh:62
void set_protected(bool prot)
Sets the protection flag.
Definition security.hh:92