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 nsStringFlags_h
8 #define nsStringFlags_h
9 
10 #include <stdint.h>
11 #include "mozilla/TypedEnumBits.h"
12 
13 namespace mozilla {
14 namespace detail {
15 // NOTE: these flags are declared public _only_ for convenience inside
16 // the string implementation.  And they are outside of the string
17 // class so that the type is the same for both narrow and wide
18 // strings.
19 
20 // bits for mDataFlags
21 enum class StringDataFlags : uint16_t {
22   // Some terminology:
23   //
24   //   "dependent buffer"    A dependent buffer is one that the string class
25   //                         does not own.  The string class relies on some
26   //                         external code to ensure the lifetime of the
27   //                         dependent buffer.
28   //
29   //   "refcounted buffer"   A refcounted buffer is one that the string class
30   //                         allocates.  When it allocates a refcounted string
31   //                         buffer, it allocates some additional space at
32   //                         the beginning of the buffer for additional
33   //                         fields, including a reference count and a
34   //                         buffer length.  See nsStringHeader.
35   //
36   //   "adopted buffer"      An adopted buffer is a raw string buffer
37   //                         allocated on the heap (using moz_xmalloc)
38   //                         of which the string class subsumes ownership.
39   //
40   // Some comments about the string data flags:
41   //
42   //   REFCOUNTED, OWNED, and INLINE are all mutually exlusive.  They
43   //   indicate the allocation type of mData.  If none of these flags
44   //   are set, then the string buffer is dependent.
45   //
46   //   REFCOUNTED, OWNED, or INLINE imply TERMINATED.  This is because
47   //   the string classes always allocate null-terminated buffers, and
48   //   non-terminated substrings are always dependent.
49   //
50   //   VOIDED implies TERMINATED, and moreover it implies that mData
51   //   points to char_traits::sEmptyBuffer.  Therefore, VOIDED is
52   //   mutually exclusive with REFCOUNTED, OWNED, and INLINE.
53   //
54   //   INLINE requires StringClassFlags::INLINE to be set on the type.
55 
56   // IsTerminated returns true
57   TERMINATED = 1 << 0,
58 
59   // IsVoid returns true
60   VOIDED = 1 << 1,
61 
62   // mData points to a heap-allocated, shareable, refcounted buffer
63   REFCOUNTED = 1 << 2,
64 
65   // mData points to a heap-allocated, raw buffer
66   OWNED = 1 << 3,
67 
68   // mData points to a writable, inline buffer
69   INLINE = 1 << 4,
70 
71   // mData points to a string literal; DataFlags::TERMINATED will also be set
72   LITERAL = 1 << 5,
73 
74   // used to check for invalid flags -- all bits above the last item
75   INVALID_MASK = (uint16_t) ~((LITERAL << 1) - 1)
76 };
77 
78 // bits for mClassFlags
79 enum class StringClassFlags : uint16_t {
80   // |this|'s buffer is inline, and requires the type to be binary-compatible
81   // with nsTAutoStringN
82   INLINE = 1 << 0,
83   // |this| requires its buffer is null-terminated
84   NULL_TERMINATED = 1 << 1,
85   // used to check for invalid flags -- all bits above the last item
86   INVALID_MASK = (uint16_t) ~((NULL_TERMINATED << 1) - 1)
87 };
88 
89 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(StringDataFlags)
90 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(StringClassFlags)
91 
92 }  // namespace detail
93 }  // namespace mozilla
94 
95 #endif
96