Classes | Public Member Functions

TRTK::Timestamp Class Reference

A class for managing timestamps of various objects by using names. Dependencies between objects can be specified and checked for being up-to-date. More...

#include <Timestamp.hpp>

List of all members.

Classes

class  ObjectInfo
 This class is a helper class for the Timestamp class and holds various information of an object like the timestamp, its dependencies etc. More...

Public Member Functions

 Timestamp ()
virtual ~Timestamp ()
ObjectInfooperator[] (const std::string &object)

Detailed Description

A class for managing timestamps of various objects by using names. Dependencies between objects can be specified and checked for being up-to-date.

This class provides means to assign timestamps to arbitrary objects such as instances of classes, functions etc. Dependencies between objects can also be specified. The purpose is to check whether one or more dependencies were already set up or are up-to-date and to skip them where appropriate. The aim is to avoid potentially expensive recomputations.

Warning:
The timestamp mechanism is though for computationally expensive tasks. If objects do not take up enough time (at least one clock tick, which in general is less than a millisecond) the timestamp between two distinctive objects does not differ (even if they were executed correctly and consecutively).

Here is a more elaborate example showing the programming idiom for the timestamp class (you might interprete the wait function as some time consuming code):

 #include <ctime>
 #include <iostream>

 #include <TRTK/Timestamp.hpp>


 void wait(const double seconds)
 {
     clock_t time = std::clock();

     while (double(std::clock() - time) / CLOCKS_PER_SEC < seconds)
     {
         // do nothing
     }
 }


 class Demo
 {
 public:

     Demo()
     {
         // Initialize the object dependencies.

         timestamp["B"].addDependency("A");

         timestamp["D"].addDependency("B");
         timestamp["D"].addDependency("C");
     }

     void A()
     {
         std::cout << "    A is called." << std::endl;

         wait(0.5);

         timestamp["A"].update();
     }

     void B()
     {
         std::cout << "    B is called." << std::endl;

         if (timestamp["B"].dependenciesHaveChanged())
         {
             if (timestamp["B"].dependencyHasChanged("A")) A();

             wait(0.5);

             timestamp["B"].update();
         }
     }

     void C()
     {
         std::cout << "    C is called." << std::endl;

         wait(0.5);

         timestamp["C"].update();
     }

     void D()
     {
         std::cout << "    D is called." << std::endl;

         if (timestamp["D"].dependenciesHaveChanged())
         {
             if (timestamp["D"].dependencyHasChanged("B")) B();
             if (timestamp["D"].dependencyHasChanged("C")) C();

             wait(0.5);

             timestamp["D"].update();
         }
     }

 private:

     TRTK::Timestamp timestamp;
 };


 int main()
 {
     Demo demo;

     std::cout << "Call D:" << std::endl;
     demo.D();

     std::cout << "Call D:" << std::endl;
     demo.D();

     std::cout << "Call A:" << std::endl;
     demo.A();

     std::cout << "Call D:" << std::endl;
     demo.D();

     std::cout << "Call D:" << std::endl;
     demo.D();

     return 0;
 }

The output is

 Call D:
     D is called.
     B is called.
     A is called.
     C is called.
 Call D:
     D is called.
 Call A:
     A is called.
 Call D:
     D is called.
     B is called.
     A is called.
 Call D:
     D is called.
See also:
ObjectInfo
Author:
Christoph Haenisch
Version:
0.3.0
Date:
last changed on 2014-07-01

Definition at line 194 of file Timestamp.hpp.


Constructor & Destructor Documentation

TRTK::Timestamp::Timestamp (  )

Constructs an Timestamp object.

Definition at line 43 of file Timestamp.cpp.

TRTK::Timestamp::~Timestamp (  ) [virtual]

Destroys an Timestamp object.

Definition at line 50 of file Timestamp.cpp.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines