1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Key/value storage services
19 # The main services is `App::data_store`, a `DataStore` holding any
20 # serializable Nit object.
27 import linux
::data_store
is conditional
(linux
)
28 import android
::data_store
is conditional
(android
)
29 import ios
::data_store
is conditional
(ios
)
33 # Services to store and load data
37 # import app::data_store
42 # var state = "Simple string or any serializable class"
44 # redef fun on_save_state do app.data_store["state"] = state
46 # redef fun on_restore_state
48 # var state = app.data_store["state"]
49 # if state isa String then self.state = state
53 var data_store
= new DataStore is lazy
56 # Simple data storage facility
58 # Write values with `[]=` and read with `[]`.
60 # import linux::data_store # Needed for testing only
69 # var data_store = new DataStore
70 # data_store["one"] = 1
71 # data_store["str"] = "Some string"
72 # data_store["a"] = new A
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
81 # Set to `null` to clear a value.
83 # data_store["one"] = null
84 # assert data_store["one"] == null
88 # Get the object stored at `key`, or null if nothing is available
89 fun [](key
: String): nullable Object is abstract
91 # Store `value` at `key`
92 fun []=(key
: String, value
: nullable Serializable) is abstract