From: "Christopher Nelson" To: Subject: Re: bison variable access Date: Wed, 7 Jul 1999 20:17:03 -0600 Message-ID: <01bec8e7$f8200080$LocalHost@thendren> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0042_01BEC8B5.AD859080" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 4.71.1712.3 X-MimeOLE: Produced By Microsoft MimeOLE V4.71.1712.3 Reply-To: djgpp AT delorie DOT com This is a multi-part message in MIME format. ------=_NextPart_000_0042_01BEC8B5.AD859080 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit >Now you're getting complicated. The only way I can think of is to use some "Variant" data >type. That would consist of a giant union of every possible datatype, and a >type-specifier. Every time you wanted to make an assignment, or read a value, you would >have to go through a giant switch and choose the right union member. > I can think of a neat way of doing it in C++ using a class and some overloaded >operators. It would still require the union, but now all you would have to do is overload >the '=' operator for every datatype, and just make the assignment into that union member. i do, in fact, just have this particular class built. the only members that it does not support are double, long double, unsigned long long, and long long. this is because my implementation does not require them, so i didn't want to waste the space. it is attatched, if you like. -={C}=- ------=_NextPart_000_0042_01BEC8B5.AD859080 Content-Type: application/octet-stream; name="any.h" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="any.h" /************************************************************************= **** =DA=BF =DA=BF =B3=C0=B4 =C3=D9=B3 =B3 =B3 =B3 =B3 =C2 =C2 =B3 =B3 =DA=C4=C4=C4=BF =DA=C4=C4=BF =B3=DA=D9 =B3 =DA=C4=BF =B3 =C3=C4=C4=C4=B4 =B3 =C3=B4 =C0=C4=BF =B3 =B3 =DA=C4=D9 =B3 =B3 =B3 =B3=C0=BF =C0=C4=D9 =C0=C4=D9 =C1 =C1 =C0=C4=C4=D9 =C1 =C1 from the Wack Libraries (C)opyright 1999 Christopher Nelson (All Rights Reserved) = *************************************************************************= ***/ #ifdef __cplusplus #ifndef __ANY_VALUE_CLASS__ #define __ANY_VALUE_CLASS__ #pragma interface "any.h" #define SCHAR char #define UCHAR unsigned char #define SSHRT short #define USHRT unsigned short #define SINT int #define UINT unsigned int #define SLONG long #define ULONG unsigned long #define FLOAT float #define STRING char * #define SYMBOL void * #define TYPE_BOOL 0 #define TYPE_SCHAR 1 #define TYPE_UCHAR 2 #define TYPE_SSHRT 3 #define TYPE_USHRT 4 #define TYPE_SINT 5 #define TYPE_UINT 6 #define TYPE_SLONG 7 #define TYPE_ULONG 8 #define TYPE_FLOAT 9 #define TYPE_STRING 10 #define TYPE_SYMBOL 11 #define TYPE_USER (1<<8) //if the type is above this number it was //generated by the user. typedef union tag_Value { bool _bool; SCHAR _schar; UCHAR _uchar; SSHRT _sshrt; USHRT _ushrt; SINT _sint; UINT _uint; SLONG _slong; ULONG _ulong; FLOAT _float; STRING _string; SYMBOL _symbol; } unionValue; class Any { public: UINT type; unionValue value; Any():type(0) { value._ulong=3D0; }; ~Any() {}; int friend operator=3D=3D(Any x, Any y) { if (x.type!=3Dy.type) return(0); switch(x.type) { case TYPE_BOOL: return(x.value._bool=3D=3Dy.value._bool); case TYPE_SCHAR: return(x.value._schar=3D=3Dy.value._schar); case TYPE_UCHAR: return(x.value._uchar=3D=3Dy.value._uchar); case TYPE_SINT: return(x.value._sint=3D=3Dy.value._sint); case TYPE_UINT: return(x.value._uint=3D=3Dy.value._uint); case TYPE_SSHRT: return(x.value._sshrt=3D=3Dy.value._sshrt); case TYPE_USHRT: return(x.value._ushrt=3D=3Dy.value._ushrt); case TYPE_SLONG: return(x.value._slong=3D=3Dy.value._slong); case TYPE_ULONG: return(x.value._ulong=3D=3Dy.value._ulong); case TYPE_FLOAT: return(x.value._float=3D=3Dy.value._float); case TYPE_STRING: return(strcmp(x.value._string, y.value._string)); case TYPE_SYMBOL: return(0); // <-- fixme!! } } void operator=3D(bool _value) { type =3D TYPE_BOOL; value._bool =3D = _value; }; void operator=3D(SCHAR _value) { type =3D TYPE_SCHAR; value._schar =3D = _value; }; void operator=3D(UCHAR _value) { type =3D TYPE_UCHAR; value._uchar =3D = _value; }; void operator=3D(SINT _value) { type =3D TYPE_SINT; value._sint =3D = _value; }; void operator=3D(UINT _value) { type =3D TYPE_UINT; value._uint =3D = _value; }; void operator=3D(SSHRT _value) { type =3D TYPE_SSHRT; value._sshrt =3D = _value; }; void operator=3D(USHRT _value) { type =3D TYPE_USHRT; value._ushrt =3D = _value; }; void operator=3D(SLONG _value) { type =3D TYPE_SLONG; value._slong =3D = _value; }; void operator=3D(ULONG _value) { type =3D TYPE_ULONG; value._ulong =3D = _value; }; void operator=3D(FLOAT _value) { type =3D TYPE_FLOAT; value._float =3D = _value; }; void operator=3D(STRING _value) { type =3D TYPE_STRING; value._string = =3D _value; }; void operator=3D(SYMBOL _value) { type =3D TYPE_SYMBOL; value._symbol = =3D _value; }; }; #endif //__ANY_VALUE_CLASS__ #endif //__cplusplus // ****< Timestamp: Wed Jul 7 16:04:20 1999 ------=_NextPart_000_0042_01BEC8B5.AD859080 Content-Type: application/octet-stream; name="any.cc" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="any.cc" #pragma implementation "any.h" #include "any.h" ------=_NextPart_000_0042_01BEC8B5.AD859080--