PageRenderTime 38ms CodeModel.GetById 20ms 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
Possible License(s): LGPL-2.1
  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. #ifndef LL_LLUISTRING_H
  28. #define LL_LLUISTRING_H
  29. #include "llstring.h"
  30. #include <string>
  31. // Use this class to store translated text that may have arguments
  32. // e.g. "Welcome [USERNAME] to [SECONDLIFE]!"
  33. // Adding or changing an argument will update the result string, preserving the origianl
  34. // Thus, subsequent changes to arguments or even the original string will produce
  35. // the correct result
  36. // Example Usage:
  37. // LLUIString mMessage("Welcome [USERNAME] to [SECONDLIFE]!");
  38. // mMessage.setArg("[USERNAME]", "Steve");
  39. // mMessage.setArg("[SECONDLIFE]", "Second Life");
  40. // llinfos << mMessage.getString() << llendl; // outputs "Welcome Steve to Second Life"
  41. // mMessage.setArg("[USERNAME]", "Joe");
  42. // llinfos << mMessage.getString() << llendl; // outputs "Welcome Joe to Second Life"
  43. // mMessage = "Bienvenido a la [SECONDLIFE] [USERNAME]"
  44. // mMessage.setArg("[SECONDLIFE]", "Segunda Vida");
  45. // llinfos << mMessage.getString() << llendl; // outputs "Bienvenido a la Segunda Vida Joe"
  46. // Implementation Notes:
  47. // Attempting to have operator[](const std::string& s) return mArgs[s] fails because we have
  48. // to call format() after the assignment happens.
  49. class LLUIString
  50. {
  51. public:
  52. // These methods all perform appropriate argument substitution
  53. // and modify mOrig where appropriate
  54. LLUIString() : mArgs(NULL), mNeedsResult(false), mNeedsWResult(false) {}
  55. LLUIString(const std::string& instring, const LLStringUtil::format_map_t& args);
  56. LLUIString(const std::string& instring) : mArgs(NULL) { assign(instring); }
  57. ~LLUIString() { delete mArgs; }
  58. void assign(const std::string& instring);
  59. LLUIString& operator=(const std::string& s) { assign(s); return *this; }
  60. void setArgList(const LLStringUtil::format_map_t& args);
  61. void setArgs(const LLStringUtil::format_map_t& args) { setArgList(args); }
  62. void setArgs(const class LLSD& sd);
  63. void setArg(const std::string& key, const std::string& replacement);
  64. const std::string& getString() const { return getUpdatedResult(); }
  65. operator std::string() const { return getUpdatedResult(); }
  66. const LLWString& getWString() const { return getUpdatedWResult(); }
  67. operator LLWString() const { return getUpdatedWResult(); }
  68. bool empty() const { return getUpdatedResult().empty(); }
  69. S32 length() const { return getUpdatedWResult().size(); }
  70. void clear();
  71. void clearArgs() { if (mArgs) mArgs->clear(); }
  72. // These utility functions are included for text editing.
  73. // They do not affect mOrig and do not perform argument substitution
  74. void truncate(S32 maxchars);
  75. void erase(S32 charidx, S32 len);
  76. void insert(S32 charidx, const LLWString& wchars);
  77. void replace(S32 charidx, llwchar wc);
  78. private:
  79. // something changed, requiring reformatting of strings
  80. void dirty();
  81. std::string& getUpdatedResult() const { if (mNeedsResult) { updateResult(); } return mResult; }
  82. LLWString& getUpdatedWResult() const{ if (mNeedsWResult) { updateWResult(); } return mWResult; }
  83. // do actual work of updating strings (non-inlined)
  84. void updateResult() const;
  85. void updateWResult() const;
  86. LLStringUtil::format_map_t& getArgs();
  87. std::string mOrig;
  88. mutable std::string mResult;
  89. mutable LLWString mWResult; // for displaying
  90. LLStringUtil::format_map_t* mArgs;
  91. // controls lazy evaluation
  92. mutable bool mNeedsResult;
  93. mutable bool mNeedsWResult;
  94. };
  95. #endif // LL_LLUISTRING_H