http://git-wip-us.apache.org/repos/asf/ignite/blob/1e18fa32/modules/platform/src/main/cpp/core/include/gridgain/impl/portable/portable_utils.h ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/include/gridgain/impl/portable/portable_utils.h b/modules/platform/src/main/cpp/core/include/gridgain/impl/portable/portable_utils.h new file mode 100644 index 0000000..9d52caa --- /dev/null +++ b/modules/platform/src/main/cpp/core/include/gridgain/impl/portable/portable_utils.h @@ -0,0 +1,336 @@ +/* + * Copyright (C) GridGain Systems. All Rights Reserved. + * _________ _____ __________________ _____ + * __ ____/___________(_)______ /__ ____/______ ____(_)_______ + * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ + * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / + * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ + */ + +#ifndef _GRIDGAIN_IMPL_PORTABLE_UTILS +#define _GRIDGAIN_IMPL_PORTABLE_UTILS + +#include <stdint.h> + +#include "gridgain/guid.h" + +namespace gridgain +{ + namespace impl + { + namespace interop + { + class InteropInputStream; + class InteropOutputStream; + } + + namespace portable + { + /** + * Portable uilts. + */ + class IGNITE_IMPORT_EXPORT PortableUtils + { + public: + /** + * Utility method to read signed 8-bit integer from stream. + * + * @param stream Stream. + * @return Value. + */ + static int8_t ReadInt8(interop::InteropInputStream* stream); + + /** + * Utility method to write signed 8-bit integer to stream. + * + * @param stream Stream. + * @param val Value. + */ + static void WriteInt8(interop::InteropOutputStream* stream, int8_t val); + + /** + * Utility method to read signed 8-bit integer array from stream. + * + * @param stream Stream. + * @param res Target array. + * @param len Array length. + */ + static void ReadInt8Array(interop::InteropInputStream* stream, int8_t* res, const int32_t len); + + /** + * Utility method to write signed 8-bit integer array to stream. + * + * @param stream Stream. + * @param val Value. + * @param len Array length. + */ + static void WriteInt8Array(interop::InteropOutputStream* stream, const int8_t* val, const int32_t len); + + /** + * Utility method to read boolean from stream. + * + * @param stream Stream. + * @return Value. + */ + static bool ReadBool(interop::InteropInputStream* stream); + + /** + * Utility method to write bool to stream. + * + * @param stream Stream. + * @param val Value. + */ + static void WriteBool(interop::InteropOutputStream* stream, bool val); + + /** + * Utility method to read bool array from stream. + * + * @param stream Stream. + * @param res Target array. + * @param len Array length. + */ + static void ReadBoolArray(interop::InteropInputStream* stream, bool* res, const int32_t len); + + /** + * Utility method to write bool array to stream. + * + * @param stream Stream. + * @param val Value. + * @param len Array length. + */ + static void WriteBoolArray(interop::InteropOutputStream* stream, const bool* val, const int32_t len); + + /** + * Utility method to read signed 16-bit integer from stream. + * + * @param stream Stream. + * @return Value. + */ + static int16_t ReadInt16(interop::InteropInputStream* stream); + + /** + * Utility method to write signed 16-bit integer to stream. + * + * @param stream Stream. + * @param val Value. + */ + static void WriteInt16(interop::InteropOutputStream* stream, int16_t val); + + /** + * Utility method to read signed 16-bit integer array from stream. + * + * @param stream Stream. + * @param res Target array. + * @param len Array length. + */ + static void ReadInt16Array(interop::InteropInputStream* stream, int16_t* res, const int32_t len); + + /** + * Utility method to write signed 16-bit integer array to stream. + * + * @param stream Stream. + * @param val Value. + * @param len Array length. + */ + static void WriteInt16Array(interop::InteropOutputStream* stream, const int16_t* val, const int32_t len); + + /** + * Utility method to read unsigned 16-bit integer from stream. + * + * @param stream Stream. + * @return Value. + */ + static uint16_t ReadUInt16(interop::InteropInputStream* stream); + + /** + * Utility method to write unsigned 16-bit integer to stream. + * + * @param stream Stream. + * @param val Value. + */ + static void WriteUInt16(interop::InteropOutputStream* stream, uint16_t val); + + /** + * Utility method to read unsigned 16-bit integer array from stream. + * + * @param stream Stream. + * @param res Target array. + * @param len Array length. + */ + static void ReadUInt16Array(interop::InteropInputStream* stream, uint16_t* res, const int32_t len); + + /** + * Utility method to write unsigned 16-bit integer array to stream. + * + * @param stream Stream. + * @param val Value. + * @param len Array length. + */ + static void WriteUInt16Array(interop::InteropOutputStream* stream, const uint16_t* val, const int32_t len); + + /** + * Utility method to read signed 32-bit integer from stream. + * + * @param stream Stream. + * @return Value. + */ + static int32_t ReadInt32(interop::InteropInputStream* stream); + + /** + * Utility method to write signed 32-bit integer to stream. + * + * @param stream Stream. + * @param val Value. + */ + static void WriteInt32(interop::InteropOutputStream* stream, int32_t val); + + /** + * Utility method to read signed 32-bit integer array from stream. + * + * @param stream Stream. + * @param res Target array. + * @param len Array length. + */ + static void ReadInt32Array(interop::InteropInputStream* stream, int32_t* res, const int32_t len); + + /** + * Utility method to write signed 32-bit integer array to stream. + * + * @param stream Stream. + * @param val Value. + * @param len Array length. + */ + static void WriteInt32Array(interop::InteropOutputStream* stream, const int32_t* val, const int32_t len); + + /** + * Utility method to read signed 64-bit integer from stream. + * + * @param stream Stream. + * @return Value. + */ + static int64_t ReadInt64(interop::InteropInputStream* stream); + + /** + * Utility method to write signed 64-bit integer to stream. + * + * @param stream Stream. + * @param val Value. + */ + static void WriteInt64(interop::InteropOutputStream* stream, int64_t val); + + /** + * Utility method to read signed 64-bit integer array from stream. + * + * @param stream Stream. + * @param res Target array. + * @param len Array length. + */ + static void ReadInt64Array(interop::InteropInputStream* stream, int64_t* res, const int32_t len); + + /** + * Utility method to write signed 64-bit integer array to stream. + * + * @param stream Stream. + * @param val Value. + * @param len Array length. + */ + static void WriteInt64Array(interop::InteropOutputStream* stream, const int64_t* val, const int32_t len); + + /** + * Utility method to read float from stream. + * + * @param stream Stream. + * @return Value. + */ + static float ReadFloat(interop::InteropInputStream* stream); + + /** + * Utility method to write float to stream. + * + * @param stream Stream. + * @param val Value. + */ + static void WriteFloat(interop::InteropOutputStream* stream, float val); + + /** + * Utility method to read float array from stream. + * + * @param stream Stream. + * @param res Target array. + * @param len Array length. + */ + static void ReadFloatArray(interop::InteropInputStream* stream, float* res, const int32_t len); + + /** + * Utility method to write float array to stream. + * + * @param stream Stream. + * @param val Value. + * @param len Array length. + */ + static void WriteFloatArray(interop::InteropOutputStream* stream, const float* val, const int32_t len); + + /** + * Utility method to read double from stream. + * + * @param stream Stream. + * @return Value. + */ + static double ReadDouble(interop::InteropInputStream* stream); + + /** + * Utility method to write double to stream. + * + * @param stream Stream. + * @param val Value. + */ + static void WriteDouble(interop::InteropOutputStream* stream, double val); + + /** + * Utility method to read double array from stream. + * + * @param stream Stream. + * @param res Target array. + * @param len Array length. + */ + static void ReadDoubleArray(interop::InteropInputStream* stream, double* res, const int32_t len); + + /** + * Utility method to write double array to stream. + * + * @param stream Stream. + * @param val Value. + * @param len Array length. + */ + static void WriteDoubleArray(interop::InteropOutputStream* stream, const double* val, const int32_t len); + + /** + * Utility method to read Guid from stream. + * + * @param stream Stream. + * @param res Value. + */ + static Guid ReadGuid(interop::InteropInputStream* stream); + + /** + * Utility method to write Guid to stream. + * + * @param stream Stream. + * @param val Value. + */ + static void WriteGuid(interop::InteropOutputStream* stream, const Guid val); + + /** + * Utility method to write string to stream. + * + * @param stream Stream. + * @param val Value. + * @param len Length. + */ + static void WriteString(interop::InteropOutputStream* stream, const char* val, const int32_t len); + }; + } + } +} + +#endif \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/ignite/blob/1e18fa32/modules/platform/src/main/cpp/core/include/gridgain/impl/portable/portable_writer_impl.h ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/include/gridgain/impl/portable/portable_writer_impl.h b/modules/platform/src/main/cpp/core/include/gridgain/impl/portable/portable_writer_impl.h new file mode 100644 index 0000000..385f035 --- /dev/null +++ b/modules/platform/src/main/cpp/core/include/gridgain/impl/portable/portable_writer_impl.h @@ -0,0 +1,851 @@ +/* + * Copyright (C) GridGain Systems. All Rights Reserved. + * _________ _____ __________________ _____ + * __ ____/___________(_)______ /__ ____/______ ____(_)_______ + * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ + * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / + * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ + */ + +#ifndef _GRIDGAIN_IMPL_PORTABLE_WRITER +#define _GRIDGAIN_IMPL_PORTABLE_WRITER + +#include <cstring> +#include <string> +#include <stdint.h> + +#include <ignite/common/common.h> +#include <ignite/common/concurrent.h> + +#include "gridgain/impl/interop/interop_output_stream.h" +#include "gridgain/impl/portable/portable_common.h" +#include "gridgain/impl/portable/portable_id_resolver.h" +#include "gridgain/impl/portable/portable_metadata_manager.h" +#include "gridgain/impl/portable/portable_utils.h" +#include "gridgain/portable/portable_consts.h" +#include "gridgain/portable/portable_type.h" +#include "gridgain/guid.h" + +namespace gridgain +{ + namespace impl + { + namespace portable + { + /** + * Internal implementation of portable reader. + */ + class IGNITE_IMPORT_EXPORT PortableWriterImpl + { + public: + /** + * Constructor. + * + * @param stream Interop stream. + * @param idRslvr Portable ID resolver. + * @param metaMgr Metadata manager. + * @param metaHnd Metadata handler. + */ + PortableWriterImpl(gridgain::impl::interop::InteropOutputStream* stream, PortableIdResolver* idRslvr, + PortableMetadataManager* metaMgr, PortableMetadataHandler* metaHnd); + + /** + * Constructor used to construct light-weight writer allowing only raw operations + * and primitive objects. + * + * @param stream Interop stream. + * @param metaMgr Metadata manager. + */ + PortableWriterImpl(gridgain::impl::interop::InteropOutputStream* stream, PortableMetadataManager* metaMgr); + + /** + * Write 8-byte signed integer. Maps to "byte" type in Java. + * + * @param val Value. + */ + void WriteInt8(const int8_t val); + + /** + * Write array of 8-byte signed integers. Maps to "byte[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteInt8Array(const int8_t* val, const int32_t len); + + /** + * Write 8-byte signed integer. Maps to "byte" type in Java. + * + * @param fieldName Field name. + * @param val Value. + */ + void WriteInt8(const char* fieldName, const int8_t val); + + /** + * Write array of 8-byte signed integers. Maps to "byte[]" type in Java. + * + * @param fieldName Field name. + * @param val Array. + * @param len Array length. + */ + void WriteInt8Array(const char* fieldName, const int8_t* val, const int32_t len); + + /** + * Write bool. Maps to "short" type in Java. + * + * @param val Value. + */ + void WriteBool(const bool val); + + /** + * Write array of bools. Maps to "bool[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteBoolArray(const bool* val, const int32_t len); + + /** + * Write bool. Maps to "short" type in Java. + * + * @param fieldName Field name. + * @param val Value. + */ + void WriteBool(const char* fieldName, const bool val); + + /** + * Write array of bools. Maps to "bool[]" type in Java. + * + * @param fieldName Field name. + * @param val Array. + * @param len Array length. + */ + void WriteBoolArray(const char* fieldName, const bool* val, const int32_t len); + + /** + * Write 16-byte signed integer. Maps to "short" type in Java. + * + * @param val Value. + */ + void WriteInt16(const int16_t val); + + /** + * Write array of 16-byte signed integers. Maps to "short[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteInt16Array(const int16_t* val, const int32_t len); + + /** + * Write 16-byte signed integer. Maps to "short" type in Java. + * + * @param fieldName Field name. + * @param val Value. + */ + void WriteInt16(const char* fieldName, const int16_t val); + + /** + * Write array of 16-byte signed integers. Maps to "short[]" type in Java. + * + * @param fieldName Field name. + * @param val Array. + * @param len Array length. + */ + void WriteInt16Array(const char* fieldName, const int16_t* val, const int32_t len); + + /** + * Write 16-byte unsigned integer. Maps to "char" type in Java. + * + * @param val Value. + */ + void WriteUInt16(const uint16_t val); + + /** + * Write array of 16-byte unsigned integers. Maps to "char[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteUInt16Array(const uint16_t* val, const int32_t len); + + /** + * Write 16-byte unsigned integer. Maps to "char" type in Java. + * + * @param fieldName Field name. + * @param val Value. + */ + void WriteUInt16(const char* fieldName, const uint16_t val); + + /** + * Write array of 16-byte unsigned integers. Maps to "char[]" type in Java. + * + * @param fieldName Field name. + * @param val Array. + * @param len Array length. + */ + void WriteUInt16Array(const char* fieldName, const uint16_t* val, const int32_t len); + + /** + * Write 32-byte signed integer. Maps to "int" type in Java. + * + * @param val Value. + */ + void WriteInt32(const int32_t val); + + /** + * Write array of 32-byte signed integers. Maps to "int[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteInt32Array(const int32_t* val, const int32_t len); + + /** + * Write 32-byte signed integer. Maps to "int" type in Java. + * + * @param fieldName Field name. + * @param val Value. + */ + void WriteInt32(const char* fieldName, const int32_t val); + + /** + * Write array of 32-byte signed integers. Maps to "int[]" type in Java. + * + * @param fieldName Field name. + * @param val Array. + * @param len Array length. + */ + void WriteInt32Array(const char* fieldName, const int32_t* val, const int32_t len); + + /** + * Write 64-byte signed integer. Maps to "long" type in Java. + * + * @param val Value. + */ + void WriteInt64(const int64_t val); + + /** + * Write array of 64-byte signed integers. Maps to "long[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteInt64Array(const int64_t* val, const int32_t len); + + /** + * Write 64-byte signed integer. Maps to "long" type in Java. + * + * @param fieldName Field name. + * @param val Value. + */ + void WriteInt64(const char* fieldName, const int64_t val); + + /** + * Write array of 64-byte signed integers. Maps to "long[]" type in Java. + * + * @param fieldName Field name. + * @param val Array. + * @param len Array length. + */ + void WriteInt64Array(const char* fieldName, const int64_t* val, const int32_t len); + + /** + * Write float. Maps to "float" type in Java. + * + * @param val Value. + */ + void WriteFloat(const float val); + + /** + * Write array of floats. Maps to "float[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteFloatArray(const float* val, const int32_t len); + + /** + * Write float. Maps to "float" type in Java. + * + * @param fieldName Field name. + * @param val Value. + */ + void WriteFloat(const char* fieldName, const float val); + + /** + * Write array of floats. Maps to "float[]" type in Java. + * + * @param fieldName Field name. + * @param val Array. + * @param len Array length. + */ + void WriteFloatArray(const char* fieldName, const float* val, const int32_t len); + + /** + * Write double. Maps to "double" type in Java. + * + * @param val Value. + */ + void WriteDouble(const double val); + + /** + * Write array of doubles. Maps to "double[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteDoubleArray(const double* val, const int32_t len); + + /** + * Write double. Maps to "double" type in Java. + * + * @param fieldName Field name. + * @param val Value. + */ + void WriteDouble(const char* fieldName, const double val); + + /** + * Write array of doubles. Maps to "double[]" type in Java. + * + * @param fieldName Field name. + * @param val Array. + * @param len Array length. + */ + void WriteDoubleArray(const char* fieldName, const double* val, const int32_t len); + + /** + * Write Guid. Maps to "UUID" type in Java. + * + * @param val Value. + */ + void WriteGuid(const Guid val); + + /** + * Write array of Guids. Maps to "UUID[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteGuidArray(const Guid* val, const int32_t len); + + /** + * Write Guid. Maps to "UUID" type in Java. + * + * @param fieldName Field name. + * @param val Value. + */ + void WriteGuid(const char* fieldName, const Guid val); + + /** + * Write array of Guids. Maps to "UUID[]" type in Java. + * + * @param fieldName Field name. + * @param val Array. + * @param len Array length. + */ + void WriteGuidArray(const char* fieldName, const Guid* val, const int32_t len); + + /** + * Write string. + * + * @param val String. + * @param len String length (characters). + */ + void WriteString(const char* val, const int32_t len); + + /** + * Write string. + * + * @param fieldName Field name. + * @param val String. + * @param len String length (characters). + */ + void WriteString(const char* fieldName, const char* val, const int32_t len); + + /** + * Start string array write. + * + * @param typ Collection type. + * @return Session ID. + */ + int32_t WriteStringArray(); + + /** + * Start string array write. + * + * @param fieldName Field name. + * @return Session ID. + */ + int32_t WriteStringArray(const char* fieldName); + + /** + * Write string element. + * + * @param id Session ID. + * @param val Value. + * @param len Length. + */ + void WriteStringElement(int32_t id, const char* val, int32_t len); + + /** + * Write NULL value. + */ + void WriteNull(); + + /** + * Write NULL value. + * + * @param fieldName Field name. + */ + void WriteNull(const char* fieldName); + + /** + * Start array write. + * + * @param typ Collection type. + * @return Session ID. + */ + int32_t WriteArray(); + + /** + * Start array write. + * + * @param fieldName Field name. + * @return Session ID. + */ + int32_t WriteArray(const char* fieldName); + + /** + * Start collection write. + * + * @param typ Collection type. + * @return Session ID. + */ + int32_t WriteCollection(gridgain::portable::CollectionType typ); + + /** + * Start collection write. + * + * @param fieldName Field name. + * @param typ Collection type. + * @return Session ID. + */ + int32_t WriteCollection(const char* fieldName, gridgain::portable::CollectionType typ); + + /** + * Start map write. + * + * @param typ Map type. + * @return Session ID. + */ + int32_t WriteMap(gridgain::portable::MapType typ); + + /** + * Start map write. + * + * @param fieldName Field name. + * @param typ Map type. + * @return Session ID. + */ + int32_t WriteMap(const char* fieldName, gridgain::portable::MapType typ); + + /** + * Write collection element. + * + * @param id Session ID. + * @param val Value. + */ + template<typename T> + void WriteElement(int32_t id, T val) + { + CheckSession(id); + + WriteTopObject<T>(val); + + elemCnt++; + } + + /** + * Write map element. + * + * @param id Session ID. + * @param key Key. + * @param val Value. + */ + template<typename K, typename V> + void WriteElement(int32_t id, K key, V val) + { + CheckSession(id); + + WriteTopObject<K>(key); + WriteTopObject<V>(val); + + elemCnt++; + } + + /** + * Commit container write session. + * + * @param id Session ID. + */ + void CommitContainer(int32_t id); + + /** + * Write object. + * + * @param val Object. + */ + template<typename T> + void WriteObject(T val) + { + CheckRawMode(true); + + WriteTopObject(val); + } + + /** + * Write object. + * + * @param fieldName Field name. + * @param val Object. + */ + template<typename T> + void WriteObject(const char* fieldName, T val) + { + CheckRawMode(false); + + // 1. Write field ID. + WriteFieldId(fieldName, GG_TYPE_OBJECT); + + // 2. Preserve space for length. + int32_t lenPos = stream->Position(); + stream->Position(lenPos + 4); + + // 3. Actual write. + WriteTopObject(val); + + // 4. Write field length. + int32_t len = stream->Position() - lenPos - 4; + stream->WriteInt32(lenPos, len); + } + + /** + * Set raw mode. + */ + void SetRawMode(); + + /** + * Get raw position. + */ + int32_t GetRawPosition(); + + /** + * Write object. + * + * @param obj Object to write. + */ + template<typename T> + void WriteTopObject(const T& obj) + { + gridgain::portable::PortableType<T> type; + + if (type.IsNull(obj)) + stream->WriteInt8(GG_HDR_NULL); + else + { + TemplatedPortableIdResolver<T> idRslvr(type); + ignite::common::concurrent::SharedPointer<PortableMetadataHandler> metaHnd; + + if (metaMgr) + metaHnd = metaMgr->GetHandler(idRslvr.GetTypeId()); + + PortableWriterImpl writerImpl(stream, &idRslvr, metaMgr, metaHnd.Get()); + gridgain::portable::PortableWriter writer(&writerImpl); + + int32_t pos = stream->Position(); + + stream->WriteInt8(GG_HDR_FULL); + stream->WriteBool(true); + stream->WriteInt32(idRslvr.GetTypeId()); + stream->WriteInt32(type.GetHashCode(obj)); + + stream->Position(pos + 18); + + type.Write(writer, obj); + + int32_t len = stream->Position() - pos; + + stream->WriteInt32(pos + 10, len); + stream->WriteInt32(pos + 14, writerImpl.GetRawPosition() - pos); + + if (metaMgr) + metaMgr->SubmitHandler(type.GetTypeName(), idRslvr.GetTypeId(), metaHnd.Get()); + } + } + + /** + * Get underlying stream. + * + * @return Stream. + */ + impl::interop::InteropOutputStream* GetStream(); + private: + /** Underlying stream. */ + gridgain::impl::interop::InteropOutputStream* stream; + + /** ID resolver. */ + PortableIdResolver* idRslvr; + + /** Metadata manager. */ + PortableMetadataManager* metaMgr; + + /** Metadata handler. */ + PortableMetadataHandler* metaHnd; + + /** Type ID. */ + int32_t typeId; + + /** Elements write session ID generator. */ + int32_t elemIdGen; + + /** Elements write session ID. */ + int32_t elemId; + + /** Elements count. */ + int32_t elemCnt; + + /** Elements start position. */ + int32_t elemPos; + + /** Raw data offset. */ + int32_t rawPos; + + IGNITE_NO_COPY_ASSIGNMENT(PortableWriterImpl) + + /** + * Write a primitive value to stream in raw mode. + * + * @param val Value. + * @param func Stream function. + */ + template<typename T> + void WritePrimitiveRaw( + const T val, + void(*func)(interop::InteropOutputStream*, T) + ) + { + CheckRawMode(true); + CheckSingleMode(true); + + func(stream, val); + } + + /** + * Write a primitive array to stream in raw mode. + * + * @param val Value. + * @param len Array length. + * @param func Stream function. + * @param hdr Header. + */ + template<typename T> + void WritePrimitiveArrayRaw( + const T* val, + const int32_t len, + void(*func)(interop::InteropOutputStream*, const T*, const int32_t), + const int8_t hdr + ) + { + CheckRawMode(true); + CheckSingleMode(true); + + if (val) + { + stream->WriteInt8(hdr); + stream->WriteInt32(len); + func(stream, val, len); + } + else + stream->WriteInt8(GG_HDR_NULL); + } + + /** + * Write a primitive value to stream. + * + * @param fieldName Field name. + * @param val Value. + * @param func Stream function. + * @param typ Field type ID. + * @param len Field length. + */ + template<typename T> + void WritePrimitive( + const char* fieldName, + const T val, + void(*func)(interop::InteropOutputStream*, T), + const int8_t typ, + const int32_t len + ) + { + CheckRawMode(false); + CheckSingleMode(true); + + WriteFieldId(fieldName, typ); + + stream->WriteInt32(1 + len); + stream->WriteInt8(typ); + + func(stream, val); + } + + /** + * Write a primitive array to stream. + * + * @param fieldName Field name. + * @param val Value. + * @param len Array length. + * @param func Stream function. + * @param hdr Header. + * @param lenShift Length shift. + */ + template<typename T> + void WritePrimitiveArray( + const char* fieldName, + const T* val, + const int32_t len, + void(*func)(interop::InteropOutputStream*, const T*, const int32_t), + const int8_t hdr, + const int32_t lenShift + ) + { + CheckRawMode(false); + CheckSingleMode(true); + + WriteFieldId(fieldName, hdr); + + if (val) + { + stream->WriteInt32(5 + (len << lenShift)); + stream->WriteInt8(hdr); + stream->WriteInt32(len); + func(stream, val, len); + } + else + { + stream->WriteInt32(1); + stream->WriteInt8(GG_HDR_NULL); + } + } + + /** + * Check raw mode. + * + * @param expected Expected raw mode of the reader. + */ + void CheckRawMode(bool expected); + + /** + * Check whether writer is currently operating in single mode. + * + * @param expected Expected value. + */ + void CheckSingleMode(bool expected); + + /** + * Start new container writer session. + * + * @param expRawMode Expected raw mode. + */ + void StartContainerSession(bool expRawMode); + + /** + * Check whether session ID matches. + * + * @param ses Expected session ID. + */ + void CheckSession(int32_t expSes); + + /** + * Write field ID. + * + * @param fieldName Field name. + * @param fieldTypeId Field type ID. + */ + void WriteFieldId(const char* fieldName, int32_t fieldTypeId); + + /** + * Write field ID and skip field length. + * + * @param fieldName Field name. + * @param fieldTypeId Field type ID. + */ + void WriteFieldIdSkipLength(const char* fieldName, int32_t fieldTypeId); + + /** + * Write field ID and length. + * + * @param fieldName Field name. + * @param fieldTypeId Field type ID. + * @param len Length. + */ + void WriteFieldIdAndLength(const char* fieldName, int32_t fieldTypeId, int32_t len); + + /** + * Write primitive value. + * + * @param obj Value. + * @param func Stream function. + * @param hdr Header. + */ + template<typename T> + void WriteTopObject0(const T obj, void(*func)(impl::interop::InteropOutputStream*, T), const int8_t hdr) + { + stream->WriteInt8(hdr); + func(stream, obj); + } + }; + + template<> + void IGNITE_IMPORT_EXPORT PortableWriterImpl::WriteTopObject(const int8_t& obj); + + template<> + void IGNITE_IMPORT_EXPORT PortableWriterImpl::WriteTopObject(const bool& obj); + + template<> + void IGNITE_IMPORT_EXPORT PortableWriterImpl::WriteTopObject(const int16_t& obj); + + template<> + void IGNITE_IMPORT_EXPORT PortableWriterImpl::WriteTopObject(const uint16_t& obj); + + template<> + void IGNITE_IMPORT_EXPORT PortableWriterImpl::WriteTopObject(const int32_t& obj); + + template<> + void IGNITE_IMPORT_EXPORT PortableWriterImpl::WriteTopObject(const int64_t& obj); + + template<> + void IGNITE_IMPORT_EXPORT PortableWriterImpl::WriteTopObject(const float& obj); + + template<> + void IGNITE_IMPORT_EXPORT PortableWriterImpl::WriteTopObject(const double& obj); + + template<> + void IGNITE_IMPORT_EXPORT PortableWriterImpl::WriteTopObject(const Guid& obj); + + template<> + inline void IGNITE_IMPORT_EXPORT PortableWriterImpl::WriteTopObject(const std::string& obj) + { + const char* obj0 = obj.c_str(); + + int32_t len = static_cast<int32_t>(strlen(obj0)); + + stream->WriteInt8(GG_TYPE_STRING); + + PortableUtils::WriteString(stream, obj0, len); + } + } + } +} + +#endif \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/1e18fa32/modules/platform/src/main/cpp/core/include/gridgain/portable/portable.h ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/include/gridgain/portable/portable.h b/modules/platform/src/main/cpp/core/include/gridgain/portable/portable.h new file mode 100644 index 0000000..b7bf323 --- /dev/null +++ b/modules/platform/src/main/cpp/core/include/gridgain/portable/portable.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) GridGain Systems. All Rights Reserved. + * _________ _____ __________________ _____ + * __ ____/___________(_)______ /__ ____/______ ____(_)_______ + * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ + * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / + * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ + */ + +#ifndef _GRIDGAIN_PORTABLE +#define _GRIDGAIN_PORTABLE + +#include "gridgain/portable/portable_consts.h" +#include "gridgain/portable/portable_containers.h" +#include "gridgain/portable/portable_type.h" +#include "gridgain/portable/portable_raw_reader.h" +#include "gridgain/portable/portable_raw_writer.h" +#include "gridgain/portable/portable_reader.h" +#include "gridgain/portable/portable_writer.h" + +#endif \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/1e18fa32/modules/platform/src/main/cpp/core/include/gridgain/portable/portable_consts.h ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/include/gridgain/portable/portable_consts.h b/modules/platform/src/main/cpp/core/include/gridgain/portable/portable_consts.h new file mode 100644 index 0000000..380ea83 --- /dev/null +++ b/modules/platform/src/main/cpp/core/include/gridgain/portable/portable_consts.h @@ -0,0 +1,98 @@ +/* + * Copyright (C) GridGain Systems. All Rights Reserved. + * _________ _____ __________________ _____ + * __ ____/___________(_)______ /__ ____/______ ____(_)_______ + * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ + * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / + * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ + */ + +#ifndef _GRIDGAIN_PORTABLE_CONSTS +#define _GRIDGAIN_PORTABLE_CONSTS + +#include <ignite/common/common.h> + +namespace gridgain +{ + namespace portable + { + /** + * Portable collection types. + */ + enum CollectionType + { + /** + * Undefined. Maps to ArrayList in Java. + */ + GG_COLLECTION_UNDEFINED = 0, + + /** + * Array list. Maps to ArrayList in Java. + */ + GG_COLLECTION_ARRAY_LIST = 1, + + /** + * Linked list. Maps to LinkedList in Java. + */ + GG_COLLECTION_LINKED_LIST = 2, + + /** + * Hash set. Maps to HashSet in Java. + */ + GG_COLLECTION_HASH_SET = 3, + + /** + * Linked hash set. Maps to LinkedHashSet in Java. + */ + GG_COLLECTION_LINKED_HASH_SET = 4, + + /** + * Tree set. Maps to TreeSet in Java. + */ + GG_COLLECTION_TREE_SET = 5, + + /** + * Concurrent skip list set. Maps to ConcurrentSkipListSet in Java. + */ + GG_COLLECTION_CONCURRENT_SKIP_LIST_SET = 6 + }; + + /** + * Portable map types. + */ + enum MapType + { + /** + * Undefined. Maps to HashMap in Java. + */ + GG_MAP_UNDEFINED = 0, + + /** + * Hash map. Maps to HashMap in Java. + */ + GG_MAP_HASH_MAP = 1, + + /** + * Linked hash map. Maps to LinkedHashMap in Java. + */ + GG_MAP_LINKED_HASH_MAP = 2, + + /** + * Tree map. Maps to TreeMap in Java. + */ + GG_MAP_TREE_MAP = 3, + + /** + * Concurrent hash map. Maps to ConcurrentHashMap in Java. + */ + GG_MAP_CONCURRENT_HASH_MAP = 4, + + /** + * Properties map. Maps to Properties in Java. + */ + GG_MAP_PROPERTIES_MAP = 5 + }; + } +} + +#endif \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/1e18fa32/modules/platform/src/main/cpp/core/include/gridgain/portable/portable_containers.h ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/include/gridgain/portable/portable_containers.h b/modules/platform/src/main/cpp/core/include/gridgain/portable/portable_containers.h new file mode 100644 index 0000000..ce264a8 --- /dev/null +++ b/modules/platform/src/main/cpp/core/include/gridgain/portable/portable_containers.h @@ -0,0 +1,516 @@ +/* + * Copyright (C) GridGain Systems. All Rights Reserved. + * _________ _____ __________________ _____ + * __ ____/___________(_)______ /__ ____/______ ____(_)_______ + * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ + * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / + * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ + */ +#ifndef _GRIDGAIN_PORTABLE_CONTAINERS +#define _GRIDGAIN_PORTABLE_CONTAINERS + +#include <stdint.h> + +#include "gridgain/impl/portable/portable_writer_impl.h" +#include "gridgain/impl/portable/portable_reader_impl.h" +#include "gridgain/impl/utils.h" +#include "gridgain/portable/portable_consts.h" + +namespace gridgain +{ + namespace portable + { + /** + * Portable string array writer. + */ + class IGNITE_IMPORT_EXPORT PortableStringArrayWriter + { + public: + /** + * Constructor. + * + * @param id Identifier. + * @param impl Writer. + */ + PortableStringArrayWriter(impl::portable::PortableWriterImpl* impl, const int32_t id); + + /** + * Write string. + * + * @param val Null-terminated character sequence. + */ + void Write(const char* val); + + /** + * Write string. + * + * @param val String. + * @param len String length (characters). + */ + void Write(const char* val, const int32_t len); + + /** + * Write string. + * + * @param val String. + */ + void Write(const std::string& val) + { + Write(val.c_str()); + } + + /** + * Close the writer. + */ + void Close(); + private: + /** Implementation delegate. */ + impl::portable::PortableWriterImpl* impl; + + /** Idnetifier. */ + const int32_t id; + }; + + /** + * Portable collection writer. + */ + template<typename T> + class IGNITE_IMPORT_EXPORT PortableArrayWriter + { + public: + /** + * Constructor. + * + * @param impl Writer. + * @param id Identifier. + */ + PortableArrayWriter(impl::portable::PortableWriterImpl* impl, const int32_t id) : impl(impl), id(id) + { + // No-op. + } + + /** + * Write a value. + * + * @param val Value. + */ + void Write(const T& val) + { + impl->WriteElement<T>(id, val); + } + + /** + * Close the writer. + */ + void Close() + { + impl->CommitContainer(id); + } + private: + /** Implementation delegate. */ + impl::portable::PortableWriterImpl* impl; + + /** Idnetifier. */ + const int32_t id; + }; + + /** + * Portable collection writer. + */ + template<typename T> + class IGNITE_IMPORT_EXPORT PortableCollectionWriter + { + public: + /** + * Constructor. + * + * @param impl Writer. + * @param id Identifier. + */ + PortableCollectionWriter(impl::portable::PortableWriterImpl* impl, const int32_t id) : impl(impl), id(id) + { + // No-op. + } + + /** + * Write a value. + * + * @param val Value. + */ + void Write(const T& val) + { + impl->WriteElement<T>(id, val); + } + + /** + * Close the writer. + */ + void Close() + { + impl->CommitContainer(id); + } + private: + /** Implementation delegate. */ + impl::portable::PortableWriterImpl* impl; + + /** Identifier. */ + const int32_t id; + }; + + /** + * Portable map writer. + */ + template<typename K, typename V> + class IGNITE_IMPORT_EXPORT PortableMapWriter + { + public: + /** + * Constructor. + * + * @param impl Writer. + */ + PortableMapWriter(impl::portable::PortableWriterImpl* impl, const int32_t id) : impl(impl), id(id) + { + // No-op. + } + + /** + * Write a value. + * + * @param key Key. + * @param val Value. + */ + void Write(const K& key, const V& val) + { + impl->WriteElement<K, V>(id, key, val); + } + + /** + * Close the writer. + */ + void Close() + { + impl->CommitContainer(id); + } + private: + /** Implementation delegate. */ + impl::portable::PortableWriterImpl* impl; + + /** Identifier. */ + const int32_t id; + }; + + /** + * Portable string array reader. + */ + class IGNITE_IMPORT_EXPORT PortableStringArrayReader + { + public: + /** + * Constructor. + * + * @param impl Reader. + * @param id Identifier. + * @param size Array size. + */ + PortableStringArrayReader(impl::portable::PortableReaderImpl* impl, const int32_t id, const int32_t size); + + /** + * Check whether next element is available for read. + * + * @return True if available. + */ + bool HasNext(); + + /** + * Get next element. + * + * @param res Array to store data to. + * @param len Expected length of string. NULL terminator will be set in case len is + * greater than real string length. + * @return Actual amount of elements read. If "len" argument is less than actual + * array size or resulting array is set to null, nothing will be written + * to resulting array and returned value will contain required array length. + * -1 will be returned in case array in stream was null. + */ + int32_t GetNext(char* res, const int32_t len); + + /** + * Get next element. + * + * @return String. + */ + std::string GetNext() + { + int32_t len = GetNext(NULL, 0); + + if (len != -1) + { + impl::utils::SafeArray<char> arr(len + 1); + + GetNext(arr.target, len + 1); + + return std::string(arr.target); + } + else + return std::string(); + } + + /** + * Get array size. + * + * @return Size or -1 if array is NULL. + */ + int32_t GetSize(); + + /** + * Whether array is NULL. + */ + bool IsNull(); + private: + /** Implementation delegate. */ + impl::portable::PortableReaderImpl* impl; + + /** Identifier. */ + const int32_t id; + + /** Size. */ + const int32_t size; + }; + + /** + * Portable array reader. + */ + template<typename T> + class PortableArrayReader + { + public: + /** + * Constructor. + * + * @param impl Reader. + * @param id Identifier. + * @param size Array size. + */ + PortableArrayReader(impl::portable::PortableReaderImpl* impl, const int32_t id, const int32_t size) : + impl(impl), id(id), size(size) + { + // No-op. + } + + /** + * Check whether next element is available for read. + * + * @return True if available. + */ + bool HasNext() + { + return impl->HasNextElement(id); + } + + /** + * Read next element. + * + * @return Next element. + */ + T GetNext() + { + return impl->ReadElement<T>(id); + } + + /** + * Get array size. + * + * @return Size or -1 if array is NULL. + */ + int32_t GetSize() + { + return size; + } + + /** + * Whether array is NULL. + */ + bool IsNull() + { + return size == -1; + } + private: + /** Implementation delegate. */ + impl::portable::PortableReaderImpl* impl; + + /** Identifier. */ + const int32_t id; + + /** Size. */ + const int32_t size; + }; + + /** + * Portable collection reader. + */ + template<typename T> + class PortableCollectionReader + { + public: + /** + * Constructor. + * + * @param impl Reader. + * @param id Identifier. + * @param type Collection type. + * @param size Collection size. + */ + PortableCollectionReader(impl::portable::PortableReaderImpl* impl, const int32_t id, + const CollectionType type, const int32_t size) : impl(impl), id(id), type(type), size(size) + { + // No-op. + } + + /** + * Check whether next element is available for read. + * + * @return True if available. + */ + bool HasNext() + { + return impl->HasNextElement(id); + } + + /** + * Read next element. + * + * @return Next element. + */ + T GetNext() + { + return impl->ReadElement<T>(id); + } + + /** + * Get collection type. + * + * @return Type. + */ + CollectionType GetType() + { + return type; + } + + /** + * Get collection size. + * + * @return Size or -1 if collection is NULL. + */ + int32_t GetSize() + { + return size; + } + + /** + * Whether collection is NULL. + */ + bool IsNull() + { + return size == -1; + } + private: + /** Implementation delegate. */ + impl::portable::PortableReaderImpl* impl; + + /** Identifier. */ + const int32_t id; + + /** Collection type. */ + const CollectionType type; + + /** Size. */ + const int32_t size; + }; + + /** + * Portable map reader. + */ + template<typename K, typename V> + class PortableMapReader + { + public: + /** + * Constructor. + * + * @param impl Reader. + * @param id Identifier. + * @param type Map type. + * @param size Map size. + */ + PortableMapReader(impl::portable::PortableReaderImpl* impl, const int32_t id, const MapType type, + const int32_t size) : impl(impl), id(id), type(type), size(size) + { + // No-op. + } + + /** + * Check whether next element is available for read. + * + * @return True if available. + */ + bool HasNext() + { + return impl->HasNextElement(id); + } + + /** + * Read next element. + * + * @param key Key. + * @param val Value. + */ + void GetNext(K* key, V* val) + { + return impl->ReadElement<K, V>(id, key, val); + } + + /** + * Get map type. + * + * @return Type. + */ + MapType GetType() + { + return type; + } + + /** + * Get map size. + * + * @return Size or -1 if map is NULL. + */ + int32_t GetSize() + { + return size; + } + + /** + * Whether map is NULL. + */ + bool IsNull() + { + return size == -1; + } + private: + /** Implementation delegate. */ + impl::portable::PortableReaderImpl* impl; + + /** Identifier. */ + const int32_t id; + + /** Map type. */ + const MapType type; + + /** Size. */ + const int32_t size; + }; + } +} + +#endif \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/1e18fa32/modules/platform/src/main/cpp/core/include/gridgain/portable/portable_raw_reader.h ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/include/gridgain/portable/portable_raw_reader.h b/modules/platform/src/main/cpp/core/include/gridgain/portable/portable_raw_reader.h new file mode 100644 index 0000000..8afbfde --- /dev/null +++ b/modules/platform/src/main/cpp/core/include/gridgain/portable/portable_raw_reader.h @@ -0,0 +1,316 @@ +/* + * Copyright (C) GridGain Systems. All Rights Reserved. + * _________ _____ __________________ _____ + * __ ____/___________(_)______ /__ ____/______ ____(_)_______ + * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ + * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / + * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ + */ + +#ifndef _GRIDGAIN_PORTABLE_RAW_READER +#define _GRIDGAIN_PORTABLE_RAW_READER + +#include <stdint.h> +#include <string> + +#include <ignite/common/common.h> + +#include "gridgain/impl/portable/portable_reader_impl.h" +#include "gridgain/portable/portable_consts.h" +#include "gridgain/portable/portable_containers.h" +#include "gridgain/guid.h" + +namespace gridgain +{ + namespace portable + { + /** + * Portable raw reader. + */ + class IGNITE_IMPORT_EXPORT PortableRawReader + { + public: + /** + * Constructor. + * + * @param impl Implementation. + */ + PortableRawReader(gridgain::impl::portable::PortableReaderImpl* impl); + + /** + * Read 8-byte signed integer. Maps to "byte" type in Java. + * + * @return Result. + */ + int8_t ReadInt8(); + + /** + * Read array of 8-byte signed integers. Maps to "byte[]" type in Java. + * + * @param res Array to store data to. + * @param len Expected length of array. + * @return Actual amount of elements read. If "len" argument is less than actual + * array size or resulting array is set to null, nothing will be written + * to resulting array and returned value will contain required array length. + * -1 will be returned in case array in stream was null. + */ + int32_t ReadInt8Array(int8_t* res, const int32_t len); + + /** + * Read bool. Maps to "boolean" type in Java. + * + * @return Result. + */ + bool ReadBool(); + + /** + * Read array of bools. Maps to "boolean[]" type in Java. + * + * @param res Array to store data to. + * @param len Expected length of array. + * @return Actual amount of elements read. If "len" argument is less than actual + * array size or resulting array is set to null, nothing will be written + * to resulting array and returned value will contain required array length. + * -1 will be returned in case array in stream was null. + */ + int32_t ReadBoolArray(bool* res, const int32_t len); + + /** + * Read 16-byte signed integer. Maps to "short" type in Java. + * + * @return Result. + */ + int16_t ReadInt16(); + + /** + * Read array of 16-byte signed integers. Maps to "short[]" type in Java. + * + * @param res Array to store data to. + * @param len Expected length of array. + * @return Actual amount of elements read. If "len" argument is less than actual + * array size or resulting array is set to null, nothing will be written + * to resulting array and returned value will contain required array length. + * -1 will be returned in case array in stream was null. + */ + int32_t ReadInt16Array(int16_t* res, const int32_t len); + + /** + * Read 16-byte unsigned integer. Maps to "char" type in Java. + * + * @return Result. + */ + uint16_t ReadUInt16(); + + /** + * Read array of 16-byte unsigned integers. Maps to "char[]" type in Java. + * + * @param res Array to store data to. + * @param len Expected length of array. + * @return Actual amount of elements read. If "len" argument is less than actual + * array size or resulting array is set to null, nothing will be written + * to resulting array and returned value will contain required array length. + * -1 will be returned in case array in stream was null. + */ + int32_t ReadUInt16Array(uint16_t* res, const int32_t len); + + /** + * Read 32-byte signed integer. Maps to "int" type in Java. + * + * @return Result. + */ + int32_t ReadInt32(); + + /** + * Read array of 32-byte signed integers. Maps to "int[]" type in Java. + * + * @param res Array to store data to. + * @param len Expected length of array. + * @return Actual amount of elements read. If "len" argument is less than actual + * array size or resulting array is set to null, nothing will be written + * to resulting array and returned value will contain required array length. + * -1 will be returned in case array in stream was null. + */ + int32_t ReadInt32Array(int32_t* res, const int32_t len); + + /** + * Read 64-byte signed integer. Maps to "long" type in Java. + * + * @return Result. + */ + int64_t ReadInt64(); + + /** + * Read array of 64-byte signed integers. Maps to "long[]" type in Java. + * + * @param res Array to store data to. + * @param len Expected length of array. + * @return Actual amount of elements read. If "len" argument is less than actual + * array size or resulting array is set to null, nothing will be written + * to resulting array and returned value will contain required array length. + * -1 will be returned in case array in stream was null. + */ + int32_t ReadInt64Array(int64_t* res, const int32_t len); + + /** + * Read float. Maps to "float" type in Java. + * + * @return Result. + */ + float ReadFloat(); + + /** + * Read array of floats. Maps to "float[]" type in Java. + * + * @param res Array to store data to. + * @param len Expected length of array. + * @return Actual amount of elements read. If "len" argument is less than actual + * array size or resulting array is set to null, nothing will be written + * to resulting array and returned value will contain required array length. + * -1 will be returned in case array in stream was null. + */ + int32_t ReadFloatArray(float* res, const int32_t len); + + /** + * Read double. Maps to "double" type in Java. + * + * @return Result. + */ + double ReadDouble(); + + /** + * Read array of doubles. Maps to "double[]" type in Java. + * + * @param res Array to store data to. + * @param len Expected length of array. + * @return Actual amount of elements read. If "len" argument is less than actual + * array size or resulting array is set to null, nothing will be written + * to resulting array and returned value will contain required array length. + * -1 will be returned in case array in stream was null. + */ + int32_t ReadDoubleArray(double* res, const int32_t len); + + /** + * Read Guid. Maps to "UUID" type in Java. + * + * @return Result. + */ + Guid ReadGuid(); + + /** + * Read array of Guids. Maps to "UUID[]" type in Java. + * + * @param res Array to store data to. + * @param len Expected length of array. + * @return Actual amount of elements read. If "len" argument is less than actual + * array size or resulting array is set to null, nothing will be written + * to resulting array and returned value will contain required array length. + * -1 will be returned in case array in stream was null. + */ + int32_t ReadGuidArray(Guid* res, const int32_t len); + + /** + * Read string. + * + * @param res Array to store data to. + * @param len Expected length of string. NULL terminator will be set in case len is + * greater than real string length. + * @return Actual amount of elements read. If "len" argument is less than actual + * array size or resulting array is set to null, nothing will be written + * to resulting array and returned value will contain required array length. + * -1 will be returned in case array in stream was null. + */ + int32_t ReadString(char* res, const int32_t len); + + /** + * Read string from the stream. + * + * @return String. + */ + std::string ReadString() + { + int32_t len = ReadString(NULL, 0); + + if (len != -1) + { + gridgain::impl::utils::SafeArray<char> arr(len + 1); + + ReadString(arr.target, len + 1); + + return std::string(arr.target); + } + else + return std::string(); + } + + /** + * Start string array read. + * + * @return String array reader. + */ + PortableStringArrayReader ReadStringArray(); + + /** + * Start array read. + * + * @return Array reader. + */ + template<typename T> + PortableArrayReader<T> ReadArray() + { + int32_t size; + + int32_t id = impl->ReadArray(&size); + + return PortableArrayReader<T>(impl, id, size); + } + + /** + * Start collection read. + * + * @return Collection reader. + */ + template<typename T> + PortableCollectionReader<T> ReadCollection() + { + CollectionType typ; + int32_t size; + + int32_t id = impl->ReadCollection(&typ, &size); + + return PortableCollectionReader<T>(impl, id, typ, size); + } + + /** + * Start map read. + * + * @return Map reader. + */ + template<typename K, typename V> + PortableMapReader<K, V> ReadMap() + { + MapType typ; + int32_t size; + + int32_t id = impl->ReadMap(&typ, &size); + + return PortableMapReader<K, V>(impl, id, typ, size); + } + + /** + * Read object. + * + * @return Object. + */ + template<typename T> + T ReadObject() + { + return impl->ReadObject<T>(); + } + private: + /** Implementation delegate. */ + gridgain::impl::portable::PortableReaderImpl* impl; + }; + } +} + +#endif \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/1e18fa32/modules/platform/src/main/cpp/core/include/gridgain/portable/portable_raw_writer.h ---------------------------------------------------------------------- diff --git a/modules/platform/src/main/cpp/core/include/gridgain/portable/portable_raw_writer.h b/modules/platform/src/main/cpp/core/include/gridgain/portable/portable_raw_writer.h new file mode 100644 index 0000000..e51b9f3 --- /dev/null +++ b/modules/platform/src/main/cpp/core/include/gridgain/portable/portable_raw_writer.h @@ -0,0 +1,292 @@ +/* + * Copyright (C) GridGain Systems. All Rights Reserved. + * _________ _____ __________________ _____ + * __ ____/___________(_)______ /__ ____/______ ____(_)_______ + * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ + * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / + * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ + */ + +#ifndef _GRIDGAIN_PORTABLE_RAW_WRITER +#define _GRIDGAIN_PORTABLE_RAW_WRITER + +#include <stdint.h> + +#include <ignite/common/common.h> + +#include "gridgain/impl/portable/portable_writer_impl.h" +#include "gridgain/portable/portable_consts.h" +#include "gridgain/portable/portable_containers.h" +#include "gridgain/guid.h" + +namespace gridgain +{ + namespace portable + { + /** + * Portable raw writer. + */ + class IGNITE_IMPORT_EXPORT PortableRawWriter + { + public: + /** + * Constructor. + * + * @param impl Implementation. + */ + PortableRawWriter(gridgain::impl::portable::PortableWriterImpl* impl); + + /** + * Write 8-byte signed integer. Maps to "byte" type in Java. + * + * @param val Value. + */ + void WriteInt8(const int8_t val); + + /** + * Write array of 8-byte signed integers. Maps to "byte[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteInt8Array(const int8_t* val, const int32_t len); + + /** + * Write bool. Maps to "short" type in Java. + * + * @param val Value. + */ + void WriteBool(const bool val); + + /** + * Write array of bools. Maps to "bool[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteBoolArray(const bool* val, const int32_t len); + + /** + * Write 16-byte signed integer. Maps to "short" type in Java. + * + * @param val Value. + */ + void WriteInt16(const int16_t val); + + /** + * Write array of 16-byte signed integers. Maps to "short[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteInt16Array(const int16_t* val, const int32_t len); + + /** + * Write 16-byte unsigned integer. Maps to "char" type in Java. + * + * @param val Value. + */ + void WriteUInt16(const uint16_t val); + + /** + * Write array of 16-byte unsigned integers. Maps to "char[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteUInt16Array(const uint16_t* val, const int32_t len); + + /** + * Write 32-byte signed integer. Maps to "int" type in Java. + * + * @param val Value. + */ + void WriteInt32(const int32_t val); + + /** + * Write array of 32-byte signed integers. Maps to "int[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteInt32Array(const int32_t* val, const int32_t len); + + /** + * Write 64-byte signed integer. Maps to "long" type in Java. + * + * @param val Value. + */ + void WriteInt64(const int64_t val); + + /** + * Write array of 64-byte signed integers. Maps to "long[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteInt64Array(const int64_t* val, const int32_t len); + + /** + * Write float. Maps to "float" type in Java. + * + * @param val Value. + */ + void WriteFloat(const float val); + + /** + * Write array of floats. Maps to "float[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteFloatArray(const float* val, const int32_t len); + + /** + * Write double. Maps to "double" type in Java. + * + * @param val Value. + */ + void WriteDouble(const double val); + + /** + * Write array of doubles. Maps to "double[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteDoubleArray(const double* val, const int32_t len); + + /** + * Write Guid. Maps to "UUID" type in Java. + * + * @param val Value. + */ + void WriteGuid(const Guid val); + + /** + * Write array of Guids. Maps to "UUID[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteGuidArray(const Guid* val, const int32_t len); + + /** + * Write string. + * + * @param val Null-terminated character array. + */ + void WriteString(const char* val); + + /** + * Write string. + * + * @param val String. + * @param len String length (characters). + */ + void WriteString(const char* val, const int32_t len); + + /** + * Write string. + * + * @param val String. + */ + void WriteString(const std::string& val) + { + WriteString(val.c_str()); + } + + /** + * Start string array write. + * + * @return String array writer. + */ + PortableStringArrayWriter WriteStringArray(); + + /** + * Write NULL value. + */ + void WriteNull(); + + /** + * Start array write. + * + * @return Array writer. + */ + template<typename T> + PortableArrayWriter<T> WriteArray() + { + int32_t id = impl->WriteArray(); + + return PortableArrayWriter<T>(impl, id); + } + + /** + * Start collection write. + * + * @return Collection writer. + */ + template<typename T> + PortableCollectionWriter<T> WriteCollection() + { + return WriteCollection<T>(GG_COLLECTION_UNDEFINED); + } + + /** + * Start collection write. + * + * @param type Collection type. + * @return Collection writer. + */ + template<typename T> + PortableCollectionWriter<T> WriteCollection(gridgain::portable::CollectionType typ) + { + int32_t id = impl->WriteCollection(typ); + + return PortableCollectionWriter<T>(impl, id); + } + + /** + * Start map write. + * + * @param typ Map type. + * @return Map writer. + */ + template<typename K, typename V> + PortableMapWriter<K, V> WriteMap() + { + return WriteMap<K, V>(GG_MAP_UNDEFINED); + } + + /** + * Start map write. + * + * @param typ Map type. + * @return Map writer. + */ + template<typename K, typename V> + PortableMapWriter<K, V> WriteMap(gridgain::portable::MapType typ) + { + int32_t id = impl->WriteMap(typ); + + return PortableMapWriter<K, V>(impl, id); + } + + /** + * Write object. + * + * @param val Object. + */ + template<typename T> + void WriteObject(T val) + { + impl->WriteObject<T>(val); + } + private: + /** Implementation delegate. */ + gridgain::impl::portable::PortableWriterImpl* impl; + }; + } +} + +#endif \ No newline at end of file
