PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llui/llsdparam.h

https://bitbucket.org/lindenlab/viewer-beta/
C Header | 126 lines | 78 code | 21 blank | 27 comment | 1 complexity | 098031a1d3361992c2fa96f2823eb744 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llsdparam.h
  3. * @brief parameter block abstraction for creating complex objects and
  4. * parsing construction parameters from xml and LLSD
  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_LLSDPARAM_H
  28. #define LL_LLSDPARAM_H
  29. #include "llinitparam.h"
  30. #include "boost/function.hpp"
  31. struct LLParamSDParserUtilities
  32. {
  33. static LLSD& getSDWriteNode(LLSD& input, LLInitParam::Parser::name_stack_range_t& name_stack_range);
  34. typedef boost::function<void (const LLSD&, LLInitParam::Parser::name_stack_t&)> read_sd_cb_t;
  35. static void readSDValues(read_sd_cb_t cb, const LLSD& sd, LLInitParam::Parser::name_stack_t& stack);
  36. static void readSDValues(read_sd_cb_t cb, const LLSD& sd);
  37. };
  38. class LLParamSDParser
  39. : public LLInitParam::Parser
  40. {
  41. LOG_CLASS(LLParamSDParser);
  42. typedef LLInitParam::Parser parser_t;
  43. public:
  44. LLParamSDParser();
  45. void readSD(const LLSD& sd, LLInitParam::BaseBlock& block, bool silent = false);
  46. void writeSD(LLSD& sd, const LLInitParam::BaseBlock& block);
  47. /*virtual*/ std::string getCurrentElementName();
  48. private:
  49. void submit(LLInitParam::BaseBlock& block, const LLSD& sd, LLInitParam::Parser::name_stack_t& name_stack);
  50. template<typename T>
  51. static bool writeTypedValue(Parser& parser, const void* val_ptr, parser_t::name_stack_t& name_stack)
  52. {
  53. LLParamSDParser& sdparser = static_cast<LLParamSDParser&>(parser);
  54. if (!sdparser.mWriteRootSD) return false;
  55. LLInitParam::Parser::name_stack_range_t range(name_stack.begin(), name_stack.end());
  56. LLSD& sd_to_write = LLParamSDParserUtilities::getSDWriteNode(*sdparser.mWriteRootSD, range);
  57. sd_to_write.assign(*((const T*)val_ptr));
  58. return true;
  59. }
  60. static bool writeU32Param(Parser& parser, const void* value_ptr, parser_t::name_stack_t& name_stack);
  61. static bool writeFlag(Parser& parser, const void* value_ptr, parser_t::name_stack_t& name_stack);
  62. static bool readFlag(Parser& parser, void* val_ptr);
  63. static bool readS32(Parser& parser, void* val_ptr);
  64. static bool readU32(Parser& parser, void* val_ptr);
  65. static bool readF32(Parser& parser, void* val_ptr);
  66. static bool readF64(Parser& parser, void* val_ptr);
  67. static bool readBool(Parser& parser, void* val_ptr);
  68. static bool readString(Parser& parser, void* val_ptr);
  69. static bool readUUID(Parser& parser, void* val_ptr);
  70. static bool readDate(Parser& parser, void* val_ptr);
  71. static bool readURI(Parser& parser, void* val_ptr);
  72. static bool readSD(Parser& parser, void* val_ptr);
  73. Parser::name_stack_t mNameStack;
  74. const LLSD* mCurReadSD;
  75. LLSD* mWriteRootSD;
  76. LLSD* mCurWriteSD;
  77. };
  78. extern LLFastTimer::DeclareTimer FTM_SD_PARAM_ADAPTOR;
  79. template<typename T>
  80. class LLSDParamAdapter : public T
  81. {
  82. public:
  83. LLSDParamAdapter() {}
  84. LLSDParamAdapter(const LLSD& sd)
  85. {
  86. LLFastTimer _(FTM_SD_PARAM_ADAPTOR);
  87. LLParamSDParser parser;
  88. // don't spam for implicit parsing of LLSD, as we want to allow arbitrary freeform data and ignore most of it
  89. bool parse_silently = true;
  90. parser.readSD(sd, *this, parse_silently);
  91. }
  92. operator LLSD() const
  93. {
  94. LLParamSDParser parser;
  95. LLSD sd;
  96. parser.writeSD(sd, *this);
  97. return sd;
  98. }
  99. LLSDParamAdapter(const T& val)
  100. : T(val)
  101. {
  102. T::operator=(val);
  103. }
  104. };
  105. #endif // LL_LLSDPARAM_H