arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

Keys.h

Go to the documentation of this file.
00001 //------------------------------------------------------------------------
00002 //
00003 //   Joe Kniss
00004 //     3-20-03
00005 //                   ________    ____   ___ 
00006 //                  |        \  /    | /  /
00007 //                  +---+     \/     |/  /
00008 //                  +--+|  |\    /|     < 
00009 //                  |  ||  | \  / |  |\  \ 
00010 //                  |      |  \/  |  | \  \ 
00011 //                   \_____|      |__|  \__\
00012 //                       Copyright  2003 
00013 //                      Joe Michael Kniss
00014 //                   <<< jmk@cs.utah.edu >>>
00015 //               "All Your Base are Belong to Us"
00016 //-------------------------------------------------------------------------
00017 
00018 ///Keys.h
00019 
00020 /// things you need to know about to play.
00021 
00022 #ifndef __GRINDER_KEYS_DOT_H
00023 #define __GRINDER_KEYS_DOT_H
00024 
00025 #include <simBase/simBase.h>
00026 #include <string>
00027 #include <map>
00028 #include <limits>
00029 #include <mathGutz.h>
00030 #include <smartptr.h>
00031 
00032 /// key "name" type for grinder,
00033 /// the type of object used for indexing into a
00034 /// keyMap
00035 typedef std::string GKeyType;
00036 
00037 /// the key map compare
00038 struct __KeyMapCmp__
00039 {
00040    bool operator()(const GKeyType &k1 , const GKeyType &k2) const
00041    {
00042       return (k1 < k1);
00043    }
00044 };
00045 
00046 /// forward decl
00047 struct KeyPair;
00048 
00049 ///////////////////////////////////////
00050 /// a single key
00051 ///   use a std::numeric_limits<double>::quiet_NaN()
00052 ///   to indicate that a value is unused. also declared
00053 ///   in Key. Not really designed for speed :(
00054 class Key : 
00055    public SimBase, 
00056    public gutz::Counted 
00057 {
00058 public:
00059    /// the key map
00060    typedef gutz::SmartPtr<Key> KeySP;
00061    typedef std::map<GKeyType, KeySP, __KeyMapCmp__> KeySPMap;
00062    typedef KeySPMap::iterator                       KeySPMapIter;
00063 
00064    Key();
00065    Key(const std::string &name, int type, const KeyPair *subKeys=0, int nSubKeys=0);
00066    virtual ~Key();
00067 
00068    /// the key's name isn't the same as it's index into 
00069    ///   the key map.
00070    std::string getName() const           {return _name;}
00071    void        setName(std::string name) {_name = name;}
00072 
00073    /// text, may not always be valid
00074    std::string getText() const           {return _text;}
00075    void        setText(std::string text) {_text = text;}
00076 
00077    /// feel free to invent your own types, start after
00078    ///  KEY_LAST
00079    enum KEY_TYPES {
00080       UNKNOWN   = 0,
00081       DEFINE    = 1<<0,
00082       VALUE     = 1<<1,
00083       VARIABLE  = 1<<2,
00084       CONSTANT  = 1<<3,
00085       FUNCTION  = 1<<4,
00086       SCOPE     = 1<<5,
00087       KEY_LAST  = 1<<6
00088    };
00089    
00090    int  getType() const   {return _type;}
00091    void setType(int type) {_type = type;}
00092 
00093    /// data type for double 4 vectors
00094    typedef gutz::vec4d DVEC_TYPE; 
00095    const DVEC_TYPE DVEC_UNUSED;  /// for comparisons
00096 
00097    virtual DVEC_TYPE getDVec() const       {return _dv;}
00098    virtual void      setDVec(DVEC_TYPE vec){_dv = vec; }
00099 
00100    /// data type for integral 4 vectors
00101    typedef gutz::vec4i IVEC_TYPE;
00102    const IVEC_TYPE  IVEC_UNUSED; /// for comparisons
00103 
00104    virtual IVEC_TYPE getIVec() const       {return _iv;}
00105    virtual void      setIVec(IVEC_TYPE vec){_iv = vec; }
00106 
00107    /// these accessors will be null if there was an error,
00108    /// or un-found key, ie. retKey.isNull() == true
00109    /// a key may have sub keys, get them...
00110    KeySPMap getAllKeys() const {return _keys;}
00111    /// get a specific key, returns Null key if not found
00112    KeySP    getKey(const GKeyType kn) const;
00113    /// set a specific key, returns Null if key not found
00114    KeySP    setKey(const GKeyType kn, const KeySP key);
00115    /// add a key
00116    KeySP    addKey(const GKeyType kn, const KeySP key);
00117    KeySP    addKey(const KeyPair kp);
00118    /// delete a key, returns Null if key not found
00119    KeySP    delKey(const GKeyType kn);
00120    /// test if a key exists
00121    bool     hasKey(const GKeyType kn) const;
00122 
00123    /// operators, used for felt and sudo compiling
00124    int   getOp() const { return _op; }
00125    void  setOp(int op) { _op = op; }
00126 
00127    /// size of subKey map
00128    int   size() { return int(_keys.size()); }
00129 
00130 protected:
00131    std::string _name;
00132    std::string _text;
00133    int         _type;
00134    int         _op;
00135    KeySPMap    _keys;
00136 
00137    DVEC_TYPE    _dv;
00138    IVEC_TYPE    _iv;
00139    
00140 };
00141 
00142 /// declared externally for convenience
00143 typedef Key::KeySP         KeySP;
00144 typedef Key::KeySPMap      KeySPMap;
00145 typedef Key::KeySPMapIter  KeySPMapIter;
00146 
00147 /// key-name key pairs
00148 struct KeyPair {
00149    KeyPair(const GKeyType keyName, const Key theKey)
00150       : kn(keyName), key(theKey) {}
00151    GKeyType kn;  /// key name
00152    Key      key; /// key
00153 };
00154 
00155 #endif
00156 

Send questions, comments, and bug reports to:
jmk