home *** CD-ROM | disk | FTP | other *** search
- #ifndef __OTLIST__
- #define __OTLIST__
- #pragma once
-
- #include <OpenTransport.h>
-
- //
- // Helper functions for the templates below
- //
- void* OTLinkNextObject(void* obj, int linkOffset);
- void* OTLinkIndexObject(void* obj, int count, int linkOffset);
-
-
- template <class ObjClass, int linkOffset>
- class OTLinkPtr
- {
- protected:
- ObjClass* fObject;
-
- public:
- //
- // static inlines for adding fudge factors, casting, etc.
- //
- static inline int GetLinkOffset() { return linkOffset; }
-
- static inline OTLink& ObjectToLink(ObjClass& obj) { return *(OTLink*) (((char*) &obj) + linkOffset); }
- static inline ObjClass& LinkToObject(OTLink& link) { return *(ObjClass*) (((char*) &link) - linkOffset); }
- static inline OTLink* ObjectToLink(ObjClass* obj) { return obj ? &ObjectToLink(*obj) : nil; }
- static inline ObjClass* LinkToObject(OTLink* link) { return link ? &LinkToObject(*link) : nil; }
-
- static inline const OTLink& ObjectToLink(const ObjClass& obj) { return *(const OTLink*) (((char*) &obj) + linkOffset); }
- static inline const ObjClass& LinkToObject(const OTLink& link) { return *(const ObjClass*) (((char*) &link) - linkOffset); }
- static inline const OTLink* ObjectToLink(const ObjClass* obj) { return obj ? &ObjectToLink(*obj) : nil; }
- static inline const ObjClass* LinkToObject(const OTLink* link) { return link ? &LinkToObject(*link) : nil; }
-
-
- //
- // Constructors
- //
- inline OTLinkPtr() : fObject(nil) {}
- inline OTLinkPtr(const OTLinkPtr& rhs) : fObject(rhs.fObject) {}
- inline OTLinkPtr(const ObjClass* rhs) const : fObject(rhs.fObject) {}
- inline OTLinkPtr(const OTLink* rhs) const : fObject(LinkToObject(rhs)) {}
- inline OTLinkPtr(ObjClass* rhs) : fObject(rhs) {}
- inline OTLinkPtr(OTLink* rhs) : fObject(LinkToObject(rhs)) {}
-
- //
- // Desctuctor -- CodeWarrior refuses to inline explicit empty desturctor
- //
- // inline ~OTLinkPtr() {}
-
- //
- // Access methods
- //
- inline OTLink* GetLinkPtr() { return ObjectToLink(fObject); }
- inline const OTLink* GetLinkPtr() const { return ObjectToLink(fObject); }
- inline OTLink& GetLinkRef() { return ObjectToLink(*fObject); }
- inline const OTLink& GetLinkRef() const { return ObjectToLink(*fObject); }
- inline ObjClass* GetObjectPtr() { return fObject; }
- inline const ObjClass* GetObjectPtr() const { return fObject; }
-
- inline ObjClass* NextObject() { return (ObjClass*) OTLinkNextObject(fObject, linkOffset); }
- inline OTLink* NextLink() { return OTLinkNext(GetLinkPtr()); }
-
- #if 0 // various attempt to trick codewarrior into generating decent code
- inline OTLinkPtr Next() { return OTLinkPtr(ObjectToLink(*fObject).fNext); }
- inline OTLinkPtr Next() { return OTLinkPtr(fObject ? ObjectToLink(*fObject).fNext : nil); }
- inline OTLinkPtr Next() { return OTLinkPtr((ObjClass*) OTLinkIndexObj((char*) fObject, linkOffset, 1)); }
- inline OTLinkPtr Next() { return OTLinkPtr((ObjClass*) OTLinkIndex<linkOffset>(fObject, 1)); }
- #else
- inline OTLinkPtr Next() { return OTLinkPtr(NextObject()); }
- #endif
-
- //
- // Standard operators
- //
- inline OTLinkPtr& operator=(const OTLinkPtr& rhs) { fObject = rhs.fObject; return *this; }
- inline OTLinkPtr& operator=(const ObjClass* rhs) { fObject = rhs; return *this; }
- inline OTLinkPtr& operator=(const OTLink* rhs) { fObject = LinkToObject(rhs); return *this; }
-
- inline OTLinkPtr& operator&() { return *this; }
- inline const OTLinkPtr& operator&() const { return *this; }
-
- inline const ObjClass& operator*() const { return *fObject; }
- inline ObjClass& operator*() { return *fObject; }
-
- inline ObjClass* operator->() { return fObject; }
- inline const ObjClass* operator->() const { return fObject; }
-
- inline ObjClass& operator[](int index) { return *(ObjClass*) OTLinkIndexObject(fObject, index, linkOffset); }
- inline const ObjClass& operator[](int index) const { return *(ObjClass*) OTLinkIndexObject(fObject, index, linkOffset); }
-
- //
- // Type coercion operators
- //
- inline operator OTLink*() { return ObjectToLink(fObject); }
- inline operator const OTLink*() const { return ObjectToLink(fObject); }
-
- inline operator ObjClass*() { return fObject; }
- inline operator const ObjClass*() const { return fObject; }
-
- inline operator void*() { return fObject; }
- inline operator const void*() const { return fObject; }
-
- inline operator bool() const { return fObject != nil; }
-
- };
-
- template <class ObjClass, int linkOffset>
- class OTLIFOOf : public OTLIFO
- {
- public:
- typedef OTLinkPtr<ObjClass, linkOffset> LinkPtr;
-
- inline OTLIFOOf() { Init(); }
-
- inline void Enqueue(LinkPtr link) { OTLIFO::Enqueue(link); }
- inline void Enqueue(ObjClass* object) { Enqueue( LinkPtr::ObjectToLink(object)); }
- inline void Enqueue(ObjClass& object) { Enqueue(&LinkPtr::ObjectToLink(object)); }
-
- inline LinkPtr Dequeue() { return LinkPtr(OTLIFO::Dequeue()); }
- inline LinkPtr StealList() { return LinkPtr(OTLIFO::StealList()); }
- };
-
- template <class ObjClass, int linkOffset>
- class OTListOf : public OTList
- {
- public:
- typedef OTLinkPtr<ObjClass, linkOffset> LinkPtr;
- typedef Boolean (*SearchProcPtr)(const void* ref, LinkPtr linkToCheck);
- typedef ObjClass* ObjPtr;
-
- inline OTListOf() { Init(); }
-
- inline void AddFirst(LinkPtr link) { OTList::AddFirst( link ); }
- inline void AddLast(LinkPtr link) { OTList::AddLast( link ); }
- inline void AddFirst(ObjClass& object) { OTList::AddFirst(&LinkPtr::ObjectToLink(object)); }
- inline void AddLast(ObjClass& object) { OTList::AddLast(&LinkPtr::ObjectToLink(object)); }
-
- inline bool Remove(LinkPtr link) { return OTList::RemoveLink(link); }
- inline bool Remove(ObjClass* obj) { return OTList::RemoveLink(LinkPtr::ObjectToLink(obj)); }
- inline bool Remove(ObjClass& obj) { return OTList::RemoveLink(&LinkPtr::ObjectToLink(obj)); }
-
- inline LinkPtr GetFirst() { return LinkPtr(OTList::GetFirst()); }
- inline LinkPtr GetLast() { return LinkPtr(OTList::GetLast()); }
- inline LinkPtr RemoveFirst() { return LinkPtr(OTList::RemoveFirst()); }
- inline LinkPtr RemoveLast() { return LinkPtr(OTList::RemoveLast()); }
-
- inline bool IsInList(LinkPtr link) { return OTList::IsInList(link); }
- inline bool IsInList(ObjClass* obj) { return OTList::IsInList(LinkPtr::ObjectToLink(obj)); }
- inline bool IsInList(ObjClass& obj) { return OTList::IsInList(&LinkPtr::ObjectToLink(obj)); }
-
- inline LinkPtr FindLink(SearchProcPtr proc, const void* ref) { return OTList::FindLink((OTListSearchProcPtr) proc, ref); }
- inline LinkPtr Remove(SearchProcPtr proc, const void* ref) { return OTList::RemoveLink(proc, ref); }
-
- inline LinkPtr GetIndexedLink(size_t index) { return OTList::GetIndexedLink(index); }
- };
-
- // Macros for declaring list/link types
-
- #define EmbeddedLinkPtr(Class, Field) OTLinkPtr<Class, offsetof(Class, Field)>
- #define EmbeddedLIFO(Class, Field) OTLIFOOf <Class, offsetof(Class, Field)>
- #define EmbeddedList(Class, Field) OTListOf <Class, offsetof(Class, Field)>
-
-
- #define DeclareEmbeddedLink(Class, Name, offset/*of(Class,f##Name) */) \
- typedef OTLinkPtr<Class, offset> Name##Ptr; \
- typedef OTLIFOOf <Class, offset> Name##LIFO; \
- typedef OTListOf <Class, offset> Name##List; \
- inline void Set##Name(Name##Ptr obj) { f##Name.fNext = obj ? &obj->f##Name : nil; } \
- inline void Set##Name(Class* obj) { f##Name.fNext = obj ? &obj->f##Name : nil; } \
- inline Name##Ptr Get##Name() { return *(Name##Ptr*) &(f##Name); }
-
- /*
- #define DeclareEmbeddedLink(Class, Name, offset) \
- typedef EmbeddedLinkPtr(Class, f##Name) Name##Ptr; \
- typedef EmbeddedLIFO( Class, f##Name) Name##LIFO; \
- typedef EmbeddedList( Class, f##Name) Name##List; \
- inline void Set##Name(Name##Ptr obj) { f##Name.fNext = obj ? &obj->f##Name : nil; } \
- inline void Set##Name(Class* obj) { f##Name.fNext = obj ? &obj->f##Name : nil; } \
- inline Name##Ptr Get##Name() { return *(Name##Ptr*) &(f##Name); }
- */
-
- #endif __OTLIST__