]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/scl/Serialization.scl
5ba1e0d94aab8a3a700e27bfc66a8957cbcb70bc
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / Serialization.scl
1 import "Prelude" hiding (readInteger, readLong, readFloat, readDouble, read)
2 import "Stream"
3 import "Vector"
4 import "ArrayList" as ArrayList
5
6 class IO a where
7     write :: OutputStream -> a -> <Proc> ()
8     read  :: InputStream -> <Proc> a
9     ioSize :: a -> Integer
10     
11     ioSize _ = fail "ioSize not implemented"
12
13 instance IO Boolean where
14     write = writeBoolean
15     read = readBoolean
16     ioSize _ = 1
17     
18 instance IO Byte where
19     write = writeByte
20     read = readByte
21     ioSize _ = 1
22     
23 instance IO Character where
24     write = writeCharacter
25     read = readCharacter
26     ioSize _ = 2
27     
28 instance IO Short where
29     write = writeShort
30     read = readShort
31     ioSize _ = 2
32
33 instance IO Integer where
34     write = writeInteger
35     read = readInteger
36     ioSize _ = 4
37     
38 instance IO Long where
39     write = writeLong
40     read = readLong
41     ioSize _ = 8
42     
43 instance IO Float where
44     write = writeFloat
45     read = readFloat
46     ioSize _ = 4
47     
48 instance IO Double where
49     write = writeDouble
50     read = readDouble
51     ioSize _ = 8
52
53 instance IO String where
54     write = writeString
55     read = readString
56     ioSize = ioSizeString
57     
58 instance IO DoubleArray where
59     write = writeDoubleArray
60     read = readDoubleArray
61     //ioSize v = do
62     //    l = length v
63     //    ioSizeLength l + 8 * l
64     
65 instance (IO a) => IO (Maybe a) where
66     read s = if read s 
67              then Just (read s)
68              else Nothing
69     write s (Just v) = do
70         write s True
71         write s v
72     write s Nothing = do
73         write s False
74     ioSize Nothing = 1
75     ioSize (Just v) = 1 + ioSize v
76
77 instance (IO a, IO b) => IO (Either a b) where
78     read s = if read s 
79              then Right (read s)
80              else Left (read s)
81     write s (Left v) = do
82         write s False
83         write s v
84     write s (Right v) = do
85         write s True
86         write s v    
87     ioSize (Left v) = 1 + ioSize v
88     ioSize (Right v) = 1 + ioSize v
89         
90 instance IO () where
91     read s = ()
92     write s _ = ()
93     ioSize _ = 0
94
95 instance (IO a, IO b) => IO (a, b) where
96     read s = do
97         a = read s
98         b = read s
99         (a, b)
100     write s (a, b) = do
101         write s a
102         write s b
103     ioSize (a, b) = ioSize a + ioSize b
104
105 instance (IO a, IO b, IO c) => IO (a, b, c) where
106     read s = do
107         a = read s
108         b = read s
109         c = read s
110         (a, b, c)
111     write s (a, b, c) = do
112         write s a
113         write s b 
114         write s c
115     ioSize (a, b, c) = ioSize a + ioSize b + ioSize c
116
117 instance (IO a, IO b, IO c, IO d) => IO (a, b, c, d) where
118     read s = do
119         a = read s
120         b = read s
121         c = read s
122         d = read s
123         (a, b, c, d)
124     write s (a, b, c, d) = do
125         write s a
126         write s b 
127         write s c
128         write s d
129     ioSize (a, b, c, d) = ioSize a + ioSize b + ioSize c + ioSize d
130     
131 instance (IO a, IO b, IO c, IO d, IO e) => IO (a, b, c, d, e) where
132     read s = do
133         a = read s
134         b = read s
135         c = read s
136         d = read s
137         e = read s
138         (a, b, c, d, e)
139     write s (a, b, c, d, e) = do
140         write s a
141         write s b 
142         write s c
143         write s d
144         write s e
145     ioSize (a, b, c, d, e) = ioSize a + ioSize b + ioSize c + ioSize d + ioSize e
146
147 instance (IO a) => IO [a] where
148     write s v = do
149         writeLength s (length v)
150         for v (write s)
151     read s = ArrayList.freeze (read s) 
152     ioSize vs = ioSizeLength (length vs) + sum [ioSize v | v <- vs]
153
154 instance (IO a) => IO (ArrayList.T a) where
155     write s v = do
156         writeLength s (ArrayList.length v)
157         ArrayList.for v (write s)    
158     read s = do
159         l = readLength s
160         result = ArrayList.newC l
161         forN l (\_ ->
162             ArrayList.add result (read s)
163         ) 
164         result
165     // TODO
166
167 instance (IO a, VecComp a) => IO (Vector a) where
168     @inline
169     write s v = do
170         l = length v
171         writeLength s l
172         forN l (\i -> write s (v ! i))
173     @inline
174     read s = do
175         l = readLength s
176         result = createMVector l
177         forN l (\i -> setMVector result i (read s))
178         freezeMVector result
179     // TODO
180     
181 readByteArray :: IO a => ByteArray -> a
182 readByteArray ar = runProc do
183     s = openForReading ar
184     v = read s
185     close s
186     v
187
188 writeByteArray :: IO a => a -> ByteArray
189 writeByteArray v = runProc do
190     b = createBuffer ()
191     s = openForWriting b
192     write s v
193     close s
194     toByteArray b
195
196 importJava "org.simantics.scl.runtime.io.SclIO" where 
197     writeBoolean :: OutputStream -> Boolean -> <Proc> ()
198     writeByte :: OutputStream -> Byte -> <Proc> ()
199     writeCharacter :: OutputStream -> Character -> <Proc> ()
200     writeShort :: OutputStream -> Short -> <Proc> ()
201     writeInteger :: OutputStream -> Integer -> <Proc> ()
202     writeLong :: OutputStream -> Long -> <Proc> () 
203     writeFloat :: OutputStream -> Float -> <Proc> ()
204     writeDouble :: OutputStream -> Double -> <Proc> ()
205     writeDoubleArray :: OutputStream -> DoubleArray -> <Proc> ()
206     writeString :: OutputStream -> String -> <Proc> ()
207     writeLength :: OutputStream -> Integer -> <Proc> ()
208     
209     readBoolean :: InputStream -> <Proc> Boolean
210     readByte :: InputStream -> <Proc> Byte
211     readCharacter :: InputStream -> <Proc> Character
212     readShort :: InputStream -> <Proc> Short
213     readInteger :: InputStream -> <Proc> Integer
214     readLong :: InputStream -> <Proc> Long
215     readFloat :: InputStream -> <Proc> Float
216     readDouble :: InputStream -> <Proc> Double
217     readDoubleArray :: InputStream -> <Proc> DoubleArray
218     readString :: InputStream -> <Proc> String
219     readLength :: InputStream -> <Proc> Integer
220     
221     ioSizeString :: String -> Integer
222     ioSizeLength :: Integer -> Integer
223     
224     @JavaName readAllByteArrayAndClose
225     readAllByteArray :: InputStream -> <Proc> ByteArray
226     @JavaName readAllStringAndClose
227     readAllString :: InputStream -> <Proc> String