GridLab
Grid Application Toolkit

A simple API for Grid Applications
GAT

Menu



Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

GATTime.c

Go to the documentation of this file.
00001 /** @file GATTime.c
00002  * Main file for the GATTime class.
00003  * 
00004  * A GATTime represents a point in time.
00005  * 
00006  * @date $Date: 2004/04/08 09:43:18 $
00007  * 
00008  * @version $Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/src/GATTime.c,v 1.20 2004/04/08 09:43:18 kdavis Exp $
00009  *
00010  *  Copyright (C) Kelly Davis
00011  *  This file is part of the GAT Engine.
00012  *  Contributed by Kelly Davis <kdavis@aei.mpg.de>.
00013  *
00014  *  Use, modification and distribution is subject to the Gridlab Software
00015  *  License. (See accompanying file GLlicense.txt or copy at
00016  *  http://www.gridlab.org/GLlicense.txt)
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 /* System Header Files */
00022 #include <string.h>
00023 #include <stdlib.h>
00024 #include <time.h>
00025 
00026 /* GAT Header Files */
00027 
00028 #include "GATType.h"
00029 #include "GATTime.h"
00030 #include "GATErrors.h"
00031 #include "GATInternal.h"
00032 #include "GATXdsWrapper.h"
00033 
00034 /* Macros */
00035 
00036 /* Structures, unions and enums */
00037 
00038 /* define the vtable types */
00039 GATOBJECT_DEFINE_VTABLE(GATTime);
00040 GATSERIALISABLE_DEFINE_VTABLE(GATTime);
00041 
00042 /* Declare the converters to/from GATObject */
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 /* Static function prototypes */
00055 static GATResult
00056 GATTime_DeSerialise_Create(GATContext context, GATObject stream, 
00057   GATdouble64 newtime, GATTime *new_object);
00058 
00059 /* File scope variables */
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 /* External functions */
00078 
00079 /** GATTime_Register_GATSerialisable
00080  *  The GATTime_Register_GATSerialisable function registers the serialization
00081  *  vtable with the GAT engine to allow generic object creation.
00082  *  This function is called by the GAT engine, there is no need to use it 
00083  *  directly.
00084  */
00085 GATResult GATTime_Register_GATSerialisable(void)
00086 {
00087   return GATObject_Register_GATSerialisable(GATType_GATTime, 
00088     &GATTime_ISerialisable__vtable);
00089 }
00090 
00091 /**
00092  * This operation constructs a GATTime 
00093  * instance corresponding to the passed 
00094  * time, the number of seconds elapsed 
00095  * since 00:00 hours, Jan 1, 1970 UTC 
00096  * from the system clock.
00097  *
00098  * @param intime The time in seconds this GATTime instance represents.
00099  *      If the value of this parameter is 0 (zero), then the current time is
00100  *      used.
00101  * @return A GATTime instance
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  * Destroys the GATTime instance
00125  *
00126  * @param this The GATTime to
00127  * destroy.
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  * This function returns a "deep clone" of the passed #GATTime_const @c this. 
00140  * This "deep clone" is returned in the variable @c thisClone. Furthermore,
00141  * this function returns its completion status, through its return value.
00142  *
00143  * @param  this The #GATTime_const to clone
00144  * @param timeClone The cloned #GATTime_const
00145  * @return The completion status of this function
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  * This fucntion returns the type, a #GATType of the passed #GATTime_const.
00180  * This function will always return #GATType_GATTime.
00181  *
00182  * @param this The #GATTime_const to query
00183  * @return The #GATType of the passed @c time
00184  */
00185 GATType GATTime_GetType(GATTime_const this)
00186 {
00187   GAT_UNUSED_PARAMETER(this);
00188   return GATType_GATTime;
00189 }
00190 
00191 /** GATResult GATTime_GetInterface(GATTime_const file, GATInterface iftype, void const **ifp)
00192  *  @brief Get an interface supported by a GATObject
00193  *
00194  *  The function GATTime_GetInterface allows to get a pointer to an 
00195  *  additional interface supported by this GATTime.
00196  *
00197  *  @param object The object to be asked for the new interface.
00198  *  @param iftype The interface the object is to be asked for.
00199  *  @param ifp The pointer, through which the result is to be returned.
00200  *
00201  *  @return An error type.
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 /* GATAdvertiseable API */
00227 
00228 /** GATResult GATTime_Serialise(GATTime object, GATObject stream, GATBool clear_dirty)
00229  *  @brief Serialise a GATTime object
00230  *
00231  *  The function GATTime_Serialise serialises the given GATTime object into the
00232  *  given stream. 
00233  *
00234  *  @param object The GATTime object to serialise.
00235  *  @param stream The stream interface to use for the serialisation.
00236  *  @param clear_dirty If the clear_dirty parameter is set to GATTrue, the 
00237  *        internal dirty flag of this object is to be reset (no used here)
00238  *
00239  *  @return An error code.
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 /** GATTime_VersionCallback
00260  *
00261  *  The function GATTime_VersionCallback is used as a callback function
00262  *  during the de-serialisation of a GATTime. It should be provided to test,
00263  *  whether the de-serialised version matches the expected version.
00264  *
00265  *  @param version The version number, which was de-serialised from the stream.
00266  *
00267  *  @return This function should return GATTrue, if the version matches the 
00268  *        expected value (version is valid), GATFalse otherwise.
00269  *
00270  *  @remark This function is called from the GAT engine, there is no need to 
00271  *        call it directly.
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 /** GATTime_DeSerialiseCallback
00285  *
00286  *  The function GATTime_DeSerialiseCallback is used as a callback function
00287  *  during the de-serialisation of a GATTime. It should be provided for the
00288  *  instantiation of the new GATTime object based on the already de-serialised 
00289  *  data items and the de-serialisation of the associated CPI provider data for 
00290  *  the given object.
00291  *
00292  *  @param context The GAT context to be used for object construction.
00293  *  @param stream The stream interface to use for the serialisation.
00294  *  @param object The pointer to the variable, which should receive the newly 
00295  *        constructed object.
00296  *  @param version The version of the saved data read from the input stream.
00297  *  @param args This parameter is the pointer to the va_list containing 
00298  *        pointers to the already de-serialised data items accordingly to the
00299  *        format string, provided during the call to the 
00300  *        GATTime_DeSerialise function.
00301  *
00302  *  @return An error code.
00303  *
00304  *  @remark This function is called from the GAT engine, there is no need to 
00305  *        call it directly.
00306  */
00307 static GATResult 
00308 GATTime_DeSerialiseCallback(GATContext context, GATObject stream, 
00309   GATObject *new_object, GATuint32 version, va_list args)
00310 {
00311   /* the version was eaten already */
00312   GATdouble64 *new_time = va_arg (args, GATdouble64 *);
00313   GATTime object = NULL;
00314   
00315   /* construct the new object */
00316   GATResult retval = GATTime_DeSerialise_Create(context, stream, 
00317     *new_time, &object);    // expects seconds
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 /** GATTime GATTime_DeSerialise(GATContext context, GATObject stream, GATBool clear_dirty)
00337  *  @brief De-serialise a GATTime object
00338  *
00339  *  The function GATTime_DeSerialise de-serialises a streamed GATTime object  
00340  *  from the given stream  It constructs a new instance of the de-serialised 
00341  *  object.
00342  *
00343  *  @param context The GAT context to be used for object construction.
00344  *  @param stream The stream interface to use for the serialisation.
00345  *  @param result The pointer to a variable, which receives the status code of
00346  *        the operation.
00347  *
00348  *  @return The newly constructed GATTime object.
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;     /* the new object will be created here */
00356   
00357   /* we must provide all instance data items to be de-serialised for this
00358      GATTime object accordingly to the provided format string */
00359   GATuint32 version = 0;
00360   GATdouble64 new_time = 0;  
00361 
00362   /* read the data */
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 /** GATTime_GetIsDirty
00379  *  
00380  *  The function GATTime_GetIsDirty retrieves the status of the dirty flag of 
00381  *  this GATTime object.
00382  *
00383  *  @param file The GATTime object to inspect for its dirty status.
00384  *  @param isdirty The pointer to a variable, which receives the dirty status.
00385  *
00386  *  @return An error code.
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 /* GATTime API functions */
00408 
00409 /**
00410  * This operation returns the time
00411  * this GATTime instance represents
00412  *
00413  * @param this The #GATTime to query
00414  * @return The time this GATTime
00415  * instance represents.
00416  */
00417 GATdouble64 GATTime_GetTime(GATTime_const this)
00418 {
00419   return this->internalTime;
00420 }
00421 
00422 /**
00423  * Tests this GATTime for equality with 
00424  * the passed GATTime instance
00425  *
00426  * If the passed GATTime is deemed equal 
00427  * if it has a numerically equivalent time 
00428  * to the passed GATTime instance.
00429  *
00430  * @param this The first GATTime to compare
00431  * @param that The second GATTime to compare
00432  * @param isequal Result of comparrison
00433  * @return Completion status
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 /* Local functions */
00454 
00455 /** GATTime_DeSerialise_Create
00456  *
00457  *  The function GATTime_DeSerialise_Create creates a new GATTime object.
00458  *
00459  *  @param context The GAT context to use for creation of the GATTime object.
00460  *  @param stream The object from which the adaptor should read the streamed
00461  *        instance data.
00462  *  @param newtime The time to be used to initialise the new object.
00463  *  @param new_object The pointer to the variable, which should receive the 
00464  *        newly constructed GATTime object.
00465  *
00466  *  @return An error code.
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