Skip to content

Commit

Permalink
Rework the constructors of OutputStream in C++ (#2609)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier authored Aug 5, 2024
1 parent f696ab6 commit 1acc9be
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 135 deletions.
72 changes: 34 additions & 38 deletions cpp/include/Ice/OutputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Ice/Format.h"
#include "Ice/StringConverter.h"
#include "Ice/Version.h"
#include "Ice/VersionFunctions.h"
#include "InstanceF.h"
#include "SlicedDataF.h"
#include "StreamableTraits.h"
Expand Down Expand Up @@ -37,35 +38,48 @@ namespace Ice
typedef size_t size_type;

/**
* Constructs an OutputStream using the latest encoding version, the compact format for
* class encoding, and the process string converters.
* Constructs an OutputStream.
* @param encoding The encoding version to use.
* @param format The class format to use.
* @param stringConverter The narrow string converter to use.
* @param wstringConverter The wide string converter to use.
*/
OutputStream();
OutputStream(
EncodingVersion encoding = currentEncoding,
FormatType format = FormatType::CompactFormat,
StringConverterPtr stringConverter = nullptr,
WstringConverterPtr wstringConverter = nullptr);

/**
* Constructs a stream using the communicator's default encoding version.
* @param communicator The communicator to use for marshaling tasks.
* Constructs an OutputStream using the format, string converter and wstring converter provided by the
* communicator, and the specified encoding.
* @param communicator The communicator.
* @param encoding The encoding version to use.
*/
OutputStream(const CommunicatorPtr& communicator);
OutputStream(const CommunicatorPtr& communicator, EncodingVersion encoding);

/**
* Constructs a stream using the given communicator and encoding version.
* @param communicator The communicator to use for marshaling tasks.
* @param version The encoding version used to encode the data.
* Constructs an OutputStream using the encoding, format, string converter and wstring converter provided by
* the communicator.
* @param communicator The communicator.
*/
OutputStream(const CommunicatorPtr& communicator, const EncodingVersion& version);
OutputStream(const CommunicatorPtr& communicator);

/**
* Constructs a stream using the given communicator and encoding version.
* @param communicator The communicator to use for marshaling tasks.
* @param version The encoding version used to encode the data.
* @param bytes Application-supplied memory that the stream uses as its initial marshaling buffer. The
* Constructs an OutputStream over an application-supplied buffer.
* @param bytes Application-supplied memory that the OutputStream uses as its initial marshaling buffer. The
* stream will reallocate if the size of the marshaled data exceeds the application's buffer.
* @param encoding The encoding version to use.
* @param format The class format to use.
* @param stringConverter The narrow string converter to use.
* @param wstringConverter The wide string converter to use.
*/
OutputStream(
const CommunicatorPtr& communicator,
const EncodingVersion& version,
std::pair<const std::byte*, const std::byte*> bytes);
std::pair<const std::byte*, const std::byte*> bytes,
EncodingVersion encoding = currentEncoding,
FormatType format = FormatType::CompactFormat,
StringConverterPtr stringConverter = nullptr,
WstringConverterPtr wstringConverter = nullptr);

/**
* Move constructor.
Expand All @@ -89,23 +103,6 @@ namespace Ice
}
}

/**
* Initializes the stream to use the communicator's default encoding version, class
* encoding format and string converters. Use this method if you originally constructed
* the stream without a communicator.
* @param communicator The communicator to use for marshaling tasks.
*/
void initialize(const CommunicatorPtr& communicator);

/**
* Initializes the stream to use the given encoding version and the communicator's
* default class encoding format and string converters. Use this method if you
* originally constructed the stream without a communicator.
* @param communicator The communicator to use for marshaling tasks.
* @param version The encoding version used to encode the data.
*/
void initialize(const CommunicatorPtr& communicator, const EncodingVersion& version);

/**
* Releases any data retained by encapsulations.
*/
Expand Down Expand Up @@ -779,14 +776,13 @@ namespace Ice
std::pair<const std::byte*, const std::byte*> finished();

/// \cond INTERNAL
OutputStream(IceInternal::Instance*, const EncodingVersion&);
void initialize(IceInternal::Instance*, const EncodingVersion&);
OutputStream(IceInternal::Instance*, EncodingVersion encoding);
/// \endcond

