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