StringifyUtils.h (1848B)
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/. */ 6 7 #ifndef mozilla_dom_quota_stringifyutils_h__ 8 #define mozilla_dom_quota_stringifyutils_h__ 9 10 #include "mozilla/ThreadLocal.h" 11 #include "nsLiteralString.h" 12 #include "nsTHashSet.h" 13 14 namespace mozilla { 15 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; 23 24 // A Stringifyable class provides a method `Stringify` that returns a string 25 // representation of the class content. 26 // 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. 32 // 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); 38 39 static void InitTLS(); 40 41 private: 42 virtual void DoStringify(nsACString& aData) = 0; 43 44 bool IsActive(); 45 void SetActive(bool aIsActive); 46 47 static MOZ_THREAD_LOCAL(nsTHashSet<Stringifyable*>*) 48 sActiveStringifyableInstances; 49 }; 50 51 } // namespace mozilla 52 53 #endif // mozilla_dom_quota_stringifyutils_h__