lldictionary.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @file lldictionary.h
  3. * @brief Lldictionary class header file
  4. *
  5. * $LicenseInfo:firstyear=2010&license=viewergpl$
  6. *
  7. * Copyright (c) 2010, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLDICTIONARY_H
  33. #define LL_LLDICTIONARY_H
  34. #include <map>
  35. #include <string>
  36. #include "llpreprocessor.h"
  37. struct LLDictionaryEntry
  38. {
  39. LLDictionaryEntry(const std::string& name);
  40. virtual ~LLDictionaryEntry() = default;
  41. const std::string mName;
  42. std::string mNameCapitalized;
  43. };
  44. // This is to avoid inlining llerrs...
  45. LL_NO_INLINE void errorDictionaryEntryAlreadyAdded();
  46. template <class Index, class Entry>
  47. class LLDictionary : public std::map<Index, Entry*>
  48. {
  49. public:
  50. typedef std::map<Index, Entry*> map_t;
  51. typedef typename map_t::iterator iterator_t;
  52. typedef typename map_t::const_iterator const_iterator_t;
  53. LL_INLINE LLDictionary() {}
  54. virtual ~LLDictionary()
  55. {
  56. for (iterator_t iter = map_t::begin(); iter != map_t::end(); ++iter)
  57. {
  58. delete iter->second;
  59. }
  60. }
  61. LL_INLINE const Entry* lookup(Index index) const
  62. {
  63. const_iterator_t iter = map_t::find(index);
  64. return iter != map_t::end() ? iter->second : NULL;
  65. }
  66. const Index lookup(const std::string& name) const
  67. {
  68. for (const_iterator_t iter = map_t::begin(), end = map_t::end();
  69. iter != end; ++iter)
  70. {
  71. const Entry* entry = iter->second;
  72. if (entry && entry->mName == name)
  73. {
  74. return iter->first;
  75. }
  76. }
  77. return notFound();
  78. }
  79. protected:
  80. // g++ v10.n (with n <= 2, at least) chokes on the LL_INLINE (which
  81. // translates into a force inline attribute for release builds), when
  82. // asked to compile with LTO. This is quite obviously a gcc bug... HB
  83. #if defined(GCC_VERSION) && GCC_VERSION >= 100000
  84. virtual Index notFound() const { return Index(-1); }
  85. #else
  86. LL_INLINE virtual Index notFound() const { return Index(-1); }
  87. #endif
  88. LL_INLINE void addEntry(Index index, Entry* entry)
  89. {
  90. if (lookup(index))
  91. {
  92. errorDictionaryEntryAlreadyAdded();
  93. }
  94. (*this)[index] = entry;
  95. }
  96. };
  97. #endif // LL_LLDICTIONARY_H