rfc:enumset
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:enumset [2021/03/14 07:57] – typo bwoebi | rfc:enumset [2021/03/14 13:37] (current) – bwoebi | ||
---|---|---|---|
Line 30: | Line 30: | ||
* It also is parent class to all '' | * It also is parent class to all '' | ||
* Its constructor creates an '' | * Its constructor creates an '' | ||
- | * Doing an '' | + | * Doing an (explicit) |
- | * Doing an (implicit) '' | + | * Doing an (explicit) '' |
* The '' | * The '' | ||
* Two '' | * Two '' | ||
Line 38: | Line 38: | ||
<code php> | <code php> | ||
enum Perm { | enum Perm { | ||
- | Read, | + | |
- | Write, | + | |
- | Exec | + | |
} | } | ||
</ | </ | ||
Line 97: | Line 97: | ||
var_dump(~Perm:: | var_dump(~Perm:: | ||
</ | </ | ||
+ | |||
+ | Naturally, these behaviours also extend to the assign-ops '' | ||
Doing a binary operation on incompatible '' | Doing a binary operation on incompatible '' | ||
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. | ||
- | '' | + | '' |
Conversely, '' | Conversely, '' | ||
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< | ||
+ | $bits = 0; | ||
+ | foreach ($perms as $perm) { | ||
+ | $bits |= $perm-> | ||
+ | } | ||
+ | return $bits; | ||
+ | } | ||
+ | |||
+ | static function fromInt(int $bits) : EnumSet< | ||
+ | $perms = new EnumSet< | ||
+ | foreach (self:: | ||
+ | if ($perm-> | ||
+ | $perms |= $perm; | ||
+ | } | ||
+ | } | ||
+ | return $perms; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $mode = stat($someFile)[" | ||
+ | $perms = FilePerm:: | ||
+ | |||
+ | $perms &= FilePerm:: | ||
+ | |||
+ | chmod($someFile, | ||
+ | </ | ||
===== FAQ ===== | ===== FAQ ===== |
rfc/enumset.1615708626.txt.gz · Last modified: 2021/03/14 07:57 by bwoebi