Source file pervasiveext.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
(*
 * Copyright (C) 2006-2009 Citrix Systems Inc.
 *
 * This program 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; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program 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.
 *)
(** apply the clean_f function after fct function has been called.
 * Even if fct raises an exception, clean_f is applied
*)

let src = Logs.Src.create "pervasiveext" ~doc:"logs from Xapi_stdext_pervasives.Pervasiveext"


let finally fct clean_f =
  let result =
    try
      fct ();
    with exn ->
      Backtrace.is_important exn;
      begin
        (* We catch and log exceptions raised by clean_f to avoid shadowing
           the original exception raised by fct *)
        try
          clean_f ();
        with cleanup_exn ->
          Logs.warn ~src (fun m -> m "finally: Error while running cleanup after failure of main function: %s" (Printexc.to_string cleanup_exn));
      end;
      raise exn in
  clean_f ();
  result

(* Those should go into the Opt module: *)

let maybe_with_default d f v =
  match v with None -> d | Some x -> f x

(** if v is not none, apply f on it and return some value else return none. *)
let may f v = maybe_with_default None (fun x -> Some (f x)) v

(** default value to d if v is none. *) 
let default d v = maybe_with_default d (fun x -> x) v

(** apply f on v if not none *)
let maybe f v = maybe_with_default () f v

(** if bool is false then we intercept and quiten any exception *)
let reraise_if bool fct =
  try fct () with exn -> if bool then raise exn else ()

(** execute fct ignoring exceptions *)
let ignore_exn fct = try fct () with _ -> ()

(* non polymorphic ignore function *)
let ignore_int v = let (_: int) = v in ()
let ignore_int64 v = let (_: int64) = v in ()
let ignore_int32 v = let (_: int32) = v in ()
let ignore_string v = let (_: string) = v in ()
let ignore_float v = let (_: float) = v in ()
let ignore_bool v = let (_: bool) = v in ()

(* To avoid some parens: *)
(* composition of functions: *)
let (++) f g x = f (g x)

(* and application *)
let ($) f a = f a