msec.h
上传用户:ets1996
上传日期:2014-09-30
资源大小:353k
文件大小:6k
源码类别:

SNMP编程

开发平台:

Visual C++

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