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/. */
7 #ifndef JSONSTRINGWRITEFUNCS_H
8 #define JSONSTRINGWRITEFUNCS_H
10 #include "mozilla/JSONWriter.h"
13 #include <type_traits>
17 // JSONWriteFunc that writes to an owned string.
18 template <typename StringType
>
19 class JSONStringWriteFunc final
: public JSONWriteFunc
{
21 !std::is_reference_v
<StringType
>,
22 "Use JSONStringRefWriteFunc instead to write to a referenced string");
25 JSONStringWriteFunc() = default;
27 void Write(const Span
<const char>& aStr
) final
{ mString
.Append(aStr
); }
29 const StringType
& StringCRef() const { return mString
; }
31 StringType
&& StringRRef() && { return std::move(mString
); }
37 // JSONWriteFunc that writes to a given nsACString reference.
38 class JSONStringRefWriteFunc final
: public JSONWriteFunc
{
40 MOZ_IMPLICIT
JSONStringRefWriteFunc(nsACString
& aString
) : mString(aString
) {}
42 void Write(const Span
<const char>& aStr
) final
{ mString
.Append(aStr
); }
44 const nsACString
& StringCRef() const { return mString
; }
50 } // namespace mozilla
52 #endif // JSONSTRINGWRITEFUNCS_H