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/. */
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
#ifndef nsTLiteralString_h
|
|
|
|
#define nsTLiteralString_h
|
|
|
|
|
|
|
|
#include "nsTStringRepr.h"
|
|
|
|
|
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-08-15 00:22:50 +03:00
|
|
|
template<typename T>
|
|
|
|
class nsTLiteralString : public mozilla::detail::nsTStringRepr<T>
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-01-09 00:51:38 +04:00
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
typedef nsTLiteralString<T> self_type;
|
|
|
|
typedef typename mozilla::detail::nsTStringRepr<T>::base_string_type base_string_type;
|
|
|
|
typedef typename base_string_type::char_type char_type;
|
|
|
|
typedef typename base_string_type::size_type size_type;
|
|
|
|
typedef typename base_string_type::DataFlags DataFlags;
|
|
|
|
typedef typename base_string_type::ClassFlags ClassFlags;
|
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>
|
2017-08-15 00:22:50 +03:00
|
|
|
explicit constexpr nsTLiteralString(const char_type (&aStr)[N])
|
2017-07-10 22:23:10 +03:00
|
|
|
: base_string_type(const_cast<char_type*>(aStr), N - 1,
|
2017-07-21 01:46:58 +03:00
|
|
|
DataFlags::TERMINATED | DataFlags::LITERAL,
|
|
|
|
ClassFlags::NULL_TERMINATED)
|
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.
|
|
|
|
*/
|
2017-08-15 00:22:50 +03:00
|
|
|
const nsTString<T>& AsString() const
|
2017-03-14 05:26:36 +03:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return *reinterpret_cast<const nsTString<T>*>(this);
|
2017-03-14 05:26:36 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
operator const nsTString<T>&() const
|
2017-03-14 05:26:36 +03:00
|
|
|
{
|
|
|
|
return AsString();
|
|
|
|
}
|
|
|
|
|
2017-09-22 18:26:26 +03:00
|
|
|
template<typename N, typename Dummy> struct raw_type { typedef N* type; };
|
2017-08-15 00:22:50 +03:00
|
|
|
|
|
|
|
#ifdef MOZ_USE_CHAR16_WRAPPER
|
2017-09-22 18:26:26 +03:00
|
|
|
template<typename Dummy> struct raw_type<char16_t, Dummy> { typedef char16ptr_t type; };
|
2017-08-15 00:22:50 +03:00
|
|
|
#endif
|
|
|
|
|
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.
|
|
|
|
*/
|
2017-09-22 18:26:26 +03:00
|
|
|
const typename raw_type<T, int>::type get() const && = delete;
|
|
|
|
const typename raw_type<T, int>::type get() const &
|
2017-03-14 05:26:36 +03:00
|
|
|
{
|
2017-08-15 00:22:50 +03:00
|
|
|
return this->mData;
|
2017-03-14 05:26:36 +03:00
|
|
|
}
|
|
|
|
|
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>
|
2017-08-15 00:22:50 +03:00
|
|
|
nsTLiteralString(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
|
|
|
|
2017-08-15 00:22:50 +03:00
|
|
|
extern template class nsTLiteralString<char>;
|
|
|
|
extern template class nsTLiteralString<char16_t>;
|
|
|
|
|
|
|
|
#endif
|