Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/string/nsTLiteralString.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef nsTLiteralString_h
8
#define nsTLiteralString_h
9
10
#include "nsTStringRepr.h"
11
12
/**
13
 * nsTLiteralString_CharT
14
 *
15
 * Stores a null-terminated, immutable sequence of characters.
16
 *
17
 * nsTString-lookalike that restricts its string value to a literal character
18
 * sequence. Can be implicitly cast to const nsTString& (the const is
19
 * essential, since this class's data are not writable). The data are assumed
20
 * to be static (permanent) and therefore, as an optimization, this class
21
 * does not have a destructor.
22
 */
23
template<typename T>
24
class nsTLiteralString : public mozilla::detail::nsTStringRepr<T>
25
{
26
public:
27
28
  typedef nsTLiteralString<T> self_type;
29
30
#ifdef __clang__
31
  // bindgen w/ clang 3.9 at least chokes on a typedef, but using is okay.
32
  using typename mozilla::detail::nsTStringRepr<T>::base_string_type;
33
#else
34
  // On the other hand msvc chokes on the using statement. It seems others
35
  // don't care either way so we lump them in here.
36
  typedef typename mozilla::detail::nsTStringRepr<T>::base_string_type base_string_type;
37
#endif
38
39
  typedef typename base_string_type::char_type char_type;
40
  typedef typename base_string_type::size_type size_type;
41
  typedef typename base_string_type::DataFlags DataFlags;
42
  typedef typename base_string_type::ClassFlags ClassFlags;
43
44
public:
45
46
  /**
47
   * constructor
48
   */
49
50
  template<size_type N>
51
  explicit constexpr nsTLiteralString(const char_type (&aStr)[N])
52
    : base_string_type(const_cast<char_type*>(aStr), N - 1,
53
                       DataFlags::TERMINATED | DataFlags::LITERAL,
54
                       ClassFlags::NULL_TERMINATED)
55
  {
56
  }
57
58
  /**
59
   * For compatibility with existing code that requires const ns[C]String*.
60
   * Use sparingly. If possible, rewrite code to use const ns[C]String&
61
   * and the implicit cast will just work.
62
   */
63
  const nsTString<T>& AsString() const
64
6.02k
  {
65
6.02k
    return *reinterpret_cast<const nsTString<T>*>(this);
66
6.02k
  }
67
68
  operator const nsTString<T>&() const
69
6.02k
  {
70
6.02k
    return AsString();
71
6.02k
  }
72
73
  template<typename N, typename Dummy> struct raw_type { typedef N* type; };
74
75
#ifdef MOZ_USE_CHAR16_WRAPPER
76
  template<typename Dummy> struct raw_type<char16_t, Dummy> { typedef char16ptr_t type; };
77
#endif
78
79
  /**
80
   * Prohibit get() on temporaries as in nsLiteralCString("x").get().
81
   * These should be written as just "x", using a string literal directly.
82
   */
83
  const typename raw_type<T, int>::type get() const && = delete;
84
  const typename raw_type<T, int>::type get() const &
85
0
  {
86
0
    return this->mData;
87
0
  }
Unexecuted instantiation: nsTLiteralString<char>::get() const &
Unexecuted instantiation: nsTLiteralString<char16_t>::get() const &
88
89
private:
90
91
  // NOT TO BE IMPLEMENTED
92
  template<size_type N>
93
  nsTLiteralString(char_type (&aStr)[N]) = delete;
94
95
  self_type& operator=(const self_type&) = delete;
96
};
97
98
extern template class nsTLiteralString<char>;
99
extern template class nsTLiteralString<char16_t>;
100
101
#endif