home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
listings
/
v_09_04
/
9n04083a
< prev
next >
Wrap
Text File
|
1991-02-18
|
2KB
|
74 lines
/********************************************************************/
/* Byte classes. Copyright by Joe Schell 1989. */
/********************************************************************/
#ifndef CLASS_byte
#define CLASS_byte
#include <limits.h> // Maximum values UCHAR_MAX and UINT_MAX.
#include <stdlib.h> // prototype exit() and EXIT_FAILURE.
#include <iostream.h>
#include <form.h>
/*------------------------------------------------------------------*/
/* byte Handle a byte. */
/*------------------------------------------------------------------*/
class byte
{
public:
byte() { c = 0; }
byte(int &i) { c = value(i); }
operator int() const { return c; }
byte operator=(int &i) { c = value(i); return *this; }
byte operator++() { c++; return *this; }
byte operator--() { c--; return *this; }
char *make_string() { return form("%2.2X", int(c));}
private:
unsigned char c; // A byte.
unsigned char value(int &i)
{
if (i > UCHAR_MAX)
{
cerr << "\nByte class: Illegal value-" << i << "\n";
exit(EXIT_FAILURE);
}
return (unsigned char)i;
}
}; // End of byte class.
/*------------------------------------------------------------------*/
/* word Handle a word */
/*------------------------------------------------------------------*/
class word
{
public:
word() { i = 0; }
word(long &x) { i = value(x); }
operator long() const { return (long)i; }
word operator++() { i++; return *this; }
word operator--() { i--; return *this; }
char *make_string() { return form("%4.4X", i); }
private:
unsigned int i; // An int.
unsigned int value(long &x)
{
if (x > UINT_MAX)
{
cerr << "\nWord class: Illegal value-" << x << "\n";
exit(EXIT_FAILURE);
}
return (unsigned int)x;
}
}; // End of word class.
#if sizeof(unsigned char) != sizeof(byte)
#error Byte class cannot be used as pointer to memory.
#endif
#if sizeof(unsigned int) != sizeof(word)
#error Word class cannot be used as pointer to memory.
#endif
#endif // #ifndef CLASS_byte