rfc:enumset

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:enumset [2021/03/14 07:57] – typo bwoebirfc:enumset [2021/03/14 13:37] (current) bwoebi
Line 30: Line 30:
   * It also is parent class to all ''UnitEnum''.   * It also is parent class to all ''UnitEnum''.
   * Its constructor creates an ''EnumSet<E>'' with all passed enum values.   * Its constructor creates an ''EnumSet<E>'' with all passed enum values.
-  * Doing an ''(array)'' cast on the ''EnumSet<E>'' instance returns all contained values. +  * Doing an (explicit) ''(array)'' cast on the ''EnumSet<E>'' instance returns all contained values. 
-  * Doing an (implicit) ''(bool)'' cast on the ''EnumSet<E>'' instance returns ''true'', unless it is empty. Then it returns ''false''.+  * Doing an (explicit) ''(bool)'' cast on the ''EnumSet<E>'' instance returns ''true'', unless it is empty. Then it returns ''false''.
   * The ''cases'' static method is promoted to ''EnumSet<E>''. It also returns an ''EnumSet<E>'' containing all enum values instead of an array.   * The ''cases'' static method is promoted to ''EnumSet<E>''. It also returns an ''EnumSet<E>'' containing all enum values instead of an array.
   * Two ''EnumSet'' instances are only weakly equal (''=='') if the contents are the same. The order is ignored for equivalence checking.   * Two ''EnumSet'' instances are only weakly equal (''=='') if the contents are the same. The order is ignored for equivalence checking.
Line 38: Line 38:
 <code php> <code php>
 enum Perm { enum Perm {
-  Read, +  case Read; 
-  Write, +  case Write; 
-  Exec+  case Exec;
 } }
 </code> </code>
Line 97: Line 97:
 var_dump(~Perm::Write); // Perm::Read | Perm::Exec var_dump(~Perm::Write); // Perm::Read | Perm::Exec
 </code> </code>
 +
 +Naturally, these behaviours also extend to the assign-ops ''|='' and ''&=''.
  
 Doing a binary operation on incompatible ''EnumSet'' instances will throw a ''TypeError''. Doing a binary operation on incompatible ''EnumSet'' instances will throw a ''TypeError''.
Line 121: Line 123:
 The keys of this iterator are continuous and starting at zero. The keys of this iterator are continuous and starting at zero.
  
-''EnumSet'' instances can be cast to array like any other object. This is equivalent to applying ''iterator_to_array()'' here.+''EnumSet'' instances can be cast to array like any other object. This is equivalent to applying ''iterator_to_array()'' here. This is not special or different to ''(array)'' casts of other objects.
  
 Conversely, ''EnumSet'' being so close to arrays in behavior, the weak comparison (''=='') semantics of ''EnumSet'' are also identical to those of arrays: Two ''EnumSet'' instances are weakly equal if the contents are the same, regardless of the ordering. Conversely, ''EnumSet'' being so close to arrays in behavior, the weak comparison (''=='') semantics of ''EnumSet'' are also identical to those of arrays: Two ''EnumSet'' instances are weakly equal if the contents are the same, regardless of the ordering.
Line 167: Line 169:
 ===== Examples ===== ===== Examples =====
  
-TBD ...+More examples ... 
 + 
 +==== Serializing and unserializing file permissions ==== 
 + 
 +<code php> 
 +enum FilePerm { 
 +    case OTHER_EXEC = 0001; case OTHER_WRITE = 0002; case OTHER_READ = 0004; 
 +    case GROUP_EXEC = 0010; case GROUP_WRITE = 0020; case GROUP_READ = 0040; 
 +    case OWNER_EXEC = 0100; case OWNER_WRITE = 0200; case OWNER_READ = 0400; 
 +     
 +    static function toInt(EnumSet<FilePerm> $perms) : int { 
 +        $bits = 0; 
 +        foreach ($perms as $perm) { 
 +            $bits |= $perm->value; 
 +        } 
 +        return $bits; 
 +    } 
 +     
 +    static function fromInt(int $bits) : EnumSet<FilePerm>
 +        $perms = new EnumSet<FilePerm>; 
 +        foreach (self::cases() as $perm) { 
 +            if ($perm->value & $bits) { 
 +                $perms |= $perm; 
 +            } 
 +        } 
 +        return $perms; 
 +    } 
 +
 + 
 +$mode = stat($someFile)["mode"]; // e.g. 0644 
 +$perms = FilePerm::fromInt($mode); // OTHER_READ | GROUP_READ | OWNER_WRITE | OWNER_READ 
 + 
 +$perms &= FilePerm::OWNER_READ | FilePerm::OWNER_WRITE | FilePerm::OWNER_EXEC; // dismiss all but owner permissions 
 + 
 +chmod($someFile, FilePerm::toInt($perms)); // saving 0600 
 +</code>
  
 ===== FAQ ===== ===== FAQ =====
rfc/enumset.1615708626.txt.gz · Last modified: 2021/03/14 07:57 by bwoebi