examples: annotate examples
[nit.git] / lib / app / data_store.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Key/value storage services
18 #
19 # The main services is `App::data_store`, a `DataStore` holding any
20 # serializable Nit object.
21 module data_store
22
23 import app_base
24 import serialization
25
26 # Platform variations
27 import linux::data_store is conditional(linux)
28 import android::data_store is conditional(android)
29 import ios::data_store is conditional(ios)
30
31 redef class App
32
33 # Services to store and load data
34 #
35 # ~~~
36 # import app::ui
37 # import app::data_store
38 #
39 # class MyWindow
40 # super Window
41 #
42 # var state = "Simple string or any serializable class"
43 #
44 # redef fun on_save_state do app.data_store["state"] = state
45 #
46 # redef fun on_restore_state
47 # do
48 # var state = app.data_store["state"]
49 # if state isa String then self.state = state
50 # end
51 # end
52 # ~~~
53 var data_store = new DataStore is lazy
54 end
55
56 # Simple data storage facility
57 #
58 # Write values with `[]=` and read with `[]`.
59 # ~~~
60 # import linux::data_store # Needed for testing only
61 #
62 # class A
63 # serialize
64 #
65 # var b = true
66 # var f = 1.234
67 # end
68 #
69 # var data_store = new DataStore
70 # data_store["one"] = 1
71 # data_store["str"] = "Some string"
72 # data_store["a"] = new A
73 #
74 # assert data_store["one"] == 1
75 # assert data_store["str"] == "Some string"
76 # assert data_store["a"].as(A).b
77 # assert data_store["a"].as(A).f == 1.234
78 # assert data_store["other"] == null
79 # ~~~
80 #
81 # Set to `null` to clear a value.
82 # ~~~
83 # data_store["one"] = null
84 # assert data_store["one"] == null
85 # ~~~
86 class DataStore
87
88 # Get the object stored at `key`, or null if nothing is available
89 fun [](key: String): nullable Object is abstract
90
91 # Store `value` at `key`
92 fun []=(key: String, value: nullable Serializable) is abstract
93 end