claraty::Bits Class Reference
#include <bits.h>
Public Member Functions | |
| Bits | operator<< (size_t n) const |
| Bits | operator>> (size_t n) const |
| Bits | operator~ () const |
| size_t | size () const |
| unsigned long | to_ulong () const |
| std::string | to_str () const |
| void | print (const char *msg="") const |
Assignment/index operations | |
| Bits & | operator= (const Bits &rhs) |
| reference | operator[] (size_t pos) |
| bool | operator[] (size_t pos) const |
Bit manipulation operators | |
| Bits & | operator &= (const Bits &rhs) |
| Bits & | operator|= (const Bits &rhs) |
| Bits & | operator^= (const Bits &rhs) |
| Bits & | operator<<= (size_t n) |
| Bits & | operator>>= (size_t n) |
| Bits & | set () |
| Bits & | set_bit (size_t pos, bool val=true) |
| Bits & | set_bits (size_t pos, size_t n) |
| Bits & | clr () |
| Bits & | clr_bit (size_t pos) |
| Bits & | clr_bits (size_t pos, size_t n) |
| Bits & | toggle () |
| Bits & | toggle_bit (size_t pos) |
| Bits & | unchecked_set (unsigned long data) |
Bit testing operations | |
| bool | operator== (const Bits &rhs) const |
| bool | operator!= (const Bits &rhs) const |
| bool | test (size_t pos) const |
| bool | is_set (size_t pos) const |
| bool | get_bit (size_t pos) const |
| bool | any () const |
| bool | none () const |
Private Member Functions | |
| void | _sanitize () |
Private Attributes | |
| unsigned long | _data |
| size_t | _Nbits |
Friends | |
| class | reference |
Classes | |
| class | reference |
Detailed Description
Definition at line 53 of file bits.h.
Constructor & Destructor Documentation
| claraty::Bits::Bits | ( | unsigned long | val = 0, |
|
| size_t | n_bits = MAX_BITS | |||
| ) | [inline] |
Definition at line 300 of file bits.h.
References _Nbits, _sanitize(), and claraty::MAX_BITS.
Referenced by operator<<(), operator>>(), and operator~().
00301 : _data(val), _Nbits(n_bits) 00302 { 00303 // Constructor 00304 // 00305 // default args: 00306 // unsigned long val = 0 00307 // size_t n_bits = MAX_BITS = sizeof(unsigned long) 00308 // 00309 // input args: 00310 // unsigned long val --- bit pattern in hexadecimal/octal/decimal number 00311 // size_t n_bits --- number of bits ( >= MAX_BITS ) 00312 // 00313 // Bits b1; // b1 = 0000 0000 0000 0000 (_Nbits = MAX_BITS) 00314 // Bits b1(0x14); // b1 = 0000 0000 0001 0100 (_Nbits = MAX_BITS) 00315 // Bits b1 = 0x14; // same as above (not using assignment operator) 00316 // Bits b1(0x14, 5); // b1 = ---- ---- ---1 0100 (_Nbits = 5) 00317 // 00318 // Bits(unsigned long) allows automatic type conversion from "unsigned long" 00319 // to "Bits" with _Nbits = MAX_BITS 00320 // 00321 if ((int) _Nbits > (int) MAX_BITS) { 00322 // throw out_of_range("Bits::Bits"); 00323 // For now, clamp to MAX_BITS and let user check size to see if it worked. 00324 _Nbits = MAX_BITS; 00325 std::cerr << "THROW ERROR: out_of_range - Bits::Bits" << std::endl; 00326 } else { 00327 _sanitize(); 00328 } 00329 }
Here is the call graph for this function:

| claraty::Bits::Bits | ( | const std::string & | str, | |
| size_t | n_bits = MAX_BITS | |||
| ) | [inline, explicit] |
Definition at line 337 of file bits.h.
References _data, claraty::_maskbit(), _Nbits, and claraty::MAX_BITS.
00338 : _Nbits(n_bits) 00339 { 00340 // Constructor 00341 // 00342 // default args: 00343 // size_t n_bits = MAX_BITS = sizeof(unsigned long) 00344 // 00345 // input args: 00346 // unsigned char* str ---binary-format number, e.g, "0100 0111 0000 1010" 00347 // size_t n_bits --- number of bits ( >= MAX_BITS ) 00348 // 00349 // explicit: no implicit automatic conversion allowed from char* str to Bits; 00350 // Bits(const char* str) must be explicitly used for the conversion 00351 // 00352 // Bits b1(""); // b1 = 0000 0000 0000 0000 (_Nbits = MAX_BITS) 00353 // Bits b2("01 0100"); // b1 = 0000 0000 0001 0100 (_Nbits = MAX_BITS) 00354 // Bits b2("01 0100", 5); // b1 = ---- ---- ---1 0100 (_Nbits = 5) 00355 // 00356 if ((int) _Nbits > (int) MAX_BITS) 00357 // throw out_of_range("Bits::Bits"); 00358 std::cerr << "THROW ERROR: out_of_range - Bits::Bits" << std::endl; 00359 00360 _data = 0; // clear all bits initially 00361 00362 int i = 0; // bit index 00363 00364 // for (int j = strlen(str) - 1; j >= 0; j--) { 00365 for (int j = str.length() - 1; j >= 0; j--) { 00366 00367 switch (str[j]) { 00368 case '0': 00369 i++; 00370 break; 00371 case '1': // set bit[i] of _data = 1 00372 _data |= _maskbit(i); 00373 i++; 00374 break; 00375 case ' ': // skip space 00376 break; 00377 default: 00378 // throw invalid_argument("Bits::Bits"); 00379 std::cerr << "THROW ERROR: invalid_argument - Bits::Bits" << std::endl; 00380 } 00381 00382 if (i >= (int) _Nbits) { // warning ?????? 00383 std::cerr << "Warning: input data too large" << std::endl; 00384 break; 00385 } 00386 } 00387 }
Here is the call graph for this function:

| claraty::Bits::Bits | ( | unsigned long | val = 0, |
|
| size_t | n_bits = MAX_BITS | |||
| ) | [inline] |
Definition at line 300 of file bits.h.
References _Nbits, _sanitize(), and claraty::MAX_BITS.
Referenced by operator<<(), operator>>(), and operator~().
00301 : _data(val), _Nbits(n_bits) 00302 { 00303 // Constructor 00304 // 00305 // default args: 00306 // unsigned long val = 0 00307 // size_t n_bits = MAX_BITS = sizeof(unsigned long) 00308 // 00309 // input args: 00310 // unsigned long val --- bit pattern in hexadecimal/octal/decimal number 00311 // size_t n_bits --- number of bits ( >= MAX_BITS ) 00312 // 00313 // Bits b1; // b1 = 0000 0000 0000 0000 (_Nbits = MAX_BITS) 00314 // Bits b1(0x14); // b1 = 0000 0000 0001 0100 (_Nbits = MAX_BITS) 00315 // Bits b1 = 0x14; // same as above (not using assignment operator) 00316 // Bits b1(0x14, 5); // b1 = ---- ---- ---1 0100 (_Nbits = 5) 00317 // 00318 // Bits(unsigned long) allows automatic type conversion from "unsigned long" 00319 // to "Bits" with _Nbits = MAX_BITS 00320 // 00321 if ((int) _Nbits > (int) MAX_BITS) { 00322 // throw out_of_range("Bits::Bits"); 00323 // For now, clamp to MAX_BITS and let user check size to see if it worked. 00324 _Nbits = MAX_BITS; 00325 std::cerr << "THROW ERROR: out_of_range - Bits::Bits" << std::endl; 00326 } else { 00327 _sanitize(); 00328 } 00329 }
Here is the call graph for this function:

| claraty::Bits::Bits | ( | const std::string & | str, | |
| size_t | n_bits = MAX_BITS | |||
| ) | [inline, explicit] |
Definition at line 337 of file bits.h.
References _data, claraty::_maskbit(), _Nbits, and claraty::MAX_BITS.
00338 : _Nbits(n_bits) 00339 { 00340 // Constructor 00341 // 00342 // default args: 00343 // size_t n_bits = MAX_BITS = sizeof(unsigned long) 00344 // 00345 // input args: 00346 // unsigned char* str ---binary-format number, e.g, "0100 0111 0000 1010" 00347 // size_t n_bits --- number of bits ( >= MAX_BITS ) 00348 // 00349 // explicit: no implicit automatic conversion allowed from char* str to Bits; 00350 // Bits(const char* str) must be explicitly used for the conversion 00351 // 00352 // Bits b1(""); // b1 = 0000 0000 0000 0000 (_Nbits = MAX_BITS) 00353 // Bits b2("01 0100"); // b1 = 0000 0000 0001 0100 (_Nbits = MAX_BITS) 00354 // Bits b2("01 0100", 5); // b1 = ---- ---- ---1 0100 (_Nbits = 5) 00355 // 00356 if ((int) _Nbits > (int) MAX_BITS) 00357 // throw out_of_range("Bits::Bits"); 00358 std::cerr << "THROW ERROR: out_of_range - Bits::Bits" << std::endl; 00359 00360 _data = 0; // clear all bits initially 00361 00362 int i = 0; // bit index 00363 00364 // for (int j = strlen(str) - 1; j >= 0; j--) { 00365 for (int j = str.length() - 1; j >= 0; j--) { 00366 00367 switch (str[j]) { 00368 case '0': 00369 i++; 00370 break; 00371 case '1': // set bit[i] of _data = 1 00372 _data |= _maskbit(i); 00373 i++; 00374 break; 00375 case ' ': // skip space 00376 break; 00377 default: 00378 // throw invalid_argument("Bits::Bits"); 00379 std::cerr << "THROW ERROR: invalid_argument - Bits::Bits" << std::endl; 00380 } 00381 00382 if (i >= (int) _Nbits) { // warning ?????? 00383 std::cerr << "Warning: input data too large" << std::endl; 00384 break; 00385 } 00386 } 00387 }
Here is the call graph for this function:

Member Function Documentation
Definition at line 416 of file bits.h.
References _data, and _sanitize().
00417 { 00418 // Assignment operator 00419 // 00420 // b2 = b1; // copies _data, but not _Nbits; zeroes out unused bits 00421 // b2 = 0x14; // first does automatic conversion using Bits(0x14) 00422 // 00423 // cannot not use the compiler-synthesized default assignment operator that 00424 // copies both the _data and _Nbits member data; it is desirable to keep 00425 // _Nbits unchanged and zero out the unused high-order bits accordingly; 00426 // the idea is similar to the assignment statement that involves 00427 // automatic type conversion (e.g., between char and int) 00428 // 00429 _data = rhs._data; 00430 _sanitize(); // zero out the unused high-order bits 00431 return *this; 00432 }
Here is the call graph for this function:

| Bits::reference claraty::Bits::operator[] | ( | size_t | pos | ) | [inline] |
Definition at line 457 of file bits.h.
References reference.
00458 { 00459 // single bit element access using b[i] used as lvalue 00460 // - it sets the member data _bit_pos = pos first, and then 00461 // returns an lvalue temporay object of type "reference" not "reference&" 00462 // - the returned temporary object together with the assigment operators 00463 // Bits::reference& Bits::reference::operator=(const reference&) and 00464 // Bits::reference& Bits::reference::operator=(bool) 00465 // enables such statement as 00466 // b[i] = b[j] = 1; 00467 // After the execution, the i-th and j-th bits of b (a Bits object) are 00468 // set to 1, and the temporary "reference" object created by b[i] 00469 // dissappears. 00470 // 00471 return reference(*this, pos); 00472 }
| bool claraty::Bits::operator[] | ( | size_t | pos | ) | const [inline] |
Definition at line 475 of file bits.h.
References _data, and claraty::_maskbit().
00476 { 00477 // single bit element access using b[i] 00478 // - when returning a built-in type by value, it automatically implies 00479 // a "const" return; it returns a value not a variable; namely, 00480 // bool f() is the same as const bool f() 00481 // - the "const" return type cannot be a lvalue 00482 // 00483 return (_data & _maskbit(pos)) != 0; 00484 }
Here is the call graph for this function:

| Bits & claraty::Bits::operator<<= | ( | size_t | n | ) | [inline] |
| Bits & claraty::Bits::operator>>= | ( | size_t | n | ) | [inline] |
| Bits & claraty::Bits::set | ( | ) | [inline] |
Definition at line 569 of file bits.h.
References _data, and _sanitize().
Referenced by claraty::DIO::_verify_io_mode(), and claraty::DIO_impl::set_IO_direction().
00570 { 00571 // set every bit to 1 00572 // 00573 _data = ~static_cast<unsigned long>(0); 00574 _sanitize(); 00575 return *this; 00576 }
Here is the call graph for this function:

| Bits & claraty::Bits::set_bit | ( | size_t | pos, | |
| bool | val = true | |||
| ) | [inline] |
Definition at line 580 of file bits.h.
References _data, claraty::_maskbit(), and _Nbits.
Referenced by claraty::DIO::clr(), claraty::DIO::clr_bit(), claraty::DIO::set(), claraty::DIO::set_bit(), and claraty::DIO_impl::set_IO_direction().
00581 { 00582 // b[pos] = val; don't change other bits 00583 // default bool val = true; b[pos] = 1 (set to 1) 00584 // 00585 if (pos >= _Nbits) 00586 // throw out_of_range("Bits::set_bit"); 00587 std::cerr << "Bits WARNING: out_of_range - Bits::set_bit" << std::endl; 00588 00589 if (val) 00590 _data |= _maskbit(pos); 00591 else 00592 _data &= ~_maskbit(pos); 00593 00594 return *this; 00595 }
Here is the call graph for this function:

| Bits & claraty::Bits::set_bits | ( | size_t | pos, | |
| size_t | n | |||
| ) | [inline] |
Definition at line 598 of file bits.h.
References _data, claraty::_maskbits(), and _Nbits.
Referenced by claraty::DIO::clr(), claraty::DIO::DIO(), and claraty::DIO::set().
00599 { 00600 // extension from STL bitset 00601 // b[pos] = ... = b[pos+n-1] = 1; don't change other bits 00602 // 00603 if (pos + n > _Nbits) // + has precedence over >= 00604 // throw out_of_range("Bits::set_bits"); 00605 std::cerr << "Bits WARNING: out_of_range - Bits::set_bits" << std::endl; 00606 00607 _data |= _maskbits(pos, n); 00608 00609 return *this; 00610 }
Here is the call graph for this function:

| Bits & claraty::Bits::clr | ( | ) | [inline] |
Definition at line 613 of file bits.h.
References _data.
Referenced by claraty::DIO_impl::set_IO_direction().
00614 { 00615 // set every bit to 0 (clear all bits) 00616 // 00617 _data = 0; 00618 return *this; 00619 }
| Bits & claraty::Bits::clr_bit | ( | size_t | pos | ) | [inline] |
Definition at line 622 of file bits.h.
References _data, claraty::_maskbit(), and _Nbits.
00623 { 00624 // b[pos] = 0 (clear one bit) 00625 // 00626 if (pos >= _Nbits) 00627 // throw out_of_range("Bits::clr_bit"); 00628 std::cerr << "Bits WARNING: out_of_range - Bits::clr_bit" << std::endl; 00629 00630 _data &= ~_maskbit(pos); 00631 00632 return *this; 00633 }
Here is the call graph for this function:

| Bits & claraty::Bits::clr_bits | ( | size_t | pos, | |
| size_t | n | |||
| ) | [inline] |
Definition at line 636 of file bits.h.
References _data, claraty::_maskbits(), and _Nbits.
Referenced by claraty::DIO::clr(), and claraty::DIO::set().
00637 { 00638 // extension from STL bitset 00639 // b[pos] = ... = b[pos+n-1] = 0; don't change other bits 00640 // 00641 if (pos + n > _Nbits) // + has precedence over >= 00642 // throw out_of_range("Bits::clr_bits"); 00643 std::cerr << "Bits WARNING: out_of_range - Bits::clr_bits" << std::endl; 00644 00645 _data &= ~_maskbits(pos, n); 00646 00647 return *this; 00648 }
Here is the call graph for this function:

| Bits & claraty::Bits::toggle | ( | ) | [inline] |
| Bits & claraty::Bits::toggle_bit | ( | size_t | pos | ) | [inline] |
Definition at line 662 of file bits.h.
References _data, claraty::_maskbit(), and _Nbits.
Referenced by claraty::DIO::toggle(), and claraty::DIO::toggle_bit().
00663 { 00664 // change the value of b[pos] 00665 // 00666 if (pos >= _Nbits) 00667 // throw out_of_range("Bits::toggle_bit"); 00668 std::cerr << "Bits WARNING: out_of_range - Bits::toggle_bit" << std::endl; 00669 00670 _data ^= _maskbit(pos); 00671 return *this; 00672 }
Here is the call graph for this function:

| Bits & claraty::Bits::unchecked_set | ( | unsigned long | data | ) | [inline] |
Definition at line 675 of file bits.h.
References _data.
Referenced by claraty::DIO::set(), and claraty::DIO_impl::set_mask().
00676 { 00677 _data = data; // user must ensure data is sanitized 00678 // _sanitize(); // does not do data sanity check 00679 00680 return *this; 00681 }
| Bits claraty::Bits::operator<< | ( | size_t | n | ) | const [inline] |
Definition at line 784 of file bits.h.
References Bits().
00785 { 00786 // for return (b1<<3) and also for b2 = b1<<3; b1 unchanged 00787 // make a left-shifted copy without changing the original object (*this) 00788 // e.g., the statement b2 = b1 << 3 should not change b1. 00789 // returns the updated object by value (Bits), not by reference (Bits&) 00790 // 00791 return Bits(*this) <<= n; 00792 }
Here is the call graph for this function:

| Bits claraty::Bits::operator>> | ( | size_t | n | ) | const [inline] |
| Bits claraty::Bits::operator~ | ( | ) | const [inline] |
| bool claraty::Bits::operator== | ( | const Bits & | rhs | ) | const [inline] |
| bool claraty::Bits::operator!= | ( | const Bits & | rhs | ) | const [inline] |
| bool claraty::Bits::test | ( | size_t | pos | ) | const [inline] |
Definition at line 742 of file bits.h.
References _data, claraty::_maskbit(), and _Nbits.
Referenced by get_bit(), is_set(), claraty::DIO::test(), and to_str().
00743 { 00744 // true if b[pos] is 1 00745 // 00746 if (pos >= _Nbits) 00747 // throw out_of_range("Bits::test"); 00748 std::cerr << "THROW ERROR: out_of_range - Bits::test" << std::endl; 00749 00750 return (_data & _maskbit(pos)) != 0; 00751 }
Here is the call graph for this function:

| bool claraty::Bits::is_set | ( | size_t | pos | ) | const [inline] |
| bool claraty::Bits::get_bit | ( | size_t | pos | ) | const [inline] |
| bool claraty::Bits::any | ( | ) | const [inline] |
| bool claraty::Bits::none | ( | ) | const [inline] |
| size_t claraty::Bits::size | ( | ) | const [inline] |
| unsigned long claraty::Bits::to_ulong | ( | ) | const [inline] |
Definition at line 703 of file bits.h.
References _data.
Referenced by claraty::DIO::get_mask(), claraty::DIO::operator unsigned long(), claraty::DIO::output(), claraty::CDIO::output(), claraty::DIO::set(), and claraty::CDIO::set().
00704 { 00705 // conversion to unsigned long 00706 // 00707 // unsigned long x = b1.to_ulong(); 00708 // unsigned short y = b1.to_ulong(); 00709 // unsigned char z = b1.to_ulong(); 00710 // BYTE z = b1.to_ulong(); // BYTE = unsigned char 00711 // 00712 return _data; 00713 }
| string claraty::Bits::to_str | ( | ) | const |
Definition at line 34 of file bits.cc.
References _data, _Nbits, and test().
Referenced by claraty::operator<<(), print(), claraty::DIO_impl::to_str(), and claraty::CDIO::to_str().
00035 { 00036 // Print out the bit pattern of the Bits object 00037 // 00038 char buffer[128], *output; 00039 output = new char[256]; // for contiguous printing on VxWorks 00040 00041 sprintf(buffer, "[%2d] {", static_cast<unsigned int>(_Nbits)); 00042 strcpy(output, buffer); 00043 00044 00045 // Convert the Bits to hex ascii 00046 // 00047 int n_bytes = (_Nbits + 7)/8; // number of bytes 00048 for (int i = n_bytes - 1; i >= 0; --i) { 00049 sprintf(buffer, "%2x ", static_cast<unsigned int>((_data >> i*8) & 0xff)); 00050 strcat(output, buffer); 00051 } 00052 strcat(output, "} "); 00053 00054 // Print the bit pattern --00 0110 0010 ... 00055 // 00056 int n = (_Nbits + 3) / 4 * 4; // total number of bits in 4-bit period 00057 int j=0; // buffer index for buffer[j]; 00058 00059 for (size_t i = n - 1; i >= 0; i--) { // i = bit index for b[i] 00060 if (i >= _Nbits) { 00061 buffer[j++] = '-'; 00062 continue; 00063 } 00064 00065 // if (_unchecked_test(i)) 00066 if (test(i)) 00067 buffer[j++] = '1'; 00068 else 00069 buffer[j++] = '0'; 00070 00071 if ( (i % 4) == 0 && i != 0) // == and != have precedence over && 00072 buffer[j++] = ' '; 00073 } 00074 buffer[j] = 0; // null character ('\0' = 0) 00075 00076 strcat(output, buffer); 00077 00078 string s(output); 00079 delete [] output; 00080 00081 return s; 00082 }
Here is the call graph for this function:

| void claraty::Bits::print | ( | const char * | msg = "" |
) | const |
Print function.
- Parameters:
-
[in] msg
Definition at line 90 of file bits.cc.
References to_str().
Referenced by claraty::DIO_impl::print(), claraty::CDIO::print(), and claraty::DIO_sim::set_hw_bits().
00091 { 00092 cout << msg << "\n " << to_str().c_str() << endl; // c_str() will not be 00093 // needed in the future 00094 }
Here is the call graph for this function:

| void claraty::Bits::_sanitize | ( | ) | [inline, private] |
Definition at line 814 of file bits.h.
References _data, _Nbits, and claraty::MAX_BITS.
Referenced by Bits(), operator<<=(), operator=(), operator>>=(), operator^=(), operator|=(), set(), and toggle().
00815 { 00816 // zero out the unused high-order bits 00817 // 00818 if (_Nbits != MAX_BITS) 00819 _data &= ~( ~static_cast<unsigned long>(0) << _Nbits ); 00820 }
Friends And Related Function Documentation
friend class reference [friend] |
Member Data Documentation
unsigned long claraty::Bits::_data [private] |
Definition at line 153 of file bits.h.
Referenced by _sanitize(), any(), Bits(), clr(), clr_bit(), clr_bits(), none(), operator &=(), operator!=(), operator<<=(), operator=(), operator==(), operator>>=(), operator[](), operator^=(), operator|=(), set(), set_bit(), set_bits(), test(), to_str(), to_ulong(), toggle(), toggle_bit(), and unchecked_set().
size_t claraty::Bits::_Nbits [private] |
Definition at line 154 of file bits.h.
Referenced by _sanitize(), Bits(), clr_bit(), clr_bits(), set_bit(), set_bits(), size(), test(), to_str(), and toggle_bit().
The documentation for this class was generated from the following files:








