Skip to content

Commit 810d092

Browse files
committed
p2p, refactor: make NetPermissionFlags a uint32 enum class
and define/update operation methods to handle type conversions explicitly. See https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-oper for more info.
1 parent 7b55a94 commit 810d092

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/net_permissions.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <netaddress.h>
66

77
#include <string>
8+
#include <type_traits>
89
#include <vector>
910

1011
#ifndef BITCOIN_NET_PERMISSIONS_H
@@ -14,7 +15,7 @@ struct bilingual_str;
1415

1516
extern const std::vector<std::string> NET_PERMISSIONS_DOC;
1617

17-
enum NetPermissionFlags {
18+
enum class NetPermissionFlags : uint32_t {
1819
PF_NONE = 0,
1920
// Can query bloomfilter even if -peerbloomfilters is false
2021
PF_BLOOMFILTER = (1U << 1),
@@ -37,6 +38,11 @@ enum NetPermissionFlags {
3738
PF_ISIMPLICIT = (1U << 31),
3839
PF_ALL = PF_BLOOMFILTER | PF_FORCERELAY | PF_RELAY | PF_NOBAN | PF_MEMPOOL | PF_DOWNLOAD | PF_ADDR,
3940
};
41+
static inline constexpr NetPermissionFlags operator|(NetPermissionFlags a, NetPermissionFlags b)
42+
{
43+
using t = typename std::underlying_type<NetPermissionFlags>::type;
44+
return static_cast<NetPermissionFlags>(static_cast<t>(a) | static_cast<t>(b));
45+
}
4046

4147
class NetPermissions
4248
{
@@ -45,11 +51,12 @@ class NetPermissions
4551
static std::vector<std::string> ToStrings(NetPermissionFlags flags);
4652
static inline bool HasFlag(NetPermissionFlags flags, NetPermissionFlags f)
4753
{
48-
return (flags & f) == f;
54+
using t = typename std::underlying_type<NetPermissionFlags>::type;
55+
return (static_cast<t>(flags) & static_cast<t>(f)) == static_cast<t>(f);
4956
}
5057
static inline void AddFlag(NetPermissionFlags& flags, NetPermissionFlags f)
5158
{
52-
flags = static_cast<NetPermissionFlags>(flags | f);
59+
flags = flags | f;
5360
}
5461
//! ClearFlag is only called with `f` == NetPermissionFlags::PF_ISIMPLICIT.
5562
//! If that should change in the future, be aware that ClearFlag should not
@@ -59,7 +66,8 @@ class NetPermissions
5966
static inline void ClearFlag(NetPermissionFlags& flags, NetPermissionFlags f)
6067
{
6168
assert(f == NetPermissionFlags::PF_ISIMPLICIT);
62-
flags = static_cast<NetPermissionFlags>(flags & ~f);
69+
using t = typename std::underlying_type<NetPermissionFlags>::type;
70+
flags = static_cast<NetPermissionFlags>(static_cast<t>(flags) & ~static_cast<t>(f));
6371
}
6472
};
6573

0 commit comments

Comments
 (0)