private:
// Optionals
bool writeOptImpl(std::int32_t, OptionalFormat);
/// \endcond

private:
//
// String
//
Expand Down
9 changes: 4 additions & 5 deletions cpp/src/Ice/ConnectionI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,6 @@ Ice::ConnectionI::ConnectionI(
_batchRequestQueue(new BatchRequestQueue(instance, endpoint->datagram())),
_readStream(_instance.get(), Ice::currentProtocolEncoding),
_readHeader(false),
_writeStream(_instance.get(), Ice::currentProtocolEncoding),
_upcallCount(0),
_state(StateNotInitialized),
_shutdownInitiated(false),
Expand Down Expand Up @@ -2167,7 +2166,7 @@ Ice::ConnectionI::initiateShutdown()
//
// Before we shut down, we send a close connection message.
//
OutputStream os(_instance.get(), Ice::currentProtocolEncoding);
OutputStream os{Ice::currentProtocolEncoding};
os.write(magic[0]);
os.write(magic[1]);
os.write(magic[2]);
Expand Down Expand Up @@ -2328,7 +2327,7 @@ Ice::ConnectionI::sendHeartbeat() noexcept
// _sendStreams message.
if (_sendStreams.empty())
{
OutputStream os(_instance.get(), Ice::currentProtocolEncoding);
OutputStream os{Ice::currentProtocolEncoding};
os.write(magic[0]);
os.write(magic[1]);
os.write(magic[2]);
Expand Down Expand Up @@ -2662,7 +2661,7 @@ Ice::ConnectionI::sendNextMessages(vector<OutgoingMessage>& callbacks)
//
// Do compression.
//
OutputStream stream(_instance.get(), Ice::currentProtocolEncoding);
OutputStream stream{currentProtocolEncoding};
doCompress(*message->stream, stream);

traceSend(*message->stream, _instance, _logger, _traceLevels);
Expand Down Expand Up @@ -2778,7 +2777,7 @@ Ice::ConnectionI::sendMessage(OutgoingMessage& message)
//
// Do compression.
//
OutputStream stream(_instance.get(), Ice::currentProtocolEncoding);
OutputStream stream{currentProtocolEncoding};
doCompress(*message.stream, stream);
stream.i = stream.b.begin();

Expand Down
2 changes: 1 addition & 1 deletion cpp/src/Ice/EndpointFactoryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ IceInternal::EndpointFactoryManager::create(const string& str, bool oaEndpoint)
// and ask the factory to read the endpoint data from that stream to create
// the actual endpoint.
//
OutputStream bs(_instance.get(), Ice::currentProtocolEncoding);
OutputStream bs;
bs.write(ue->type());
ue->streamWrite(&bs);
InputStream is(_instance.get(), bs.getEncoding(), bs);
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/Ice/MarshaledResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using namespace IceInternal;

// currentProtocolEncoding because we're writing the protocol header.
MarshaledResult::MarshaledResult(const Current& current)
: _ostr(current.adapter->getCommunicator(), Ice::currentProtocolEncoding)
: _ostr(current.adapter->getCommunicator(), currentProtocolEncoding)
{
_ostr.writeBlob(replyHdr, sizeof(replyHdr));
_ostr.write(current.requestId);
Expand Down
6 changes: 3 additions & 3 deletions cpp/src/Ice/OutgoingResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace
OutgoingResponse makeOutgoingResponseCore(std::exception_ptr exc, const Current& current)
{
assert(exc);
OutputStream ostr(current.adapter->getCommunicator(), Ice::currentProtocolEncoding);
OutputStream ostr{current.adapter->getCommunicator(), Ice::currentProtocolEncoding};

if (current.requestId != 0)
{
Expand Down Expand Up @@ -238,7 +238,7 @@ Ice::makeOutgoingResponse(
std::optional<FormatType> format) noexcept
{
assert(marshal);
OutputStream ostr(current.adapter->getCommunicator(), Ice::currentProtocolEncoding);
OutputStream ostr{current.adapter->getCommunicator(), Ice::currentProtocolEncoding};
if (current.requestId != 0)
{
try
Expand Down Expand Up @@ -288,7 +288,7 @@ Ice::makeEmptyOutgoingResponse(const Current& current) noexcept
OutgoingResponse
Ice::makeOutgoingResponse(bool ok, pair<const byte*, const byte*> encapsulation, const Current& current) noexcept
{
OutputStream ostr(current.adapter->getCommunicator(), Ice::currentProtocolEncoding);
OutputStream ostr{current.adapter->getCommunicator(), Ice::currentProtocolEncoding};
if (current.requestId != 0)
{
try
Expand Down
83 changes: 35 additions & 48 deletions cpp/src/Ice/OutputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,41 +59,54 @@ namespace
};
}

Ice::OutputStream::OutputStream()
: _closure(nullptr),
_encoding(currentEncoding),
_format(FormatType::CompactFormat),
_currentEncaps(0)
Ice::OutputStream::OutputStream(
EncodingVersion encoding,
FormatType format,
StringConverterPtr stringConverter,
WstringConverterPtr wstringConverter)
: _stringConverter(std::move(stringConverter)),
_wstringConverter(std::move(wstringConverter)),
_closure(nullptr),
_encoding(std::move(encoding)),
_format(format),
_currentEncaps(nullptr)
{
}

Ice::OutputStream::OutputStream(const CommunicatorPtr& communicator) : _closure(0), _currentEncaps(0)
Ice::OutputStream::OutputStream(
pair<const byte*, const byte*> buf,
EncodingVersion encoding,
FormatType format,
StringConverterPtr stringConverter,
WstringConverterPtr wstringConverter)
: Buffer(buf.first, buf.second),
_stringConverter(std::move(stringConverter)),
_wstringConverter(std::move(wstringConverter)),
_closure(nullptr),
_encoding(std::move(encoding)),
_format(format),
_currentEncaps(nullptr)
{
initialize(communicator);
b.reset();
}

Ice::OutputStream::OutputStream(const CommunicatorPtr& communicator, const EncodingVersion& encoding)
: _closure(0),
_currentEncaps(0)
Ice::OutputStream::OutputStream(const CommunicatorPtr& communicator, EncodingVersion encoding)
: OutputStream(getInstance(communicator).get(), std::move(encoding))
{
initialize(communicator, encoding);
}

Ice::OutputStream::OutputStream(
const CommunicatorPtr& communicator,
const EncodingVersion& encoding,
pair<const byte*, const byte*> buf)
: Buffer(buf.first, buf.second),
_closure(0),
_currentEncaps(0)
Ice::OutputStream::OutputStream(const CommunicatorPtr& communicator)
: OutputStream(communicator, getInstance(communicator)->defaultsAndOverrides()->defaultEncoding)
{
initialize(communicator, encoding);
b.reset();
}

Ice::OutputStream::OutputStream(Instance* instance, const EncodingVersion& encoding) : _closure(0), _currentEncaps(0)
Ice::OutputStream::OutputStream(Instance* instance, EncodingVersion encoding)
: OutputStream(
std::move(encoding),
instance->defaultsAndOverrides()->defaultFormat,
instance->getStringConverter(),
instance->getWstringConverter())
{
initialize(instance, encoding);
}

Ice::OutputStream::OutputStream(OutputStream&& other) noexcept
Expand Down Expand Up @@ -142,32 +155,6 @@ Ice::OutputStream::operator=(OutputStream&& other) noexcept
return *this;
}

void
Ice::OutputStream::initialize(const CommunicatorPtr& communicator)
{
assert(communicator);
Instance* instance = getInstance(communicator).get();
initialize(instance, instance->defaultsAndOverrides()->defaultEncoding);
}

void
Ice::OutputStream::initialize(const CommunicatorPtr& communicator, const EncodingVersion& encoding)
{
assert(communicator);
initialize(getInstance(communicator).get(), encoding);
}

void
Ice::OutputStream::initialize(Instance* instance, const EncodingVersion& encoding)
{
assert(instance);
_encoding = encoding;

_format = instance->defaultsAndOverrides()->defaultFormat;
_stringConverter = instance->getStringConverter();
_wstringConverter = instance->getWstringConverter();
}

void
Ice::OutputStream::clear()
{
Expand Down
Loading

0 comments on commit 1acc9be

Please sign in to comment.