C++11 standard conformant bitmasks using enum class

后端 未结 5 1144
春和景丽
春和景丽 2021-01-31 03:17

Can you implement standard conformant (as described in 17.5.2.1.3 of the n3242 draft) type safe bitmasks using enum class? The way I read it, a type T is a bitmask if it support

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-31 03:55

    I think you can... You'll have to add operators for bitmasky things. I didn't do it here but you could easily overload any relational operator.

      /**
       *
       */
      // NOTE: I changed to a more descriptive and consistent name
      //       This needs to be a real bitmask type.
      enum class file_permissions : int
      {
        no_perms        = 0,
    
        owner_read      =  0400,
        owner_write     =  0200,
        owner_exe       =  0100,
        owner_all       =  0700,
    
        group_read      =   040,
        group_write     =   020,
        group_exe       =   010,
        group_all       =   070,
    
        others_read     =    04,
        others_write    =    02,
        others_exe      =    01,
        others_all      =    07,
    
        all_all     = owner_all | group_all | others_all, // 0777
    
        set_uid_on_exe  = 04000,
        set_gid_on_exe  = 02000,
        sticky_bit      = 01000,
    
        perms_mask      = all_all | set_uid_on_exe | set_gid_on_exe | sticky_bit, // 07777
    
        perms_not_known = 0xffff,
    
        add_perms       = 0x1000,
        remove_perms    = 0x2000,
        symlink_perms   = 0x4000
      };
    
      inline constexpr file_permissions
      operator&(file_permissions x, file_permissions y)
      {
        return static_cast
          (static_cast(x) & static_cast(y));
      }
    
      inline constexpr file_permissions
      operator|(file_permissions x, file_permissions y)
      {
        return static_cast
          (static_cast(x) | static_cast(y));
      }
    
      inline constexpr file_permissions
      operator^(file_permissions x, file_permissions y)
      {
        return static_cast
          (static_cast(x) ^ static_cast(y));
      }
    
      inline constexpr file_permissions
      operator~(file_permissions x)
      {
        return static_cast(~static_cast(x));
      }
    
      inline file_permissions &
      operator&=(file_permissions & x, file_permissions y)
      {
        x = x & y;
        return x;
      }
    
      inline file_permissions &
      operator|=(file_permissions & x, file_permissions y)
      {
        x = x | y;
        return x;
      }
    
      inline file_permissions &
      operator^=(file_permissions & x, file_permissions y)
      {
        x = x ^ y;
        return x;
      }
    

提交回复
热议问题