1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586(*
* 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;(try(* We catch and log exceptions raised by clean_f to avoid shadowing
the original exception raised by fct *)clean_f()withcleanup_exn->Logs.warn~src(funm->m"finally: Error while running cleanup after failure of main \
function: %s"(Printexc.to_stringcleanup_exn)));raiseexninclean_f();resultletmaybe_with_defaultdfv=Option.fold~none:d~some:fvletmayfv=Option.mapfvletdefaultdv=Option.value~default:dvletmaybefv=Option.iterfv(** execute fct ignoring exceptions *)letignore_exnfct=tryfct()with_->()(** if not bool ignore exceptions raised by fct () *)letreraise_ifboolfct=ifboolthenfct()elseignore_exnfct(* 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