// -*- C++ -*- // $RCSfile: enum.h,v $ // $Revision: 1.4 $ // $Author: langer $ // $Date: 2000/10/13 20:03:09 $ /* This software was produced by NIST, an agency of the U.S. government, * and by statute is not subject to copyright in the United States. * Recipients of this software assume all responsibilities associated * with its operation, modification and maintenance. However, to * facilitate maintenance we ask that before distributing modifed * versions of this software, you first contact the authors at * oof_manager@ctcms.nist.gov. */ /* Template for enumerated type that can be read and written by name. * * Example: * typedef enum {A, B, C} Letter; // define a normal enumerated type * Enum x(A); // define a printable variable of that type * // before using the variable, define the printable versions: * Enum::name(A, "hay"); * Enum::name(B, "bee"); * Enum::name(C, "see"); * cout << x << endl; // This line prints "hay" * cin >> x; // this line reads "hay", "bee", or "see" and sets x * switch(x) { // x can be used wherever a Letter could be used. * case A: ... * } */ #ifndef ENUM_H #define ENUM_H template class Enum; #ifdef MENUGFX template class EnumVarObject; #endif #include #include "charstring.h" #include "vec.h" #ifdef MENUGFX #include "menuDef.h" #include "varobjects.h" #endif template ostream &operator<<(ostream &, const Enum&); template istream &operator>>(istream &, Enum&); template class Enum { private: ENUMTYPE x; public: Enum() : x(ENUMTYPE(0)) {} Enum(const ENUMTYPE &y) : x(y) {} virtual ~Enum() {} operator ENUMTYPE() const { return x; } static void name(const ENUMTYPE&, const CharString &nm); static Vec &names(); #if defined(sun) && !defined(__GNUG__) friend ostream &operator<<(ostream &, const Enum&); friend istream &operator>>(istream &, Enum&); #else friend ostream &operator<< <>(ostream &, const Enum&); friend istream &operator>> <>(istream &, Enum&); #endif friend bool operator==(const Enum &p, const Enum &q) { return p.x == q.x; } friend bool operator!=(const Enum &p, const Enum &q) { return p.x != q.x; } friend bool operator==(const Enum &p, const ENUMTYPE &q) { return p.x == q; } friend bool operator!=(const Enum &p, const ENUMTYPE &q) { return p.x != q; } }; template void writebinary(FILE*, const Enum&, const int n=1); template int readbinary(FILE*, Enum&, const int n=1); #ifdef MENUGFX // classes for putting Enum's into menu forms: // EVO is a base class for the templated EnumVarObject class. // The callback function can't be a template since it has no // arguments that can be used to determine the type. So it has // to invoke a virtual function through the EVO base class. class EVO : public VarObject { public: EVO(Variable *vr, Form *frm, VarObjectType typ) : VarObject(vr, frm, typ) { } virtual ~EVO() {} virtual void set() = 0; }; template class EnumVarObject : public EVO { private: Vec value; // values taken by enum, indexed by position in menu static void CB_choice(FL_OBJECT*, long); public: EnumVarObject(Variable*, Form*, VarObjectType, int, int, int, int); FL_OBJECT *choice; virtual void set(); virtual void show(); virtual void hide(); virtual CharString get(); }; #endif #include "enum.C" #endif