12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273(*
* 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
*)letsrc=Logs.Src.create"pervasiveext"~doc:"logs from Xapi_stdext_pervasives.Pervasiveext"letfinallyfctclean_f=letresult=tryfct();withexn->Backtrace.is_importantexn;begin(* We catch and log exceptions raised by clean_f to avoid shadowing
the original exception raised by fct *)tryclean_f();withcleanup_exn->Logs.warn~src(funm->m"finally: Error while running cleanup after failure of main function: %s"(Printexc.to_stringcleanup_exn));end;raiseexninclean_f();result(* Those should go into the Opt module: *)letmaybe_with_defaultdfv=matchvwithNone->d|Somex->fx(** if v is not none, apply f on it and return some value else return none. *)letmayfv=maybe_with_defaultNone(funx->Some(fx))v(** default value to d if v is none. *)letdefaultdv=maybe_with_defaultd(funx->x)v(** apply f on v if not none *)letmaybefv=maybe_with_default()fv(** if bool is false then we intercept and quiten any exception *)letreraise_ifboolfct=tryfct()withexn->ifboolthenraiseexnelse()(** execute fct ignoring exceptions *)letignore_exnfct=tryfct()with_->()(* non polymorphic ignore function *)letignore_intv=let(_:int)=vin()letignore_int64v=let(_:int64)=vin()letignore_int32v=let(_:int32)=vin()letignore_stringv=let(_:string)=vin()letignore_floatv=let(_:float)=vin()letignore_boolv=let(_:bool)=vin()(* To avoid some parens: *)(* composition of functions: *)let(++)fgx=f(gx)(* and application *)let($)fa=fa