OpenVPN 3 Core Library
Loading...
Searching...
No Matches
msfind.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12
13// map/set find
14
15#pragma once
16
17#include <utility>
18
19namespace openvpn::MSF {
20
21template <typename ITERATOR>
22class Iter : public ITERATOR
23{
24 public:
25 template <typename MAP_SET>
26 Iter(const MAP_SET &ms, ITERATOR &&iter)
27 : ITERATOR(std::move(iter)),
28 exists_(*this != ms.end())
29 {
30 }
31
32 Iter(ITERATOR &&iter)
33 : ITERATOR(std::move(iter)),
34 exists_(true)
35 {
36 }
37
38 explicit operator bool() const
39 {
40 return exists_;
41 }
42
43 private:
44 bool exists_;
45};
46
47// Like ordinary map/set find, but returns an iterator
48// that defines an operator bool() method for testing if
49// the iterator is defined, so instead of:
50//
51// if (iter != map.end())
52// do_stuff();
53//
54// you can say:
55//
56// if (iter)
57// do_stuff();
58//
59template <typename MAP_SET, typename KEY>
60inline auto find(MAP_SET &ms, const KEY &k)
61{
62 return Iter<decltype(ms.find(k))>(ms, ms.find(k));
63}
64
65// Does key exist in map/set?
66template <typename MAP_SET, typename KEY>
67inline bool exists(MAP_SET &ms, const KEY &k)
68{
69 return ms.contains(k);
70}
71
72// Convert an ordinary, dereferenceable iterator to an MSF::Iter
73template <typename ITERATOR>
74inline auto iter(ITERATOR i)
75{
76 return Iter<ITERATOR>(std::move(i));
77}
78} // namespace openvpn::MSF
Iter(const MAP_SET &ms, ITERATOR &&iter)
Definition msfind.hpp:26
Iter(ITERATOR &&iter)
Definition msfind.hpp:32
auto iter(ITERATOR i)
Definition msfind.hpp:74
auto find(MAP_SET &ms, const KEY &k)
Definition msfind.hpp:60
bool exists(MAP_SET &ms, const KEY &k)
Definition msfind.hpp:67