PageRenderTime 33ms CodeModel.GetById 22ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llui/lluistring.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 115 lines | 44 code | 20 blank | 51 comment | 3 complexity | 314fa86b053b6899d1a191d639626c4a MD5 | raw file
  1/** 
  2 * @file lluistring.h
  3 * @author: Steve Bennetts
  4 * @brief A fancy wrapper for std::string supporting argument substitutions.
  5 *
  6 * $LicenseInfo:firstyear=2006&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_LLUISTRING_H
 29#define LL_LLUISTRING_H
 30
 31#include "llstring.h"
 32#include <string>
 33
 34// Use this class to store translated text that may have arguments
 35// e.g. "Welcome [USERNAME] to [SECONDLIFE]!"
 36
 37// Adding or changing an argument will update the result string, preserving the origianl
 38// Thus, subsequent changes to arguments or even the original string will produce
 39//  the correct result
 40
 41// Example Usage:
 42// LLUIString mMessage("Welcome [USERNAME] to [SECONDLIFE]!");
 43// mMessage.setArg("[USERNAME]", "Steve");
 44// mMessage.setArg("[SECONDLIFE]", "Second Life");
 45// llinfos << mMessage.getString() << llendl; // outputs "Welcome Steve to Second Life"
 46// mMessage.setArg("[USERNAME]", "Joe");
 47// llinfos << mMessage.getString() << llendl; // outputs "Welcome Joe to Second Life"
 48// mMessage = "Bienvenido a la [SECONDLIFE] [USERNAME]"
 49// mMessage.setArg("[SECONDLIFE]", "Segunda Vida");
 50// llinfos << mMessage.getString() << llendl; // outputs "Bienvenido a la Segunda Vida Joe"
 51
 52// Implementation Notes:
 53// Attempting to have operator[](const std::string& s) return mArgs[s] fails because we have
 54// to call format() after the assignment happens.
 55
 56class LLUIString
 57{
 58public:
 59	// These methods all perform appropriate argument substitution
 60	// and modify mOrig where appropriate
 61	LLUIString() : mArgs(NULL), mNeedsResult(false), mNeedsWResult(false) {}
 62	LLUIString(const std::string& instring, const LLStringUtil::format_map_t& args);
 63	LLUIString(const std::string& instring) : mArgs(NULL) { assign(instring); }
 64	~LLUIString() { delete mArgs; }
 65
 66	void assign(const std::string& instring);
 67	LLUIString& operator=(const std::string& s) { assign(s); return *this; }
 68
 69	void setArgList(const LLStringUtil::format_map_t& args);
 70	void setArgs(const LLStringUtil::format_map_t& args) { setArgList(args); }
 71	void setArgs(const class LLSD& sd);
 72	void setArg(const std::string& key, const std::string& replacement);
 73
 74	const std::string& getString() const { return getUpdatedResult(); }
 75	operator std::string() const { return getUpdatedResult(); }
 76
 77	const LLWString& getWString() const { return getUpdatedWResult(); }
 78	operator LLWString() const { return getUpdatedWResult(); }
 79
 80	bool empty() const { return getUpdatedResult().empty(); }
 81	S32 length() const { return getUpdatedWResult().size(); }
 82
 83	void clear();
 84	void clearArgs() { if (mArgs) mArgs->clear(); }
 85
 86	// These utility functions are included for text editing.
 87	// They do not affect mOrig and do not perform argument substitution
 88	void truncate(S32 maxchars);
 89	void erase(S32 charidx, S32 len);
 90	void insert(S32 charidx, const LLWString& wchars);
 91	void replace(S32 charidx, llwchar wc);
 92
 93private:
 94	// something changed, requiring reformatting of strings
 95	void dirty();
 96
 97	std::string& getUpdatedResult() const { if (mNeedsResult) { updateResult(); } return mResult; }
 98	LLWString& getUpdatedWResult() const{ if (mNeedsWResult) { updateWResult(); } return mWResult; }
 99
100	// do actual work of updating strings (non-inlined)
101	void updateResult() const;
102	void updateWResult() const;
103	LLStringUtil::format_map_t& getArgs();
104
105	std::string mOrig;
106	mutable std::string mResult;
107	mutable LLWString mWResult; // for displaying
108	LLStringUtil::format_map_t* mArgs;
109
110	// controls lazy evaluation
111	mutable bool	mNeedsResult;
112	mutable bool	mNeedsWResult;
113};
114
115#endif // LL_LLUISTRING_H