//===========================================================================// // File: schain.hh // // Project: MUGNA Brick: Connection engine // // Contents: Interface definition for safe chains // //---------------------------------------------------------------------------// // Date Who Modification // // -------- --- ---------------------------------------------------------- // // 09/29/94 ECH Initial coding. // // 11/01/94 JMA Made compatible with SGI CC, merged with iterator // // 11/03/94 ECH Made compatible with BC4.0 // // 11/05/94 JMA Made compatible with GNU C++ // // 12/12/94 ECH Changed release mechanism // // 06/07/96 ECH Implemented socket based remove functionality // //---------------------------------------------------------------------------// // Copyright (C) 1994-1995, Virtual World Entertainment, Inc. // // PROPRIETARY AND CONFIDENTIAL // //===========================================================================// #pragma once #include "Stuff.hpp" #include "SafeSocket.hpp" #include "MemoryBlock.hpp" namespace Stuff { //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SafeChainLink ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class SafeChainLink: public Link { public: friend class SafeChain; friend class SafeChainIterator; static void InitializeClass(unsigned block_count); static void TerminateClass(); public: void TestInstance(); private: ~SafeChainLink(); SafeChainLink( SafeChain *chain, Plug *plug, SafeChainLink *nextSafeChainLink, SafeChainLink *prevSafeChainLink ); SafeChainLink *m_nextSafeChainLink; SafeChainLink *m_prevSafeChainLink; private: static MemoryBlock *s_AllocatedMemory; void* operator new(size_t) {return s_AllocatedMemory->New();} void operator delete(void *where) {s_AllocatedMemory->Delete(where);} }; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SafeChain ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class _declspec(novtable) SafeChain: public SafeSocket { friend class SafeChainLink; friend class SafeChainIterator; public: // //----------------------------------------------------------------------- //----------------------------------------------------------------------- // Public interface //----------------------------------------------------------------------- //----------------------------------------------------------------------- // explicit SafeChain(void *node=NULL); ~SafeChain(); void TestInstance(); static void TestClass(); static void ProfileClass(); // //----------------------------------------------------------------------- // DeletePlugs - For each plug in the socket, the routine unregisters it // and then deletes it. //----------------------------------------------------------------------- // void AddPlug(Plug *plug); // //----------------------------------------------------------------------- // RemovePlug - Remove a plug from this socket, untyped access. //----------------------------------------------------------------------- // void DeletePlugs(); // //----------------------------------------------------------------------- // IsPlugMember - Determine if the plug is a member of this socket. //----------------------------------------------------------------------- // void* GetNthItem(unsigned index); // //----------------------------------------------------------------------- // IsEmpty - Returns true if the socket contains no plugs. //----------------------------------------------------------------------- // unsigned GetSize(); bool IsEmpty(); private: // //----------------------------------------------------------------------- // Private utilities //----------------------------------------------------------------------- // SafeChainLink* InsertSafeChainLink( Plug *plug, SafeChainLink *link ); // //----------------------------------------------------------------------- // Private data //----------------------------------------------------------------------- // SafeChainLink *m_head; SafeChainLink *m_tail; }; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SafeChainOf ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ template class SafeChainOf: public SafeChain { public: // //-------------------------------------------------------------------- //-------------------------------------------------------------------- // Public interface //-------------------------------------------------------------------- //-------------------------------------------------------------------- // explicit SafeChainOf(void *node=NULL) {} // //-------------------------------------------------------------------- // Socket methods (see Socket for full listing) //-------------------------------------------------------------------- // void Add(T plug) {SafeChain::AddPlug(Cast_Pointer(Plug*, plug));} void Remove(T plug) {SafeChain::RemovePlug(Cast_Pointer(Plug*, plug));} }; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SafeChainIterator ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class _declspec(novtable) SafeChainIterator: public SafeIterator { public: // //-------------------------------------------------------------------- //-------------------------------------------------------------------- // Public interface //-------------------------------------------------------------------- //-------------------------------------------------------------------- // // //-------------------------------------------------------------------- // Constructors, Destructor and testing //-------------------------------------------------------------------- // SafeChainIterator( SafeChain *chain, bool move_next_on_remove ); SafeChainIterator(const SafeChainIterator &iterator); ~SafeChainIterator(); void TestInstance() const; // //-------------------------------------------------------------------- // Iterator methods (see Iterator for full listing) //-------------------------------------------------------------------- // Iterator& First(); Iterator& Last(); Iterator& Next(); Iterator& Previous(); void* ReadAndNextItem(); void* ReadAndPreviousItem(); void* GetCurrentItem(); void* GetNthItem(unsigned index); void Remove(); void InsertPlug(Plug*); protected: // //-------------------------------------------------------------------- //-------------------------------------------------------------------- // Private interface //-------------------------------------------------------------------- //-------------------------------------------------------------------- // void ReceiveMemo( IteratorMemo memo, void *content ); // //-------------------------------------------------------------------- // Private data //-------------------------------------------------------------------- // SafeChainLink *m_currentLink; bool m_moveNextOnRemove; }; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SafeChainIteratorOf ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ template class SafeChainIteratorOf: public SafeChainIterator { public: // //-------------------------------------------------------------------- //-------------------------------------------------------------------- // Public interface //-------------------------------------------------------------------- //-------------------------------------------------------------------- // // //-------------------------------------------------------------------- // Constructors and Destructor //-------------------------------------------------------------------- // SafeChainIteratorOf( SafeChainOf *chain, bool move_next_on_remove=true ): SafeChainIterator(chain, move_next_on_remove) {} SafeChainIteratorOf(const SafeChainIteratorOf &iterator): SafeChainIterator(iterator) {} Iterator* MakeClone() {return new SafeChainIteratorOf(*this);} // //-------------------------------------------------------------------- // Iterator methods (see Iterator for full listing) //-------------------------------------------------------------------- // T ReadAndNext() {return (T)SafeChainIterator::ReadAndNextItem();} T ReadAndPrevious() {return (T)SafeChainIterator::ReadAndPreviousItem();} T GetCurrent() {return (T)SafeChainIterator::GetCurrentItem();} T GetNth(unsigned index) {return (T)SafeChainIterator::GetNthItem(index);} void Insert(T plug) {SafeChainIterator::InsertPlug(Cast_Object(Plug*,plug));} T operator[](unsigned index) {return GetNth(index);} operator T() {return SafeChainIterator::GetCurrent();} }; }