libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
util.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 <algorithm>
27#include <array>
28#include <ctime>
29#include <functional>
30#include <memory>
31#include <string>
32#include <vector>
33
34#include "libkeepass/export.hh"
35
36namespace keepass {
37
45template <typename T> inline T clamp(T min, T max, T val) {
46 return std::max<T>(min, std::min<T>(max, val));
47}
48
58template <typename T>
59inline bool indirect_equal(const std::vector<T>& v0, const std::vector<T>& v1) {
60 return std::equal(v0.begin(), v0.end(), v1.begin(),
61 [](const T& a, const T& b) { return *a == *b; });
62}
63
72template <typename T>
73inline bool indirect_equal(const std::shared_ptr<T>& p0, const std::shared_ptr<T>& p1) {
74 if (p0 != nullptr && p1 != nullptr)
75 return *p0 == *p1;
76
77 return p0 == nullptr && p1 == nullptr;
78}
79
88template <typename T, const std::vector<std::shared_ptr<T>>& (T::*F)() const>
89inline void dfs(const std::shared_ptr<T>& current,
90 std::function<void(const std::shared_ptr<T>&, std::size_t)> callback,
91 std::size_t level = 0) {
92 for (auto child : ((current.get())->*F)()) {
93 // Note that we're not invoking the callback for the root.
94 callback(child, level);
95 dfs<T, F>(child, callback, level + 1);
96 }
97}
98
107LIBKEEPASS_API std::string time_to_str(const std::time_t& time);
108
113LIBKEEPASS_API std::array<uint8_t, 16> generate_uuid();
114
115} // namespace keepass
Visibility/export macro for the shared library.
#define LIBKEEPASS_API
Marks a class, function or variable as part of the public ABI.
Definition export.hh:43
void dfs(const std::shared_ptr< T > &current, std::function< void(const std::shared_ptr< T > &, std::size_t)> callback, std::size_t level=0)
Definition util.hh:89
LIBKEEPASS_API std::string time_to_str(const std::time_t &time)
bool indirect_equal(const std::vector< T > &v0, const std::vector< T > &v1)
Definition util.hh:59
T clamp(T min, T max, T val)
Definition util.hh:45
LIBKEEPASS_API std::array< uint8_t, 16 > generate_uuid()
Generates a cryptographically random 128-bit UUID.