package batteries

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file batRef.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
(*
 * Ref - Operations on references
 * Copyright (C) 2008 David Teller
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version,
 * with the special exception on linking described in file LICENSE.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)


type 'a t = 'a ref

let post r f =
  let old = !r in
  r := f old;
  old

let pre r f =
  r := f !r;
  !r

let swap a b =
  let buf = !a in
  a := !b;
  b := buf

(*$T swap
  let a = ref 1 and b = ref 2 in swap a b; !a = 2 && !b = 1
*)

let pre_incr  r = pre  r ( ( + ) 1 )
let pre_decr  r = pre  r ( ( + ) (-1) )
let post_incr r = post r ( ( + ) 1 )
let post_decr r = post r ( ( + ) (-1) )

(*$T pre_incr
  let r = ref 0 in pre_incr r = 1 && !r = 1
*)
(*$T post_incr
  let r = ref 0 in post_incr r = 0 && !r = 1
*)

let copy r = ref (!r)

(*$T copy
  let r = ref 0 in let s = copy r in r := 1; !s == 0 && !r == 1
*)

let protect r v body =
  let old = !r in
  try
    r := v;
    let res = body() in
    r := old;
    res
  with x ->
    r := old;
    raise x

(*$T protect
  let r = ref 0 in let b () = incr r; !r in protect r 2 b = 3 && !r = 0
  let r = ref 0 in let b () = incr r; if !r=3 then raise Not_found in (try protect r 2 b; false with Not_found -> true) && !r = 0
*)


external ref : 'a -> 'a ref = "%makemutable"
(** Return a fresh reference containing the given value. *)

external ( ! ) : 'a ref -> 'a = "%field0"
(** [!r] returns the current contents of reference [r].
    Equivalent to [fun r -> r.contents]. *)

external ( := ) : 'a ref -> 'a -> unit = "%setfield0"
(** [r := a] stores the value of [a] in reference [r].
    Equivalent to [fun r v -> r.contents <- v]. *)

external set : 'a ref -> 'a -> unit = "%setfield0"
(** As [ := ] *)

external get : 'a ref -> 'a = "%field0"
(** As [ ! ]*)

let print print_a out r = print_a out !r

let toggle r = r := not !r

(*$T toggle
  let r = ref true in toggle r; !r = false
  let r = ref false in toggle r; !r = true
*)

let oset r x = r := Some x

let oget_exn r = match !r with None -> raise Not_found | Some x -> x

(*  FAIL $T oset, oget_exn
    let r = ref None in oset r 3; oget_exn r = 3
*)

let compare c x y = c !x !y
(*$T compare
  let a = ref 1 and b = ref 2 in compare Int.compare a b < 0
*)

let ord o x y = o !x !y
(*$T ord
  let a = ref 1 and b = ref 2 in ord Int.ord a b = BatOrd.Lt
*)

let eq e x y = e !x !y
  (*$T eq
    let a = ref 1 and b = ref 2 in eq Int.equal a b = false
    let a = ref 1 and b = ref 1 in eq Int.equal a b = true
  *)
OCaml

Innovation. Community. Security.