PageRenderTime 92ms CodeModel.GetById 80ms RepoModel.GetById 1ms 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
Possible License(s): LGPL-2.1
  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. #ifndef LL_LLNAMETABLE_H
  27. #define LL_LLNAMETABLE_H
  28. #include <map>
  29. #include "string_table.h"
  30. template <class DATA>
  31. class LLNameTable
  32. {
  33. public:
  34. LLNameTable()
  35. : mNameMap()
  36. {
  37. }
  38. ~LLNameTable()
  39. {
  40. }
  41. void addEntry(const std::string& name, DATA data)
  42. {
  43. addEntry(name.c_str(), data);
  44. }
  45. void addEntry(const char *name, DATA data)
  46. {
  47. char *tablename = gStringTable.addString(name);
  48. mNameMap[tablename] = data;
  49. }
  50. BOOL checkName(const std::string& name) const
  51. {
  52. return checkName(name.c_str());
  53. }
  54. // "logically const" even though it modifies the global nametable
  55. BOOL checkName(const char *name) const
  56. {
  57. char *tablename = gStringTable.addString(name);
  58. return mNameMap.count(tablename) ? TRUE : FALSE;
  59. }
  60. DATA resolveName(const std::string& name) const
  61. {
  62. return resolveName(name.c_str());
  63. }
  64. // "logically const" even though it modifies the global nametable
  65. DATA resolveName(const char *name) const
  66. {
  67. char *tablename = gStringTable.addString(name);
  68. const_iter_t iter = mNameMap.find(tablename);
  69. if (iter != mNameMap.end())
  70. return iter->second;
  71. else
  72. return 0;
  73. }
  74. // O(N)! (currently only used in one place... (newsim/llstate.cpp))
  75. const char *resolveData(const DATA &data) const
  76. {
  77. const_iter_t iter = mNameMap.begin();
  78. const_iter_t end = mNameMap.end();
  79. for (; iter != end; ++iter)
  80. {
  81. if (iter->second == data)
  82. return iter->first;
  83. }
  84. return NULL;
  85. }
  86. typedef std::map<const char *, DATA> name_map_t;
  87. typedef typename std::map<const char *,DATA>::iterator iter_t;
  88. typedef typename std::map<const char *,DATA>::const_iterator const_iter_t;
  89. name_map_t mNameMap;
  90. };
  91. #endif