Merge branch 'master' into jwi-bcc64xsingletonwarning
[ACE_TAO.git] / ACE / ASNMP / asnmp / ctr64.cpp
blob708597995d3e1e79ec1ecb243ea1399ab14dc8af
2 //=============================================================================
3 /**
4 * @file ctr64.cpp
6 * Implementation for Counter64 ( 64 bit counter class).
8 * @author Peter E MellquistMichael R MacFaden mrm@cisco.com - rework & ACE port
9 */
10 //=============================================================================
12 /*===================================================================
13 Copyright (c) 1996
14 Hewlett-Packard Company
16 ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
17 Permission to use, copy, modify, distribute and/or sell this software
18 and/or its documentation is hereby granted without fee. User agrees
19 to display the above copyright notice and this license notice in all
20 copies of the software and any documentation of the software. User
21 agrees to assume all liability for the use of the software; Hewlett-Packard
22 makes no representations about the suitability of this software for any
23 purpose. It is provided "AS-IS without warranty of any kind,either express
24 or implied. User hereby grants a royalty-free license to any and all
25 derivatives based upon this software code base.
26 =====================================================================*/
28 #include "asnmp/ctr64.h"
29 #include "ace/OS_NS_stdio.h"
31 #define MAX32 4294967295u
33 //-----------[ syntax type ]----------------------------------------------
34 SmiUINT32 Counter64::get_syntax()
36 return sNMP_SYNTAX_CNTR64;
39 //------------------[ constructor with values ]--------------------------
40 Counter64::Counter64( unsigned long hiparm, unsigned long loparm)
42 smival.syntax = sNMP_SYNTAX_CNTR64;
43 smival.value.hNumber.hipart = hiparm;
44 smival.value.hNumber.lopart = loparm;
47 //------------------[ constructor with low value only ]------------------
48 Counter64::Counter64( ACE_UINT64 llw )
50 smival.syntax = sNMP_SYNTAX_CNTR64;
51 smival.value.hNumber.hipart = (unsigned long) (llw >> 32);
52 smival.value.hNumber.lopart = (unsigned long) llw & 0xffffffff;
55 //------------------[ copy constructor ]---------------------------------
56 Counter64::Counter64( const Counter64 &ctr64 )
57 : SnmpSyntax (ctr64)
59 smival.syntax = sNMP_SYNTAX_CNTR64;
60 smival.value.hNumber.hipart = ctr64.high();
61 smival.value.hNumber.lopart = ctr64.low();
64 //------------------[ destructor ]---------------------------------
65 Counter64::~Counter64()
70 //------------------[ Counter64::high() ]------------------------------
71 // return the high part
72 unsigned long Counter64::high() const
74 return smival.value.hNumber.hipart;
78 //------------------[ Counter64::low() ]-------------------------------
79 // return the low part
80 unsigned long Counter64::low() const
82 return smival.value.hNumber.lopart;
85 //------------------[ set_high( const unsigned long h) ]-----------
86 // set the high part
87 void Counter64::set_high( const unsigned long h)
89 smival.value.hNumber.hipart = h;
92 //------------------[ set_low( const unsigned long l) ]------------
93 // set the low part
94 void Counter64::set_low( const unsigned long l)
96 smival.value.hNumber.lopart = l;
100 //-----------[ to_long_double( Counter64 c64) ]-----------------------------
101 // convert a Counter 64 to a long double
102 long double Counter64::to_long_double() const
104 long double ld = this->high();
105 ld *= MAX32;
106 ld += this->low();
107 return ld;
111 //-----------[ ld_to_c64( long double ld) ]----------------------------
112 // convert a long double to a Counter64
113 // semantics changed from prior version
114 Counter64& Counter64::assign( long double ld)
116 smival.syntax = sNMP_SYNTAX_CNTR64;
117 unsigned long h = smival.value.hNumber.hipart = (unsigned long)(ld / MAX32);
118 smival.value.hNumber.lopart = (unsigned long)(ld - h);
119 return *this;
122 //----------------[ general Value = operator ]---------------------
123 SnmpSyntax& Counter64::operator=( SnmpSyntax &val)
125 // protect against assignment from itself
126 if ( this == &val )
127 return *this;
129 smival.value.hNumber.lopart = 0; // pessimsitic - assume no mapping
130 smival.value.hNumber.hipart = 0;
132 // try to make assignment valid
133 if (val.valid()){
134 switch (val.get_syntax()){
135 case sNMP_SYNTAX_CNTR64:
136 smival.value.hNumber.hipart =
137 ((Counter64 &)val).smival.value.hNumber.hipart;
138 smival.value.hNumber.lopart =
139 ((Counter64 &)val).smival.value.hNumber.lopart;
140 break;
142 case sNMP_SYNTAX_CNTR32:
143 case sNMP_SYNTAX_TIMETICKS:
144 case sNMP_SYNTAX_GAUGE32:
145 // case sNMP_SYNTAX_UINT32: .. indistinguishable from GAUGE32
146 case sNMP_SYNTAX_INT32:
147 // take advantage of union...
148 smival.value.hNumber.lopart = ((Counter64 &)val).smival.value.uNumber;
149 smival.value.hNumber.hipart = 0;
150 break;
153 return *this;
156 // overloaded assignment
157 Counter64& Counter64::operator=( const ACE_UINT64 rhs)
159 smival.value.hNumber.hipart = (unsigned long) (rhs >> 32);
160 smival.value.hNumber.lopart = (unsigned long) rhs;
161 return *this;
164 // overloaded assignment
165 Counter64& Counter64::operator=( const Counter64 &rhs)
167 smival.value.hNumber.hipart = rhs.high();
168 smival.value.hNumber.lopart = rhs.low();
169 return *this;
172 // otherwise, behave like an unsigned long int
173 Counter64::operator ACE_UINT64()
175 ACE_UINT64 val = smival.value.hNumber.hipart;
176 val = val << 32; // shift right 4 bytes
177 val |= smival.value.hNumber.lopart;
178 return val;
181 //----------------[ Counter64::clone() ]-----------------------------------
182 // create a new instance of this Value
183 SnmpSyntax* Counter64::clone() const
185 return ( SnmpSyntax *) new Counter64(*this);
188 //----------------[ Counter64::valid() ]-------------------------------------
189 int Counter64::valid() const
191 return 1;
194 //----------[ return ASCII format ]-------------------------
195 // TODO: Fix up to do real 64bit decimal value printing...
196 // For now, print > 32-bit values in hex
197 const char * Counter64::to_string()
199 if ( high() != 0 )
200 ACE_OS::sprintf(output_buffer, "0x%X%08X",
201 (unsigned int)high(), (unsigned int)low());
202 else
203 ACE_OS::sprintf(output_buffer, "%d", (int) low());
204 return output_buffer;