PageRenderTime 18ms CodeModel.GetById 8ms RepoModel.GetById 1ms 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
Possible License(s): LGPL-2.1
  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. #ifndef LL_LLDICTIONARY_H
  27. #define LL_LLDICTIONARY_H
  28. #include <map>
  29. #include <string>
  30. struct LL_COMMON_API LLDictionaryEntry
  31. {
  32. LLDictionaryEntry(const std::string &name);
  33. virtual ~LLDictionaryEntry() {}
  34. const std::string mName;
  35. std::string mNameCapitalized;
  36. };
  37. template <class Index, class Entry>
  38. class LLDictionary : public std::map<Index, Entry *>
  39. {
  40. public:
  41. typedef std::map<Index, Entry *> map_t;
  42. typedef typename map_t::iterator iterator_t;
  43. typedef typename map_t::const_iterator const_iterator_t;
  44. LLDictionary() {}
  45. virtual ~LLDictionary()
  46. {
  47. for (iterator_t iter = map_t::begin(); iter != map_t::end(); ++iter)
  48. delete (iter->second);
  49. }
  50. const Entry *lookup(Index index) const
  51. {
  52. const_iterator_t dictionary_iter = map_t::find(index);
  53. if (dictionary_iter == map_t::end()) return NULL;
  54. return dictionary_iter->second;
  55. }
  56. const Index lookup(const std::string &name) const
  57. {
  58. for (const_iterator_t dictionary_iter = map_t::begin();
  59. dictionary_iter != map_t::end();
  60. dictionary_iter++)
  61. {
  62. const Entry *entry = dictionary_iter->second;
  63. if (entry->mName == name)
  64. {
  65. return dictionary_iter->first;
  66. }
  67. }
  68. return notFound();
  69. }
  70. protected:
  71. virtual Index notFound() const
  72. {
  73. // default is to assert
  74. // don't assert -- makes it impossible to work on mesh-development and viewer-development simultaneously
  75. // -- davep 2010.10.29
  76. //llassert(false);
  77. return Index(-1);
  78. }
  79. void addEntry(Index index, Entry *entry)
  80. {
  81. if (lookup(index))
  82. {
  83. llerrs << "Dictionary entry already added (attempted to add duplicate entry)" << llendl;
  84. }
  85. (*this)[index] = entry;
  86. }
  87. };
  88. #endif // LL_LLDICTIONARY_H