http://git-wip-us.apache.org/repos/asf/ignite/blob/764c97b9/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_utils.h ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_utils.h b/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_utils.h new file mode 100644 index 0000000..88130d8 --- /dev/null +++ b/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_utils.h @@ -0,0 +1,546 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _IGNITE_IMPL_BINARY_BINARY_UTILS +#define _IGNITE_IMPL_BINARY_BINARY_UTILS + +#include <stdint.h> + +#include "ignite/common/utils.h" + +#include "ignite/guid.h" +#include "ignite/date.h" +#include "ignite/timestamp.h" + +namespace ignite +{ + namespace impl + { + namespace interop + { + class InteropInputStream; + class InteropOutputStream; + } + + namespace binary + { + /** + * Binary uilts. + */ + class IGNITE_IMPORT_EXPORT BinaryUtils + { + 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 read Date from stream. + * + * @param stream Stream. + * @param res Value. + */ + static Date ReadDate(interop::InteropInputStream* stream); + + /** + * Utility method to write Date to stream. + * + * @param stream Stream. + * @param val Value. + */ + static void WriteDate(interop::InteropOutputStream* stream, const Date val); + + /** + * Utility method to read Timestamp from stream. + * + * @param stream Stream. + * @param res Value. + */ + static Timestamp ReadTimestamp(interop::InteropInputStream* stream); + + /** + * Utility method to write Timestamp to stream. + * + * @param stream Stream. + * @param val Value. + */ + static void WriteTimestamp(interop::InteropOutputStream* stream, const Timestamp 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); + + /** + * Convert Date type to standard C type time_t. + * + * @param date Date type value. + * @return Corresponding value of time_t. + */ + static inline time_t DateToCTime(const Date& date) + { + return static_cast<time_t>(date.GetSeconds()); + } + + /** + * Convert Timestamp type to standard C type time_t. + * + * @param ts Timestamp type value. + * @return Corresponding value of time_t. + */ + static inline time_t TimestampToCTime(const Timestamp& ts) + { + return static_cast<time_t>(ts.GetSeconds()); + } + + /** + * Convert Date type to standard C type time_t. + * + * @param date Date type value. + * @param ctime Corresponding value of struct tm. + * @return True on success. + */ + static inline bool DateToCTm(const Date& date, tm& ctime) + { + time_t tmt = DateToCTime(date); + + return common::IgniteGmTime(tmt, ctime); + } + + /** + * Convert Timestamp type to standard C type struct tm. + * + * @param ts Timestamp type value. + * @param ctime Corresponding value of struct tm. + * @return True on success. + */ + static inline bool TimestampToCTm(const Timestamp& ts, tm& ctime) + { + time_t tmt = TimestampToCTime(ts); + + return common::IgniteGmTime(tmt, ctime); + } + + /** + * Convert standard C type time_t to Date struct tm. + * + * @param ctime Standard C type time_t. + * @return Corresponding value of Date. + */ + static inline Date CTimeToDate(time_t ctime) + { + return Date(ctime * 1000); + } + + /** + * Convert standard C type time_t to Timestamp type. + * + * @param ctime Standard C type time_t. + * @param ns Nanoseconds second fraction. + * @return Corresponding value of Timestamp. + */ + static inline Timestamp CTimeToTimestamp(time_t ctime, int32_t ns) + { + return Timestamp(ctime, ns); + } + + /** + * Convert standard C type struct tm to Date type. + * + * @param ctime Standard C type struct tm. + * @return Corresponding value of Date. + */ + static inline Date CTmToDate(const tm& ctime) + { + time_t time = common::IgniteTimeGm(ctime); + + return CTimeToDate(time); + } + + /** + * Convert standard C type struct tm to Timestamp type. + * + * @param ctime Standard C type struct tm. + * @param ns Nanoseconds second fraction. + * @return Corresponding value of Timestamp. + */ + static inline Timestamp CTmToTimestamp(const tm& ctime, int32_t ns) + { + time_t time = common::IgniteTimeGm(ctime); + + return CTimeToTimestamp(time, ns); + } + + /** + * Make Date in human understandable way. + * + * Created Date uses GMT timezone. + * + * @param year Year. + * @param month Month. + * @param day Day. + * @param hour Hour. + * @param min Min. + * @param sec Sec. + * @return Date. + */ + static Date MakeDateGmt(int year = 1900, int month = 1, + int day = 1, int hour = 0, int min = 0, int sec = 0); + + /** + * Make Date in human understandable way. + * + * Created Date uses local timezone. + * + * @param year Year. + * @param month Month. + * @param day Day. + * @param hour Hour. + * @param min Min. + * @param sec Sec. + * @return Date. + */ + static Date MakeDateLocal(int year = 1900, int month = 1, + int day = 1, int hour = 0, int min = 0, int sec = 0); + + /** + * Make Date in human understandable way. + * + * Created Timestamp uses GMT timezone. + * + * @param year Year. + * @param month Month. + * @param day Day. + * @param hour Hour. + * @param min Minute. + * @param sec Second. + * @param ns Nanosecond. + * @return Timestamp. + */ + static Timestamp MakeTimestampGmt(int year = 1900, int month = 1, + int day = 1, int hour = 0, int min = 0, int sec = 0, long ns = 0); + + /** + * Make Date in human understandable way. + * + * Created Timestamp uses Local timezone. + * + * @param year Year. + * @param month Month. + * @param day Day. + * @param hour Hour. + * @param min Minute. + * @param sec Second. + * @param ns Nanosecond. + * @return Timestamp. + */ + static Timestamp MakeTimestampLocal(int year = 1900, int month = 1, + int day = 1, int hour = 0, int min = 0, int sec = 0, long ns = 0); + }; + } + } +} + +#endif //_IGNITE_IMPL_BINARY_BINARY_UTILS
http://git-wip-us.apache.org/repos/asf/ignite/blob/764c97b9/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_writer_impl.h ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_writer_impl.h b/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_writer_impl.h new file mode 100644 index 0000000..fda507f --- /dev/null +++ b/modules/platforms/cpp/binary/include/ignite/impl/binary/binary_writer_impl.h @@ -0,0 +1,985 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _IGNITE_IMPL_BINARY_BINARY_WRITER +#define _IGNITE_IMPL_BINARY_BINARY_WRITER + +#include <cstring> +#include <string> +#include <stdint.h> + +#include <ignite/common/common.h> +#include <ignite/common/concurrent.h> + +#include "ignite/impl/interop/interop_output_stream.h" +#include "ignite/impl/binary/binary_common.h" +#include "ignite/impl/binary/binary_id_resolver.h" +#include "ignite/impl/binary/binary_type_manager.h" +#include "ignite/impl/binary/binary_utils.h" +#include "ignite/impl/binary/binary_schema.h" +#include "ignite/binary/binary_consts.h" +#include "ignite/binary/binary_type.h" +#include "ignite/guid.h" +#include "ignite/date.h" +#include "ignite/timestamp.h" +#include "binary_type_manager.h" + +namespace ignite +{ + namespace impl + { + namespace binary + { + /** + * Internal implementation of binary reader. + */ + class IGNITE_IMPORT_EXPORT BinaryWriterImpl + { + public: + /** + * Constructor. + * + * @param stream Interop stream. + * @param idRslvr Binary ID resolver. + * @param metaMgr Type manager. + * @param metaHnd Type handler. + */ + BinaryWriterImpl(ignite::impl::interop::InteropOutputStream* stream, BinaryIdResolver* idRslvr, + BinaryTypeManager* metaMgr, BinaryTypeHandler* metaHnd, int32_t start); + + /** + * Constructor used to construct light-weight writer allowing only raw operations + * and primitive objects. + * + * @param stream Interop stream. + * @param metaMgr Type manager. + */ + BinaryWriterImpl(ignite::impl::interop::InteropOutputStream* stream, BinaryTypeManager* 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 Date. Maps to "Date" type in Java. + * + * @param val Value. + */ + void WriteDate(const Date& val); + + /** + * Write array of Dates. Maps to "Date[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteDateArray(const Date* val, const int32_t len); + + /** + * Write Date. Maps to "Date" type in Java. + * + * @param fieldName Field name. + * @param val Value. + */ + void WriteDate(const char* fieldName, const Date& val); + + /** + * Write array of Dates. Maps to "Date[]" type in Java. + * + * @param fieldName Field name. + * @param val Array. + * @param len Array length. + */ + void WriteDateArray(const char* fieldName, const Date* val, const int32_t len); + + /** + * Write Timestamp. Maps to "Timestamp" type in Java. + * + * @param val Value. + */ + void WriteTimestamp(const Timestamp& val); + + /** + * Write array of Timestamps. Maps to "Timestamp[]" type in Java. + * + * @param val Array. + * @param len Array length. + */ + void WriteTimestampArray(const Timestamp* val, const int32_t len); + + /** + * Write Timestamp. Maps to "Timestamp" type in Java. + * + * @param fieldName Field name. + * @param val Value. + */ + void WriteTimestamp(const char* fieldName, const Timestamp& val); + + /** + * Write array of Timestamps. Maps to "Timestamp[]" type in Java. + * + * @param fieldName Field name. + * @param val Array. + * @param len Array length. + */ + void WriteTimestampArray(const char* fieldName, const Timestamp* 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(ignite::binary::CollectionType typ); + + /** + * Start collection write. + * + * @param fieldName Field name. + * @param typ Collection type. + * @return Session ID. + */ + int32_t WriteCollection(const char* fieldName, ignite::binary::CollectionType typ); + + /** + * Write values in interval [first, last). + * + * @param first Iterator pointing to the beginning of the interval. + * @param last Iterator pointing to the end of the interval. + * @param typ Collection type. + */ + template<typename InputIterator> + void WriteCollection(InputIterator first, InputIterator last, ignite::binary::CollectionType typ) + { + StartContainerSession(true); + + WriteCollectionWithinSession(first, last, typ); + } + + /** + * Write values in interval [first, last). + * + * @param fieldName Field name. + * @param first Iterator pointing to the beginning of the interval. + * @param last Iterator pointing to the end of the interval. + * @param typ Collection type. + */ + template<typename InputIterator> + void WriteCollection(const char* fieldName, InputIterator first, InputIterator last, + ignite::binary::CollectionType typ) + { + StartContainerSession(false); + + WriteFieldId(fieldName, IGNITE_TYPE_COLLECTION); + + WriteCollectionWithinSession(first, last, typ); + } + + /** + * Start map write. + * + * @param typ Map type. + * @return Session ID. + */ + int32_t WriteMap(ignite::binary::MapType typ); + + /** + * Start map write. + * + * @param fieldName Field name. + * @param typ Map type. + * @return Session ID. + */ + int32_t WriteMap(const char* fieldName, ignite::binary::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); + + WriteFieldId(fieldName, IGNITE_TYPE_OBJECT); + + WriteTopObject(val); + } + + /** + * Set raw mode. + */ + void SetRawMode(); + + /** + * Get raw position. + */ + int32_t GetRawPosition() const; + + /** + * Write object. + * + * @param obj Object to write. + */ + template<typename T> + void WriteTopObject(const T& obj) + { + ignite::binary::BinaryType<T> type; + + if (type.IsNull(obj)) + stream->WriteInt8(IGNITE_HDR_NULL); + else + { + TemplatedBinaryIdResolver<T> idRslvr(type); + ignite::common::concurrent::SharedPointer<BinaryTypeHandler> metaHnd; + + if (metaMgr) + metaHnd = metaMgr->GetHandler(idRslvr.GetTypeId()); + + int32_t pos = stream->Position(); + + BinaryWriterImpl writerImpl(stream, &idRslvr, metaMgr, metaHnd.Get(), pos); + ignite::binary::BinaryWriter writer(&writerImpl); + + stream->WriteInt8(IGNITE_HDR_FULL); + stream->WriteInt8(IGNITE_PROTO_VER); + stream->WriteInt16(IGNITE_BINARY_FLAG_USER_TYPE); + stream->WriteInt32(idRslvr.GetTypeId()); + stream->WriteInt32(type.GetHashCode(obj)); + + // Reserve space for the Object Lenght, Schema ID and Schema or Raw Offsett. + stream->Reserve(12); + + type.Write(writer, obj); + + writerImpl.PostWrite(); + + if (metaMgr) + metaMgr->SubmitHandler(type.GetTypeName(), idRslvr.GetTypeId(), metaHnd.Get()); + } + } + + /** + * Perform all nessasary post-write operations. + * Includes: + * - writing object length; + * - writing schema offset; + * - writing schema id; + * - writing schema to the tail. + */ + void PostWrite(); + + /** + * Check if the writer has object schema. + * + * @return True if has schema. + */ + bool HasSchema() const; + + /** + * Writes contating schema and clears current schema. + */ + void WriteAndClearSchema(); + + /** + * Get underlying stream. + * + * @return Stream. + */ + impl::interop::InteropOutputStream* GetStream(); + private: + /** Underlying stream. */ + ignite::impl::interop::InteropOutputStream* stream; + + /** ID resolver. */ + BinaryIdResolver* idRslvr; + + /** Type manager. */ + BinaryTypeManager* metaMgr; + + /** Type handler. */ + BinaryTypeHandler* 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; + + /** Schema of the current object. */ + BinarySchema schema; + + /** Writing start position. */ + int32_t start; + + IGNITE_NO_COPY_ASSIGNMENT(BinaryWriterImpl) + + /** + * 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(IGNITE_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->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->WriteInt8(hdr); + stream->WriteInt32(len); + func(stream, val, len); + } + else + { + stream->WriteInt8(IGNITE_HDR_NULL); + } + } + + /** + * Write values in interval [first, last). + * New session should be started prior to call to this method. + * @param first Iterator pointing to the beginning of the interval. + * @param last Iterator pointing to the end of the interval. + * @param typ Collection type. + */ + template<typename InputIterator> + void WriteCollectionWithinSession(InputIterator first, InputIterator last, + ignite::binary::CollectionType typ) + { + stream->WriteInt8(IGNITE_TYPE_COLLECTION); + stream->Position(stream->Position() + 4); + stream->WriteInt8(typ); + + for (InputIterator i = first; i != last; ++i) + WriteElement(elemId, *i); + + CommitContainer(elemId); + } + + /** + * Check raw mode. + * + * @param expected Expected raw mode of the reader. + */ + void CheckRawMode(bool expected) const; + + /** + * Check whether writer is currently operating in single mode. + * + * @param expected Expected value. + */ + void CheckSingleMode(bool expected) const; + + /** + * 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) const; + + /** + * Write field ID. + * + * @param fieldName Field name. + * @param fieldTypeId Field type ID. + */ + void WriteFieldId(const char* fieldName, int32_t fieldTypeId); + + /** + * 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 BinaryWriterImpl::WriteTopObject(const int8_t& obj); + + template<> + void IGNITE_IMPORT_EXPORT BinaryWriterImpl::WriteTopObject(const bool& obj); + + template<> + void IGNITE_IMPORT_EXPORT BinaryWriterImpl::WriteTopObject(const int16_t& obj); + + template<> + void IGNITE_IMPORT_EXPORT BinaryWriterImpl::WriteTopObject(const uint16_t& obj); + + template<> + void IGNITE_IMPORT_EXPORT BinaryWriterImpl::WriteTopObject(const int32_t& obj); + + template<> + void IGNITE_IMPORT_EXPORT BinaryWriterImpl::WriteTopObject(const int64_t& obj); + + template<> + void IGNITE_IMPORT_EXPORT BinaryWriterImpl::WriteTopObject(const float& obj); + + template<> + void IGNITE_IMPORT_EXPORT BinaryWriterImpl::WriteTopObject(const double& obj); + + template<> + void IGNITE_IMPORT_EXPORT BinaryWriterImpl::WriteTopObject(const Guid& obj); + + template<> + void IGNITE_IMPORT_EXPORT BinaryWriterImpl::WriteTopObject(const Date& obj); + + template<> + void IGNITE_IMPORT_EXPORT BinaryWriterImpl::WriteTopObject(const Timestamp& obj); + + template<> + inline void IGNITE_IMPORT_EXPORT BinaryWriterImpl::WriteTopObject(const std::string& obj) + { + const char* obj0 = obj.c_str(); + + int32_t len = static_cast<int32_t>(strlen(obj0)); + + stream->WriteInt8(IGNITE_TYPE_STRING); + + BinaryUtils::WriteString(stream, obj0, len); + } + } + } +} + +#endif //_IGNITE_IMPL_BINARY_BINARY_WRITER \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/764c97b9/modules/platforms/cpp/binary/include/ignite/impl/interop/interop.h ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/binary/include/ignite/impl/interop/interop.h b/modules/platforms/cpp/binary/include/ignite/impl/interop/interop.h new file mode 100644 index 0000000..9f0d89e --- /dev/null +++ b/modules/platforms/cpp/binary/include/ignite/impl/interop/interop.h @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _IGNITE_IMPL_INTEROP_INTEROP +#define _IGNITE_IMPL_INTEROP_INTEROP + +#include "ignite/impl/interop/interop_memory.h" +#include "ignite/impl/interop/interop_output_stream.h" +#include "ignite/impl/interop/interop_input_stream.h" + +#endif //_IGNITE_IMPL_INTEROP_INTEROP \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/764c97b9/modules/platforms/cpp/binary/include/ignite/impl/interop/interop_input_stream.h ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/binary/include/ignite/impl/interop/interop_input_stream.h b/modules/platforms/cpp/binary/include/ignite/impl/interop/interop_input_stream.h new file mode 100644 index 0000000..3652516 --- /dev/null +++ b/modules/platforms/cpp/binary/include/ignite/impl/interop/interop_input_stream.h @@ -0,0 +1,250 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _IGNITE_IMPL_INTEROP_INTEROP_INPUT_STREAM +#define _IGNITE_IMPL_INTEROP_INTEROP_INPUT_STREAM + +#include "ignite/impl/interop/interop_memory.h" + +namespace ignite +{ + namespace impl + { + namespace interop + { + /** + * Interop input stream implementation. + */ + class IGNITE_IMPORT_EXPORT InteropInputStream { + public: + /** + * Constructor. + * + * @param mem Memory. + */ + InteropInputStream(InteropMemory* mem); + + /** + * Read signed 8-byte int. + * + * @return Value. + */ + int8_t ReadInt8(); + + /** + * Read signed 8-byte int at the given position. + * + * @param pos Position. + * @return Value. + */ + int32_t ReadInt8(int32_t pos); + + /** + * Read signed 8-byte int array. + * + * @param res Allocated array. + * @param len Length. + */ + void ReadInt8Array(int8_t* const res, const int32_t len); + + /** + * Read bool. + * + * @return Value. + */ + bool ReadBool(); + + /** + * Read bool array. + * + * @param res Allocated array. + * @param len Length. + */ + void ReadBoolArray(bool* const res, const int32_t len); + + /** + * Read signed 16-byte int. + * + * @return Value. + */ + int16_t ReadInt16(); + + /** + * Read signed 16-byte int at the given position. + * + * @param pos Position. + * @return Value. + */ + int32_t ReadInt16(int32_t pos); + + /** + * Read signed 16-byte int array. + * + * @param res Allocated array. + * @param len Length. + */ + void ReadInt16Array(int16_t* const res, const int32_t len); + + /** + * Read unsigned 16-byte int. + * + * @return Value. + */ + uint16_t ReadUInt16(); + + /** + * Read unsigned 16-byte int array. + * + * @param res Allocated array. + * @param len Length. + */ + void ReadUInt16Array(uint16_t* const res, const int32_t len); + + /** + * Read signed 32-byte int. + * + * @return Value. + */ + int32_t ReadInt32(); + + /** + * Read signed 32-byte int at the given position. + * + * @param pos Position. + * @return Value. + */ + int32_t ReadInt32(int32_t pos); + + /** + * Read signed 32-byte int array. + * + * @param res Allocated array. + * @param len Length. + */ + void ReadInt32Array(int32_t* const res, const int32_t len); + + /** + * Read signed 64-byte int. + * + * @return Value. + */ + int64_t ReadInt64(); + + /** + * Read signed 64-byte int array. + * + * @param res Allocated array. + * @param len Length. + */ + void ReadInt64Array(int64_t* const res, const int32_t len); + + /** + * Read float. + * + * @return Value. + */ + float ReadFloat(); + + /** + * Read float array. + * + * @param res Allocated array. + * @param len Length. + */ + void ReadFloatArray(float* const res, const int32_t len); + + /** + * Read double. + * + * @return Value. + */ + double ReadDouble(); + + /** + * Read double array. + * + * @param res Allocated array. + * @param len Length. + */ + void ReadDoubleArray(double* const res, const int32_t len); + + /** + * Get remaining bytes. + * + * @return Remaining bytes. + */ + int32_t Remaining() const; + + /** + * Get position. + * + * @return Position. + */ + int32_t Position() const; + + /** + * Set position. + * + * @param Position. + */ + void Position(int32_t pos); + + /** + * Synchronize data from underlying memory. + */ + void Synchronize(); + private: + /** Memory. */ + InteropMemory* mem; + + /** Pointer to data. */ + int8_t* data; + + /** Length. */ + int len; + + /** Current position. */ + int pos; + + /** + * Ensure there is enough data in the stream. + * + * @param cnt Amount of byte expected to be available. + */ + void EnsureEnoughData(int32_t cnt) const; + + /** + * Copy data from the stream shifting it along the way. + * + * @param ptr Pointer to data. + * @param off Offset. + * @param cnt Amount of data to copy. + */ + void CopyAndShift(int8_t* dest, int32_t off, int32_t cnt); + + /** + * Shift stream to the right. + * + * @param cnt Amount of bytes to shift the stream to. + */ + void Shift(int32_t cnt); + }; + } + } +} + +#endif //_IGNITE_IMPL_INTEROP_INTEROP_INPUT_STREAM \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/764c97b9/modules/platforms/cpp/binary/include/ignite/impl/interop/interop_memory.h ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/binary/include/ignite/impl/interop/interop_memory.h b/modules/platforms/cpp/binary/include/ignite/impl/interop/interop_memory.h new file mode 100644 index 0000000..50b5328 --- /dev/null +++ b/modules/platforms/cpp/binary/include/ignite/impl/interop/interop_memory.h @@ -0,0 +1,269 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _IGNITE_IMPL_INTEROP_INTEROP_MEMORY +#define _IGNITE_IMPL_INTEROP_INTEROP_MEMORY + +#include <stdint.h> + +#include <ignite/common/common.h> + +namespace ignite +{ + namespace impl + { + namespace interop + { + /** Memory header length. */ + const int IGNITE_MEM_HDR_LEN = 20; + + /** Memory header offset: capacity. */ + const int IGNITE_MEM_HDR_OFF_CAP = 8; + + /** Memory header offset: length. */ + const int IGNITE_MEM_HDR_OFF_LEN = 12; + + /** Memory header offset: flags. */ + const int IGNITE_MEM_HDR_OFF_FLAGS = 16; + + /** Flag: external. */ + const int IGNITE_MEM_FLAG_EXT = 0x1; + + /** Flag: pooled. */ + const int IGNITE_MEM_FLAG_POOLED = 0x2; + + /** Flag: acquired. */ + const int IGNITE_MEM_FLAG_ACQUIRED = 0x4; + + /** + * Interop memory. + */ + class IGNITE_IMPORT_EXPORT InteropMemory + { + public: + /** + * Get raw data pointer. + * + * @param memPtr Memory pointer. + * @return Raw data pointer. + */ + static int8_t* Data(const int8_t* memPtr); + + /** + * Set raw data pointer. + * + * @param memPtr Memory pointer. + * @param ptr Raw data pointer. + */ + static void Data(int8_t* memPtr, void* ptr); + + /** + * Get capacity. + * + * @param memPtr Memory pointer. + * @return Capacity. + */ + static int32_t Capacity(const int8_t* memPtr); + + /** + * Set capacity. + * + * @param memPtr Memory pointer. + * @param val Value. + */ + static void Capacity(int8_t* memPtr, int32_t val); + + /** + * Get length. + * + * @param memPtr Memory pointer. + * @return Length. + */ + static int32_t Length(const int8_t* memPtr); + + /** + * Set length. + * + * @param memPtr Memory pointer. + * @param val Value. + */ + static void Length(int8_t* memPtr, int32_t val); + + /** + * Get flags. + * + * @param memPtr Memory pointer. + * @return Flags. + */ + static int32_t Flags(const int8_t* memPtr); + + /** + * Set flags. + * + * @param memPtr Memory pointer. + * @param val Value. + */ + static void Flags(int8_t* memPtr, int32_t val); + + /** + * Get "external" flag state. + * + * @param memPtr Memory pointer. + * @return Flag state. + */ + static bool IsExternal(const int8_t* memPtr); + + /** + * Get "external" flag state. + * + * @param flags Flags. + * @return Flag state. + */ + static bool IsExternal(int32_t flags); + + /** + * Get "pooled" flag state. + * + * @param memPtr Memory pointer. + * @return Flag state. + */ + static bool IsPooled(const int8_t* memPtr); + + /** + * Get "pooled" flag state. + * + * @param flags Flags. + * @return Flag state. + */ + static bool IsPooled(int32_t flags); + + /** + * Get "acquired" flag state. + * + * @param memPtr Memory pointer. + * @return Flag state. + */ + static bool IsAcquired(const int8_t* memPtr); + + /** + * Get "acquired" flag state. + * + * @param flags Flags. + * @return Flag state. + */ + static bool IsAcquired(int32_t flags); + + /** + * Destructor. + */ + virtual ~InteropMemory() { } + + /** + * Get cross-platform memory pointer. + * + * @return Memory pointer. + */ + int8_t* Pointer(); + + /** + * Get cross-platform pointer in long form. + */ + int64_t PointerLong(); + + /** + * Get raw data pointer. + * + * @return Data pointer. + */ + int8_t* Data(); + + /** + * Get capacity. + * + * @return Capacity. + */ + int32_t Capacity() const; + + /** + * Set capacity. + * + * @param val Capacity. + */ + void Capacity(int32_t val); + + /** + * Get length. + * + * @return Length. + */ + int32_t Length() const; + + /** + * Set length. + * + * @param val Length. + */ + void Length(int32_t val); + + /** + * Reallocate memory. + * + * @param cap Desired capactiy. + */ + virtual void Reallocate(int32_t cap) = 0; + protected: + /** Memory pointer. */ + int8_t* memPtr; + }; + + /** + * Interop unpooled memory. + */ + class IGNITE_IMPORT_EXPORT InteropUnpooledMemory : public InteropMemory + { + public: + /** + * Constructor create new unpooled memory object from scratch. + * + * @param cap Capacity. + */ + explicit InteropUnpooledMemory(int32_t cap); + + /** + * Constructor creating unpooled memory object from existing memory pointer. + * + * @param memPtr Memory pointer. + */ + explicit InteropUnpooledMemory(int8_t* memPtr); + + /** + * Destructor. + */ + ~InteropUnpooledMemory(); + + virtual void Reallocate(int32_t cap); + private: + /** Whether this instance is owner of memory chunk. */ + bool owning; + + IGNITE_NO_COPY_ASSIGNMENT(InteropUnpooledMemory) + }; + } + } +} + +#endif //_IGNITE_IMPL_INTEROP_INTEROP_MEMORY \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/764c97b9/modules/platforms/cpp/binary/include/ignite/impl/interop/interop_output_stream.h ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/binary/include/ignite/impl/interop/interop_output_stream.h b/modules/platforms/cpp/binary/include/ignite/impl/interop/interop_output_stream.h new file mode 100644 index 0000000..c5bb884 --- /dev/null +++ b/modules/platforms/cpp/binary/include/ignite/impl/interop/interop_output_stream.h @@ -0,0 +1,250 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _IGNITE_IMPL_INTEROP_INTEROP_OUTPUT_STREAM +#define _IGNITE_IMPL_INTEROP_INTEROP_OUTPUT_STREAM + +#include "ignite/impl/interop/interop_memory.h" + +namespace ignite +{ + namespace impl + { + namespace interop + { + /** + * Interop output stream. + */ + class IGNITE_IMPORT_EXPORT InteropOutputStream { + public: + /** + * Create new output stream with the given capacity. + * + * @param mem Memory. + */ + InteropOutputStream(InteropMemory* mem); + + /** + * Write signed 8-byte integer. + * + * @param val Value. + */ + void WriteInt8(const int8_t val); + + /** + * Write signed 8-byte integer at the given position. + * + * @param val Value. + */ + void WriteInt8(const int8_t val, const int32_t pos); + + /** + * Write signed 8-byte integer array. + * + * @param val Value. + * @param len Length. + */ + void WriteInt8Array(const int8_t* val, const int32_t len); + + /** + * Write bool. + * + * @param val Value. + */ + void WriteBool(const bool val); + + /** + * Write bool array. + * + * @param val Value. + * @param len Length. + */ + void WriteBoolArray(const bool* val, const int32_t len); + + /** + * Write signed 16-byte integer. + * + * @param val Value. + */ + void WriteInt16(const int16_t val); + + /** + * Write signed 16-byte integer at the given position. + * + * @param pos Position. + * @param val Value. + */ + void WriteInt16(const int32_t pos, const int16_t val); + + /** + * Write signed 16-byte integer array. + * + * @param val Value. + * @param len Length. + */ + void WriteInt16Array(const int16_t* val, const int32_t len); + + /** + * Write unsigned 16-byte integer. + * + * @param val Value. + */ + void WriteUInt16(const uint16_t val); + + /** + * Write unsigned 16-byte integer array. + * + * @param val Value. + * @param len Length. + */ + void WriteUInt16Array(const uint16_t* val, const int32_t len); + + /** + * Write signed 32-byte integer. + * + * @param val Value. + */ + void WriteInt32(const int32_t val); + + /** + * Write signed 32-byte integer at the given position. + * + * @param pos Position. + * @param val Value. + */ + void WriteInt32(const int32_t pos, const int32_t val); + + /** + * Write signed 32-byte integer array. + * + * @param val Value. + * @param len Length. + */ + void WriteInt32Array(const int32_t* val, const int32_t len); + + /** + * Write signed 64-byte integer. + * + * @param val Value. + */ + void WriteInt64(const int64_t val); + + /** + * Write signed 64-byte integer array. + * + * @param val Value. + * @param len Length. + */ + void WriteInt64Array(const int64_t* val, const int32_t len); + + /** + * Write float. + * + * @param val Value. + */ + void WriteFloat(const float val); + + /** + * Write float array. + * + * @param val Value. + * @param len Length. + */ + void WriteFloatArray(const float* val, const int32_t len); + + /** + * Write double. + * + * @param val Value. + */ + void WriteDouble(const double val); + + /** + * Write double array. + * + * @param val Value. + * @param len Length. + */ + void WriteDoubleArray(const double* val, const int32_t len); + + /** + * Get current stream position. + */ + int32_t Position() const; + + /** + * Set current stream position (absolute). + * + * @param val Position (absolute). + */ + void Position(const int32_t val); + + /** + * Reserve specified number of bytes in stream. + * + * @param num Number of bytes to reserve. + * @return Absolute position to reserved space. + */ + int32_t Reserve(int32_t num); + + /** + * Synchronize data with underlying memory. + */ + void Synchronize(); + private: + /** Memory. */ + InteropMemory* mem; + + /** Pointer to data. */ + int8_t* data; + + /** Capacity. */ + int cap; + + /** Current position. */ + int pos; + + IGNITE_NO_COPY_ASSIGNMENT(InteropOutputStream) + + /** + * Ensure that stream enough capacity optionally extending it. + * + * @param reqCap Requsted capacity. + */ + void EnsureCapacity(int32_t reqCap); + + /** + * Shift stream to the right. + * + * @param cnt Amount of bytes to shift the stream to. + */ + void Shift(int32_t cnt); + + /** + * Copy data to the stream shifting it along the way. + * + * @param ptr Pointer to data. + * @param off Offset. + * @param len Length. + */ + void CopyAndShift(const int8_t* src, int32_t off, int32_t len); + }; + } + } +} + +#endif //_IGNITE_IMPL_INTEROP_INTEROP_OUTPUT_STREAM \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/764c97b9/modules/platforms/cpp/binary/include/ignite/impl/interop/interop_stream_position_guard.h ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/binary/include/ignite/impl/interop/interop_stream_position_guard.h b/modules/platforms/cpp/binary/include/ignite/impl/interop/interop_stream_position_guard.h new file mode 100644 index 0000000..69124e0 --- /dev/null +++ b/modules/platforms/cpp/binary/include/ignite/impl/interop/interop_stream_position_guard.h @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _IGNITE_IMPL_INTEROP_INTEROP_STREAM_POSITION_GUARD +#define _IGNITE_IMPL_INTEROP_INTEROP_STREAM_POSITION_GUARD + +#include "ignite/impl/interop/interop_memory.h" + +namespace ignite +{ + namespace impl + { + namespace interop + { + /** + * Interop stream position guard. + */ + template<typename T> + class IGNITE_IMPORT_EXPORT InteropStreamPositionGuard { + public: + /** + * Create new position guard and saves current stream position. + * + * @param stream Stream which position should be saved. + */ + InteropStreamPositionGuard(T& stream) : stream(&stream), pos(stream.Position()) + { + //No-op + } + + /** + * Destructor. + * + * Restores stream's position to a saved one on destruction. + */ + ~InteropStreamPositionGuard() + { + if (stream) + stream->Position(pos); + } + + /** + * Releases guard so it will not restore streams position on destruction. + * + * @param val Value. + */ + void Release() + { + stream = 0; + } + + private: + /** Stream. */ + T* stream; + + /** Saved position. */ + int32_t pos; + + IGNITE_NO_COPY_ASSIGNMENT(InteropStreamPositionGuard) + }; + } + } +} + +#endif //_IGNITE_IMPL_INTEROP_INTEROP_STREAM_POSITION_GUARD
