1 // MAKE THIS A HEADER FILE
 2 
 3 
 4 /*------------------------------------------------------------------------------
 5 Name:      Timestamp.h
 6 Project:   xmlBlaster.org
 7 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
 8 Comment:   Create unique timestamp
 9 Version:   $Id: Timestamp.h 13445 2005-07-15 01:54:35Z ruff $
10 ------------------------------------------------------------------------------*/
11 
12 #ifndef _UTIL_TIMESTAMP_H
13 #define _UTIL_TIMESTAMP_H
14 
15 #include <util/xmlBlasterDef.h>
16 #include <string>
17 #include <time.h>
18 #include <util/thread/ThreadImpl.h>
19 
20 /*
21 #if defined(_WINDOWS)   
22    ostream& operator <<(ostream& target, const __int64& x);
23 #endif
24 */
25 
26 namespace org { namespace xmlBlaster { namespace util {
27 
28 /**
29  * High performing timestamp class, time elapsed since 1970, the nanos are simulated
30  * as a unique counter. 
31  * <br />
32  * The counter is rewound on any second step (this is different from its omonimous java
33  * class since there is no portable way in c++ to get the system time with a millisecond
34  * precision yet. Besides this difference, the c++ timestamp is a singleton working as a
35  * factory of timestamps (which are of the type long long -> typedef int64_t Timestamp;).
36  * <br />
37  * Guarantees that any created Timestamp instance is unique in the current process.
38  * <br /><br />
39  * Fails only if
40  * <ul>
41  *   <li>a CPU can create more than 999999999 Timestamp instances per second</li>
42  *   <li>In ~ 288 years when Long.MAX_VALUE = 9223372036854775807 overflows (current value is 1013338358124000008)</li>
43  * </ul>
44  * @author <a href="mailto:xmlBlaster@marcelruff.info">Marcel Ruff</a>
45  * @author <a href="mailto:laghi@swissinfo.org">Michele Laghi</a>
46  */
47 
48    class Dll_Export TimestampFactory {
49 
50    friend TimestampFactory& getInstance();   
51     
52    private:
53       Timestamp lastTimestamp_;
54       org::xmlBlaster::util::thread::Mutex getterMutex_;
55 
56       /**
57        * The default constructor is made private to implement the singleton
58        * pattern.
59        */
60       TimestampFactory();
61       TimestampFactory(const TimestampFactory &factory);
62       TimestampFactory& operator =(const TimestampFactory &factory);
63       
64 
65    public:
66 
67       ~TimestampFactory();
68 
69       /**
70        * The method to call to get the singleton Timestamp object.
71        */
72       static TimestampFactory& getInstance();
73     
74       /**
75        * Constructs a current timestamp which is guaranteed to be unique in time for this process. 
76        * @return a 64 bit integer (typedef int64_t Timestamp)
77        */
78       Timestamp getTimestamp();
79 
80       static std::string toXml(Timestamp timestamp, const std::string& extraOffset="", bool literal=false);
81 
82       static std::string getTimeAsString(Timestamp timestamp);
83 
84    };
85 
86 }}} // namespace
87 
88 #endif


syntax highlighted by Code2HTML, v. 0.9.1