5#ifndef ICE_OUTPUT_STREAM_H
6#define ICE_OUTPUT_STREAM_H
61 const std::pair<const Byte*, const Byte*>& bytes);
67 if(_currentEncaps != &_preAllocatedEncaps)
100 IceInternal::Instance* instance()
const {
return _instance; }
130 void resetEncapsulation();
150 assert(_currentEncaps && _currentEncaps->encoder);
151 _currentEncaps->encoder->startInstance(ValueSlice, data);
159 assert(_currentEncaps && _currentEncaps->encoder);
160 _currentEncaps->encoder->endInstance();
170 assert(_currentEncaps && _currentEncaps->encoder);
171 _currentEncaps->encoder->startInstance(ExceptionSlice, data);
179 assert(_currentEncaps && _currentEncaps->encoder);
180 _currentEncaps->encoder->endInstance();
197 IceInternal::checkSupportedEncoding(encoding);
199 Encaps* oldEncaps = _currentEncaps;
202 _currentEncaps = &_preAllocatedEncaps;
206 _currentEncaps =
new Encaps();
207 _currentEncaps->previous = oldEncaps;
209 _currentEncaps->format = format;
210 _currentEncaps->encoding = encoding;
211 _currentEncaps->start = b.size();
214 write(_currentEncaps->encoding);
222 assert(_currentEncaps);
225 const Int sz =
static_cast<Int>(b.size() - _currentEncaps->start);
226 write(sz, &(*(b.begin() + _currentEncaps->start)));
228 Encaps* oldEncaps = _currentEncaps;
229 _currentEncaps = _currentEncaps->previous;
230 if(oldEncaps == &_preAllocatedEncaps)
246 IceInternal::checkSupportedEncoding(encoding);
260 throwEncapsulationException(__FILE__, __LINE__);
263 Container::size_type position = b.size();
264 resize(position +
static_cast<size_t>(sz));
265 memcpy(&b[position], &v[0],
static_cast<size_t>(sz));
275 return _currentEncaps ? _currentEncaps->encoding : _encoding;
285 void startSlice(
const std::string& typeId,
int compactId,
bool last)
287 assert(_currentEncaps && _currentEncaps->encoder);
288 _currentEncaps->encoder->startSlice(typeId, compactId, last);
296 assert(_currentEncaps && _currentEncaps->encoder);
297 _currentEncaps->encoder->endSlice();
342 *dest =
static_cast<Byte>(v);
366 rewrite(
static_cast<Int>(b.size() - position) - 4, position);
384 Container::size_type position = b.size();
386 memcpy(&b[position], &v[0], sz);
394 template<
typename T>
void write(
const T& v)
396 StreamHelper<T, StreamableTraits<T>::helper>
::write(
this, v);
412 StreamableTraits<T>::helper,
413 StreamableTraits<T>::fixedLength>::optionalFormat))
415 StreamOptionalHelper<T,
416 StreamableTraits<T>::helper,
417 StreamableTraits<T>::fixedLength>
::write(
this, *v);
425 template<
typename T>
void write(
const std::vector<T>& v)
433 write(&v[0], &v[0] + v.size());
442 template<
typename T>
void write(
const T* begin,
const T* end)
445 for(
const T* p = begin; p != end; ++p)
451#ifdef ICE_CPP11_MAPPING
456 template<
typename T>
void writeAll(
const T& v)
464 template<
typename T,
typename... Te>
void writeAll(
const T& v,
const Te&... ve)
473 template<
size_t I = 0,
typename... Te>
474 typename std::enable_if<I ==
sizeof...(Te),
void>::type
475 writeAll(std::tuple<Te...>)
483 template<
size_t I = 0,
typename... Te>
484 typename std::enable_if<I <
sizeof...(Te),
void>::type
485 writeAll(std::tuple<Te...> tuple)
487 write(std::get<I>(tuple));
488 writeAll<I + 1, Te...>(tuple);
495 void writeAll(std::initializer_list<int> tags,
const IceUtil::Optional<T>& v)
497 write(*(tags.begin() + tags.size() - 1), v);
503 template<
typename T,
typename... Te>
504 void writeAll(std::initializer_list<int> tags,
const IceUtil::Optional<T>& v,
const IceUtil::Optional<Te>&... ve)
506 size_t index = tags.size() -
sizeof...(ve) - 1;
507 write(*(tags.begin() + index), v);
508 writeAll(tags, ve...);
522 assert(_currentEncaps);
523 if(_currentEncaps->encoder)
525 return _currentEncaps->encoder->writeOptional(tag, format);
529 return writeOptImpl(tag, format);
555 b.push_back(
static_cast<Byte>(v));
562 void write(
const std::vector<bool>& v);
569 void write(
const bool* begin,
const bool* end);
590 Container::size_type position = b.size();
592 write(v, &b[position]);
604 const Byte* src =
reinterpret_cast<const Byte*
>(&v) +
sizeof(
Int) - 1;
610 const Byte* src =
reinterpret_cast<const Byte*
>(&v);
670 void write(
const std::string& v,
bool convert =
true)
672 Int sz =
static_cast<Int>(v.size());
673 if(convert && sz > 0)
675 writeConverted(v.data(),
static_cast<size_t>(sz));
682 Container::size_type position = b.size();
683 resize(position +
static_cast<size_t>(sz));
684 memcpy(&b[position], v.data(),
static_cast<size_t>(sz));
696 void write(
const char* vdata,
size_t vsize,
bool convert =
true)
698 Int sz =
static_cast<Int>(vsize);
699 if(convert && sz > 0)
701 writeConverted(vdata, vsize);
708 Container::size_type position = b.size();
709 resize(position +
static_cast<size_t>(sz));
710 memcpy(&b[position], vdata, vsize);
721 void write(
const char* vdata,
bool convert =
true)
723 write(vdata, strlen(vdata), convert);
733 void write(
const std::string* begin,
const std::string* end,
bool convert =
true);
746 void write(
const std::wstring* begin,
const std::wstring* end);
748#ifdef ICE_CPP11_MAPPING
753 void writeProxy(const ::std::shared_ptr<ObjectPrx>& v);
759 template<typename T, typename ::std::enable_if<::std::is_base_of<ObjectPrx, T>::value>::type* =
nullptr>
760 void write(const ::std::shared_ptr<T>& v)
762 writeProxy(::std::static_pointer_cast<ObjectPrx>(v));
775 template<
typename T>
void write(
const IceInternal::ProxyHandle<T>& v)
781#ifdef ICE_CPP11_MAPPING
786 template<typename T, typename ::std::enable_if<::std::is_base_of<Value, T>::value>::type* =
nullptr>
787 void write(const ::std::shared_ptr<T>& v)
790 _currentEncaps->encoder->write(v);
800 _currentEncaps->encoder->write(v);
807 template<
typename T>
void write(
const IceInternal::Handle<T>& v)
864 bool writeOptImpl(
Int, OptionalFormat);
872 void writeConverted(
const char*,
size_t);
880 void throwEncapsulationException(
const char*,
int);
886 IceInternal::Instance* _instance;
894 enum SliceType { NoSlice, ValueSlice, ExceptionSlice };
896 typedef std::vector<ValuePtr> ValueList;
902 virtual ~EncapsEncoder();
904 virtual void write(
const ValuePtr&) = 0;
907 virtual void startInstance(SliceType,
const SlicedDataPtr&) = 0;
908 virtual void endInstance() = 0;
909 virtual void startSlice(
const std::string&,
int,
bool) = 0;
910 virtual void endSlice() = 0;
912 virtual bool writeOptional(
Int, OptionalFormat)
917 virtual void writePendingValues()
923 EncapsEncoder(OutputStream* stream, Encaps* encaps) : _stream(stream), _encaps(encaps), _typeIdIndex(0)
927 Int registerTypeId(
const std::string&);
929 OutputStream* _stream;
932 typedef std::map<ValuePtr, Int> PtrToIndexMap;
933 typedef std::map<std::string, Int> TypeIdMap;
936 PtrToIndexMap _marshaledMap;
941 TypeIdMap _typeIdMap;
945 class ICE_API EncapsEncoder10 :
public EncapsEncoder
949 EncapsEncoder10(OutputStream* stream, Encaps* encaps) :
950 EncapsEncoder(stream, encaps), _sliceType(NoSlice), _valueIdIndex(0)
954 virtual void write(
const ValuePtr&);
955 virtual void write(
const UserException&);
957 virtual void startInstance(SliceType,
const SlicedDataPtr&);
958 virtual void endInstance();
959 virtual void startSlice(
const std::string&,
int,
bool);
960 virtual void endSlice();
962 virtual void writePendingValues();
966 Int registerValue(
const ValuePtr&);
969 SliceType _sliceType;
972 Container::size_type _writeSlice;
976 PtrToIndexMap _toBeMarshaledMap;
979 class ICE_API EncapsEncoder11 :
public EncapsEncoder
983 EncapsEncoder11(OutputStream* stream, Encaps* encaps) :
984 EncapsEncoder(stream, encaps), _preAllocatedInstanceData(0), _current(0), _valueIdIndex(1)
988 virtual void write(
const ValuePtr&);
989 virtual void write(
const UserException&);
991 virtual void startInstance(SliceType,
const SlicedDataPtr&);
992 virtual void endInstance();
993 virtual void startSlice(
const std::string&,
int,
bool);
994 virtual void endSlice();
996 virtual bool writeOptional(Int, OptionalFormat);
1000 void writeSlicedData(
const SlicedDataPtr&);
1001 void writeInstance(
const ValuePtr&);
1005 InstanceData(InstanceData* p) : previous(p), next(0)
1009 previous->next =
this;
1022 SliceType sliceType;
1027 Container::size_type writeSlice;
1028 Container::size_type sliceFlagsPos;
1029 PtrToIndexMap indirectionMap;
1030 ValueList indirectionTable;
1032 InstanceData* previous;
1035 InstanceData _preAllocatedInstanceData;
1036 InstanceData* _current;
1041 class Encaps :
private ::IceUtil::noncopyable
1064 Container::size_type start;
1065 EncodingVersion encoding;
1068 EncapsEncoder* encoder;
1079 EncodingVersion _encoding;
1083 Encaps* _currentEncaps;
1087 Encaps _preAllocatedEncaps;
#define ICE_API
Definition Config.h:197
#define ICE_ENUM(CLASS, ENUMERATOR)
Definition Config.h:378
T * get() const
Definition Handle.h:25
Encapsulates an optional value, which may or may not be present.
Definition Optional.h:30
void startValue(const SlicedDataPtr &data)
Marks the start of a class instance.
Definition OutputStream.h:148
void * setClosure(void *p)
Associates closure data with this stream.
void startEncapsulation()
Writes the start of an encapsulation using the default encoding version and class encoding format.
void write(const Double *begin, const Double *end)
Writes a double sequence to the stream.
size_t size_type
Definition OutputStream.h:31
void write(const ObjectPrx &v)
Writes a proxy to the stream.
void writePendingValues()
Encodes the state of class instances whose insertion was delayed during a previous call to write.
void write(const Float *begin, const Float *end)
Writes a float sequence to the stream.
void write(Int v)
Writes an int to the stream.
Definition OutputStream.h:588
void write(Int v, Container::iterator dest)
Overwrites a 32-bit integer value at the given destination in the stream.
Definition OutputStream.h:601
void write(const T &v)
Writes a data value to the stream.
Definition OutputStream.h:394
void write(const Byte *start, const Byte *end)
Writes a byte sequence to the stream.
void write(Long v)
Writes a long to the stream.
void startException(const SlicedDataPtr &data)
Marks the start of an exception instance.
Definition OutputStream.h:168
void writeException(const UserException &v)
Writes an exception to the stream.
void write(bool v)
Writes a boolean to the stream.
Definition OutputStream.h:553
void write(const ObjectPtr &v)
Writes a value instance to the stream.
Definition OutputStream.h:797
size_type startSize()
Writes a placeholder value for the size and returns the starting position of the size value; after wr...
Definition OutputStream.h:352
size_type pos()
Obtains the current position of the stream.
Definition OutputStream.h:830
void initialize(const CommunicatorPtr &communicator, const EncodingVersion &version)
Initializes the stream to use the given encoding version and the communicator's default class encodin...
void resize(Container::size_type sz)
Resizes the stream to a new size.
Definition OutputStream.h:138
void writeBlob(const std::vector< Byte > &v)
Copies the specified blob of bytes to the stream without modification.
void write(const IceInternal::ProxyHandle< T > &v)
Writes a proxy to the stream.
Definition OutputStream.h:775
void write(const std::vector< T > &v)
Writes a sequence of data values to the stream.
Definition OutputStream.h:425
void write(Double v)
Writes a double to the stream.
std::pair< const Byte *, const Byte * > finished()
Indicates that marshaling is complete.
const EncodingVersion & getEncoding() const
Determines the current encoding version.
Definition OutputStream.h:273
void write(Float v)
Writes a float to the stream.
void write(const bool *begin, const bool *end)
Writes a byte sequence to the stream.
void write(Byte v)
Writes a byte to the stream.
Definition OutputStream.h:537
void write(const Int *begin, const Int *end)
Writes an int sequence to the stream.
void write(const std::vector< bool > &v)
Writes a byte sequence to the stream.
void startSlice(const std::string &typeId, int compactId, bool last)
Writes the start of a value or exception slice.
Definition OutputStream.h:285
bool writeOptional(Int tag, OptionalFormat format)
Writes the tag and format of an optional value.
Definition OutputStream.h:520
void write(const std::wstring *begin, const std::wstring *end)
Writes a wide string sequence to the stream.
void initialize(const CommunicatorPtr &communicator)
Initializes the stream to use the communicator's default encoding version, class encoding format and ...
void endEncapsulation()
Ends the current encapsulation.
Definition OutputStream.h:220
void write(const T *begin, const T *end)
Writes a sequence of data values to the stream.
Definition OutputStream.h:442
void writeEncapsulation(const Byte *v, Int sz)
Copies the marshaled form of an encapsulation to the buffer.
Definition OutputStream.h:256
void endValue()
Marks the end of a class instance.
Definition OutputStream.h:157
void endSize(size_type position)
Updates the size value at the given position to contain a size based on the stream's current position...
Definition OutputStream.h:364
void rewriteSize(Int v, Container::iterator dest)
Replaces a size value at the given destination in the stream.
Definition OutputStream.h:332
void write(const IceInternal::Handle< T > &v)
Writes a value instance to the stream.
Definition OutputStream.h:807
void endException()
Marks the end of an exception instance.
Definition OutputStream.h:177
void write(Short v)
Writes a short to the stream.
void write(const Short *begin, const Short *end)
Writes a short sequence to the stream.
void write(const std::string *begin, const std::string *end, bool convert=true)
Writes a string sequence to the stream.
OutputStream(const CommunicatorPtr &communicator, const EncodingVersion &version)
Constructs a stream using the given communicator and encoding version.
void rewrite(Int v, size_type pos)
Overwrite a 32-bit integer value at the given position in the stream.
Definition OutputStream.h:841
OutputStream(const CommunicatorPtr &communicator, const EncodingVersion &version, const std::pair< const Byte *, const Byte * > &bytes)
Constructs a stream using the given communicator and encoding version.
void write(const Long *begin, const Long *end)
Writes a long sequence to the stream.
void startEncapsulation(const EncodingVersion &encoding, FormatType format)
Writes the start of an encapsulation using the given encoding version and class encoding format.
Definition OutputStream.h:195
void writeBlob(const Byte *v, Container::size_type sz)
Copies the specified blob of bytes to the stream without modification.
Definition OutputStream.h:380
void writeEmptyEncapsulation(const EncodingVersion &encoding)
Writes an empty encapsulation using the given encoding version.
Definition OutputStream.h:244
void write(const std::string &v, bool convert=true)
Writes a string to the stream.
Definition OutputStream.h:670
void writeSize(Int v)
Writes a size value.
Definition OutputStream.h:312
~OutputStream()
Definition OutputStream.h:63
void endSlice()
Marks the end of a value or exception slice.
Definition OutputStream.h:294
OutputStream()
Constructs an OutputStream using the latest encoding version, the default format for class encoding,...
void write(const std::wstring &v)
Writes a wide string to the stream.
void writeEnum(Int v, Int maxValue)
Writes an enumerator to the stream.
void * getClosure() const
Obtains the closure data associated with this stream.
void write(const char *vdata, size_t vsize, bool convert=true)
Writes a string to the stream.
Definition OutputStream.h:696
void write(const char *vdata, bool convert=true)
Writes a string to the stream.
Definition OutputStream.h:721
void write(Int tag, const IceUtil::Optional< T > &v)
Writes an optional data value to the stream.
Definition OutputStream.h:404
OutputStream(const CommunicatorPtr &communicator)
Constructs a stream using the communicator's default encoding version.
void clear()
Releases any data retained by encapsulations.
void setFormat(FormatType format)
Sets the class encoding format.
void swap(OutputStream &other)
Swaps the contents of one stream with another.
void finished(std::vector< Byte > &v)
Indicates that marshaling is complete.
Base class for all Ice user exceptions.
Definition Exception.h:68
Definition BuiltinSequences.h:113
IceInternal::Handle< Object > ObjectPtr
Definition ObjectF.h:21
int Int
The mapping for the Slice int type.
Definition Config.h:54
float Float
The mapping for the Slice float type.
Definition Config.h:63
IceInternal::ProxyHandle< ::IceProxy::Ice::Object > ObjectPrx
Smart pointer for an object proxy.
Definition ProxyF.h:47
IceUtil::Int64 Long
The mapping for the Slice long type.
Definition Config.h:60
IceInternal::Handle< SlicedData > SlicedDataPtr
Definition SlicedDataF.h:29
double Double
The mapping for the Slice double type.
Definition Config.h:65
short Short
The mapping for the Slice short type.
Definition Config.h:52
unsigned char Byte
The mapping for the Slice byte type.
Definition Config.h:50
::IceInternal::Handle< Communicator > CommunicatorPtr
Definition Communicator.h:608
ObjectPtr ValuePtr
Definition ObjectF.h:22
IceUtil::Shared * upCast(::Ice::AsyncResult *)
FormatType
Describes the possible formats for classes and exceptions.
Definition Format.h:21
@ DefaultFormat
Indicates that no preference was specified.
Definition Format.h:25
A version structure for the encoding version.
Definition Version.h:225