PageRenderTime 21ms CodeModel.GetById 2ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/llcommon/lldictionary.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 96 lines | 59 code | 8 blank | 29 comment | 9 complexity | bfd1651a197a08e9cf3760d6321b0e21 MD5 | raw file
 1/** 
 2 * @file lldictionary.h
 3 * @brief Lldictionary class header file
 4 *
 5 * $LicenseInfo:firstyear=2002&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_LLDICTIONARY_H
28#define LL_LLDICTIONARY_H
29
30#include <map>
31#include <string>
32
33struct LL_COMMON_API LLDictionaryEntry
34{
35	LLDictionaryEntry(const std::string &name);
36	virtual ~LLDictionaryEntry() {}
37	const std::string mName;
38	std::string mNameCapitalized;
39};
40
41template <class Index, class Entry>
42class LLDictionary : public std::map<Index, Entry *>
43{
44public:
45	typedef std::map<Index, Entry *> map_t;
46	typedef typename map_t::iterator iterator_t;
47	typedef typename map_t::const_iterator const_iterator_t;
48	
49	LLDictionary() {}
50	virtual ~LLDictionary()
51	{
52		for (iterator_t iter = map_t::begin(); iter != map_t::end(); ++iter)
53			delete (iter->second);
54	}
55
56	const Entry *lookup(Index index) const
57	{
58		const_iterator_t dictionary_iter = map_t::find(index);
59		if (dictionary_iter == map_t::end()) return NULL;
60		return dictionary_iter->second;
61	}
62	const Index lookup(const std::string &name) const 
63	{
64		for (const_iterator_t dictionary_iter = map_t::begin();
65			 dictionary_iter != map_t::end();
66			 dictionary_iter++)
67		{
68			const Entry *entry = dictionary_iter->second;
69			if (entry->mName == name)
70			{
71				return dictionary_iter->first;
72			}
73		}
74		return notFound();
75	}
76
77protected:
78	virtual Index notFound() const
79	{
80		// default is to assert
81		// don't assert -- makes it impossible to work on mesh-development and viewer-development simultaneously
82		//			-- davep 2010.10.29
83		//llassert(false);
84		return Index(-1);
85	}
86	void addEntry(Index index, Entry *entry)
87	{
88		if (lookup(index))
89		{
90			llerrs << "Dictionary entry already added (attempted to add duplicate entry)" << llendl;
91		}
92		(*this)[index] = entry;
93	}
94};
95
96#endif // LL_LLDICTIONARY_H