]> git.stg.codes - ssmd.git/blob - 3rdparty/snmp++/include/snmp_pp/msec.h
Initial adding
[ssmd.git] / 3rdparty / snmp++ / include / snmp_pp / msec.h
1 /*_############################################################################
2   _## 
3   _##  msec.h  
4   _##
5   _##  SNMP++v3.2.25
6   _##  -----------------------------------------------
7   _##  Copyright (c) 2001-2010 Jochen Katz, Frank Fock
8   _##
9   _##  This software is based on SNMP++2.6 from Hewlett Packard:
10   _##  
11   _##    Copyright (c) 1996
12   _##    Hewlett-Packard Company
13   _##  
14   _##  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
15   _##  Permission to use, copy, modify, distribute and/or sell this software 
16   _##  and/or its documentation is hereby granted without fee. User agrees 
17   _##  to display the above copyright notice and this license notice in all 
18   _##  copies of the software and any documentation of the software. User 
19   _##  agrees to assume all liability for the use of the software; 
20   _##  Hewlett-Packard and Jochen Katz make no representations about the 
21   _##  suitability of this software for any purpose. It is provided 
22   _##  "AS-IS" without warranty of any kind, either express or implied. User 
23   _##  hereby grants a royalty-free license to any and all derivatives based
24   _##  upon this software code base. 
25   _##  
26   _##  Stuttgart, Germany, Thu Sep  2 00:07:47 CEST 2010 
27   _##  
28   _##########################################################################*/
29 /*
30   Copyright (c) 1999
31   Hewlett-Packard Company
32
33   ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
34   Permission to use, copy, modify, distribute and/or sell this software
35   and/or its documentation is hereby granted without fee. User agrees
36   to display the above copyright notice and this license notice in all
37   copies of the software and any documentation of the software. User
38   agrees to assume all liability for the use of the software; Hewlett-Packard
39   makes no representations about the suitability of this software for any
40   purpose. It is provided "AS-IS without warranty of any kind,either express
41   or implied. User hereby grants a royalty-free license to any and all
42   derivatives based upon this software code base.
43 */
44 // $Id: msec.h 307 2007-09-14 20:01:45Z katz $
45
46 #ifndef _MSEC_H_
47 #define _MSEC_H_
48
49 //----[ includes ]-----------------------------------------------------
50 #include <sys/types.h> /* NOTE: due to 10.10 bug, order is important
51                         * in that all routines must include types.h
52                         * and time.h in same order otherwise you will
53                         * get conflicting definitions of "fd_set"
54                         * resulting in link time errors.
55                         */
56 #ifdef WIN32
57 #elif defined (CPU) && CPU == PPC603
58 #include <sys/times.h>
59 #else
60 #include <sys/time.h>
61 #include <sys/param.h>
62 #endif
63
64 #include <time.h>
65
66 #include "snmp_pp/config_snmp_pp.h"
67 #include "snmp_pp/smi.h"
68 #include "snmp_pp/reentrant.h"
69
70 #ifdef SNMP_PP_NAMESPACE
71 namespace Snmp_pp {
72 #endif
73
74 //----[ defines ]------------------------------------------------------
75 #define MSECOUTBUF 20
76
77 //----[ msec class ]---------------------------------------------------
78 /**
79  * Time handling...
80  */
81 class DLLOPT msec
82 {
83  public:
84   /**
85    * Constructor, sets the time to the current system time.
86    */
87   msec() { refresh(); };
88
89   /**
90    * Constructor using another msec object
91    *
92    * @param in_msec - Time for this object
93    */
94   msec(const msec &in_msec) : m_time(in_msec.m_time), m_changed(true) {};
95
96   /**
97    * Constructor using seconds and milli sconds.
98    *
99    * @param sec    - Seconds
100    * @param milsec - Milli seconds
101    */
102   msec(const int sec, const int milsec) : m_changed(true)
103     { m_time.tv_sec  = sec; m_time.tv_usec = milsec; };
104
105   DLLOPT friend int operator==(const msec &t1, const msec &t2);
106   DLLOPT friend int operator!=(const msec &t1, const msec &t2);
107   DLLOPT friend int operator<(const msec &t1, const msec &t2);
108   DLLOPT friend int operator>(const msec &t1, const msec &t2);
109   DLLOPT friend int operator<=(const msec &t1, const msec &t2)
110     { return((t1 < t2) || (t1 == t2)); };
111   DLLOPT friend int operator>=(const msec &t1, const msec &t2)
112     { return((t1 > t2) || (t1 == t2)); };
113
114   msec &operator-=(const long millisec);
115   msec &operator-=(const timeval &t1);
116   msec &operator+=(const long millisec);
117   msec &operator+=(const timeval &t1);
118   msec &operator=(const msec &t)
119     { m_time = t.m_time; m_changed = true; return *this; };
120   msec &operator=(const timeval &t1);
121
122   /**
123    * Use as an unsigned long.
124    *
125    * @return Time in milli seconds
126    */
127   operator unsigned long() const
128     { return ((m_time.tv_sec * 1000) + m_time.tv_usec); };
129
130   /**
131    * Set the time to the current system time.
132    */
133   void refresh();
134
135   /**
136    * Set the object out into the future as far as possible.
137    */
138   void SetInfinite()
139     { m_time.tv_sec = (time_t) -1; m_time.tv_usec = 0; m_changed = true; };
140
141   /**
142    * Check if the time is infinite.
143    *
144    * @return True, if the time is infinite.
145    */
146   int IsInfinite() const
147     { return ((m_time.tv_sec == (time_t) -1) && (m_time.tv_usec == 0)); };
148
149   /**
150    * Get the difference between this and the given time.
151    * If future is before this objects time, "timeout" will be set to zero.
152    *
153    * @param future  - Time to compare to
154    * @param timeout - Will be filled with the difference
155    */
156   void GetDelta(const msec &future, timeval &timeout) const;
157
158   /**
159    * Get the difference between this object and the current system time.
160    * If the system time is before this objects time,
161    * "timeout" will be set to zero.
162    *
163    * @param timeout - Will be filled with the difference
164    */
165   void GetDeltaFromNow(timeval &timeout) const
166     { msec now; now.GetDelta(*this, timeout); };
167
168   /**
169    * Return the time as printable string.
170    */
171   const char *get_printable() const;
172
173 private:
174   timeval m_time;
175   SNMP_PP_MUTABLE char m_output_buffer[MSECOUTBUF];
176   SNMP_PP_MUTABLE bool m_changed;
177
178 #if !defined HAVE_LOCALTIME_R && !defined HAVE_REENTRANT_LOCALTIME
179 #ifdef _THREADS
180   static SnmpSynchronized m_localtime_mutex;
181 #endif
182 #endif
183 };
184
185 #ifdef SNMP_PP_NAMESPACE
186 } // end of namespace Snmp_pp
187 #endif 
188
189 #endif // _MSEC_H_