PageRenderTime 27ms CodeModel.GetById 11ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/llenum.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 78 lines | 45 code | 7 blank | 26 comment | 5 complexity | 6598cabfefe82e85155d69d10b2805ae MD5 | raw file
 1/** 
 2 * @file llenum.h
 3 * @author Tom Yedwab
 4 * @brief Utility class for storing enum value <-> string lookup.
 5 *
 6 * $LicenseInfo:firstyear=2006&license=viewerlgpl$
 7 * Second Life Viewer Source Code
 8 * Copyright (C) 2010, Linden Research, Inc.
 9 * 
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation;
13 * version 2.1 of the License only.
14 * 
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * Lesser General Public License for more details.
19 * 
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 * 
24 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
25 * $/LicenseInfo$
26 */
27
28#ifndef LL_LLENUM_H
29#define LL_LLENUM_H
30
31class LLEnum
32{
33public:
34	typedef std::pair<const std::string, const U32> enum_t;
35	enum
36	{
37		UNDEFINED = 0xffffffff,
38	};
39
40	LLEnum(const enum_t values_array[], const U32 length)
41	{
42		for (U32 i=0; i<length; ++i)
43		{
44			mEnumMap.insert(values_array[i]);
45			if (values_array[i].second >= mEnumArray.size())
46			{
47				mEnumArray.resize(values_array[i].second+1);
48			}
49			mEnumArray[values_array[i].second] = values_array[i].first;
50		}
51	}
52
53	U32 operator[](std::string str)
54	{
55		std::map<const std::string, const U32>::iterator itor;
56		itor = mEnumMap.find(str);
57		if (itor != mEnumMap.end())
58		{
59			return itor->second;
60		}
61		return UNDEFINED;
62	}
63
64	const std::string operator[](U32 index)
65	{
66		if (index < mEnumArray.size())
67		{
68			return mEnumArray[index];
69		}
70		return "";
71	}
72
73private:
74	std::map<const std::string, const U32> mEnumMap;
75	std::vector<std::string> mEnumArray;
76};
77
78#endif // LL_LLENUM_H