diff --git a/_oasis b/_oasis index b348e689..90e512cb 100644 --- a/_oasis +++ b/_oasis @@ -48,7 +48,7 @@ Flag "advanced" Library "containers" Path: src/core 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 BuildDepends: bytes diff --git a/doc/intro.txt b/doc/intro.txt index edbad184..400e2e7c 100644 --- a/doc/intro.txt +++ b/doc/intro.txt @@ -40,6 +40,7 @@ CCOrd CCPair CCPrint CCRandom +CCRef CCString CCVector } diff --git a/src/core/CCRef.ml b/src/core/CCRef.ml new file mode 100644 index 00000000..136d0cfe --- /dev/null +++ b/src/core/CCRef.ml @@ -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 + + diff --git a/src/core/CCRef.mli b/src/core/CCRef.mli new file mode 100644 index 00000000..475c0d97 --- /dev/null +++ b/src/core/CCRef.mli @@ -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 +