SNMP++  3.3.11
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$
43 
44 #ifndef _SNMP_MSEC_H_
45 #define _SNMP_MSEC_H_
46 
47 //----[ includes ]-----------------------------------------------------
48 #include <libsnmp.h>
49 
50 #ifdef WIN32
51 #elif defined (CPU) && CPU == PPC603
52 #include <sys/times.h>
53 #endif
54 
55 #include "snmp_pp/config_snmp_pp.h"
56 #include "snmp_pp/smi.h"
57 #include "snmp_pp/reentrant.h"
58 
59 #ifdef SNMP_PP_NAMESPACE
60 namespace Snmp_pp {
61 #endif
62 
63 //----[ defines ]------------------------------------------------------
64 #define MSECOUTBUF 20
65 
66 //----[ msec class ]---------------------------------------------------
67 /**
68  * Time handling...
69  */
70 class DLLOPT msec
71 {
72  public:
73  /**
74  * Constructor, sets the time to the current system time.
75  */
76  msec() { refresh(); };
77 
78  /**
79  * Constructor using another msec object
80  *
81  * @param in_msec - Time for this object
82  */
83  msec(const msec &in_msec) : m_time(in_msec.m_time), m_changed(true) {};
84 
85  /**
86  * Constructor using seconds and milli sconds.
87  *
88  * @param sec - Seconds
89  * @param milsec - Milli seconds
90  */
91  msec(const int sec, const int milsec) : m_changed(true)
92  { m_time.tv_sec = sec; m_time.tv_usec = milsec; };
93 
94  DLLOPT friend int operator==(const msec &t1, const msec &t2);
95  DLLOPT friend int operator!=(const msec &t1, const msec &t2);
96  DLLOPT friend int operator<(const msec &t1, const msec &t2);
97  DLLOPT friend int operator>(const msec &t1, const msec &t2);
98  DLLOPT friend int operator<=(const msec &t1, const msec &t2)
99  { return((t1 < t2) || (t1 == t2)); };
100  DLLOPT friend int operator>=(const msec &t1, const msec &t2)
101  { return((t1 > t2) || (t1 == t2)); };
102 
103  msec &operator-=(const long millisec);
104  msec &operator-=(const timeval &t1);
105  msec &operator+=(const long millisec);
106  msec &operator+=(const timeval &t1);
107  msec &operator=(const msec &t)
108  { m_time = t.m_time; m_changed = true; return *this; };
109  msec &operator=(const timeval &t1);
110 
111  /**
112  * Use as an unsigned long.
113  *
114  * @return Time in milli seconds
115  */
116  operator unsigned long() const
117  { return ((m_time.tv_sec * 1000) + m_time.tv_usec); };
118 
119  /**
120  * Set the time to the current system time.
121  */
122  void refresh();
123 
124  /**
125  * Set the object out into the future as far as possible.
126  */
127  void SetInfinite()
128  { m_time.tv_sec = (time_t) -1; m_time.tv_usec = 0; m_changed = true; };
129 
130  /**
131  * Check if the time is infinite.
132  *
133  * @return True, if the time is infinite.
134  */
135  int IsInfinite() const
136  { return ((m_time.tv_sec == (time_t) -1) && (m_time.tv_usec == 0)); };
137 
138  /**
139  * Get the difference between this and the given time.
140  * If future is before this objects time, "timeout" will be set to zero.
141  *
142  * @param future - Time to compare to
143  * @param timeout - Will be filled with the difference
144  */
145  void GetDelta(const msec &future, timeval &timeout) const;
146 
147  /**
148  * Get the difference between this object and the current system time.
149  * If the system time is before this objects time,
150  * "timeout" will be set to zero.
151  *
152  * @param timeout - Will be filled with the difference
153  */
154  void GetDeltaFromNow(timeval &timeout) const
155  { msec now; now.GetDelta(*this, timeout); };
156 
157  /**
158  * Return the time as printable string.
159  */
160  const char *get_printable() const;
161 
162 private:
163  timeval m_time;
164  SNMP_PP_MUTABLE char m_output_buffer[MSECOUTBUF];
166 
167 #if !defined HAVE_LOCALTIME_R && !defined HAVE_REENTRANT_LOCALTIME
168 #ifdef _THREADS
170 #endif
171 #endif
172 };
173 
174 #ifdef SNMP_PP_NAMESPACE
175 } // end of namespace Snmp_pp
176 #endif
177 
178 #endif // _SNMP_MSEC_H_
DLLOPT friend int operator>=(const msec &t1, const msec &t2)
Definition: msec.h:100
msec(const msec &in_msec)
Constructor using another msec object.
Definition: msec.h:83
#define SNMP_PP_MUTABLE
msec()
Constructor, sets the time to the current system time.
Definition: msec.h:76
Time handling...
Definition: msec.h:70
SNMP_PP_MUTABLE bool m_changed
Definition: msec.h:165
void SetInfinite()
Set the object out into the future as far as possible.
Definition: msec.h:127
void GetDelta(const msec &future, timeval &timeout) const
Get the difference between this and the given time.
#define DLLOPT
static SnmpSynchronized m_localtime_mutex
Definition: msec.h:169
timeval m_time
Definition: msec.h:163
msec(const int sec, const int milsec)
Constructor using seconds and milli sconds.
Definition: msec.h:91
#define MSECOUTBUF
Definition: msec.h:64
int IsInfinite() const
Check if the time is infinite.
Definition: msec.h:135
DLLOPT friend int operator<=(const msec &t1, const msec &t2)
Definition: msec.h:98
void GetDeltaFromNow(timeval &timeout) const
Get the difference between this object and the current system time.
Definition: msec.h:154
msec & operator=(const msec &t)
Definition: msec.h:107