/
dbVirtual.opa
108 lines (90 loc) · 2.96 KB
/
dbVirtual.opa
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
/*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA 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 Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Utility functions on virtual path
*
* @category DATABASE
* @author Quentin Bourgerie
* @destination PUBLIC
* @stability Not stable
*/
/**
* {1 About this module}
*
* A virtual reference path parametrized by the type of value which
* can be read, and type of value that take for write.
*
* {1 Where should I start?}
*
* {1 What if I need more?}
*/
/**
* {1 Types defined in this module}
*/
@opacapi
@abstract type virtual_ref_path('read, 'write) = {
read : -> 'read;
write : 'write -> void;
}
/**
* A virtual value path parametrized by the type of value which
* can be read.
*/
@opacapi
@abstract type virtual_val_path('read) = {
read : -> 'read;
}
/**
* {1 Interface}
*/
DbVirtual = {{
/**
* Read value on the given path.
*/
read(path : virtual_ref_path) = path.read()
/**
* Write the value to the virtual reference path.
*/
write(path : virtual_ref_path, d) = path.write(d)
/**
* [DbVirtual.make_ref(ref, v_read, v_write)] Make a virtual reference path from
* a [ref] reference path, a function that handle read access and
* one for write access
*/
make_ref(ref_path : ref_path('a),
v_read : 'a -> 'read, v_write : 'write -> 'a) = {
read = (-> v_read(Db.read(ref_path)))
write = (w -> Db.write(ref_path, v_write(w)))
} : virtual_ref_path('read, 'write)
/**
* [DbVirtual.make_ref(val, v_read)] Make a virtual value path from
* a [val] value path and a function that handle read access.
*/
make_val(val_path : val_path('a),
v_read : 'a -> 'read) = {
read = (-> v_read(Db.read(val_path)))
} : virtual_val_path('read)
}}
////////////////////////////
// Needed by the compiler //
////////////////////////////
@opacapi DbVirtual_make_val = DbVirtual.make_val
@opacapi DbVirtual_make_ref = DbVirtual.make_ref
@opacapi DbVirtual_hack_coerce_default(_handler : {read : 'd -> 'vr; write : 'wr -> 'd}, x : 'vr) = x
@opacapi DbVirtual_hack_coerce_option(_handler : {read : 'd -> 'vr; write : 'wr -> 'd}, x : option('vr)) = x
@opacapi DbVirtual_hack_coerce_vvpath(_handler : {read : 'd -> 'vr; write : 'wr -> 'd}, x : virtual_val_path('vr)) = x
@opacapi DbVirtual_hack_coerce_vrpath(_handler : {read : 'd -> 'vr; write : 'wr -> 'd}, x : virtual_ref_path('vr, 'wr)) = x
////////////////////////////
// Needed by the compiler //
////////////////////////////