00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 static const char *rcsid = "$Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/src/GATTime.c,v 1.20 2004/04/08 09:43:18 kdavis Exp $";
00020
00021
00022 #include <string.h>
00023 #include <stdlib.h>
00024 #include <time.h>
00025
00026
00027
00028 #include "GATType.h"
00029 #include "GATTime.h"
00030 #include "GATErrors.h"
00031 #include "GATInternal.h"
00032 #include "GATXdsWrapper.h"
00033
00034
00035
00036
00037
00038
00039 GATOBJECT_DEFINE_VTABLE(GATTime);
00040 GATSERIALISABLE_DEFINE_VTABLE(GATTime);
00041
00042
00043 GATOBJECT_DEFINE_CONVERTERS(GATTime);
00044
00045 struct GATTime_S
00046 {
00047 GATTime_vtable *GATObject__vtable;
00048 GATTime_ISerialisable_vtable *GATSerialisable__vtable;
00049
00050 GATBool isdirty;
00051 GATdouble64 internalTime;
00052 };
00053
00054
00055 static GATResult
00056 GATTime_DeSerialise_Create(GATContext context, GATObject stream,
00057 GATdouble64 newtime, GATTime *new_object);
00058
00059
00060 static GATTime_vtable GATTime__vtable = {
00061 GATTime_GetType,
00062 GATTime_Destroy,
00063 GATTime_Equals,
00064 GATTime_Clone,
00065 GATTime_GetInterface,
00066 NULL
00067 };
00068
00069 static GATTime_ISerialisable_vtable
00070 GATTime_ISerialisable__vtable =
00071 {
00072 GATTime_Serialise,
00073 GATTime_DeSerialise,
00074 GATTime_GetIsDirty,
00075 };
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 GATResult GATTime_Register_GATSerialisable(void)
00086 {
00087 return GATObject_Register_GATSerialisable(GATType_GATTime,
00088 &GATTime_ISerialisable__vtable);
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 GATTime GATTime_Create(GATdouble64 intime)
00104 {
00105 GATTime newobject = (GATTime) malloc(sizeof(struct GATTime_S));
00106 if(NULL != newobject)
00107 {
00108 memset(newobject, 0, sizeof(sizeof(struct GATTime_S)));
00109 newobject->GATObject__vtable = &GATTime__vtable;
00110 newobject->GATSerialisable__vtable = &GATTime_ISerialisable__vtable;
00111 newobject->isdirty = GATFalse;
00112
00113 if (0 == intime)
00114 {
00115 intime = time(0);
00116 }
00117 newobject->internalTime = intime;
00118 }
00119
00120 return newobject;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129 void GATTime_Destroy(GATTime *object)
00130 {
00131 if( (NULL != object) && (NULL != *object) )
00132 {
00133 free(*object);
00134 *object = NULL;
00135 }
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 GATResult GATTime_Clone(GATTime_const object, GATTime *timeClone)
00148 {
00149 GATResult retval = GAT_INVALID_PARAMETER;
00150
00151 if( (NULL != object) && (NULL != timeClone) )
00152 {
00153 GATTime newGATTime = (GATTime) malloc( sizeof(struct GATTime_S) );
00154 (*timeClone) = NULL;
00155
00156 if(NULL != newGATTime)
00157 {
00158 memset(newGATTime, 0, sizeof(sizeof(struct GATTime_S)));
00159 newGATTime->GATObject__vtable = object->GATObject__vtable;
00160 newGATTime->GATSerialisable__vtable = &GATTime_ISerialisable__vtable;
00161
00162 newGATTime->isdirty = object->isdirty;
00163 newGATTime->internalTime = object->internalTime;
00164
00165 (*timeClone) = newGATTime;
00166
00167 retval = GAT_SUCCESS;
00168 }
00169 else
00170 {
00171 retval = GAT_MEMORYFAILURE;
00172 }
00173 }
00174
00175 return retval;
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185 GATType GATTime_GetType(GATTime_const this)
00186 {
00187 GAT_UNUSED_PARAMETER(this);
00188 return GATType_GATTime;
00189 }
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 GATResult
00204 GATTime_GetInterface(GATTime_const object, GATInterface iftype,
00205 void const **ifp)
00206 {
00207 GATResult retval = GAT_INVALID_PARAMETER;
00208
00209 if (NULL != ifp)
00210 {
00211 *ifp = NULL;
00212 if (GATInterface_ISerialisable == iftype)
00213 {
00214 *ifp = (void const *) &object->GATSerialisable__vtable;
00215 retval = GAT_SUCCESS;
00216 }
00217 else
00218 {
00219 retval = GAT_NO_INTERFACE;
00220 }
00221 }
00222 return retval;
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 GATResult
00242 GATTime_Serialise(GATTime object, GATObject stream, GATBool clear_dirty)
00243 {
00244 GATResult retval = GAT_INVALID_HANDLE;
00245 if (NULL != object)
00246 {
00247 retval = GATXds_SerialiseObject(GATTime_ToGATObject(object), stream,
00248 clear_dirty, 0, "uint32 double_fmt", GATTIME_VERSION1,
00249 "%15.6lf", object->internalTime);
00250
00251 if (GAT_SUCCEEDED(retval) && clear_dirty)
00252 {
00253 object->isdirty = GATFalse;
00254 }
00255 }
00256 return retval;
00257 }
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 static GATBool
00274 GATTime_VersionCallback(GATuint32 version)
00275 {
00276 GATBool retval = GATFalse;
00277 if ((version & ~GATTIME_MINOR_MASK) <= GATTIME_LASTVERSION)
00278 {
00279 retval = GATTrue;
00280 }
00281 return retval;
00282 }
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 static GATResult
00308 GATTime_DeSerialiseCallback(GATContext context, GATObject stream,
00309 GATObject *new_object, GATuint32 version, va_list args)
00310 {
00311
00312 GATdouble64 *new_time = va_arg (args, GATdouble64 *);
00313 GATTime object = NULL;
00314
00315
00316 GATResult retval = GATTime_DeSerialise_Create(context, stream,
00317 *new_time, &object);
00318
00319 GAT_UNUSED_PARAMETER(version);
00320
00321 if (GAT_SUCCESS == retval)
00322 {
00323 if (NULL != object)
00324 {
00325 *new_object = GATTime_ToGATObject(object);
00326 }
00327 else
00328 {
00329 GATTime_Destroy(&object);
00330 retval = GAT_INVALID_PARAMETER;
00331 }
00332 }
00333 return retval;
00334 }
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 GATTime
00351 GATTime_DeSerialise(GATContext context, GATObject stream, GATResult *result)
00352 {
00353 GAT_USES_STATUS(context, "GATTime_DeSerialise");
00354
00355 GATObject object = NULL;
00356
00357
00358
00359 GATuint32 version = 0;
00360 GATdouble64 new_time = 0;
00361
00362
00363 GAT_CREATE_STATUS(GATXds_DeSerialiseObject(context, stream,
00364 GATTime_DeSerialiseCallback, GATTime_VersionCallback, &object,
00365 "uint32 double", &version, &new_time));
00366
00367 if (NULL != result)
00368 {
00369 *result = GAT_RETURN_STATUS();
00370 }
00371 else
00372 {
00373 GAT_STORE_STATUS();
00374 }
00375 return GATObject_ToGATTime(object);
00376 }
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388 GATResult GATTime_GetIsDirty(GATTime_const object, GATBool *isdirty)
00389 {
00390 GATResult retval = GAT_INVALID_HANDLE;
00391 if (NULL != object)
00392 {
00393 if (NULL != isdirty)
00394 {
00395 *isdirty = object->isdirty;
00396 retval = GAT_SUCCESS;
00397 }
00398 else
00399 {
00400 retval = GAT_INVALID_PARAMETER;
00401 }
00402 }
00403 return retval;
00404 }
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417 GATdouble64 GATTime_GetTime(GATTime_const this)
00418 {
00419 return this->internalTime;
00420 }
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435 GATResult
00436 GATTime_Equals(GATTime_const this, GATTime_const that, GATBool *isequal)
00437 {
00438 GATResult retval = GAT_INVALID_PARAMETER;
00439
00440 if( (NULL != this) && (NULL != that) && (NULL != isequal) )
00441 {
00442 (*isequal) = GATFalse;
00443 if(0 == this->internalTime - that->internalTime)
00444 {
00445 (*isequal) = GATTrue;
00446 }
00447 retval = GAT_SUCCESS;
00448 }
00449 return retval;
00450 }
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468 static GATResult
00469 GATTime_DeSerialise_Create(GATContext context, GATObject stream,
00470 GATdouble64 newtime, GATTime *object)
00471 {
00472 GAT_USES_STATUS(context, "GATTime_DeSerialise_Create");
00473 GATTime new_object = (GATTime) malloc(sizeof(struct GATTime_S));
00474
00475 GAT_UNUSED_PARAMETER(stream);
00476
00477 if(NULL != new_object)
00478 {
00479 memset(new_object, 0, sizeof(struct GATTime_S));
00480 new_object->GATObject__vtable = &GATTime__vtable;
00481 new_object->GATSerialisable__vtable = &GATTime_ISerialisable__vtable;
00482
00483 new_object->isdirty = GATFalse;
00484 new_object->internalTime = newtime;
00485 }
00486 else
00487 {
00488 GAT_CREATE_STATUS(GAT_MEMORYFAILURE);
00489 }
00490
00491 if (GAT_SUCCEEDED(GAT_CURRENT_STATUS()))
00492 {
00493 if (NULL != new_object)
00494 {
00495 *object = new_object;
00496 }
00497 else
00498 {
00499 GATTime_Destroy(&new_object);
00500 GAT_CREATE_STATUS(GAT_INVALID_PARAMETER);
00501 }
00502 }
00503 else
00504 {
00505 GATTime_Destroy(&new_object);
00506 }
00507 return GAT_RETURN_STATUS();
00508 }
00509