Pol  Revision:4b29d2b
blong.cpp
Go to the documentation of this file.
1 
7 #include <sstream>
8 #include <string>
9 
10 #include "../clib/stlutil.h"
11 #include "berror.h"
12 #include "bobject.h"
13 #include "impstr.h"
14 
15 namespace Pol
16 {
17 namespace Bscript
18 {
19 #if BOBJECTIMP_DEBUG
20 BLong::BLong( int lval ) : BObjectImp( OTLong ), lval_( static_cast<int>( lval ) ) {}
21 
22 BLong::BLong( const BLong& L ) : BObjectImp( OTLong ), lval_( L.lval_ ) {}
23 #endif
24 
25 
26 std::string BLong::pack() const
27 {
28  OSTRINGSTREAM os;
29  os << "i" << lval_;
30  return OSTRINGSTREAM_STR( os );
31 }
32 
33 std::string BLong::pack( int val )
34 {
35  OSTRINGSTREAM os;
36  os << "i" << val;
37  return OSTRINGSTREAM_STR( os );
38 }
39 
40 
41 void BLong::packonto( std::ostream& os ) const
42 {
43  os << "i" << lval_;
44 }
45 
46 BObjectImp* BLong::unpack( const char* pstr )
47 {
48  int lv;
49  ISTRINGSTREAM is( pstr );
50  if ( is >> lv )
51  {
52  return new BLong( lv );
53  }
54  else
55  {
56  return new BError( "Error extracting Integer value" );
57  }
58 }
59 
60 BObjectImp* BLong::unpack( std::istream& is )
61 {
62  int lv;
63  if ( is >> lv )
64  {
65  return new BLong( lv );
66  }
67  else
68  {
69  return new BError( "Error extracting Integer value" );
70  }
71 }
72 
74 {
75  return new BLong( *this );
76 }
77 
78 size_t BLong::sizeEstimate() const
79 {
80  return sizeof( BLong );
81 }
82 
83 bool BLong::isTrue() const
84 {
85  return ( lval_ != 0 );
86 }
87 
88 bool BLong::operator==( const BObjectImp& objimp ) const
89 {
90  if ( objimp.isa( OTLong ) )
91  {
92  return lval_ == ( (BLong&)objimp ).lval_;
93  }
94  else if ( objimp.isa( OTDouble ) )
95  {
96  return lval_ == ( (Double&)objimp ).value();
97  }
98  else if ( objimp.isa( OTBoolean ) )
99  {
100  return isTrue() == static_cast<const BBoolean&>( objimp ).isTrue();
101  }
102  else
103  {
104  return false;
105  }
106 }
107 
108 bool BLong::operator<( const BObjectImp& objimp ) const
109 {
110  if ( objimp.isa( OTLong ) )
111  {
112  return lval_ < ( (BLong&)objimp ).lval_;
113  }
114  else if ( objimp.isa( OTDouble ) )
115  {
116  return lval_ < ( (Double&)objimp ).value();
117  }
118 
119  return base::operator<( objimp );
120 }
121 
122 std::string BLong::getStringRep() const
123 {
124  OSTRINGSTREAM os;
125 
126  os << lval_;
127 
128  return OSTRINGSTREAM_STR( os );
129 }
130 
132 {
133  return objimp.selfPlusObj( *this );
134 }
135 BObjectImp* BLong::selfPlusObj( const BLong& objimp ) const
136 {
137  return new BLong( lval_ + objimp.lval_ );
138 }
139 BObjectImp* BLong::selfPlusObj( const Double& objimp ) const
140 {
141  return new Double( lval_ + objimp.value() );
142 }
143 BObjectImp* BLong::selfPlusObj( const String& objimp ) const
144 {
145  return new String( getStringRep() + objimp.data() );
146 }
147 
149 {
150  objimp.selfPlusObj( *this, obj );
151 }
152 void BLong::selfPlusObj( BLong& objimp, BObject& /*obj*/ )
153 {
154  lval_ += objimp.value();
155 }
156 void BLong::selfPlusObj( Double& objimp, BObject& obj )
157 {
158  obj.setimp( selfPlusObj( objimp ) );
159 }
160 void BLong::selfPlusObj( String& objimp, BObject& obj )
161 {
162  obj.setimp( selfPlusObj( objimp ) );
163 }
164 
166 {
167  return objimp.selfMinusObj( *this );
168 }
169 BObjectImp* BLong::selfMinusObj( const BLong& objimp ) const
170 {
171  return new BLong( lval_ - objimp.value() );
172 }
173 BObjectImp* BLong::selfMinusObj( const Double& objimp ) const
174 {
175  return new Double( lval_ - objimp.value() );
176 }
177 BObjectImp* BLong::selfMinusObj( const String& objimp ) const
178 {
179  String s( getStringRep() );
180  return s.selfMinusObj( objimp );
181 }
183 {
184  objimp.selfMinusObj( *this, obj );
185 }
186 void BLong::selfMinusObj( BLong& objimp, BObject& /*obj*/ )
187 {
188  lval_ -= objimp.value();
189 }
190 void BLong::selfMinusObj( Double& objimp, BObject& obj )
191 {
192  obj.setimp( selfMinusObj( objimp ) );
193 }
194 void BLong::selfMinusObj( String& objimp, BObject& obj )
195 {
196  obj.setimp( selfMinusObj( objimp ) );
197 }
198 
200 {
201  return objimp.selfTimesObj( *this );
202 }
203 BObjectImp* BLong::selfTimesObj( const BLong& objimp ) const
204 {
205  return new BLong( lval_ * objimp.lval_ );
206 }
207 BObjectImp* BLong::selfTimesObj( const Double& objimp ) const
208 {
209  return new Double( lval_ * objimp.value() );
210 }
212 {
213  objimp.selfTimesObj( *this, obj );
214 }
215 void BLong::selfTimesObj( BLong& objimp, BObject& /*obj*/ )
216 {
217  lval_ *= objimp.lval_;
218 }
219 void BLong::selfTimesObj( Double& objimp, BObject& obj )
220 {
221  obj.setimp( selfTimesObj( objimp ) );
222 }
223 
225 {
226  return objimp.selfDividedByObj( *this );
227 }
228 BObjectImp* BLong::selfDividedByObj( const BLong& objimp ) const
229 {
230  int divisor = objimp.lval_;
231  if ( !divisor )
232  return new BError( "Divide by Zero" );
233  else
234  return new BLong( lval_ / divisor );
235 }
237 {
238  double divisor = objimp.value();
239  if ( divisor == 0.0 )
240  return new BError( "Divide by Zero" );
241  else
242  return new Double( lval_ / divisor );
243 }
245 {
246  objimp.selfDividedByObj( *this, obj );
247 }
248 void BLong::selfDividedByObj( BLong& objimp, BObject& obj )
249 {
250  if ( !objimp.lval_ )
251  obj.setimp( new BError( "Divide by Zero" ) );
252  else
253  lval_ /= objimp.lval_;
254 }
256 {
257  obj.setimp( selfDividedByObj( objimp ) );
258 }
259 
261 {
262  return objimp.selfModulusObj( *this );
263 }
264 BObjectImp* BLong::selfModulusObj( const BLong& objimp ) const
265 {
266  int divisor = objimp.lval_;
267  if ( !divisor )
268  return new BError( "Divide by Zero" );
269  else
270  return new BLong( lval_ % divisor );
271 }
272 BObjectImp* BLong::selfModulusObj( const Double& objimp ) const
273 {
274  int divisor = static_cast<int>( objimp.value() );
275  if ( divisor == 0 )
276  return new BError( "Divide by Zero" );
277  else
278  return new Double( lval_ % divisor );
279 }
281 {
282  objimp.selfModulusObj( *this, obj );
283 }
284 void BLong::selfModulusObj( BLong& objimp, BObject& obj )
285 {
286  if ( !objimp.lval_ )
287  obj.setimp( new BError( "Divide by Zero" ) );
288  else
289  lval_ %= objimp.lval_;
290 }
291 void BLong::selfModulusObj( Double& objimp, BObject& obj )
292 {
293  obj.setimp( selfModulusObj( objimp ) );
294 }
295 
296 
298 {
299  return objimp.selfBitShiftRightObj( *this );
300 }
302 {
303  return new BLong( lval_ >> objimp.lval_ );
304 }
306 {
307  objimp.selfBitShiftRightObj( *this, obj );
308 }
309 void BLong::selfBitShiftRightObj( BLong& objimp, BObject& /*obj*/ )
310 {
311  lval_ >>= objimp.lval_;
312 }
313 
315 {
316  return objimp.selfBitShiftLeftObj( *this );
317 }
319 {
320  return new BLong( lval_ << objimp.lval_ );
321 }
323 {
324  objimp.selfBitShiftLeftObj( *this, obj );
325 }
326 void BLong::selfBitShiftLeftObj( BLong& objimp, BObject& /*obj*/ )
327 {
328  lval_ <<= objimp.lval_;
329 }
330 
332 {
333  return objimp.selfBitAndObj( *this );
334 }
335 BObjectImp* BLong::selfBitAndObj( const BLong& objimp ) const
336 {
337  return new BLong( lval_ & objimp.lval_ );
338 }
340 {
341  objimp.selfBitAndObj( *this, obj );
342 }
343 void BLong::selfBitAndObj( BLong& objimp, BObject& /*obj*/ )
344 {
345  lval_ &= objimp.lval_;
346 }
347 
349 {
350  return objimp.selfBitOrObj( *this );
351 }
352 BObjectImp* BLong::selfBitOrObj( const BLong& objimp ) const
353 {
354  return new BLong( lval_ | objimp.lval_ );
355 }
357 {
358  objimp.selfBitOrObj( *this, obj );
359 }
360 void BLong::selfBitOrObj( BLong& objimp, BObject& /*obj*/ )
361 {
362  lval_ |= objimp.lval_;
363 }
364 
366 {
367  return objimp.selfBitXorObj( *this );
368 }
369 BObjectImp* BLong::selfBitXorObj( const BLong& objimp ) const
370 {
371  return new BLong( lval_ ^ objimp.lval_ );
372 }
374 {
375  objimp.selfBitXorObj( *this, obj );
376 }
377 void BLong::selfBitXorObj( BLong& objimp, BObject& /*obj*/ )
378 {
379  lval_ ^= objimp.lval_;
380 }
381 
383 {
384  return new BLong( ~lval_ );
385 }
386 }
387 }
virtual BObjectImp * selfBitOrObj(const BObjectImp &objimp) const
Definition: object.cpp:757
virtual BObjectImp * selfBitOrObj(const BLong &objimp) const POL_OVERRIDE
Definition: blong.cpp:352
virtual BObjectImp * selfPlusObj(const BLong &objimp) const POL_OVERRIDE
Definition: blong.cpp:135
int value() const
Definition: bobject.h:592
virtual bool operator==(const BObjectImp &objimp) const POL_OVERRIDE
Definition: blong.cpp:88
virtual BObjectImp * selfTimesObj(const BObjectImp &objimp) const
Definition: object.cpp:463
bool isa(BObjectType type) const
Definition: bobject.h:353
virtual BObjectImp * selfBitXorObj(const BObjectImp &objimp) const
Definition: object.cpp:806
virtual BObjectImp * selfBitShiftRightObj(const BObjectImp &objimp) const
Definition: object.cpp:610
virtual BObjectImp * selfPlusObjImp(const BObjectImp &objimp) const POL_OVERRIDE
Definition: blong.cpp:131
virtual BObjectImp * bitnot() const POL_OVERRIDE
Definition: blong.cpp:382
static BObjectImp * unpack(const char *pstr)
Definition: blong.cpp:46
virtual BObjectImp * selfBitShiftRightObj(const BLong &objimp) const POL_OVERRIDE
Definition: blong.cpp:301
#define OSTRINGSTREAM_STR(x)
Definition: stlutil.h:76
virtual BObjectImp * selfBitShiftLeftObj(const BObjectImp &objimp) const
Definition: object.cpp:659
virtual BObjectImp * selfModulusObj(const BLong &objimp) const POL_OVERRIDE
Definition: blong.cpp:264
virtual BObjectImp * selfDividedByObjImp(const BObjectImp &objimp) const POL_OVERRIDE
Definition: blong.cpp:224
virtual BObjectImp * selfMinusObjImp(const BObjectImp &objimp) const POL_OVERRIDE
Definition: blong.cpp:165
virtual BObjectImp * selfBitAndObj(const BObjectImp &objimp) const
Definition: object.cpp:708
virtual BObjectImp * selfBitShiftRightObjImp(const BObjectImp &objimp) const POL_OVERRIDE
Definition: blong.cpp:297
virtual BObjectImp * selfMinusObj(const BLong &objimp) const POL_OVERRIDE
Definition: blong.cpp:169
virtual BObjectImp * selfTimesObjImp(const BObjectImp &objimp) const POL_OVERRIDE
Definition: blong.cpp:199
virtual BObjectImp * selfBitXorObjImp(const BObjectImp &objimp) const POL_OVERRIDE
Definition: blong.cpp:365
virtual BObjectImp * selfMinusObj(const BObjectImp &objimp) const POL_OVERRIDE
Definition: str.cpp:290
void setimp(BObjectImp *imp)
Definition: bobject.h:446
virtual BObjectImp * selfDividedByObj(const BObjectImp &objimp) const
Definition: object.cpp:512
virtual bool operator<(const BObjectImp &objimp) const
Definition: object.cpp:283
virtual void packonto(std::ostream &os) const POL_OVERRIDE
Definition: blong.cpp:41
virtual BObjectImp * selfBitAndObj(const BLong &objimp) const POL_OVERRIDE
Definition: blong.cpp:335
virtual BObjectImp * selfBitShiftLeftObj(const BLong &objimp) const POL_OVERRIDE
Definition: blong.cpp:318
#define OSTRINGSTREAM
Definition: stlutil.h:75
virtual BObjectImp * selfBitAndObjImp(const BObjectImp &objimp) const POL_OVERRIDE
Definition: blong.cpp:331
virtual size_t sizeEstimate() const POL_OVERRIDE
Definition: blong.cpp:78
BLong(int lval=0L)
Definition: blong.cpp:20
virtual BObjectImp * copy() const POL_OVERRIDE
Definition: blong.cpp:73
virtual bool isTrue() const POL_OVERRIDE
Definition: blong.cpp:83
#define ISTRINGSTREAM
Definition: stlutil.h:73
virtual std::string getStringRep() const POL_OVERRIDE
Definition: blong.cpp:122
virtual BObjectImp * selfMinusObj(const BObjectImp &objimp) const
Definition: object.cpp:414
virtual BObjectImp * selfModulusObjImp(const BObjectImp &objimp) const POL_OVERRIDE
Definition: blong.cpp:260
virtual bool operator<(const BObjectImp &objimp) const POL_OVERRIDE
Definition: blong.cpp:108
double value() const
Definition: bobject.h:713
virtual BObjectImp * selfTimesObj(const BLong &objimp) const POL_OVERRIDE
Definition: blong.cpp:203
virtual BObjectImp * selfModulusObj(const BObjectImp &objimp) const
Definition: object.cpp:561
virtual BObjectImp * selfDividedByObj(const BLong &objimp) const POL_OVERRIDE
Definition: blong.cpp:228
const char * data() const
Definition: impstr.h:66
Definition: berror.cpp:12
virtual BObjectImp * selfBitOrObjImp(const BObjectImp &objimp) const POL_OVERRIDE
Definition: blong.cpp:348
virtual std::string pack() const POL_OVERRIDE
Definition: blong.cpp:26
virtual BObjectImp * selfPlusObj(const BObjectImp &objimp) const
Definition: object.cpp:365
virtual BObjectImp * selfBitShiftLeftObjImp(const BObjectImp &objimp) const POL_OVERRIDE
Definition: blong.cpp:314
virtual BObjectImp * selfBitXorObj(const BLong &objimp) const POL_OVERRIDE
Definition: blong.cpp:369