PageRenderTime 23ms CodeModel.GetById 2ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/llcommon/llnametable.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 105 lines | 63 code | 14 blank | 28 comment | 6 complexity | eb59ad7686cd07c878194af5753ef91a MD5 | raw file
  1/** 
  2 * @file llnametable.h
  3 * @brief LLNameTable class is a table to associate pointers with string names
  4 *
  5 * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#ifndef LL_LLNAMETABLE_H
 28#define LL_LLNAMETABLE_H
 29
 30#include <map>
 31
 32#include "string_table.h"
 33
 34template <class DATA>
 35class LLNameTable
 36{
 37public:
 38	LLNameTable()
 39	:	mNameMap()
 40	{
 41	}
 42
 43	~LLNameTable() 
 44	{ 
 45	}
 46
 47	void addEntry(const std::string& name, DATA data)
 48	{
 49		addEntry(name.c_str(), data);
 50	}
 51
 52	void addEntry(const char *name, DATA data)
 53	{
 54		char *tablename = gStringTable.addString(name);
 55		mNameMap[tablename] = data;
 56	}
 57
 58	BOOL checkName(const std::string& name) const
 59	{
 60		return checkName(name.c_str());
 61	}
 62
 63	// "logically const" even though it modifies the global nametable
 64	BOOL checkName(const char *name) const
 65	{
 66		char *tablename = gStringTable.addString(name);
 67		return mNameMap.count(tablename) ? TRUE : FALSE;
 68	}
 69
 70	DATA resolveName(const std::string& name) const
 71	{
 72		return resolveName(name.c_str());
 73	}
 74
 75	// "logically const" even though it modifies the global nametable
 76	DATA resolveName(const char *name) const
 77	{
 78		char *tablename = gStringTable.addString(name);
 79		const_iter_t iter = mNameMap.find(tablename);
 80		if (iter != mNameMap.end())
 81			return iter->second;
 82		else
 83			return 0;
 84	}
 85
 86	// O(N)! (currently only used in one place... (newsim/llstate.cpp))
 87	const char *resolveData(const DATA &data) const
 88	{
 89		const_iter_t iter = mNameMap.begin();
 90		const_iter_t end = mNameMap.end();
 91		for (; iter != end; ++iter)
 92		{
 93			if (iter->second == data)
 94				return iter->first;
 95		}
 96   		return NULL;
 97	}		
 98
 99	typedef std::map<const char *, DATA> name_map_t;
100	typedef typename std::map<const char *,DATA>::iterator iter_t;
101	typedef typename std::map<const char *,DATA>::const_iterator const_iter_t;
102	name_map_t		mNameMap;
103};
104
105#endif