GridLab
Grid Application Toolkit

A simple API for Grid Applications
GAT

Menu



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

GATTimePeriod.c

Go to the documentation of this file.
00001 /** @file GATTimePeriod.c
00002  * Main file for the GATTimePeriod class.
00003  * 
00004  * An instance of this class represents a time duration, a length
00005  * of time with uncertain start point. 
00006  * 
00007  * @date $Date: 2004/04/02 12:31:58 $
00008  * 
00009  * @version $Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/src/GATTimePeriod.c,v 1.19 2004/04/02 12:31:58 hartmutkaiser Exp $
00010  *
00011  *  Copyright (C) Kelly Davis
00012  *  This file is part of the GAT Engine.
00013  *  Contributed by Kelly Davis <kdavis@aei.mpg.de>.
00014  *
00015  *  Use, modification and distribution is subject to the Gridlab Software
00016  *  License. (See accompanying file GLlicense.txt or copy at
00017  *  http://www.gridlab.org/GLlicense.txt)
00018  */
00019  
00020 static const char *rcsid = "$Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/src/GATTimePeriod.c,v 1.19 2004/04/02 12:31:58 hartmutkaiser Exp $";
00021 
00022 /* System Header Files */
00023 #include <string.h>
00024 #include <stdlib.h>
00025 
00026 /* GAT Header Files */
00027 #include "GATErrors.h"
00028 #include "GATInternal.h"
00029 #include "GATTime.h"
00030 #include "GATTimePeriod.h"
00031 #include "GATXdsWrapper.h"
00032 
00033 /* Macros */
00034 
00035 /* Structures, unions and enums */
00036 
00037 /* define the vtable types */
00038 GATOBJECT_DEFINE_VTABLE(GATTimePeriod);
00039 GATSERIALISABLE_DEFINE_VTABLE(GATTimePeriod);
00040 
00041 /* Declare the converters to/from GATObject */
00042 GATOBJECT_DEFINE_CONVERTERS(GATTimePeriod);
00043 
00044 struct GATTimePeriod_S
00045 {  
00046   GATTimePeriod_vtable *GATObject__vtable;
00047   GATTimePeriod_ISerialisable_vtable *GATSerialisable__vtable;
00048 
00049   GATdouble64 internalDuration;
00050 };
00051 
00052 /* Static function prototypes */
00053 static GATResult
00054 GATTimePeriod_DeSerialise_Create(GATContext context, GATObject stream, 
00055   GATdouble64 duration, GATTimePeriod *new_object);
00056 
00057 /* File scope variables */
00058 static GATTimePeriod_vtable GATTimePeriod__vtable = {
00059   GATTimePeriod_GetType,
00060   GATTimePeriod_Destroy,
00061   GATTimePeriod_Equals,
00062   GATTimePeriod_Clone,
00063   GATTimePeriod_GetInterface,
00064   NULL
00065 };
00066 
00067 static GATTimePeriod_ISerialisable_vtable 
00068   GATTimePeriod_ISerialisable__vtable = 
00069 {
00070   GATTimePeriod_Serialise,
00071   GATTimePeriod_DeSerialise,
00072   GATTimePeriod_GetIsDirty,
00073 };
00074 
00075 /* External functions */
00076 
00077 /** GATTimePeriod_Register_GATSerialisable
00078  *  The GATTimePeriod_Register_GATSerialisable function registers the 
00079  *  serialization vtable with the GAT engine to allow generic object creation.
00080  *  This function is called by the GAT engine, there is no need to use it 
00081  *  directly.
00082  */
00083 GATResult GATTimePeriod_Register_GATSerialisable(void)
00084 {
00085   return GATObject_Register_GATSerialisable(GATType_GATTimePeriod, 
00086     &GATTimePeriod_ISerialisable__vtable);
00087 }
00088 
00089 /**
00090  *
00091  * This operation constructs a GATTimePeriod instance
00092  * corresponding to the passed duration.
00093  *
00094  * @param duration Number of seconds this period of 
00095  * time lasts.
00096  * @return A GATTimePeriod instance
00097  */
00098 GATTimePeriod GATTimePeriod_Create(GATdouble64 duration)
00099 {
00100   GATTimePeriod newobject = 
00101     (GATTimePeriod) malloc(sizeof(struct GATTimePeriod_S));
00102   if(NULL != newobject)
00103   {
00104     newobject->GATObject__vtable = &GATTimePeriod__vtable;
00105     newobject->GATSerialisable__vtable = &GATTimePeriod_ISerialisable__vtable;
00106     
00107     newobject->internalDuration = duration;
00108   }
00109   return newobject;
00110 }
00111 
00112 /** GATTimePeriod_Create
00113  *  @brief Create a new GATTimePeriod object
00114  *
00115  *  The function @c GATTimePeriod_Create constructs a GATTimePeriod instance
00116  *  corresponding to the passed duration.
00117  *
00118  *  @param start The start time to calculate the time period.
00119  *  @param end The end time to calculate the time period.
00120  *
00121  *  @return Returns a handle to the newly created GATTimePeriod object.
00122  *        Returns 0 (zero) if an error occurs.
00123  */
00124 GATTimePeriod GATTimePeriod_Create_Difference(GATTime start, GATTime end)
00125 {
00126   GATTimePeriod newobject = NULL;
00127   if (NULL != start && NULL != end)
00128   {
00129     newobject = (GATTimePeriod) malloc(sizeof(struct GATTimePeriod_S));
00130     if(NULL != newobject)
00131     {
00132       memset(newobject, 0, sizeof(struct GATTimePeriod_S));
00133       newobject->GATObject__vtable = &GATTimePeriod__vtable;
00134       newobject->GATSerialisable__vtable = &GATTimePeriod_ISerialisable__vtable;
00135       
00136       newobject->internalDuration = GATTime_GetTime(end) - GATTime_GetTime(start);
00137     }
00138   }
00139   return newobject;
00140 }
00141 
00142 /**
00143  *
00144  * Destroys this GATTimePeriod.
00145  *
00146  * @param this The GATTimePeriod to destroy
00147  */
00148 void GATTimePeriod_Destroy(GATTimePeriod *object)
00149 {
00150   if( (NULL != object) && (NULL != *object) )
00151   {
00152     free(*object);
00153     *object = NULL;
00154   }
00155 }
00156 
00157 /**
00158  * This GATTimePeriod, is deemed equal if it has a 
00159  * numerically equivalent time duration to the passed
00160  * GATTimePeriod instance.
00161  *
00162  * @param this The first GATTimePeriod to query
00163  * @param that The second GATTimePeriod to query
00164  * @return 1 on equality 0 otherwise
00165  */
00166 GATResult 
00167 GATTimePeriod_Equals(GATTimePeriod_const lhs, GATTimePeriod_const that,
00168   GATBool *isequal)
00169 {
00170   GATResult retval = GAT_INVALID_HANDLE;
00171   if( (NULL != lhs) && (NULL != that) )
00172   {
00173     if (NULL != isequal)
00174     {
00175       (*isequal) = (lhs->internalDuration == that->internalDuration) ? 
00176         GATTrue : GATFalse;
00177       retval = GAT_SUCCESS;
00178     }
00179     else
00180     {
00181       retval = GAT_INVALID_PARAMETER;
00182     }
00183   }
00184   return retval;
00185 }
00186 
00187 /**
00188  * This fucntion returns the type, a #GATType of the passed #GATTimePeriod_const.
00189  * This function will always return #GATType_GATTimePeriod.
00190  *
00191  * @param this The #GATTimePeriod_const to query
00192  * @return The #GATType of the passed @c this
00193  */
00194 GATType GATTimePeriod_GetType(GATTimePeriod_const this)
00195 {
00196   GAT_UNUSED_PARAMETER(this);
00197   return GATType_GATTimePeriod;
00198 }
00199 
00200 /**
00201  * This function returns a "deep clone" of the passed #GATTimePeriod_const @c this. 
00202  * This "deep clone" is returned in the variable @c thisClone. Furthermore,
00203  * this function returns its completion status, through its return value.
00204  *
00205  * @param  this The #GATTimePeriod_const to clone
00206  * @param thisClone The cloned #GATTimePeriod_const
00207  * @return The completion status of this function
00208  */
00209 GATResult 
00210 GATTimePeriod_Clone(GATTimePeriod_const object, GATTimePeriod *thisClone)
00211 {
00212   GATResult retval = GAT_INVALID_HANDLE;
00213   if (NULL != object)
00214   {
00215     GATTimePeriod newGATTimePeriod = 
00216      (GATTimePeriod) malloc( sizeof(struct GATTimePeriod_S) );
00217     
00218     if(NULL != newGATTimePeriod)
00219     {
00220       memset(newGATTimePeriod, 0, sizeof(struct GATTimePeriod_S));
00221       newGATTimePeriod->GATObject__vtable = object->GATObject__vtable;
00222       newGATTimePeriod->GATSerialisable__vtable = &GATTimePeriod_ISerialisable__vtable;
00223       newGATTimePeriod->internalDuration = object->internalDuration;
00224       
00225       if (NULL != thisClone)
00226       {
00227         (*thisClone) = newGATTimePeriod;
00228         retval = GAT_SUCCESS;
00229       }
00230       else
00231       {
00232         GATTimePeriod_Destroy(&newGATTimePeriod);
00233         retval = GAT_INVALID_PARAMETER;
00234       }
00235     }
00236     else
00237     {
00238       retval = GAT_MEMORYFAILURE;
00239     }
00240   }
00241   return retval;
00242 }
00243 
00244 /** GATResult GATTimePeriod_GetInterface(GATTimePeriod_const file, GATInterface iftype, void const **ifp)
00245  *  @brief Get an interface supported by a GATObject
00246  *
00247  *  The function GATTimePeriod_GetInterface allows to get a pointer to an 
00248  *  additional interface supported by this GATTimePeriod.
00249  *
00250  *  @param object The object to be asked for the new interface.
00251  *  @param iftype The interface the object is to be asked for.
00252  *  @param ifp The pointer, through which the result is to be returned.
00253  *
00254  *  @return An error type.
00255  */
00256 GATResult 
00257 GATTimePeriod_GetInterface(GATTimePeriod_const object, GATInterface iftype, 
00258   void const **ifp)
00259 {
00260   GATResult retval = GAT_INVALID_PARAMETER;
00261 
00262   if (NULL != ifp)
00263   {
00264     *ifp = NULL;
00265     if (GATInterface_ISerialisable == iftype)
00266     {
00267       *ifp = (void const *) &object->GATSerialisable__vtable;
00268       retval = GAT_SUCCESS;
00269     }
00270     else
00271     {
00272       retval = GAT_NO_INTERFACE;
00273     }
00274   }
00275   return retval;
00276 }
00277 
00278 
00279 /* GATAdvertiseable API */
00280 
00281 /** GATResult GATTimePeriod_Serialise(GATTimePeriod object, GATObject stream, GATBool clear_dirty)
00282  *  @brief Serialise a GATTimePeriod object
00283  *
00284  *  The function GATTimePeriod_Serialise serialises the given GATTimePeriod 
00285  *  object into the given stream. 
00286  *
00287  *  @param object The GATTimePeriod object to serialise.
00288  *  @param stream The stream interface to use for the serialisation.
00289  *  @param clear_dirty If the clear_dirty parameter is set to GATTrue, the 
00290  *        internal dirty flag of this object is to be reset (no used here)
00291  *
00292  *  @return An error code.
00293  */
00294 GATResult 
00295 GATTimePeriod_Serialise(GATTimePeriod object, GATObject stream, 
00296   GATBool clear_dirty)
00297 {
00298   GATResult retval = GAT_INVALID_HANDLE;
00299   if (NULL != object)
00300   {
00301     retval = GATXds_SerialiseObject(GATTimePeriod_ToGATObject(object), stream, 
00302       clear_dirty, 0, "uint32 double_fmt",
00303       GATTIMEPERIOD_VERSION1, "%15.6lf", object->internalDuration);
00304   }
00305   return retval;
00306 }
00307 
00308 /** GATTimePeriod_VersionCallback
00309  *
00310  *  The function GATTimePeriod_VersionCallback is used as a callback function
00311  *  during the de-serialisation of a GATTimePeriod. It should be provided to test,
00312  *  whether the de-serialised version matches the expected version.
00313  *
00314  *  @param version The version number, which was de-serialised from the stream.
00315  *
00316  *  @return This function should return GATTrue, if the version matches the 
00317  *        expected value (version is valid), GATFalse otherwise.
00318  *
00319  *  @remark This function is called from the GAT engine, there is no need to 
00320  *        call it directly.
00321  */
00322 static GATBool
00323 GATTimePeriod_VersionCallback(GATuint32 version)
00324 {
00325   GATBool retval = GATFalse;
00326   if ((version & ~GATTIMEPERIOD_MINOR_MASK) <= GATTIMEPERIOD_LASTVERSION)
00327   {
00328     retval = GATTrue;
00329   }
00330   return retval;
00331 }
00332 
00333 /** GATTimePeriod_DeSerialiseCallback
00334  *
00335  *  The function GATTimePeriod_DeSerialiseCallback is used as a callback function
00336  *  during the de-serialisation of a GATTimePeriod. It should be provided for the
00337  *  instantiation of the new GATTimePeriod object based on the already de-serialised 
00338  *  data items and the de-serialisation of the associated CPI provider data for 
00339  *  the given object.
00340  *
00341  *  @param context The GAT context to be used for object construction.
00342  *  @param stream The stream interface to use for the serialisation.
00343  *  @param object The pointer to the variable, which should receive the newly 
00344  *        constructed object.
00345  *  @param version The version of the saved data read from the input stream.
00346  *  @param args This parameter is the pointer to the va_list containing 
00347  *        pointers to the already de-serialised data items accordingly to the
00348  *        format string, provided during the call to the 
00349  *        GATTimePeriod_DeSerialise function.
00350  *
00351  *  @return An error code.
00352  *
00353  *  @remark This function is called from the GAT engine, there is no need to 
00354  *        call it directly.
00355  */
00356 static GATResult 
00357 GATTimePeriod_DeSerialiseCallback(GATContext context, GATObject stream, 
00358   GATObject *new_object, GATuint32 version, va_list args)
00359 {
00360   /* the version was eaten already */
00361   GATdouble64 *duration = va_arg(args, GATdouble64 *);
00362   GATTimePeriod object = NULL;
00363   
00364   /* construct the new object */
00365   GATResult retval = GATTimePeriod_DeSerialise_Create(context, stream, 
00366     *duration, &object);
00367 
00368   GAT_UNUSED_PARAMETER(version);
00369 
00370   if (GAT_SUCCESS == retval)
00371   {
00372     if (NULL != object)
00373     {
00374       *new_object = GATTimePeriod_ToGATObject(object);
00375     }
00376     else
00377     {
00378       GATTimePeriod_Destroy(&object);
00379       retval = GAT_INVALID_PARAMETER;
00380     }
00381   }
00382   return retval;
00383 }
00384 
00385 /** GATTimePeriod GATTimePeriod_DeSerialise(GATContext context, GATObject stream, GATBool clear_dirty)
00386  *  @brief De-serialise a GATTimePeriod object
00387  *
00388  *  The function GATTimePeriod_DeSerialise de-serialises a streamed  
00389  *  GATTimePeriod object from the given stream  It constructs a new instance of 
00390  *  the de-serialised object.
00391  *
00392  *  @param context The GAT context to be used for object construction.
00393  *  @param stream The stream interface to use for the serialisation.
00394  *  @param result The pointer to a variable, which receives the status code of
00395  *        the operation.
00396  *
00397  *  @return The newly constructed GATTimePeriod object.
00398  */
00399 GATTimePeriod 
00400 GATTimePeriod_DeSerialise(GATContext context, GATObject stream, 
00401   GATResult *result)
00402 {
00403   GAT_USES_STATUS(context, "GATTimePeriod_DeSerialise");
00404   GATObject object = NULL;     /* the new object will be created here */
00405   
00406   /* we must provide all instance data items to be de-serialised for this
00407      GATTimePeriod object accordingly to the provided format string */
00408   GATuint32 version = 0;
00409   GATdouble64 duration = 0;
00410 
00411   /* read the data */
00412   GAT_CREATE_STATUS(GATXds_DeSerialiseObject(context, stream, 
00413     GATTimePeriod_DeSerialiseCallback, GATTimePeriod_VersionCallback, &object, 
00414     "uint32 double", &version, &duration));
00415 
00416   if (NULL != result)
00417   { 
00418     *result = GAT_CURRENT_STATUS();
00419   }
00420   else
00421   {
00422     GAT_STORE_STATUS();
00423   }
00424   return GATObject_ToGATTimePeriod(object);
00425 }
00426 
00427 /** GATTimePeriod_GetIsDirty
00428  *  
00429  *  The function GATTimePeriod_GetIsDirty retrieves the status of the dirty flag of 
00430  *  this GATTimePeriod object.
00431  *
00432  *  @param file The GATTimePeriod object to inspect for its dirty status.
00433  *  @param isdirty The pointer to a variable, which receives the dirty status.
00434  *
00435  *  @return An error code.
00436  */
00437 GATResult GATTimePeriod_GetIsDirty(GATTimePeriod_const object, GATBool *isdirty)
00438 {
00439   GATResult retval = GAT_INVALID_HANDLE;
00440   if (NULL != object)
00441   {
00442     if (NULL != isdirty)
00443     {
00444       *isdirty = GATFalse;    /* GATTimePeriod is never dirty */
00445       retval = GAT_SUCCESS;
00446     }
00447     else
00448     {
00449       retval = GAT_INVALID_PARAMETER;
00450     }
00451   }  
00452   return retval;
00453 }
00454 
00455 
00456 /* GATTimePeriod API functions */
00457 
00458 /** GATTimePeriod_GetDuration
00459  *  This operation returns the number of seconds this time
00460  *  period lasts.
00461  *
00462  *  @param this The GATTimePeriod to query
00463  *
00464  *  @return Number of seconds this time period lasts
00465  */
00466 GATdouble64 GATTimePeriod_GetDuration(GATTimePeriod_const object)
00467 {
00468   GATdouble64 retval = 0.;
00469   if (NULL != object)
00470   {
00471     retval = object->internalDuration;
00472   }
00473   return retval;
00474 }
00475 
00476 
00477 /* Local functions */
00478 
00479 /** GATTimePeriod_DeSerialise_Create
00480  *
00481  *  The function GATTimePeriod_DeSerialise_Create creates a new GATTimePeriod object.
00482  *
00483  *  @param context The GAT context to use for creation of the GATTimePeriod object.
00484  *  @param stream The object from which the adaptor should read the streamed
00485  *        instance data.
00486  *  @param duration The time duration to be used for the construction of the 
00487  *        new GATTimePeriod object
00488  *  @param new_object The pointer to the variable, which should receive the 
00489  *        newly constructed GATTimePeriod object.
00490  *
00491  *  @return An error code.
00492  */
00493 static GATResult 
00494 GATTimePeriod_DeSerialise_Create(GATContext context, GATObject stream, 
00495   GATdouble64 duration, GATTimePeriod *object)
00496 {
00497   GAT_USES_STATUS(context, "GATTimePeriod_DeSerialise_Create");
00498   GATTimePeriod new_object = 
00499     (GATTimePeriod) malloc(sizeof(struct GATTimePeriod_S));
00500 
00501   GAT_UNUSED_PARAMETER(stream);
00502   
00503   if(NULL != new_object)
00504   {
00505     memset(new_object, 0, sizeof(struct GATTimePeriod_S));
00506     new_object->GATObject__vtable = &GATTimePeriod__vtable;
00507     new_object->GATSerialisable__vtable = &GATTimePeriod_ISerialisable__vtable;
00508     
00509     new_object->internalDuration = duration;
00510   }
00511   else
00512   {
00513     GAT_CREATE_STATUS(GAT_MEMORYFAILURE);
00514   }
00515   
00516   if (GAT_SUCCEEDED(GAT_CURRENT_STATUS()))
00517   {
00518     if (NULL != new_object)
00519     {
00520       *object = new_object;
00521     }
00522     else
00523     {
00524       GATTimePeriod_Destroy(&new_object);
00525       GAT_CREATE_STATUS(GAT_INVALID_PARAMETER);
00526     }
00527   }
00528   else
00529   {
00530     GATTimePeriod_Destroy(&new_object);
00531   }
00532   return GAT_RETURN_STATUS();
00533 }
00534 
00535 
00536