/indra/llui/llfunctorregistry.h

https://bitbucket.org/lindenlab/viewer-beta/ · C++ Header · 140 lines · 81 code · 19 blank · 40 comment · 6 complexity · 1da7455d14ba0228fe43a5fb7377685b MD5 · raw file

  1. /**
  2. * @file llfunctorregistry.h
  3. * @author Kent Quirk
  4. * @brief Maintains a registry of named callback functors taking a single LLSD parameter
  5. *
  6. * $LicenseInfo:firstyear=2008&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. #ifndef LL_LLFUNCTORREGISTRY_H
  28. #define LL_LLFUNCTORREGISTRY_H
  29. #include <string>
  30. #include <map>
  31. #include <boost/function.hpp>
  32. #include "llsd.h"
  33. #include "llsingleton.h"
  34. /**
  35. * @class LLFunctorRegistry
  36. * @brief Maintains a collection of named functors for remote binding
  37. * (mainly for use in callbacks from notifications and other signals)
  38. * @see LLNotifications
  39. *
  40. * This class maintains a collection of named functors in a singleton.
  41. * We wanted to be able to persist notifications with their callbacks
  42. * across restarts of the viewer; we couldn't store functors that way.
  43. * Using this registry, systems that require a functor to be maintained
  44. * long term can register it at system startup, and then pass in the
  45. * functor by name.
  46. */
  47. template <typename FUNCTOR_TYPE>
  48. class LLFunctorRegistry : public LLSingleton<LLFunctorRegistry<FUNCTOR_TYPE> >
  49. {
  50. friend class LLSingleton<LLFunctorRegistry>;
  51. LOG_CLASS(LLFunctorRegistry);
  52. private:
  53. LLFunctorRegistry() : LOGFUNCTOR("LogFunctor"), DONOTHING("DoNothing")
  54. {
  55. mMap[LOGFUNCTOR] = log_functor;
  56. mMap[DONOTHING] = do_nothing;
  57. }
  58. public:
  59. typedef FUNCTOR_TYPE ResponseFunctor;
  60. typedef typename std::map<std::string, FUNCTOR_TYPE> FunctorMap;
  61. bool registerFunctor(const std::string& name, ResponseFunctor f)
  62. {
  63. bool retval = true;
  64. typename FunctorMap::iterator it = mMap.find(name);
  65. if (mMap.count(name) == 0)
  66. {
  67. mMap[name] = f;
  68. }
  69. else
  70. {
  71. llerrs << "attempt to store duplicate name '" << name << "' in LLFunctorRegistry. NOT ADDED." << llendl;
  72. retval = false;
  73. }
  74. return retval;
  75. }
  76. bool unregisterFunctor(const std::string& name)
  77. {
  78. if (mMap.count(name) == 0)
  79. {
  80. llwarns << "trying to remove '" << name << "' from LLFunctorRegistry but it's not there." << llendl;
  81. return false;
  82. }
  83. mMap.erase(name);
  84. return true;
  85. }
  86. FUNCTOR_TYPE getFunctor(const std::string& name)
  87. {
  88. typename FunctorMap::iterator it = mMap.find(name);
  89. if (mMap.count(name) != 0)
  90. {
  91. return mMap[name];
  92. }
  93. else
  94. {
  95. lldebugs << "tried to find '" << name << "' in LLFunctorRegistry, but it wasn't there." << llendl;
  96. return mMap[LOGFUNCTOR];
  97. }
  98. }
  99. const std::string LOGFUNCTOR;
  100. const std::string DONOTHING;
  101. private:
  102. static void log_functor(const LLSD& notification, const LLSD& payload)
  103. {
  104. lldebugs << "log_functor called with payload: " << payload << llendl;
  105. }
  106. static void do_nothing(const LLSD& notification, const LLSD& payload)
  107. {
  108. // what the sign sez
  109. }
  110. FunctorMap mMap;
  111. };
  112. template <typename FUNCTOR_TYPE>
  113. class LLFunctorRegistration
  114. {
  115. public:
  116. LLFunctorRegistration(const std::string& name, FUNCTOR_TYPE functor)
  117. {
  118. LLFunctorRegistry<FUNCTOR_TYPE>::instance().registerFunctor(name, functor);
  119. }
  120. };
  121. #endif//LL_LLFUNCTORREGISTRY_H