PageRenderTime 34ms CodeModel.GetById 18ms app.highlight 11ms RepoModel.GetById 2ms 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
  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
 28#ifndef LL_LLSDPARAM_H
 29#define LL_LLSDPARAM_H
 30
 31#include "llinitparam.h"
 32#include "boost/function.hpp"
 33
 34struct LLParamSDParserUtilities
 35{
 36	static LLSD& getSDWriteNode(LLSD& input, LLInitParam::Parser::name_stack_range_t& name_stack_range);
 37
 38	typedef boost::function<void (const LLSD&, LLInitParam::Parser::name_stack_t&)> read_sd_cb_t;
 39	static void readSDValues(read_sd_cb_t cb, const LLSD& sd, LLInitParam::Parser::name_stack_t& stack);
 40	static void readSDValues(read_sd_cb_t cb, const LLSD& sd);
 41};
 42
 43class LLParamSDParser 
 44:	public LLInitParam::Parser
 45{
 46LOG_CLASS(LLParamSDParser);
 47
 48typedef LLInitParam::Parser parser_t;
 49
 50public:
 51	LLParamSDParser();
 52	void readSD(const LLSD& sd, LLInitParam::BaseBlock& block, bool silent = false);
 53	void writeSD(LLSD& sd, const LLInitParam::BaseBlock& block);
 54
 55	/*virtual*/ std::string getCurrentElementName();
 56
 57private:
 58	void submit(LLInitParam::BaseBlock& block, const LLSD& sd, LLInitParam::Parser::name_stack_t& name_stack);
 59
 60	template<typename T>
 61	static bool writeTypedValue(Parser& parser, const void* val_ptr, parser_t::name_stack_t& name_stack)
 62	{
 63		LLParamSDParser& sdparser = static_cast<LLParamSDParser&>(parser);
 64		if (!sdparser.mWriteRootSD) return false;
 65		
 66		LLInitParam::Parser::name_stack_range_t range(name_stack.begin(), name_stack.end());
 67		LLSD& sd_to_write = LLParamSDParserUtilities::getSDWriteNode(*sdparser.mWriteRootSD, range);
 68
 69		sd_to_write.assign(*((const T*)val_ptr));
 70		return true;
 71	}
 72
 73	static bool writeU32Param(Parser& parser, const void* value_ptr, parser_t::name_stack_t& name_stack);
 74	static bool writeFlag(Parser& parser, const void* value_ptr, parser_t::name_stack_t& name_stack);
 75
 76	static bool readFlag(Parser& parser, void* val_ptr);
 77	static bool readS32(Parser& parser, void* val_ptr);
 78	static bool readU32(Parser& parser, void* val_ptr);
 79	static bool readF32(Parser& parser, void* val_ptr);
 80	static bool readF64(Parser& parser, void* val_ptr);
 81	static bool readBool(Parser& parser, void* val_ptr);
 82	static bool readString(Parser& parser, void* val_ptr);
 83	static bool readUUID(Parser& parser, void* val_ptr);
 84	static bool readDate(Parser& parser, void* val_ptr);
 85	static bool readURI(Parser& parser, void* val_ptr);
 86	static bool readSD(Parser& parser, void* val_ptr);
 87
 88	Parser::name_stack_t	mNameStack;
 89	const LLSD*				mCurReadSD;
 90	LLSD*					mWriteRootSD;
 91	LLSD*					mCurWriteSD;
 92};
 93
 94
 95extern LLFastTimer::DeclareTimer FTM_SD_PARAM_ADAPTOR;
 96template<typename T>
 97class LLSDParamAdapter : public T
 98{
 99public:
100	LLSDParamAdapter() {}
101	LLSDParamAdapter(const LLSD& sd)
102	{
103		LLFastTimer _(FTM_SD_PARAM_ADAPTOR);
104		LLParamSDParser parser;
105		// don't spam for implicit parsing of LLSD, as we want to allow arbitrary freeform data and ignore most of it
106		bool parse_silently = true;
107		parser.readSD(sd, *this, parse_silently);
108	}
109
110	operator LLSD() const
111	{
112		LLParamSDParser parser;
113		LLSD sd;
114		parser.writeSD(sd, *this);
115		return sd;
116	}
117		
118	LLSDParamAdapter(const T& val)
119	: T(val)
120	{
121		T::operator=(val);
122	}
123};
124
125#endif // LL_LLSDPARAM_H
126