1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
// $Id$
// ============================================================================
//
// = LIBRARY
// asnmp
//
// = FILENAME
// ctr64.cpp
//
// = DESCRIPTION
// Implementation for Counter64 ( 64 bit counter class).
//
// = AUTHOR
// Peter E Mellquist
// Michael R MacFaden mrm@cisco.com - rework & ACE port
// ============================================================================
/*===================================================================
Copyright (c) 1996
Hewlett-Packard Company
ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
Permission to use, copy, modify, distribute and/or sell this software
and/or its documentation is hereby granted without fee. User agrees
to display the above copyright notice and this license notice in all
copies of the software and any documentation of the software. User
agrees to assume all liability for the use of the software; Hewlett-Packard
makes no representations about the suitability of this software for any
purpose. It is provided "AS-IS without warranty of any kind,either express
or implied. User hereby grants a royalty-free license to any and all
derivatives based upon this software code base.
=====================================================================*/
#include "asnmp/ctr64.h"
ACE_RCSID(asnmp, ctr64, "$Id$")
#define MAX32 4294967295u
//-----------[ syntax type ]----------------------------------------------
SmiUINT32 Counter64::get_syntax()
{
return sNMP_SYNTAX_CNTR64;
}
//------------------[ constructor with values ]--------------------------
Counter64::Counter64( unsigned long hiparm, unsigned long loparm)
{
smival.syntax = sNMP_SYNTAX_CNTR64;
smival.value.hNumber.hipart = hiparm;
smival.value.hNumber.lopart = loparm;
}
//------------------[ constructor with low value only ]------------------
Counter64::Counter64( unsigned long long llw )
{
smival.syntax = sNMP_SYNTAX_CNTR64;
smival.value.hNumber.hipart = (unsigned long) (llw >> 32);
smival.value.hNumber.lopart = (unsigned long) llw & 0xffffffff;
}
//------------------[ copy constructor ]---------------------------------
Counter64::Counter64( const Counter64 &ctr64 )
{
smival.syntax = sNMP_SYNTAX_CNTR64;
smival.value.hNumber.hipart = ctr64.high();
smival.value.hNumber.lopart = ctr64.low();
}
//------------------[ destructor ]---------------------------------
Counter64::~Counter64()
{
}
//------------------[ Counter64::high() ]------------------------------
// return the high part
unsigned long Counter64::high() const
{
return smival.value.hNumber.hipart;
}
//------------------[ Counter64::low() ]-------------------------------
// return the low part
unsigned long Counter64::low() const
{
return smival.value.hNumber.lopart;
}
//------------------[ set_high( const unsigned long h) ]-----------
// set the high part
void Counter64::set_high( const unsigned long h)
{
smival.value.hNumber.hipart = h;
}
//------------------[ set_low( const unsigned long l) ]------------
// set the low part
void Counter64::set_low( const unsigned long l)
{
smival.value.hNumber.lopart = l;
}
//-----------[ to_long_double( Counter64 c64) ]-----------------------------
// convert a Counter 64 to a long double
long double Counter64::to_long_double() const
{
long double ld = this->high();
ld *= MAX32;
ld += this->low();
return ld;
}
//-----------[ ld_to_c64( long double ld) ]----------------------------
// convert a long double to a Counter64
// semantics changed from prior version
Counter64& Counter64::assign( long double ld)
{
smival.syntax = sNMP_SYNTAX_CNTR64;
unsigned long h = smival.value.hNumber.hipart = (unsigned long)(ld / MAX32);
smival.value.hNumber.lopart = (unsigned long)(ld - h);
return *this;
}
//----------------[ general Value = operator ]---------------------
SnmpSyntax& Counter64::operator=( SnmpSyntax &val)
{
// protect against assignment from itself
if ( this == &val )
return *this;
smival.value.hNumber.lopart = 0; // pessimsitic - assume no mapping
smival.value.hNumber.hipart = 0;
// try to make assignment valid
if (val.valid()){
switch (val.get_syntax()){
case sNMP_SYNTAX_CNTR64:
smival.value.hNumber.hipart =
((Counter64 &)val).smival.value.hNumber.hipart;
smival.value.hNumber.lopart =
((Counter64 &)val).smival.value.hNumber.lopart;
break;
case sNMP_SYNTAX_CNTR32:
case sNMP_SYNTAX_TIMETICKS:
case sNMP_SYNTAX_GAUGE32:
// case sNMP_SYNTAX_UINT32: .. indistinguishable from GAUGE32
case sNMP_SYNTAX_INT32:
// take advantage of union...
smival.value.hNumber.lopart = ((Counter64 &)val).smival.value.uNumber;
smival.value.hNumber.hipart = 0;
break;
}
}
return *this;
}
// overloaded assignment
Counter64& Counter64::operator=( const unsigned long long rhs)
{
smival.value.hNumber.hipart = (unsigned long) (rhs >> 32);
smival.value.hNumber.lopart = (unsigned long) rhs;
return *this;
}
// overloaded assignment
Counter64& Counter64::operator=( const Counter64 &rhs)
{
smival.value.hNumber.hipart = rhs.high();
smival.value.hNumber.lopart = rhs.low();
return *this;
}
// otherwise, behave like an unsigned long int
Counter64::operator unsigned long long()
{
unsigned long long val = smival.value.hNumber.hipart;
val = val << 32; // shift right 4 bytes
val |= smival.value.hNumber.lopart;
return val;
}
//----------------[ Counter64::clone() ]-----------------------------------
// create a new instance of this Value
SnmpSyntax* Counter64::clone() const
{
return ( SnmpSyntax *) new Counter64(*this);
}
//----------------[ Counter64::valid() ]-------------------------------------
int Counter64::valid() const
{
return 1;
}
//----------[ return ASCII format ]-------------------------
// TODO: Fix up to do real 64bit decimal value printing...
// For now, print > 32-bit values in hex
char * Counter64::to_string()
{
if ( high() != 0 )
sprintf(output_buffer, "0x%X%08X",
(unsigned int)high(), (unsigned int)low());
else
sprintf(output_buffer, "%d", (int) low());
return output_buffer;
}
|