PageRenderTime 17ms CodeModel.GetById 10ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

/indra/llmath/lltreenode.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 117 lines | 78 code | 15 blank | 24 comment | 3 complexity | 43849b67300a6ca4f79ac4084e1a4541 MD5 | raw file
  1/** 
  2 * @file lltreenode.h
  3 *
  4 * $LicenseInfo:firstyear=2005&license=viewerlgpl$
  5 * Second Life Viewer Source Code
  6 * Copyright (C) 2010, Linden Research, Inc.
  7 * 
  8 * This library is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU Lesser General Public
 10 * License as published by the Free Software Foundation;
 11 * version 2.1 of the License only.
 12 * 
 13 * This library is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 16 * Lesser General Public License for more details.
 17 * 
 18 * You should have received a copy of the GNU Lesser General Public
 19 * License along with this library; if not, write to the Free Software
 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 21 * 
 22 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 23 * $/LicenseInfo$
 24 */
 25
 26#ifndef LL_LLTREENODE_H
 27#define LL_LLTREENODE_H
 28
 29#include "stdtypes.h"
 30#include "xform.h"
 31#include "llpointer.h"
 32#include "llrefcount.h"
 33
 34#include <vector>
 35
 36template <class T> class LLTreeNode;
 37template <class T> class LLTreeTraveler;
 38template <class T> class LLTreeListener;
 39
 40template <class T>
 41class LLTreeListener: public LLRefCount
 42{
 43public:
 44	virtual void handleInsertion(const LLTreeNode<T>* node, T* data) = 0;
 45	virtual void handleRemoval(const LLTreeNode<T>* node, T* data) = 0;
 46	virtual void handleDestruction(const LLTreeNode<T>* node) = 0;
 47	virtual void handleStateChange(const LLTreeNode<T>* node) = 0;
 48};
 49
 50template <class T>
 51class LLTreeNode 
 52{
 53public:
 54	virtual ~LLTreeNode();
 55	
 56	virtual bool insert(T* data);
 57	virtual bool remove(T* data);
 58	virtual void notifyRemoval(T* data);
 59	virtual U32 getListenerCount()					{ return mListeners.size(); }
 60	virtual LLTreeListener<T>* getListener(U32 index) const { return mListeners[index]; }
 61	virtual void addListener(LLTreeListener<T>* listener) { mListeners.push_back(listener); }
 62
 63protected:
 64	void destroyListeners()
 65	{
 66		for (U32 i = 0; i < mListeners.size(); i++)
 67		{
 68			mListeners[i]->handleDestruction(this);
 69		}
 70		mListeners.clear();
 71	}
 72	
 73public:
 74	std::vector<LLPointer<LLTreeListener<T> > > mListeners;
 75};
 76
 77template <class T>
 78class LLTreeTraveler
 79{
 80public:
 81	virtual ~LLTreeTraveler() { }; 
 82	virtual void traverse(const LLTreeNode<T>* node) = 0;
 83	virtual void visit(const LLTreeNode<T>* node) = 0;
 84};
 85
 86template <class T>
 87LLTreeNode<T>::~LLTreeNode()
 88{ 
 89	destroyListeners();
 90};
 91
 92template <class T>
 93bool LLTreeNode<T>::insert(T* data)
 94{ 
 95	for (U32 i = 0; i < mListeners.size(); i++)
 96	{
 97		mListeners[i]->handleInsertion(this, data);
 98	}
 99	return true;
100};
101
102template <class T>
103bool LLTreeNode<T>::remove(T* data)
104{
105	return true;
106};
107
108template <class T>
109void LLTreeNode<T>::notifyRemoval(T* data)
110{
111	for (U32 i = 0; i < mListeners.size(); i++)
112	{
113		mListeners[i]->handleRemoval(this, data);
114	}
115}
116
117#endif