Debian Codegraph @

components/keyrings/common/data/data.cc

raw
1 /* Copyright (c) 2021, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <functional>
25
26 #include "data.h"
27
28 namespace keyring_common {
29 namespace data {
30
31 /** Constructor to create a data object */
32 Data::Data(const Sensitive_data data, Type type) : data_(data), type_(type) {
33 set_validity();
34 }
35
36 /* Following constructors imply no data */
37 Data::Data() : Data("", "") {}
38 Data::Data(Type type) : Data("", type) {}
39
40 /** Copy constructor */
41 Data::Data(const Data &src) : Data(src.data_, src.type_) {}
42
43 /** Move constructor */
44 Data::Data(Data &&src) noexcept {
45 std::swap(src.data_, data_);
46 std::swap(src.type_, type_);
47 std::swap(src.valid_, valid_);
48 }
49
50 /* Assignment operator */
51 Data &Data::operator=(const Data &src) = default;
52
53 Data &Data::operator=(Data &&src) noexcept {
54 std::swap(src.data_, data_);
55 std::swap(src.type_, type_);
56 std::swap(src.valid_, valid_);
57
58 return *this;
59 }
60
61 /** Destructor */
62 Data::~Data() { valid_ = false; }
63
64 /** Return self */
65 const Data Data::get_data() const { return *this; }
66
67 /** Get data */
68 Sensitive_data Data::data() const { return data_; }
69
70 /** Get data's type */
71 Type Data::type() const { return type_; }
72
73 /** Status of object's validity */
74 bool Data::valid() const { return valid_; }
75
76 /** Set data */
77 void Data::set_data(const Sensitive_data &data) {
78 data_ = data;
79 set_validity();
80 }
81
82 /** Set data */
83 void Data::set_data(const Data &src) { *this = src; }
84
85 /** Set type */
86 void Data::set_type(Type type) {
87 type_ = type;
88 set_validity();
89 }
90
91 /** Comparison */
92 bool Data::operator==(const Data &other) {
93 return data_ == other.data_ && type_ == other.type_ && valid_ == other.valid_;
94 }
95
96 /** Set validity of the data object */
97 void Data::set_validity() { valid_ = type_ != ""; }
98
99 } // namespace data
100 } // namespace keyring_common