persist.h00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _GEL_PERSIST_H
00023 #define _GEL_PERSIST_H
00024
00025 #include <sys/param.h>
00026 #include <stddef.h>
00027
00028 #define PERSIST_INVALID_ID (persist_id_t) (0xffff)
00029 #define PERSIST_FREE_SLOT (persist_id_t) (0x7fff)
00030
00031 typedef unsigned short persist_id_t;
00032
00045 class persistent_store
00046 {
00047 protected:
00048
00050
00051
00052
00053
00054
00055 typedef struct
00056 {
00057 persist_id_t id;
00058 unsigned char size;
00059 unsigned char data[0];
00060 } persistent_area_t;
00061
00063
00064
00065 static persistent_area_t *find_store (persist_id_t id);
00066
00068
00069
00070 static persistent_area_t *allocate_store (persist_id_t id, size_t len);
00071
00072
00073 static persistent_area_t start_area asm ("0xb000");
00074
00075
00076 static persistent_area_t end_area asm ("0xb200");
00077
00078
00079 persistent_area_t *area;
00080
00082
00083
00084
00085 void
00086 save (const unsigned char *object);
00087
00089
00090
00091
00092
00093
00094
00095 int
00096 create (persist_id_t id, void *object, size_t len);
00097
00098 inline
00099 persistent_store ()
00100 {}
00101
00102 private:
00103
00104 inline
00105 persistent_store (const persistent_store&)
00106 {
00107 ;
00108 }
00109
00110
00111 inline persistent_store&
00112 operator = (const persistent_store&)
00113 {
00114 return *this;
00115 }
00116
00117 protected:
00119 inline
00120 persistent_store (persist_id_t id, void *object, size_t len)
00121 {
00122 create (id, object, len);
00123 }
00124
00125 public:
00127
00128
00129
00130 void
00131 revoke ();
00132 };
00133
00134
00139 class persistent_object : private persistent_store
00140 {
00141 protected:
00142
00143
00144 static persistent_object *first;
00145
00146
00147 persistent_object *next;
00148
00149
00150 unsigned char *object;
00151
00152 inline
00153 persistent_object ()
00154 {}
00155
00156 private:
00157
00158 inline
00159 persistent_object (const persistent_object&)
00160 {
00161 ;
00162 }
00163
00164
00165 inline persistent_object&
00166 operator = (const persistent_object&)
00167 {
00168 return *this;
00169 }
00170 public:
00171
00173 inline
00174 persistent_object (persist_id_t id, void *object, size_t len)
00175 : persistent_store (id, object, len)
00176 {
00177 next = first;
00178 first = this;
00179 }
00180
00182
00183
00184
00185
00186
00187
00188 static void
00189 save_all ();
00190 };
00191
00192
00193
00195
00196
00197
00198
00199
00200
00201 template <class type>
00202 class persistent : private persistent_store
00203 {
00204 type obj;
00205
00207 inline void
00208 sync ()
00209 {
00210 save ((const unsigned char*) &obj);
00211 }
00212 public:
00214 inline
00215 persistent (persist_id_t id)
00216 : persistent_store (id, &obj, sizeof (obj))
00217 {
00218 }
00219
00221
00222
00223
00224 inline
00225 persistent (persist_id_t id, const type& init)
00226 : persistent_store ()
00227 {
00228 if (create (id, &obj, sizeof (type)))
00229 {
00230 obj = init;
00231 sync ();
00232 }
00233 }
00234
00236
00237
00238
00239 inline
00240 persistent (persist_id_t id, type& init)
00241 : persistent_store ()
00242 {
00243 if (create (id, &obj, sizeof (type)))
00244 {
00245 obj = init;
00246 sync ();
00247 }
00248 }
00249
00251
00252
00253 inline
00254 ~persistent ()
00255 {
00256 ;
00257 }
00258
00260
00261
00262 inline persistent<type>&
00263 operator = (const type& value)
00264 {
00265 obj = value;
00266 sync ();
00267 return *this;
00268 }
00269
00271
00272
00273 inline persistent<type>&
00274 operator = (type& value)
00275 {
00276 obj = value;
00277 sync ();
00278 return *this;
00279 }
00280
00282 inline
00283 operator const type& ()
00284 {
00285 return obj;
00286 }
00287
00288 inline persistent<type>&
00289 operator ++ (int)
00290 {
00291 obj++;
00292 sync ();
00293 return *this;
00294 }
00295
00296 inline persistent<type>&
00297 operator -- (int)
00298 {
00299 obj--;
00300 sync ();
00301 return *this;
00302 }
00303 };
00304
00305
00306 #endif
|