rfc:enumset

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:enumset [2021/03/14 07:50] – created 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 144: Line 146:
 Luckily there already is a function returning all the enums: ''cases''. We just need to make it return ''EnumSet<E>'' instead. Luckily there already is a function returning all the enums: ''cases''. We just need to make it return ''EnumSet<E>'' instead.
  
-It signature thus is ''public static function cases(): EnumSet<E>''.+Its signature thus is ''public static function cases(): EnumSet<E>''.
  
 The order of the returned ''EnumSet'' will be the order of definition of the individual enum cases. The order of the returned ''EnumSet'' will be the order of definition of the individual enum cases.
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.1615708206.txt.gz · Last modified: 2021/03/14 07:50 by bwoebi