File: common.mli

package info (click to toggle)
bin-prot 1.2.23-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 620 kB
  • ctags: 1,699
  • sloc: ml: 5,126; ansic: 1,586; makefile: 121
file content (185 lines) | stat: -rw-r--r-- 6,805 bytes parent folder | download
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
(* File: common.mli

    Copyright (C) 2007-

      Jane Street Holding, LLC
      Author: Markus Mottl
      email: mmottl\@janestreet.com
      WWW: http://www.janestreet.com/ocaml

   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 of the License, or (at your option) any later version.

   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
*)

(** Common: common definitions used by binary protocol converters *)

open Bigarray

(** {2 Buffers} *)

(** Position within buffers *)
type pos = int

(** Reference to a position within buffers *)
type pos_ref = pos ref

(** Buffers *)
type buf = (char, int8_unsigned_elt, c_layout) Array1.t

val create_buf : int -> buf
(** [create_buf n] creates a buffer of size [n]. *)

val check_pos : buf -> pos -> unit
(** [check_pos buf pos] @raise Buffer_short if position [pos] exceeds
    the length of buffer [buf]. *)

val check_next : buf -> pos -> unit
(** [check_next buf pos] @raise Buffer_short if the next position after
    [pos] exceeds the length of buffer [buf]. *)

val safe_get_pos : buf -> pos_ref -> pos
(** [safe_get_pos buf pos_ref] @return the position referenced by
    [pos_ref] within buffer [buf].  @raise Buffer_short if the position
    exceeds the length of the buffer. *)

val blit_string_buf :
  ?src_pos : int -> string -> ?dst_pos : int -> buf -> len : int -> unit
(** [blit_string_buf ?src_pos src ?dst_pos dst ~len] blits [len]
    bytes of the source string [src] starting at position [src_pos]
    to buffer [dst] starting at position [dst_pos].

    @raise Invalid_argument if the designated ranges are invalid.
*)

val blit_buf_string :
  ?src_pos : int -> buf -> ?dst_pos : int -> string -> len : int -> unit
(** [blit_buf_string ?src_pos src ?dst_pos dst ~len] blits [len]
    bytes of the source buffer [src] starting at position [src_pos]
    to string [dst] starting at position [dst_pos].

    @raise Invalid_argument if the designated ranges are invalid.
*)

val blit_buf :
  ?src_pos : int -> src : buf -> ?dst_pos : int -> dst : buf -> int -> unit
(** [blit_buf ?src_pos ~src ?dst_pos ~dst len] blits [len] bytes of the
    source buffer [src] starting at position [src_pos] to destination
    buffer [dst] starting at position [dst_pos].

    @raise Invalid_argument if the designated ranges are invalid.
*)


(** {2 Errors and exceptions} *)

exception Buffer_short  (** Buffer too short for read/write operation *)
exception No_variant_match  (** Used internally for backtracking *)

module ReadError : sig
  type t =
    | Neg_int8  (** Negative integer was positive or zero *)
    | Int_code  (** Unknown integer code while reading integer *)
    | Int_overflow  (** Overflow reading integer *)
    | Nat0_code  (** Unknown integer code while reading natural number *)
    | Nat0_overflow  (** Overflow reading natural number *)
    | Int32_code  (** Unknown integer code while reading 32bit integer *)
    | Int64_code  (** Unknown integer code while reading 64bit integer *)
    | Nativeint_code  (** Unknown integer code while reading native integer *)
    | Unit_code  (** Illegal unit value *)
    | Bool_code  (** Illegal boolean value *)
    | Option_code  (** Illegal option code *)
    | String_too_long  (** String too long *)
    | Variant_tag  (** Untagged integer encoding for variant tag *)
    | Array_too_long  (** Array too long *)
    | Hashtbl_too_long  (** Hashtable too long *)
    | Sum_tag of string  (** Illegal sum tag for given type *)
    | Variant of string  (** Illegal variant for given type *)
    | Poly_rec_bound of string
        (** Attempt to read data bound through polymorphic record fields *)
    | Variant_wrong_type of string
        (** Unexpected attempt to read variant with given non-variant type *)
    | Silly_type of string
        (** [Silly_type type_name] indicates unhandled but silly case
            where a type of the sort [type 'a type_name = 'a] is used
            with a polymorphic variant as type parameter and included
            in another polymorphic variant type. *)
    | Empty_type of string
        (** Attempt to read data that corresponds to an empty type. *)

  val to_string : t -> string
  (** [to_string err] @return string representation of read error [err]. *)
end

exception Read_error of ReadError.t * pos
(** [ReadError (err, err_pos)] *)

exception Read_exc of exn * pos
(** [ReadExc (exn, err_pos)] *)

exception Poly_rec_write of string
(** [PolyRecWrite type] gets raised when the user attempts to write or
    estimate the size of a value of a type that is bound through a
    polymorphic record field in type definition [type]. *)

exception Empty_type of string
(** [EmptyType] gets raised when the user attempts to write or estimate
    the size of a value of an empty type, which would not make sense. *)

val raise_read_error : ReadError.t -> pos -> 'a
(** [raise_read_error err pos] *)

val raise_read_exc : exn -> pos -> 'a
(** [raise_read_exc exc pos] *)

val raise_concurrent_modification : string -> 'a
(** [raise_concurrent_modification loc] @raise Failure if a binary writer
    detects a concurrent change to the underlying data structure. *)

val array_bound_error : unit -> 'a
(** [array_bound_error ()] *)


(** {2 Bigarrays} *)

type vec32 = (float, float32_elt, fortran_layout) Array1.t
type vec64 = (float, float64_elt, fortran_layout) Array1.t
type vec = vec64

type mat32 = (float, float32_elt, fortran_layout) Array2.t
type mat64 = (float, float64_elt, fortran_layout) Array2.t
type mat = mat64


(** {2 Miscellaneous} *)

val copy_htbl_list : ('a, 'b) Hashtbl.t -> ('a * 'b) list -> ('a, 'b) Hashtbl.t
(** [copy_htbl_list htbl lst] adds all [(key, value)] pairs in [lst]
    to hash table [htbl]. *)

external variant_of_int : int -> [> ] = "%identity"


(** {2 NOTE: unsafe functions!!!} *)

external unsafe_blit_buf :
  src_pos : int -> src : buf -> dst_pos : int -> dst : buf -> len : int -> unit
  = "bin_prot_blit_buf_stub"

external unsafe_blit_string_buf :
  src_pos : int -> string -> dst_pos : int -> buf -> len : int -> unit
  = "bin_prot_blit_string_buf_stub" "noalloc"

external unsafe_blit_buf_string :
  src_pos : int -> buf -> dst_pos : int -> string -> len : int -> unit
  = "bin_prot_blit_buf_string_stub" "noalloc"