lldispatcher.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @file lldispatcher.h
  3. * @brief LLDispatcher class header file.
  4. *
  5. * $LicenseInfo:firstyear=2004&license=viewergpl$
  6. *
  7. * Copyright (c) 2004-2009, 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_LLDISPATCHER_H
  33. #define LL_LLDISPATCHER_H
  34. #include <map>
  35. #include <vector>
  36. #include <string>
  37. #include "llpreprocessor.h"
  38. class LLDispatcher;
  39. class LLMessageSystem;
  40. class LLUUID;
  41. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  42. // Class LLDispatchHandler
  43. //
  44. // Abstract base class for handling dispatches. Derive your own classes,
  45. // construct them, and add them to the dispatcher you want to use.
  46. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  47. class LLDispatchHandler
  48. {
  49. public:
  50. typedef std::vector<std::string> sparam_t;
  51. LLDispatchHandler() = default;
  52. virtual ~LLDispatchHandler() = default;
  53. virtual bool operator()(const LLDispatcher* dispatcher,
  54. const std::string& key,
  55. const LLUUID& invoice,
  56. const sparam_t& string) = 0;
  57. };
  58. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  59. // Class LLDispatcher
  60. //
  61. // Basic utility class that handles dispatching keyed operations to function
  62. // objects implemented as LLDispatchHandler derivations.
  63. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  64. class LLDispatcher
  65. {
  66. protected:
  67. LOG_CLASS(LLDispatcher);
  68. public:
  69. typedef std::vector<std::string> keys_t;
  70. typedef std::vector<std::string> sparam_t;
  71. LLDispatcher() = default;
  72. virtual ~LLDispatcher() = default;
  73. // Returns true if they keyed handler exists in this dispatcher.
  74. LL_INLINE bool isHandlerPresent(const std::string& name) const
  75. {
  76. return mHandlers.count(name) != 0;
  77. }
  78. // Call this method with the name of the request that has come in. If the
  79. // handler is present, it is called with the params and returns the return
  80. // value from.
  81. bool dispatch(const std::string& name, const LLUUID& invoice,
  82. const sparam_t& strings) const;
  83. //const iparam_t& itegers) const;
  84. // Add a handler. If one with the same key already exists, its pointer is
  85. // returned, otherwise returns NULL. This object does not do memory
  86. // management of the LLDispatchHandler, and relies on the caller to delete
  87. // the object if necessary.
  88. LLDispatchHandler* addHandler(const std::string& name,
  89. LLDispatchHandler* func);
  90. // Helper method to unpack the dispatcher message bus format. Returns true
  91. // on success.
  92. static bool unpackMessage(LLMessageSystem* msg, std::string& method,
  93. LLUUID& invoice, sparam_t& parameters);
  94. static bool unpackLargeMessage(LLMessageSystem* msg, std::string& method,
  95. LLUUID& invoice, sparam_t& parameters);
  96. protected:
  97. typedef std::map<std::string, LLDispatchHandler*> dispatch_map_t;
  98. dispatch_map_t mHandlers;
  99. };
  100. #endif // LL_LLDISPATCHER_H