Commits

camlspotter committed 7e6436f

added ref converters

Comments (0)

Files changed (5)

-1.1.4
+1.1.4 (not yet)
 -------------
 
 - Added xml_conv
+- Added ref encoder/decoder for conv(ocaml)
 
 1.1.3
 -------------
             "Meta_conv.Internal.option_of: option expected"
             ?trace v
     
+    let ref_of extract f ?trace v =
+      match extract v with 
+      | Some v -> f ?trace v >>= fun x -> `Ok { contents= x }
+      | None -> 
+          primitive_decoding_failure 
+            "Meta_conv.Internal.ref_of: option expected"
+            ?trace v
+    
     let lazy_t_of (errorf : 'target Error.t -> 'exn) f ?trace:_ v = 
       `Ok (lazy (
         (* trace is reset to avoid leak *)
       let tbl = Hashtbl.create 101 in (* CR jfuruse: size fixed *)
       List.iter (fun (k,v) -> Hashtbl.add tbl k v) abs;
       `Ok tbl
+
   end
 
 end
         None :        : Error. Target value cannot have the option type
     *)
     
+    val ref_of : 
+      (target -> target option) 
+      -> 'a decoder -> 'a ref decoder
+    (** typical ref_of_<targe_type>. 
+        It takes predicate to check the arg is a reference or something else.
+    
+        Some v : Found a record { contents = <v> }
+        None   : Error. Target value cannot have the option type
+    *)
+    
     val lazy_t_of : 
       (target Error.t -> 'a) (** error handler *)
       -> 'a decoder -> 'a lazy_t decoder

ocaml/ocaml_conv.ml

 let ocaml_of_option f = function
   | None -> Variant ("None", [])
   | Some v -> Variant ("Some", [f v])
+let ocaml_of_ref f = function
+  | { contents=v } -> Record [ "contents", f v ]
 let ocaml_of_lazy_t f z = f (Lazy.force z)
 
 let ocaml_of_mc_lazy_t (f : 'a encoder) (v : ('a, 'target) mc_lazy_t) = match Lazy.force v with
   | Variant ("Some", [v]) -> Some (Some v)
   | _ -> None) f
 
+let ref_of_ocaml f = Helper.ref_of (function
+  | Record [ "contents", v ] -> Some v
+  | _ -> None) f
+
 let lazy_t_of_ocaml (d : ('a, Ocaml.t) Decoder.t) : ('a lazy_t, Ocaml.t) Decoder.t = 
   Helper.lazy_t_of (fun (e : Ocaml.t Meta_conv.Error.t) -> raise (Error e)) d
 

ocaml/ocaml_conv.mli

 val ocaml_of_list      : 'a encoder -> 'a list encoder
 val ocaml_of_array     : 'a encoder -> 'a array encoder
 val ocaml_of_option    : 'a encoder -> 'a option encoder
+val ocaml_of_ref       : 'a encoder -> 'a ref encoder
 val ocaml_of_lazy_t    : 'a encoder -> 'a Lazy.t encoder
 val ocaml_of_mc_lazy_t : 'a encoder -> ('a, target) mc_lazy_t encoder
 val ocaml_of_mc_fields : 'a encoder -> (string * 'a) list encoder
 val list_of_ocaml      : 'a decoder -> 'a list decoder
 val array_of_ocaml     : 'a decoder -> 'a array decoder
 val option_of_ocaml    : 'a decoder -> 'a option decoder
+val ref_of_ocaml       : 'a decoder -> 'a ref decoder
 val lazy_t_of_ocaml    : 'a decoder -> 'a lazy_t decoder
 
 (*