Mercurial > ~dholland > hg > ag > index.cgi
diff anagram/support/register.h @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/anagram/support/register.h Sat Dec 22 17:52:45 2007 -0500 @@ -0,0 +1,133 @@ +/* + * AnaGram, A System for Syntax Directed Programming + * Copyright 1993-2002 Parsifal Software. All Rights Reserved. + * See the file COPYING for license and usage terms. + * + * register.h + */ + +#ifndef REGISTER_H +#define REGISTER_H + +#include "agbaltree.h" +#include "agstack.h" +#include "assert.h" + + +template <class Descriptor> +class ObjectRegister { +private: + //Don't want this to happen + void *operator new(size_t sz) { + assert(0); +#ifdef __GNUC__ + return ::new char[sz]; +#else + return 0; +#endif + } + +protected: + int index; + Descriptor *descriptor; + static AgStack<Descriptor> descriptorList; + ObjectRegister(const Descriptor &d); + ObjectRegister(const ObjectRegister<Descriptor> &d); + +public: + // Constructors + //ObjectRegister() : index(0), descriptor(0) {} + ObjectRegister(); + ObjectRegister(int x); + ObjectRegister<Descriptor> &operator = (const ObjectRegister<Descriptor> &d); + ObjectRegister<Descriptor> &operator = (int x); + // Destructor + ~ObjectRegister() {} + + // Register management functions + static void reset(); + int next(); + inline int isNull() { return index == 0; } + inline int isNotNull() { return index != 0; } + static unsigned count(); + + // Object operators + Descriptor *operator -> () const { return descriptor; } + operator Descriptor & () const { return *descriptor; } + operator int() const { return index; } + + int operator == (const ObjectRegister<Descriptor> &d) const { + return index == d.index; + } + int operator != (const ObjectRegister<Descriptor> &d) const { + return index != d.index; + } + int operator < (const ObjectRegister<Descriptor> &d) const { + return index < d.index; + } + int operator > (const ObjectRegister<Descriptor> &d) const { + return index > d.index; + } + int operator <= (const ObjectRegister<Descriptor> &d) const { + return index <= d.index; + } + int operator >= (const ObjectRegister<Descriptor> &d) const { + return index >= d.index; + } +}; + +template <class Descriptor> +class KeyedObjectRegister : public ObjectRegister<Descriptor> { +protected: + static const Descriptor *testObject; + + KeyedObjectRegister(); + +public: + class Tree; + friend class Tree; + class Tree : public AgBalancedTree<int> { + int compare(const int &x, const int &y) const; + }; + + static Tree tree; + + KeyedObjectRegister(const Descriptor &d); + KeyedObjectRegister(unsigned x); + static void reset(); +}; + +template <class Register> +class Each : public Register { +public: + //Each(int x = Register::first) { + // index = x; + // descriptor = (unsigned) index < descriptorList.size() ? + // &descriptorList[index] : 0; + //} + + //void restart(int x = Register::first) { + // index = x; + // descriptor = (unsigned) index < descriptorList.size() ? + // &descriptorList[index] : 0; + //} + + int loopNotFinished() { + return (unsigned) Each<Register>::index + < + Each<Register>::descriptorList.size(); + } + + //void getNext() { + // index++; + // descriptor = (unsigned) index < descriptorList.size() ? + // &descriptorList[index] : 0; + //} + + Each(int x = Register::first); + void restart(int x = Register::first); + void getNext(); +}; + + +#endif /* REGISTER_H */