2014-05-05 21:30:46 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-01-09 00:51:38 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* nsTLiteralString_CharT
|
|
|
|
*
|
|
|
|
* Stores a null-terminated, immutable sequence of characters.
|
|
|
|
*
|
2017-03-14 05:26:36 +03:00
|
|
|
* nsTString-lookalike that restricts its string value to a literal character
|
|
|
|
* sequence. Can be implicitly cast to const nsTString& (the const is
|
|
|
|
* essential, since this class's data are not writable). The data are assumed
|
|
|
|
* to be static (permanent) and therefore, as an optimization, this class
|
|
|
|
* does not have a destructor.
|
2014-05-05 21:30:46 +04:00
|
|
|
*/
|
2017-03-14 05:26:36 +03:00
|
|
|
class nsTLiteralString_CharT : public mozilla::detail::nsTStringRepr_CharT
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-01-09 00:51:38 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
typedef nsTLiteralString_CharT self_type;
|
2014-01-09 00:51:38 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
public:
|
2014-01-09 00:51:38 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* constructor
|
|
|
|
*/
|
2014-01-09 00:51:38 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
template<size_type N>
|
2015-07-28 04:11:51 +03:00
|
|
|
explicit nsTLiteralString_CharT(const char_type (&aStr)[N])
|
2017-03-14 05:26:36 +03:00
|
|
|
: base_string_type(const_cast<char_type*>(aStr), N - 1, F_TERMINATED | F_LITERAL)
|
2017-03-14 05:26:36 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-03-14 05:26:36 +03:00
|
|
|
/**
|
|
|
|
* For compatibility with existing code that requires const ns[C]String*.
|
|
|
|
* Use sparingly. If possible, rewrite code to use const ns[C]String&
|
|
|
|
* and the implicit cast will just work.
|
|
|
|
*/
|
|
|
|
const nsTString_CharT& AsString() const
|
|
|
|
{
|
|
|
|
return *reinterpret_cast<const nsTString_CharT*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
operator const nsTString_CharT&() const
|
|
|
|
{
|
|
|
|
return AsString();
|
|
|
|
}
|
|
|
|
|
2017-03-14 05:26:36 +03:00
|
|
|
/**
|
|
|
|
* Prohibit get() on temporaries as in nsLiteralCString("x").get().
|
|
|
|
* These should be written as just "x", using a string literal directly.
|
|
|
|
*/
|
|
|
|
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
|
|
|
|
char16ptr_t get() const && = delete;
|
|
|
|
char16ptr_t get() const &
|
|
|
|
#else
|
|
|
|
const char_type* get() const && = delete;
|
|
|
|
const char_type* get() const &
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
return mData;
|
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
private:
|
2014-01-09 00:51:38 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// NOT TO BE IMPLEMENTED
|
|
|
|
template<size_type N>
|
2015-01-07 02:35:02 +03:00
|
|
|
nsTLiteralString_CharT(char_type (&aStr)[N]) = delete;
|
2017-03-14 05:26:36 +03:00
|
|
|
|
|
|
|
self_type& operator=(const self_type&) = delete;
|
2014-05-05 21:30:46 +04:00
|
|
|
};
|
2017-03-14 05:26:36 +03:00
|
|
|
|
|
|
|
static_assert(sizeof(nsTLiteralString_CharT) == sizeof(nsTString_CharT),
|
|
|
|
"nsTLiteralString_CharT can masquerade as nsTString_CharT, "
|
|
|
|
"so they must have identical layout");
|