/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsIOutputStream.idl"

%{C++
#include "mozilla/Try.h"

namespace mozilla {
template<class ElementType, size_t Extent> class Span;
}
%}
native Bytes(mozilla::Span<const uint8_t>);

/**
 * This interface allows writing of primitive data types (integers,
 * floating-point values, booleans, etc.) to a stream in a binary, untagged,
 * fixed-endianness format.  This might be used, for example, to implement
 * network protocols or to produce architecture-neutral binary disk files,
 * i.e. ones that can be read and written by both big-endian and little-endian
 * platforms.  Output is written in big-endian order (high-order byte first),
 * as this is traditional network order.
 *
 * @See nsIBinaryInputStream
 */

[scriptable, builtinclass, uuid(204ee610-8765-11d3-90cf-0040056a906e)]
interface nsIBinaryOutputStream : nsIOutputStream {
    void setOutputStream(in nsIOutputStream aOutputStream);

    /**
     * Write a boolean as an 8-bit char to the stream.
     */
    [must_use]
    void writeBoolean(in boolean aBoolean);

    [must_use]
    void write8(in uint8_t aByte);
    [must_use]
    void write16(in uint16_t a16);
    [must_use]
    void write32(in uint32_t a32);
    [must_use]
    void write64(in uint64_t a64);

    [must_use]
    void writeFloat(in float aFloat);
    [must_use]
    void writeDouble(in double aDouble);

    /**
     * Write an 8-bit pascal style string to the stream.
     * 32-bit length field, followed by length 8-bit chars.
     */
    [must_use]
    void writeStringZ(in string aString);

    /**
     * Write a 16-bit pascal style string to the stream.
     * 32-bit length field, followed by length PRUnichars.
     */
    [must_use]
    void writeWStringZ(in wstring aString);

    /**
     * Write an 8-bit pascal style string (UTF8-encoded) to the stream.
     * 32-bit length field, followed by length 8-bit chars.
     */
    [must_use]
    void writeUtf8Z(in wstring aString);

    /**
     * Write an opaque byte array to the stream.
     */
    [binaryname(WriteBytesFromJS), must_use]
    void writeBytes([size_is(aLength)] in string aString,
                    [optional] in uint32_t aLength);

    /**
     * Non-scriptable and saner-signature version of the same.
     */
    [noscript, nostdcall, binaryname(WriteBytes), must_use]
    void writeBytesNative(in Bytes aBytes);

    /**
     * Write an opaque byte array to the stream.
     */
    [must_use]
    void writeByteArray(in Array<uint8_t> aBytes);

    %{C++
    /**
     * C++ helper to write a `const nsACString&` as an 8-bit pascal style string
     * to the stream.
     * 32-bit length field, followed by length 8-bit chars.
     */
    [[nodiscard]] nsresult WriteCString(const nsACString& aString) {
        MOZ_TRY(Write32(aString.Length()));
        return WriteBytes(aString);
    }
    %}
};

%{C++

[[nodiscard]] inline nsresult
NS_WriteOptionalStringZ(nsIBinaryOutputStream* aStream, const char* aString)
{
    bool nonnull = (aString != nullptr);
    MOZ_TRY(aStream->WriteBoolean(nonnull));
    if (nonnull) {
        MOZ_TRY(aStream->WriteStringZ(aString));
    }
    return NS_OK;
}

[[nodiscard]] inline nsresult
NS_WriteOptionalWStringZ(nsIBinaryOutputStream* aStream, const char16_t* aString)
{
    bool nonnull = (aString != nullptr);
    MOZ_TRY(aStream->WriteBoolean(nonnull));
    if (nonnull) {
        MOZ_TRY(aStream->WriteWStringZ(aString));
    }
    return NS_OK;
}

%}
