add CCRef module

This commit is contained in:
Simon Cruanes 2015-01-29 00:35:34 +01:00
parent dea635f8e3
commit f40cdbe64f
4 changed files with 117 additions and 1 deletions

2
_oasis
View file

@ -48,7 +48,7 @@ Flag "advanced"
Library "containers" Library "containers"
Path: src/core Path: src/core
Modules: CCVector, CCPrint, CCError, CCHeap, CCList, CCOpt, CCPair, Modules: CCVector, CCPrint, CCError, CCHeap, CCList, CCOpt, CCPair,
CCFun, CCHash, CCInt, CCBool, CCFloat, CCArray, CCFun, CCHash, CCInt, CCBool, CCFloat, CCArray, CCRef,
CCOrd, CCRandom, CCString, CCHashtbl, CCMap, CCFormat CCOrd, CCRandom, CCString, CCHashtbl, CCMap, CCFormat
BuildDepends: bytes BuildDepends: bytes

View file

@ -40,6 +40,7 @@ CCOrd
CCPair CCPair
CCPrint CCPrint
CCRandom CCRandom
CCRef
CCString CCString
CCVector CCVector
} }

56
src/core/CCRef.ml Normal file
View file

@ -0,0 +1,56 @@
(*
copyright (c) 2013-2014, simon cruanes
all rights reserved.
redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer. redistributions in binary
form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with
the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*)
(** {1 References} *)
type 'a print = Format.formatter -> 'a -> unit
type 'a pp = Buffer.t -> 'a -> unit
type 'a ord = 'a -> 'a -> int
type 'a eq = 'a -> 'a -> bool
type 'a sequence = ('a -> unit) -> unit
type 'a t = 'a ref
let create x = ref x
let map f r = ref (f !r)
let iter f r = f !r
let update f r = r := (f !r)
let compare f r1 r2 = f !r1 !r2
let equal f r1 r2 = f !r1 !r2
let to_list r = [!r]
let to_seq r yield = yield !r
let print pp_x fmt r = pp_x fmt !r
let pp pp_x buf r = pp_x buf !r

59
src/core/CCRef.mli Normal file
View file

@ -0,0 +1,59 @@
(*
copyright (c) 2013-2014, simon cruanes
all rights reserved.
redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer. redistributions in binary
form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with
the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*)
(** {1 References} *)
type 'a print = Format.formatter -> 'a -> unit
type 'a pp = Buffer.t -> 'a -> unit
type 'a ord = 'a -> 'a -> int
type 'a eq = 'a -> 'a -> bool
type 'a sequence = ('a -> unit) -> unit
type 'a t = 'a ref
val map : ('a -> 'b) -> 'a t -> 'b t
(** Transform the value *)
val create : 'a -> 'a t
(** Alias to {!ref} *)
val iter : ('a -> unit) -> 'a t -> unit
(** Call the function on the content of the reference *)
val update : ('a -> 'a) -> 'a t -> unit
(** Update the reference's content with the given function *)
val compare : 'a ord -> 'a t ord
val equal : 'a eq -> 'a t eq
val to_list : 'a t -> 'a list
val to_seq : 'a t -> 'a sequence
val print : 'a print -> 'a t print
val pp : 'a pp -> 'a t pp