verify_flags.h raw

   1  // Copyright (c) 2009-2010 Satoshi Nakamoto
   2  // Copyright (c) 2009-present The Bitcoin Core developers
   3  // Distributed under the MIT software license, see the accompanying
   4  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   5  
   6  #ifndef BITCOIN_SCRIPT_VERIFY_FLAGS_H
   7  #define BITCOIN_SCRIPT_VERIFY_FLAGS_H
   8  
   9  #include <compare>
  10  #include <cstdint>
  11  
  12  enum class script_verify_flag_name : uint8_t;
  13  
  14  class script_verify_flags
  15  {
  16  public:
  17      using value_type = uint64_t;
  18  
  19      consteval script_verify_flags() = default;
  20  
  21      // also allow construction with hard-coded 0 (but not other integers)
  22      consteval explicit(false) script_verify_flags(value_type f) : m_value{f} { if (f != 0) throw 0; }
  23  
  24      // implicit construction from a hard-coded SCRIPT_VERIFY_* constant is also okay
  25      constexpr explicit(false) script_verify_flags(script_verify_flag_name f) : m_value{value_type{1} << static_cast<uint8_t>(f)} { }
  26  
  27      // rule of 5
  28      constexpr script_verify_flags(const script_verify_flags&) = default;
  29      constexpr script_verify_flags(script_verify_flags&&) = default;
  30      constexpr script_verify_flags& operator=(const script_verify_flags&) = default;
  31      constexpr script_verify_flags& operator=(script_verify_flags&&) = default;
  32      constexpr ~script_verify_flags() = default;
  33  
  34      // integer conversion needs to be very explicit
  35      static constexpr script_verify_flags from_int(value_type f) { script_verify_flags r; r.m_value = f; return r; }
  36      constexpr value_type as_int() const { return m_value; }
  37  
  38      // bitwise operations
  39      constexpr script_verify_flags operator~() const { return from_int(~m_value); }
  40      friend constexpr script_verify_flags operator|(script_verify_flags a, script_verify_flags b) { return from_int(a.m_value | b.m_value); }
  41      friend constexpr script_verify_flags operator&(script_verify_flags a, script_verify_flags b) { return from_int(a.m_value & b.m_value); }
  42  
  43      // in-place bitwise operations
  44      constexpr script_verify_flags& operator|=(script_verify_flags vf) { m_value |= vf.m_value; return *this; }
  45      constexpr script_verify_flags& operator&=(script_verify_flags vf) { m_value &= vf.m_value; return *this; }
  46  
  47      // tests
  48      constexpr explicit operator bool() const { return m_value != 0; }
  49      constexpr bool operator==(script_verify_flags other) const { return m_value == other.m_value; }
  50  
  51      /** Compare two script_verify_flags. <, >, <=, and >= are auto-generated from this. */
  52      friend constexpr std::strong_ordering operator<=>(const script_verify_flags& a, const script_verify_flags& b) noexcept
  53      {
  54          return a.m_value <=> b.m_value;
  55      }
  56  
  57  private:
  58      value_type m_value{0}; // default value is SCRIPT_VERIFY_NONE
  59  };
  60  
  61  inline constexpr script_verify_flags operator~(script_verify_flag_name f)
  62  {
  63      return ~script_verify_flags{f};
  64  }
  65  
  66  inline constexpr script_verify_flags operator|(script_verify_flag_name f1, script_verify_flag_name f2)
  67  {
  68      return script_verify_flags{f1} | f2;
  69  }
  70  
  71  #endif // BITCOIN_SCRIPT_VERIFY_FLAGS_H
  72