swkey.h

00001 /******************************************************************************
00002  *  swkey.h     - code for base class 'swkey'.  swkey is the basis for all
00003  *                              types of keys for indexing into modules (e.g. verse, word,
00004  *                              place, etc.)
00005  *
00006  * $Id: swkey.h 1958 2006-08-22 00:15:10Z scribe $
00007  *
00008  * Copyright 1998 CrossWire Bible Society (http://www.crosswire.org)
00009  *      CrossWire Bible Society
00010  *      P. O. Box 2528
00011  *      Tempe, AZ  85280-2528
00012  *
00013  * This program is free software; you can redistribute it and/or modify it
00014  * under the terms of the GNU General Public License as published by the
00015  * Free Software Foundation version 2.
00016  *
00017  * This program is distributed in the hope that it will be useful, but
00018  * WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  * General Public License for more details.
00021  *
00022  */
00023 
00024 #ifndef SWKEY_H
00025 #define SWKEY_H
00026 
00027 #include <swobject.h>
00028 
00029 #include <defs.h>
00030 
00031 SWORD_NAMESPACE_START
00032 
00033 #define KEYERR_OUTOFBOUNDS 1
00034 
00035 #define SWKEY_OPERATORS \
00036   SWKey &operator =(const char *ikey) { setText(ikey); return *this; } \
00037   SWKey &operator =(const SWKey &ikey) { copyFrom(ikey); return *this; } \
00038   SWKey &operator =(SW_POSITION pos) { setPosition(pos); return *this; } \
00039   operator const char *() const { return getText(); } \
00040   bool operator ==(const SWKey &ikey) { return equals(ikey); } \
00041   bool operator !=(const SWKey &ikey) { return !equals(ikey); } \
00042   virtual bool operator >(const SWKey &ikey) { return (compare(ikey) > 0); } \
00043   virtual bool operator <(const SWKey &ikey) { return (compare(ikey) < 0); } \
00044   virtual bool operator >=(const SWKey &ikey) { return (compare(ikey) > -1); }  \
00045   virtual bool operator <=(const SWKey &ikey) { return (compare(ikey) < 1); } \
00046   SWKey &operator -=(int steps) { decrement(steps); return *this; } \
00047   SWKey &operator +=(int steps) { increment(steps); return *this; } \
00048   SWKey &operator++(int) { return *this += 1; } \
00049   SWKey &operator--(int) { return *this -= 1; }
00050 
00051 
00054 class SW_POSITION {
00055         char pos;
00056 public:
00057         SW_POSITION(char ipos) { pos = ipos; }
00058         operator char() { return pos; }
00059 };
00060 
00061 #define POS_TOP ((char)1)
00062 #define POS_BOTTOM ((char)2)
00063 
00064 #define TOP SW_POSITION(POS_TOP)
00065 #define BOTTOM SW_POSITION(POS_BOTTOM)
00066 
00071 class SWDLLEXPORT SWKey : public SWObject {
00072         long index;
00073         static SWClass classdef;
00074         void init();
00075 
00076 protected:
00077         char *keytext;
00078         mutable char *rangeText;
00079         mutable bool boundSet;
00080         char persist;
00081         char error;
00082 
00083 public:
00084 
00085         // misc pointer for whatever
00086         void *userData;
00087 
00093         SWKey(const char *ikey = 0);
00094 
00098         SWKey(SWKey const &k);
00099 
00102         virtual ~SWKey();
00103 
00108         virtual SWKey *clone() const;
00109 
00114         char Persist() const;
00115 
00121         char Persist(signed char ipersist);
00122 
00126         virtual char Error();
00127 
00131         virtual void setText(const char *ikey);
00132 
00136         virtual void copyFrom(const SWKey &ikey);
00137 
00140         virtual const char *getText() const;
00141         virtual const char *getShortText() const { return getText(); }
00142         virtual const char *getRangeText() const;
00143         virtual bool isBoundSet() const { return boundSet; }
00144 
00151         virtual int compare(const SWKey &ikey);
00152 
00157         virtual bool equals(const SWKey &ikey) { return !compare(ikey); }
00158 
00159         virtual void setPosition(SW_POSITION);
00160 
00165         virtual void decrement(int steps = 1);
00166 
00171         virtual void increment(int steps = 1);
00172 
00175         char Traversable() { return (isTraversable()) ? 1:0; }
00176 
00179         virtual bool isTraversable() const { return false; }
00180 
00202         virtual long Index() const { return index; }
00203 
00206         virtual long Index(long iindex) { index = iindex; return index; }
00207 
00208         SWKEY_OPERATORS
00209 
00210         };
00211 
00212 SWORD_NAMESPACE_END
00213 #endif