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.
This commit is contained in:
1 file changed
+12
-4
+12
-4
@@ -5,6 +5,7 @@
|
|||||||
#include <netaddress.h>
|
#include <netaddress.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#ifndef BITCOIN_NET_PERMISSIONS_H
|
#ifndef BITCOIN_NET_PERMISSIONS_H
|
||||||
@@ -14,7 +15,7 @@ struct bilingual_str;
|
|||||||
|
|
||||||
extern const std::vector<std::string> NET_PERMISSIONS_DOC;
|
extern const std::vector<std::string> NET_PERMISSIONS_DOC;
|
||||||
|
|
||||||
enum NetPermissionFlags {
|
enum class NetPermissionFlags : uint32_t {
|
||||||
PF_NONE = 0,
|
PF_NONE = 0,
|
||||||
// Can query bloomfilter even if -peerbloomfilters is false
|
// Can query bloomfilter even if -peerbloomfilters is false
|
||||||
PF_BLOOMFILTER = (1U << 1),
|
PF_BLOOMFILTER = (1U << 1),
|
||||||
@@ -37,6 +38,11 @@ enum NetPermissionFlags {
|
|||||||
PF_ISIMPLICIT = (1U << 31),
|
PF_ISIMPLICIT = (1U << 31),
|
||||||
PF_ALL = PF_BLOOMFILTER | PF_FORCERELAY | PF_RELAY | PF_NOBAN | PF_MEMPOOL | PF_DOWNLOAD | PF_ADDR,
|
PF_ALL = PF_BLOOMFILTER | PF_FORCERELAY | PF_RELAY | PF_NOBAN | PF_MEMPOOL | PF_DOWNLOAD | PF_ADDR,
|
||||||
};
|
};
|
||||||
|
static inline constexpr NetPermissionFlags operator|(NetPermissionFlags a, NetPermissionFlags b)
|
||||||
|
{
|
||||||
|
using t = typename std::underlying_type<NetPermissionFlags>::type;
|
||||||
|
return static_cast<NetPermissionFlags>(static_cast<t>(a) | static_cast<t>(b));
|
||||||
|
}
|
||||||
|
|
||||||
class NetPermissions
|
class NetPermissions
|
||||||
{
|
{
|
||||||
@@ -45,11 +51,12 @@ public:
|
|||||||
static std::vector<std::string> ToStrings(NetPermissionFlags flags);
|
static std::vector<std::string> ToStrings(NetPermissionFlags flags);
|
||||||
static inline bool HasFlag(NetPermissionFlags flags, NetPermissionFlags f)
|
static inline bool HasFlag(NetPermissionFlags flags, NetPermissionFlags f)
|
||||||
{
|
{
|
||||||
return (flags & f) == f;
|
using t = typename std::underlying_type<NetPermissionFlags>::type;
|
||||||
|
return (static_cast<t>(flags) & static_cast<t>(f)) == static_cast<t>(f);
|
||||||
}
|
}
|
||||||
static inline void AddFlag(NetPermissionFlags& flags, NetPermissionFlags f)
|
static inline void AddFlag(NetPermissionFlags& flags, NetPermissionFlags f)
|
||||||
{
|
{
|
||||||
flags = static_cast<NetPermissionFlags>(flags | f);
|
flags = flags | f;
|
||||||
}
|
}
|
||||||
//! ClearFlag is only called with `f` == NetPermissionFlags::PF_ISIMPLICIT.
|
//! ClearFlag is only called with `f` == NetPermissionFlags::PF_ISIMPLICIT.
|
||||||
//! If that should change in the future, be aware that ClearFlag should not
|
//! If that should change in the future, be aware that ClearFlag should not
|
||||||
@@ -59,7 +66,8 @@ public:
|
|||||||
static inline void ClearFlag(NetPermissionFlags& flags, NetPermissionFlags f)
|
static inline void ClearFlag(NetPermissionFlags& flags, NetPermissionFlags f)
|
||||||
{
|
{
|
||||||
assert(f == NetPermissionFlags::PF_ISIMPLICIT);
|
assert(f == NetPermissionFlags::PF_ISIMPLICIT);
|
||||||
flags = static_cast<NetPermissionFlags>(flags & ~f);
|
using t = typename std::underlying_type<NetPermissionFlags>::type;
|
||||||
|
flags = static_cast<NetPermissionFlags>(static_cast<t>(flags) & ~static_cast<t>(f));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user