llsdparam.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:5k
源码类别:

游戏引擎

开发平台:

C++ Builder

  1. /** 
  2.  * @file llsdparam.cpp
  3.  * @brief parameter block abstraction for creating complex objects and 
  4.  * parsing construction parameters from xml and LLSD
  5.  *
  6.  * $LicenseInfo:firstyear=2008&license=viewergpl$
  7.  * 
  8.  * Copyright (c) 2008-2010, 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. #include "linden_common.h"
  34. // Project includes
  35. #include "llsdparam.h"
  36. //
  37. // LLParamSDParser
  38. //
  39. LLParamSDParser::LLParamSDParser()
  40. {
  41. using boost::bind;
  42. registerParserFuncs<S32>(bind(&LLParamSDParser::readTypedValue<S32>, this, _1, &LLSD::asInteger),
  43. bind(&LLParamSDParser::writeTypedValue<S32>, this, _1, _2));
  44. registerParserFuncs<U32>(bind(&LLParamSDParser::readTypedValue<U32>, this, _1, &LLSD::asInteger),
  45. bind(&LLParamSDParser::writeU32Param, this, _1, _2));
  46. registerParserFuncs<F32>(bind(&LLParamSDParser::readTypedValue<F32>, this, _1, &LLSD::asReal),
  47. bind(&LLParamSDParser::writeTypedValue<F32>, this, _1, _2));
  48. registerParserFuncs<F64>(bind(&LLParamSDParser::readTypedValue<F64>, this, _1, &LLSD::asReal),
  49. bind(&LLParamSDParser::writeTypedValue<F64>, this, _1, _2));
  50. registerParserFuncs<bool>(bind(&LLParamSDParser::readTypedValue<F32>, this, _1, &LLSD::asBoolean),
  51. bind(&LLParamSDParser::writeTypedValue<F32>, this, _1, _2));
  52. registerParserFuncs<std::string>(bind(&LLParamSDParser::readTypedValue<std::string>, this, _1, &LLSD::asString),
  53. bind(&LLParamSDParser::writeTypedValue<std::string>, this, _1, _2));
  54. registerParserFuncs<LLUUID>(bind(&LLParamSDParser::readTypedValue<LLUUID>, this, _1, &LLSD::asUUID),
  55. bind(&LLParamSDParser::writeTypedValue<LLUUID>, this, _1, _2));
  56. registerParserFuncs<LLDate>(bind(&LLParamSDParser::readTypedValue<LLDate>, this, _1, &LLSD::asDate),
  57. bind(&LLParamSDParser::writeTypedValue<LLDate>, this, _1, _2));
  58. registerParserFuncs<LLURI>(bind(&LLParamSDParser::readTypedValue<LLURI>, this, _1, &LLSD::asURI),
  59. bind(&LLParamSDParser::writeTypedValue<LLURI>, this, _1, _2));
  60. registerParserFuncs<LLSD>(bind(&LLParamSDParser::readSDParam, this, _1),
  61. bind(&LLParamSDParser::writeTypedValue<LLSD>, this, _1, _2));
  62. }
  63. bool LLParamSDParser::readSDParam(void* value_ptr)
  64. {
  65. if (!mCurReadSD) return false;
  66. *((LLSD*)value_ptr) = *mCurReadSD;
  67. return true;
  68. }
  69. // special case handling of U32 due to ambiguous LLSD::assign overload
  70. bool LLParamSDParser::writeU32Param(const void* val_ptr, const parser_t::name_stack_t& name_stack)
  71. {
  72. if (!mWriteSD) return false;
  73. LLSD* sd_to_write = getSDWriteNode(name_stack);
  74. if (!sd_to_write) return false;
  75. sd_to_write->assign((S32)*((const U32*)val_ptr));
  76. return true;
  77. }
  78. void LLParamSDParser::readSD(const LLSD& sd, LLInitParam::BaseBlock& block, bool silent)
  79. {
  80. mCurReadSD = NULL;
  81. mNameStack.clear();
  82. setParseSilently(silent);
  83. readSDValues(sd, block);
  84. }
  85. void LLParamSDParser::writeSD(LLSD& sd, const LLInitParam::BaseBlock& block)
  86. {
  87. mWriteSD = &sd;
  88. block.serializeBlock(*this);
  89. }
  90. void LLParamSDParser::readSDValues(const LLSD& sd, LLInitParam::BaseBlock& block)
  91. {
  92. if (sd.isMap())
  93. {
  94. for (LLSD::map_const_iterator it = sd.beginMap();
  95. it != sd.endMap();
  96. ++it)
  97. {
  98. mNameStack.push_back(make_pair(it->first, newParseGeneration()));
  99. readSDValues(it->second, block);
  100. mNameStack.pop_back();
  101. }
  102. }
  103. else if (sd.isArray())
  104. {
  105. for (LLSD::array_const_iterator it = sd.beginArray();
  106. it != sd.endArray();
  107. ++it)
  108. {
  109. mNameStack.back().second = newParseGeneration();
  110. readSDValues(*it, block);
  111. }
  112. }
  113. else
  114. {
  115. mCurReadSD = &sd;
  116. block.submitValue(mNameStack, *this);
  117. }
  118. }
  119. /*virtual*/ std::string LLParamSDParser::getCurrentElementName()
  120. {
  121. std::string full_name = "sd";
  122. for (name_stack_t::iterator it = mNameStack.begin();
  123. it != mNameStack.end();
  124. ++it)
  125. {
  126. full_name += llformat("[%s]", it->first.c_str());
  127. }
  128. return full_name;
  129. }
  130. LLSD* LLParamSDParser::getSDWriteNode(const parser_t::name_stack_t& name_stack)
  131. {
  132. //TODO: implement nested LLSD writing
  133. return mWriteSD;
  134. }