llfunctorregistry.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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=viewergpl$
  7. *
  8. * Copyright (c) 2008-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #ifndef LL_LLFUNCTORREGISTRY_H
  34. #define LL_LLFUNCTORREGISTRY_H
  35. #include "boost/function.hpp"
  36. #include "hbfastmap.h"
  37. #include "llsd.h"
  38. #include "llsingleton.h"
  39. // LLFunctorRegistry maintains a collection of named functors in a singleton.
  40. // We wanted to be able to persist notifications with their callbacks across
  41. // restarts of the viewer; we could not store functors that way. Using this
  42. // registry, systems that require a functor to be maintained long term can
  43. // register it at system startup, and then pass in the functor by name.
  44. template<typename FUNCTOR_TYPE>
  45. class LLFunctorRegistry : public LLSingleton<LLFunctorRegistry<FUNCTOR_TYPE> >
  46. {
  47. friend class LLSingleton<LLFunctorRegistry>;
  48. protected:
  49. LOG_CLASS(LLFunctorRegistry);
  50. public:
  51. typedef FUNCTOR_TYPE ResponseFunctor;
  52. typedef typename flat_hmap<std::string, FUNCTOR_TYPE> map_t;
  53. void registerFunctor(const std::string& name, ResponseFunctor f)
  54. {
  55. if (mMap.count(name))
  56. {
  57. llerrs << "Attempt to store duplicate name: " << name << llendl;
  58. }
  59. mMap.emplace(name, f);
  60. }
  61. void unregisterFunctor(const std::string& name)
  62. {
  63. if (!mMap.erase(name))
  64. {
  65. llwarns << "Could not find: " << name << llendl;
  66. }
  67. }
  68. FUNCTOR_TYPE getFunctor(const std::string& name) const
  69. {
  70. auto it = mMap.find(name);
  71. if (it != mMap.end())
  72. {
  73. return it->second;
  74. }
  75. // This a common/normal occurrence: do not use llwarns here. HB
  76. LL_DEBUGS("FunctorRegistry") << "Could not find: " << name << LL_ENDL;
  77. return do_nothing;
  78. }
  79. private:
  80. LLFunctorRegistry() = default;
  81. static void do_nothing(const LLSD&, const LLSD& payload)
  82. {
  83. LL_DEBUGS("FunctorRegistry") << "Payload: " << payload << LL_ENDL;
  84. }
  85. private:
  86. map_t mMap;
  87. };
  88. template<typename FUNCTOR_TYPE>
  89. class LLFunctorRegistration
  90. {
  91. public:
  92. LLFunctorRegistration(const std::string& name, FUNCTOR_TYPE functor)
  93. {
  94. LLFunctorRegistry<FUNCTOR_TYPE>::getInstance()->registerFunctor(name,
  95. functor);
  96. }
  97. };
  98. #endif//LL_LLFUNCTORREGISTRY_H