Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / quota / StringifyUtils.h
blobd2df7e5bebe17af6b670864a1eb9d62d87b06773
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_quota_stringifyutils_h__
8 #define mozilla_dom_quota_stringifyutils_h__
10 #include "mozilla/ThreadLocal.h"
11 #include "nsTHashSet.h"
12 #include "nsLiteralString.h"
14 namespace mozilla {
16 // Use these constants for common delimiters. Note that `Stringify` already
17 // encloses each call to `DoStringify` with `kStringify[Start/End]Instance`.
18 constexpr auto kStringifyDelimiter = "|"_ns;
19 constexpr auto kStringifyStartSet = "["_ns;
20 constexpr auto kStringifyEndSet = "]"_ns;
21 constexpr auto kStringifyStartInstance = "{"_ns;
22 constexpr auto kStringifyEndInstance = "}"_ns;
24 // A Stringifyable class provides a method `Stringify` that returns a string
25 // representation of the class content.
27 // It's content is just appended by the override of `DoStringify` but
28 // `Stringifyable` ensures that we won't call `DoStringify` twice for the same
29 // call and instance. A single `DoStringify` function thus needs not to be
30 // aware of "should I `Stringify` the content of this member or not", it can
31 // just do it always.
33 // Stringifyable does not bloat the object by additional members but uses
34 // thread local memory only when needed.
35 class Stringifyable {
36 public:
37 void Stringify(nsACString& aData);
39 static void InitTLS();
41 private:
42 virtual void DoStringify(nsACString& aData) = 0;
44 bool IsActive();
45 void SetActive(bool aIsActive);
47 static MOZ_THREAD_LOCAL(nsTHashSet<Stringifyable*>*)
48 sActiveStringifyableInstances;
51 } // namespace mozilla
53 #endif // mozilla_dom_quota_stringifyutils_h__