// Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved #ifndef __SNMPCONT_H #define __SNMPCONT_H #include #include template class SnmpList { private: CCriticalSection * criticalSection ; CList clist ; protected: public: SnmpList ( BOOL threadSafeArg = FALSE ) ; virtual ~SnmpList () ; int GetCount() const; BOOL IsEmpty() const; TYPE& GetHead(); TYPE GetHead() const; TYPE& GetTail(); TYPE GetTail() const; TYPE RemoveHead(); TYPE RemoveTail(); POSITION AddHead(ARG_TYPE newElement); POSITION AddTail(ARG_TYPE newElement); void AddHead(SnmpList* pNewList); void AddTail(SnmpList* pNewList); void RemoveAll(); POSITION GetHeadPosition() const; POSITION GetTailPosition() const; TYPE& GetNext(POSITION& rPosition); TYPE GetNext(POSITION& rPosition) const; TYPE& GetPrev(POSITION& rPosition); TYPE GetPrev(POSITION& rPosition) const; TYPE& GetAt(POSITION position); TYPE GetAt(POSITION position) const; void SetAt(POSITION pos, ARG_TYPE newElement); void RemoveAt(POSITION position); POSITION InsertBefore(POSITION position, ARG_TYPE newElement); POSITION InsertAfter(POSITION position, ARG_TYPE newElement); POSITION Find(ARG_TYPE searchValue, POSITION startAfter = NULL) const; POSITION FindIndex(int nIndex) const; } ; template SnmpList :: SnmpList ( BOOL threadSafeArg ) : criticalSection ( NULL ) { if ( threadSafeArg ) criticalSection = new CCriticalSection ; else criticalSection = NULL ; } template SnmpList :: ~SnmpList () { if ( criticalSection ) delete criticalSection ; } template int SnmpList :: GetCount() const { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; int count = clist.GetCount () ; return count ; } else { return clist.GetCount () ; } } template BOOL SnmpList :: IsEmpty() const { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; BOOL isEmpty = clist.IsEmpty () ; return isEmpty ; } else { return clist.IsEmpty () ; } } template TYPE &SnmpList :: GetHead () { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; TYPE &head = clist.GetHead () ; return head; } else { return clist.GetHead () ; } } template TYPE SnmpList :: GetHead () const { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; TYPE head = clist.GetHead () ; return head ; } else { return clist.GetHead () ; } } template TYPE &SnmpList :: GetTail() { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; TYPE &tail = clist.GetTail () ; return tail ; } else { return clist.GetTail () ; } } template TYPE SnmpList :: GetTail() const { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; TYPE tail = clist.GetTail () ; return tail ; } else { return clist.GetTail () ; } } template TYPE SnmpList :: RemoveHead() { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; TYPE head = clist.RemoveHead () ; return head ; } else { return clist.RemoveHead () ; } } template TYPE SnmpList :: RemoveTail() { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; TYPE tail = clist.RemoveTail () ; return tail ; } else { return clist.RemoveTail () ; } } template POSITION SnmpList :: AddHead(ARG_TYPE newElement) { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; POSITION position = clist.AddHead ( newElement ) ; return position ; } else { return clist.AddHead ( newElement ) ; } } template POSITION SnmpList :: AddTail(ARG_TYPE newElement) { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; POSITION position = clist.AddTail ( newElement ) ; return position ; } else { return clist.AddTail ( newElement ) ; } } template void SnmpList :: AddHead(SnmpList *pNewList) { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; clist.AddHead ( pNewList->clist ) ; } else { clist.AddHead ( pNewList->clist ) ; } } template void SnmpList :: AddTail(SnmpList *pNewList) { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; clist.AddTail ( pNewList->clist ) ; } else { clist.AddTail ( pNewList->clist ) ; } } template void SnmpList :: RemoveAll () { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; clist.RemoveAll () ; } else { clist.RemoveAll () ; } } template POSITION SnmpList :: GetHeadPosition() const { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; POSITION position = clist.GetHeadPosition () ; return position ; } else { return clist.GetHeadPosition () ; } } template POSITION SnmpList :: GetTailPosition() const { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; POSITION position = clist.GetTailPosition () ; return position ; } else { return clist.GetTailPosition () ; } } template TYPE& SnmpList :: GetNext(POSITION& rPosition) { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; TYPE &type = clist.GetNext ( rPosition ) ; return type ; } else { return clist.GetNext ( rPosition ) ; } } template TYPE SnmpList :: GetNext(POSITION& rPosition) const { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; TYPE type = clist.GetNext ( rPosition ) ; return type ; } else { return clist.GetNext ( rPosition ) ; } } template TYPE& SnmpList :: GetPrev(POSITION& rPosition) { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; TYPE &type = clist.GetPrev ( rPosition ) ; return type ; } else { return clist.GetPrev ( rPosition ) ; } } template TYPE SnmpList :: GetPrev(POSITION& rPosition) const { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; TYPE type = clist.GetPrev ( rPosition ) ; return type ; } else { return clist.GetPrev ( rPosition ) ; } } template TYPE& SnmpList :: GetAt(POSITION rPosition) { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; TYPE &type = clist.GetAt ( rPosition ) ; return type ; } else { return clist.GetAt ( rPosition ) ; } } template TYPE SnmpList :: GetAt(POSITION rPosition) const { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; TYPE type = clist.GetAt ( rPosition ) ; return type ; } else { return clist.GetAt ( rPosition ) ; } } template void SnmpList :: SetAt(POSITION pos, ARG_TYPE newElement) { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; clist.SetAt ( pos , newElement ) ; } else { clist.SetAt ( pos , newElement ) ; } } template void SnmpList :: RemoveAt(POSITION position) { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; clist.RemoveAt ( position ) ; } else { clist.RemoveAt ( position ) ; } } template POSITION SnmpList :: InsertBefore(POSITION position, ARG_TYPE newElement) { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; POSITION position = clist.InsertBefore ( position , newElement ) ; return position ; } else { return clist.InsertBefore ( position , newElement ) ; } } template POSITION SnmpList :: InsertAfter(POSITION position, ARG_TYPE newElement) { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; POSITION position = clist.InsertAfter ( position , newElement ) ; return position ; } else { return clist.InsertAfter ( position , newElement ) ; } } template POSITION SnmpList :: Find(ARG_TYPE searchValue, POSITION startAfter ) const { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; POSITION position = clist.Find ( searchValue , startAfter ) ; return position ; } else { return clist.Find ( searchValue , startAfter ) ; } } template POSITION SnmpList :: FindIndex(int nIndex) const { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; POSITION position = clist.Find ( nIndex ) ; return position ; } else { return clist.Find ( nIndex ) ; } } template class SnmpStack : public SnmpList { private: CCriticalSection * criticalSection ; protected: public: SnmpStack ( BOOL threadSafeArg = FALSE ) ; virtual ~SnmpStack () ; void Add ( ARG_TYPE type ) ; TYPE Get () ; TYPE Delete () ; } ; template SnmpStack :: SnmpStack ( BOOL threadSafeArg ) : SnmpList ( FALSE ) , criticalSection ( NULL ) { if ( threadSafeArg ) criticalSection = new CCriticalSection ; else criticalSection = NULL ; } template SnmpStack :: ~SnmpStack () { if ( criticalSection ) delete criticalSection ; } template void SnmpStack :: Add ( ARG_TYPE type ) { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; AddHead ( type ) ; } else { AddHead ( type ) ; } } template TYPE SnmpStack :: Get () { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; TYPE type = GetHead () ; return type ; } else { return GetHead () ; } } template TYPE SnmpStack :: Delete () { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; TYPE type = RemoveHead () ; return type ; } else { return RemoveHead () ; } } template class SnmpQueue : public SnmpList { private: CCriticalSection * criticalSection ; protected: public: SnmpQueue ( BOOL threadSafeArg = FALSE ) ; virtual ~SnmpQueue () ; void Add ( ARG_TYPE type ) ; TYPE Get () ; TYPE Delete () ; void Rotate () ; } ; template SnmpQueue :: SnmpQueue ( BOOL threadSafeArg ) : SnmpList ( FALSE ) , criticalSection ( NULL ) { if ( threadSafeArg ) criticalSection = new CCriticalSection ; else criticalSection = NULL ; } template SnmpQueue :: ~SnmpQueue () { if ( criticalSection ) delete criticalSection ; } template void SnmpQueue :: Add ( ARG_TYPE type ) { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; AddTail ( type ) ; } else { AddTail ( type ) ; } } template TYPE SnmpQueue :: Get () { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; TYPE type = GetHead () ; return type ; } else { return GetHead () ; } } template TYPE SnmpQueue :: Delete () { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; TYPE type = RemoveHead () ; return type ; } else { return RemoveHead () ; } } template void SnmpQueue :: Rotate () { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; TYPE type = Delete () ; Add ( type ) ; } else { TYPE type = Delete () ; Add ( type ) ; } } template class SnmpMap { private: CCriticalSection * criticalSection ; CMap cmap ; protected: public: SnmpMap ( BOOL threadSafe = FALSE ) ; virtual ~SnmpMap () ; int GetCount () const ; BOOL IsEmpty () const ; BOOL Lookup(ARG_KEY key, VALUE& rValue) const ; VALUE& operator[](ARG_KEY key) ; void SetAt(ARG_KEY key, ARG_VALUE newValue) ; BOOL RemoveKey(ARG_KEY key) ; void RemoveAll () ; POSITION GetStartPosition() const ; void GetNextAssoc(POSITION& rNextPosition, KEY& rKey, VALUE& rValue) const ; BOOL GetCurrentAssoc(POSITION rPosition, KEY& rKey, VALUE& rValue) const; } ; template SnmpMap :: SnmpMap ( BOOL threadSafeArg ) : criticalSection ( NULL ) { if ( threadSafeArg ) criticalSection = new CCriticalSection ; else criticalSection = FALSE ; } template SnmpMap :: ~SnmpMap () { if ( criticalSection ) delete criticalSection ; } template int SnmpMap :: GetCount() const { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; int count = cmap.GetCount () ; return count ; } else { return cmap.GetCount () ; } } template BOOL SnmpMap :: IsEmpty() const { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; BOOL isEmpty = cmap.IsEmpty () ; return isEmpty ; } else { return cmap.IsEmpty () ; } } template BOOL SnmpMap :: Lookup(ARG_KEY key, VALUE& rValue) const { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; BOOL lookup = cmap.Lookup ( key , rValue ) ; return lookup ; } else { return cmap.Lookup ( key , rValue ) ; } } template VALUE& SnmpMap :: operator[](ARG_KEY key) { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; VALUE &value = cmap.operator [] ( key ) ; return value ; } else { return cmap.operator [] ( key ) ; } } template void SnmpMap :: SetAt(ARG_KEY key, ARG_VALUE newValue) { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; cmap.SetAt ( key , newValue ) ; } else { cmap.SetAt ( key , newValue ) ; } } template BOOL SnmpMap :: RemoveKey(ARG_KEY key) { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; BOOL removeKey = cmap.RemoveKey ( key ) ; return removeKey ; } else { return cmap.RemoveKey ( key ) ; } } template void SnmpMap :: RemoveAll() { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; cmap.RemoveAll () ; } else { cmap.RemoveAll () ; } } template POSITION SnmpMap :: GetStartPosition() const { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; POSITION position = cmap.GetStartPosition () ; return position ; } else { return cmap.GetStartPosition () ; } } template void SnmpMap :: GetNextAssoc(POSITION& rNextPosition, KEY& rKey, VALUE& rValue) const { if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; cmap.GetNextAssoc ( rNextPosition , rKey , rValue ) ; } else { cmap.GetNextAssoc ( rNextPosition , rKey , rValue ) ; } } template BOOL SnmpMap :: GetCurrentAssoc(POSITION rPosition, KEY& rKey, VALUE& rValue) const { BOOL t_Status ; if ( criticalSection ) { criticalSection->Lock () ; ScopeGuard t_1 = MakeObjGuard ( *criticalSection , CCriticalSection :: Unlock ) ; t_Status = cmap.GetCurrentAssoc ( rPosition , rKey , rValue ) ; } else { t_Status = cmap.GetCurrentAssoc ( rPosition , rKey , rValue ) ; } return t_Status ; } #endif // __SNMPCONT_H