Import Upstream version 20180207
[hcoop/debian/mlton.git] / basis-library / posix / flags.sml
1 (* Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 * Copyright (C) 1997-2000 NEC Research Institute.
4 *
5 * MLton is released under a BSD-style license.
6 * See the file MLton-LICENSE for details.
7 *)
8
9 functor BitFlags(structure S : sig
10 eqtype t
11 val castToSysWord: t -> SysWord.word
12 val castFromSysWord: SysWord.word -> t
13 val andb: t * t -> t
14 val notb: t -> t
15 val orb: t * t -> t
16 end): BIT_FLAGS_EXTRA =
17 struct
18 type flags = S.t
19
20 val all: flags = S.castFromSysWord (SysWord.~ 0w1)
21 val empty: flags = S.castFromSysWord 0w0
22
23 fun toWord f = S.castToSysWord f
24 fun fromWord w = S.castFromSysWord (SysWord.andb (w, toWord all))
25
26 val flags: flags list -> flags = List.foldl S.orb empty
27
28 val intersect: flags list -> flags = List.foldl S.andb all
29
30 fun clear (f, f') = S.andb (S.notb f, f')
31
32 fun allSet (f, f') = S.andb (f, f') = f'
33
34 fun anySet (f, f') = S.andb (f, f') <> empty
35 end