diff options
author | robot-contrib <robot-contrib@yandex-team.com> | 2022-07-24 01:44:23 +0300 |
---|---|---|
committer | robot-contrib <robot-contrib@yandex-team.com> | 2022-07-24 01:44:23 +0300 |
commit | 4d56ab79e5691cc41755bcc40f4c2125beee347e (patch) | |
tree | db273cf5da935c20759a5af7f29cae217762e4ae | |
parent | 574b79287767a711217816f3c0719095e56f2b66 (diff) | |
download | ydb-4d56ab79e5691cc41755bcc40f4c2125beee347e.tar.gz |
Update contrib/libs/apache/avro to 1.11.0
59 files changed, 4098 insertions, 5070 deletions
diff --git a/contrib/libs/apache/avro/CMakeLists.linux.txt b/contrib/libs/apache/avro/CMakeLists.linux.txt index 1da4991ef69..a6b72527d60 100644 --- a/contrib/libs/apache/avro/CMakeLists.linux.txt +++ b/contrib/libs/apache/avro/CMakeLists.linux.txt @@ -10,6 +10,13 @@ add_library(libs-apache-avro) target_compile_options(libs-apache-avro PRIVATE -DAVRO_SOURCE + -DBOOST_ALL_NO_LIB + -DBOOST_ATOMIC_DYN_LINK + -DBOOST_FILESYSTEM_DYN_LINK + -DBOOST_IOSTREAMS_DYN_LINK + -DBOOST_PROGRAM_OPTIONS_DYN_LINK + -DBOOST_REGEX_DYN_LINK + -DBOOST_SYSTEM_DYN_LINK -DSNAPPY_CODEC_AVAILABLE -Wno-everything ) diff --git a/contrib/libs/apache/avro/api/AvroTraits.hh b/contrib/libs/apache/avro/api/AvroTraits.hh index 91e2130c76f..7b5a636ec33 100644 --- a/contrib/libs/apache/avro/api/AvroTraits.hh +++ b/contrib/libs/apache/avro/api/AvroTraits.hh @@ -21,7 +21,7 @@ #include "Config.hh" #include "Types.hh" -#include <stdint.h> +#include <cstdint> #include <type_traits> /** @file @@ -34,13 +34,13 @@ namespace avro { * Define an is_serializable trait for types we can serialize natively. * New types will need to define the trait as well. */ -template <typename T> -struct is_serializable : public std::false_type{}; +template<typename T> +struct is_serializable : public std::false_type {}; -template <typename T> -struct is_promotable : public std::false_type{}; +template<typename T> +struct is_promotable : public std::false_type {}; -template <typename T> +template<typename T> struct type_to_avro { static const Type type = AVRO_NUM_TYPES; }; @@ -52,16 +52,18 @@ struct type_to_avro { * is_defined<T>::value will be true or false depending on whether T is a * complete type or not respectively. */ -template <class T> +template<class T> struct is_defined { typedef char yes[1]; typedef char no[2]; - template <class U> static yes& test(char(*)[sizeof(U)]) { throw 0; }; + template<class U> + static yes &test(char (*)[sizeof(U)]) { throw 0; }; - template <class U> static no& test(...) { throw 0; }; + template<class U> + static no &test(...) { throw 0; }; static const bool value = sizeof(test<T>(0)) == sizeof(yes); }; @@ -72,34 +74,36 @@ struct is_defined { * is_not_defined<T>::value will be true or false depending on whether T is an * incomplete type or not respectively. */ -template <class T> +template<class T> struct is_not_defined { typedef char yes[1]; typedef char no[2]; - template <class U> static yes& test(char(*)[sizeof(U)]) { throw 0; }; + template<class U> + static yes &test(char (*)[sizeof(U)]) { throw 0; }; - template <class U> static no& test(...) { throw 0; }; + template<class U> + static no &test(...) { throw 0; }; static const bool value = sizeof(test<T>(0)) == sizeof(no); }; -#define DEFINE_PRIMITIVE(CTYPE, AVROTYPE) \ -template <> \ -struct is_serializable<CTYPE> : public std::true_type{}; \ -\ -template <> \ -struct type_to_avro<CTYPE> { \ - static const Type type = AVROTYPE; \ -}; - -#define DEFINE_PROMOTABLE_PRIMITIVE(CTYPE, AVROTYPE) \ -template <> \ -struct is_promotable<CTYPE> : public std::true_type{}; \ -\ -DEFINE_PRIMITIVE(CTYPE, AVROTYPE) +#define DEFINE_PRIMITIVE(CTYPE, AVROTYPE) \ + template<> \ + struct is_serializable<CTYPE> : public std::true_type {}; \ + \ + template<> \ + struct type_to_avro<CTYPE> { \ + static const Type type = AVROTYPE; \ + }; + +#define DEFINE_PROMOTABLE_PRIMITIVE(CTYPE, AVROTYPE) \ + template<> \ + struct is_promotable<CTYPE> : public std::true_type {}; \ + \ + DEFINE_PRIMITIVE(CTYPE, AVROTYPE) DEFINE_PROMOTABLE_PRIMITIVE(int32_t, AVRO_INT) DEFINE_PROMOTABLE_PRIMITIVE(int64_t, AVRO_LONG) @@ -110,7 +114,6 @@ DEFINE_PRIMITIVE(Null, AVRO_NULL) DEFINE_PRIMITIVE(std::string, AVRO_STRING) DEFINE_PRIMITIVE(std::vector<uint8_t>, AVRO_BYTES) - } // namespace avro #endif diff --git a/contrib/libs/apache/avro/api/Compiler.hh b/contrib/libs/apache/avro/api/Compiler.hh index 892d60580d0..bdcbb355e28 100644 --- a/contrib/libs/apache/avro/api/Compiler.hh +++ b/contrib/libs/apache/avro/api/Compiler.hh @@ -20,7 +20,7 @@ #define avro_Compiler_hh__ #include "Config.hh" -#include <stdint.h> +#include <cstdint> #include <istream> namespace avro { @@ -34,7 +34,7 @@ class AVRO_DECL InputStream; class AVRO_DECL ValidSchema; -/// Given a stream comtaining a JSON schema, compiles the schema to a +/// Given a stream containing a JSON schema, compiles the schema to a /// ValidSchema object. Throws if the schema cannot be compiled to a valid /// schema @@ -46,17 +46,17 @@ AVRO_DECL void compileJsonSchema(std::istream &is, ValidSchema &schema); /// AVRO_DECL bool compileJsonSchema(std::istream &is, ValidSchema &schema, - std::string &error); + std::string &error); -AVRO_DECL ValidSchema compileJsonSchemaFromStream(InputStream& is); +AVRO_DECL ValidSchema compileJsonSchemaFromStream(InputStream &is); -AVRO_DECL ValidSchema compileJsonSchemaFromMemory(const uint8_t* input, size_t len); +AVRO_DECL ValidSchema compileJsonSchemaFromMemory(const uint8_t *input, size_t len); -AVRO_DECL ValidSchema compileJsonSchemaFromString(const char* input); +AVRO_DECL ValidSchema compileJsonSchemaFromString(const char *input); -AVRO_DECL ValidSchema compileJsonSchemaFromString(const std::string& input); +AVRO_DECL ValidSchema compileJsonSchemaFromString(const std::string &input); -AVRO_DECL ValidSchema compileJsonSchemaFromFile(const char* filename); +AVRO_DECL ValidSchema compileJsonSchemaFromFile(const char *filename); } // namespace avro diff --git a/contrib/libs/apache/avro/api/Config.hh b/contrib/libs/apache/avro/api/Config.hh index 69d36f2abe1..dd2438debbe 100644 --- a/contrib/libs/apache/avro/api/Config.hh +++ b/contrib/libs/apache/avro/api/Config.hh @@ -19,27 +19,25 @@ #ifndef avro_Config_hh #define avro_Config_hh -// Windows DLL suport +// Windows DLL support #ifdef _WIN32 -#pragma warning (disable: 4275 4251) +#pragma warning(disable : 4275 4251) #if defined(AVRO_DYN_LINK) #ifdef AVRO_SOURCE -# define AVRO_DECL __declspec(dllexport) +#define AVRO_DECL __declspec(dllexport) #else -# define AVRO_DECL __declspec(dllimport) -#endif // AVRO_SOURCE -#endif // AVRO_DYN_LINK +#define AVRO_DECL __declspec(dllimport) +#endif // AVRO_SOURCE +#endif // AVRO_DYN_LINK #include <intsafe.h> -typedef SSIZE_T ssize_t; - -#endif // _WIN32 +using ssize_t = SSIZE_T; +#endif // _WIN32 #ifndef AVRO_DECL #define AVRO_DECL #endif #endif - diff --git a/contrib/libs/apache/avro/api/DataFile.hh b/contrib/libs/apache/avro/api/DataFile.hh index 50169106b19..94a1dab8e31 100644 --- a/contrib/libs/apache/avro/api/DataFile.hh +++ b/contrib/libs/apache/avro/api/DataFile.hh @@ -21,10 +21,10 @@ #include "Config.hh" #include "Encoder.hh" -#include "buffer/Buffer.hh" -#include "ValidSchema.hh" #include "Specific.hh" #include "Stream.hh" +#include "ValidSchema.hh" +#include "buffer/Buffer.hh" #include <map> #include <string> @@ -38,11 +38,11 @@ namespace avro { /** Specify type of compression to use when writing data files. */ enum Codec { - NULL_CODEC, - DEFLATE_CODEC, + NULL_CODEC, + DEFLATE_CODEC, #ifdef SNAPPY_CODEC_AVAILABLE - SNAPPY_CODEC + SNAPPY_CODEC #endif }; @@ -70,16 +70,16 @@ class AVRO_DECL DataFileWriterBase : boost::noncopyable { const DataFileSync sync_; int64_t objectCount_; - typedef std::map<std::string, std::vector<uint8_t> > Metadata; + typedef std::map<std::string, std::vector<uint8_t>> Metadata; Metadata metadata_; int64_t lastSync_; - static std::unique_ptr<OutputStream> makeStream(const char* filename); + static std::unique_ptr<OutputStream> makeStream(const char *filename); static DataFileSync makeSync(); void writeHeader(); - void setMetadata(const std::string& key, const std::string& value); + void setMetadata(const std::string &key, const std::string &value); /** * Generates a sync marker in the file. @@ -95,7 +95,7 @@ public: /** * Returns the current encoder for this writer. */ - Encoder& encoder() const { return *encoderPtr_; } + Encoder &encoder() const { return *encoderPtr_; } /** * Returns true if the buffer has sufficient data for a sync to be @@ -106,7 +106,7 @@ public: /** * Returns the byte offset (within the current file) of the start of the current block being written. */ - uint64_t getCurrentBlockStart(); + uint64_t getCurrentBlockStart() const; /** * Increments the object count. @@ -117,10 +117,10 @@ public: /** * Constructs a data file writer with the given sync interval and name. */ - DataFileWriterBase(const char* filename, const ValidSchema& schema, - size_t syncInterval, Codec codec = NULL_CODEC); + DataFileWriterBase(const char *filename, const ValidSchema &schema, + size_t syncInterval, Codec codec = NULL_CODEC); DataFileWriterBase(std::unique_ptr<OutputStream> outputStream, - const ValidSchema& schema, size_t syncInterval, Codec codec); + const ValidSchema &schema, size_t syncInterval, Codec codec); ~DataFileWriterBase(); /** @@ -132,7 +132,7 @@ public: /** * Returns the schema for this data file. */ - const ValidSchema& schema() const { return schema_; } + const ValidSchema &schema() const { return schema_; } /** * Flushes any unwritten data into the file. @@ -143,25 +143,24 @@ public: /** * An Avro datafile that can store objects of type T. */ -template <typename T> +template<typename T> class DataFileWriter : boost::noncopyable { std::unique_ptr<DataFileWriterBase> base_; + public: /** * Constructs a new data file. */ - DataFileWriter(const char* filename, const ValidSchema& schema, - size_t syncInterval = 16 * 1024, Codec codec = NULL_CODEC) : - base_(new DataFileWriterBase(filename, schema, syncInterval, codec)) { } + DataFileWriter(const char *filename, const ValidSchema &schema, + size_t syncInterval = 16 * 1024, Codec codec = NULL_CODEC) : base_(new DataFileWriterBase(filename, schema, syncInterval, codec)) {} - DataFileWriter(std::unique_ptr<OutputStream> outputStream, const ValidSchema& schema, - size_t syncInterval = 16 * 1024, Codec codec = NULL_CODEC) : - base_(new DataFileWriterBase(std::move(outputStream), schema, syncInterval, codec)) { } + DataFileWriter(std::unique_ptr<OutputStream> outputStream, const ValidSchema &schema, + size_t syncInterval = 16 * 1024, Codec codec = NULL_CODEC) : base_(new DataFileWriterBase(std::move(outputStream), schema, syncInterval, codec)) {} /** * Writes the given piece of data into the file. */ - void write(const T& datum) { + void write(const T &datum) { base_->syncIfNeeded(); avro::encode(base_->encoder(), datum); base_->incr(); @@ -172,7 +171,6 @@ public: */ uint64_t getCurrentBlockStart() { return base_->getCurrentBlockStart(); } - /** * Closes the current file. Once closed this datafile object cannot be * used for writing any more. @@ -182,7 +180,7 @@ public: /** * Returns the schema for this data file. */ - const ValidSchema& schema() const { return base_->schema(); } + const ValidSchema &schema() const { return base_->schema(); } /** * Flushes any unwritten data into the file. @@ -200,17 +198,17 @@ class AVRO_DECL DataFileReaderBase : boost::noncopyable { int64_t objectCount_; bool eof_; Codec codec_; - int64_t blockStart_; - int64_t blockEnd_; + int64_t blockStart_{}; + int64_t blockEnd_{}; ValidSchema readerSchema_; ValidSchema dataSchema_; DecoderPtr dataDecoder_; std::unique_ptr<InputStream> dataStream_; - typedef std::map<std::string, std::vector<uint8_t> > Metadata; + typedef std::map<std::string, std::vector<uint8_t>> Metadata; Metadata metadata_; - DataFileSync sync_; + DataFileSync sync_{}; // for compressed buffer std::unique_ptr<boost::iostreams::filtering_istream> os_; @@ -220,11 +218,12 @@ class AVRO_DECL DataFileReaderBase : boost::noncopyable { void readDataBlock(); void doSeek(int64_t position); + public: /** * Returns the current decoder for this reader. */ - Decoder& decoder() { return *dataDecoder_; } + Decoder &decoder() { return *dataDecoder_; } /** * Returns true if and only if there is more to read. @@ -242,9 +241,9 @@ public: * This function should be called exactly once after constructing * the DataFileReaderBase object. */ - DataFileReaderBase(const char* filename); + explicit DataFileReaderBase(const char *filename); - DataFileReaderBase(std::unique_ptr<InputStream> inputStream); + explicit DataFileReaderBase(std::unique_ptr<InputStream> inputStream); /** * Initializes the reader so that the reader and writer schemas @@ -259,17 +258,17 @@ public: * This must be called exactly once after constructing the * DataFileReaderBase object. */ - void init(const ValidSchema& readerSchema); + void init(const ValidSchema &readerSchema); /** * Returns the schema for this object. */ - const ValidSchema& readerSchema() { return readerSchema_; } + const ValidSchema &readerSchema() { return readerSchema_; } /** * Returns the schema stored with the data file. */ - const ValidSchema& dataSchema() { return dataSchema_; } + const ValidSchema &dataSchema() { return dataSchema_; } /** * Closes the reader. No further operation is possible on this reader. @@ -297,27 +296,26 @@ public: /** * Return the last synchronization point before our current position. */ - int64_t previousSync(); + int64_t previousSync() const; }; /** * Reads the contents of data file one after another. */ -template <typename T> +template<typename T> class DataFileReader : boost::noncopyable { std::unique_ptr<DataFileReaderBase> base_; + public: /** * Constructs the reader for the given file and the reader is * expected to use the given schema. */ - DataFileReader(const char* filename, const ValidSchema& readerSchema) : - base_(new DataFileReaderBase(filename)) { + DataFileReader(const char *filename, const ValidSchema &readerSchema) : base_(new DataFileReaderBase(filename)) { base_->init(readerSchema); } - DataFileReader(std::unique_ptr<InputStream> inputStream, const ValidSchema& readerSchema) : - base_(new DataFileReaderBase(std::move(inputStream))) { + DataFileReader(std::unique_ptr<InputStream> inputStream, const ValidSchema &readerSchema) : base_(new DataFileReaderBase(std::move(inputStream))) { base_->init(readerSchema); } @@ -325,13 +323,11 @@ public: * Constructs the reader for the given file and the reader is * expected to use the schema that is used with data. */ - DataFileReader(const char* filename) : - base_(new DataFileReaderBase(filename)) { + explicit DataFileReader(const char *filename) : base_(new DataFileReaderBase(filename)) { base_->init(); } - DataFileReader(std::unique_ptr<InputStream> inputStream) : - base_(new DataFileReaderBase(std::move(inputStream))) { + explicit DataFileReader(std::unique_ptr<InputStream> inputStream) : base_(new DataFileReaderBase(std::move(inputStream))) { base_->init(); } @@ -344,7 +340,7 @@ public: * The schema present in the data file will be used for reading * from this reader. */ - DataFileReader(std::unique_ptr<DataFileReaderBase> base) : base_(std::move(base)) { + explicit DataFileReader(std::unique_ptr<DataFileReaderBase> base) : base_(std::move(base)) { base_->init(); } @@ -358,7 +354,7 @@ public: * from this reader. */ DataFileReader(std::unique_ptr<DataFileReaderBase> base, - const ValidSchema& readerSchema) : base_(std::move(base)) { + const ValidSchema &readerSchema) : base_(std::move(base)) { base_->init(readerSchema); } @@ -367,7 +363,7 @@ public: * \return true if an object has been successfully read into \p datum and * false if there are no more entries in the file. */ - bool read(T& datum) { + bool read(T &datum) { if (base_->hasMore()) { base_->decr(); avro::decode(base_->decoder(), datum); @@ -379,12 +375,12 @@ public: /** * Returns the schema for this object. */ - const ValidSchema& readerSchema() { return base_->readerSchema(); } + const ValidSchema &readerSchema() { return base_->readerSchema(); } /** * Returns the schema stored with the data file. */ - const ValidSchema& dataSchema() { return base_->dataSchema(); } + const ValidSchema &dataSchema() { return base_->dataSchema(); } /** * Closes the reader. No further operation is possible on this reader. @@ -415,5 +411,5 @@ public: int64_t previousSync() { return base_->previousSync(); } }; -} // namespace avro +} // namespace avro #endif diff --git a/contrib/libs/apache/avro/api/Decoder.hh b/contrib/libs/apache/avro/api/Decoder.hh index 5356d79f32f..77b3f9489f7 100644 --- a/contrib/libs/apache/avro/api/Decoder.hh +++ b/contrib/libs/apache/avro/api/Decoder.hh @@ -20,13 +20,13 @@ #define avro_Decoder_hh__ #include "Config.hh" -#include <stdint.h> +#include <cstdint> +#include <memory> #include <string> #include <vector> -#include <memory> -#include "ValidSchema.hh" #include "Stream.hh" +#include "ValidSchema.hh" /// \file /// @@ -47,11 +47,11 @@ namespace avro { */ class AVRO_DECL Decoder { public: - virtual ~Decoder() { }; + virtual ~Decoder() = default; /// All future decoding will come from is, which should be valid /// until replaced by another call to init() or this Decoder is /// destructed. - virtual void init(InputStream& is) = 0; + virtual void init(InputStream &is) = 0; /// Decodes a null from the current stream. virtual void decodeNull() = 0; @@ -81,12 +81,12 @@ public: /** * Decodes a UTF-8 string from the stream and assigns it to value. */ - virtual void decodeString(std::string& value) = 0; + virtual void decodeString(std::string &value) = 0; /// Skips a string on the current stream. virtual void skipString() = 0; - /// Decodes arbitray binary data from the current stream. + /// Decodes arbitrary binary data from the current stream. std::vector<uint8_t> decodeBytes() { std::vector<uint8_t> result; decodeBytes(result); @@ -95,7 +95,7 @@ public: /// Decodes arbitrary binary data from the current stream and puts it /// in value. - virtual void decodeBytes(std::vector<uint8_t>& value) = 0; + virtual void decodeBytes(std::vector<uint8_t> &value) = 0; /// Skips bytes on the current stream. virtual void skipBytes() = 0; @@ -116,9 +116,9 @@ public: * Decodes a fixed from the current stream. * \param[in] n The size (byte count) of the fixed being read. * \param[out] value The value that receives the fixed. The vector will - * be size-adjusted based on the fixed's size. + * be size-adjusted based on the fixed schema's size. */ - virtual void decodeFixed(size_t n, std::vector<uint8_t>& value) = 0; + virtual void decodeFixed(size_t n, std::vector<uint8_t> &value) = 0; /// Skips fixed length binary on the current stream. virtual void skipFixed(size_t n) = 0; @@ -174,7 +174,7 @@ public: /** * Shared pointer to Decoder. */ -typedef std::shared_ptr<Decoder> DecoderPtr; +using DecoderPtr = std::shared_ptr<Decoder>; /** * ResolvingDecoder is derived from \ref Decoder, with an additional @@ -187,13 +187,13 @@ public: /// order in the schema because the writer's field order could /// be different. In order to avoid buffering and later use, /// we return the values in the writer's field order. - virtual const std::vector<size_t>& fieldOrder() = 0; + virtual const std::vector<size_t> &fieldOrder() = 0; }; /** * Shared pointer to ResolvingDecoder. */ -typedef std::shared_ptr<ResolvingDecoder> ResolvingDecoderPtr; +using ResolvingDecoderPtr = std::shared_ptr<ResolvingDecoder>; /** * Returns an decoder that can decode binary Avro standard. */ @@ -203,13 +203,13 @@ AVRO_DECL DecoderPtr binaryDecoder(); * Returns an decoder that validates sequence of calls to an underlying * Decoder against the given schema. */ -AVRO_DECL DecoderPtr validatingDecoder(const ValidSchema& schema, - const DecoderPtr& base); +AVRO_DECL DecoderPtr validatingDecoder(const ValidSchema &schema, + const DecoderPtr &base); /** * Returns an decoder that can decode Avro standard for JSON. */ -AVRO_DECL DecoderPtr jsonDecoder(const ValidSchema& schema); +AVRO_DECL DecoderPtr jsonDecoder(const ValidSchema &schema); /** * Returns a decoder that decodes avro data from base written according to @@ -217,10 +217,9 @@ AVRO_DECL DecoderPtr jsonDecoder(const ValidSchema& schema); * The client uses the decoder as if the data were written using readerSchema. * // FIXME: Handle out of order fields. */ -AVRO_DECL ResolvingDecoderPtr resolvingDecoder(const ValidSchema& writer, - const ValidSchema& reader, const DecoderPtr& base); - +AVRO_DECL ResolvingDecoderPtr resolvingDecoder(const ValidSchema &writer, + const ValidSchema &reader, const DecoderPtr &base); -} // namespace avro +} // namespace avro #endif diff --git a/contrib/libs/apache/avro/api/Encoder.hh b/contrib/libs/apache/avro/api/Encoder.hh index 7849e934586..4ba5fa7594d 100644 --- a/contrib/libs/apache/avro/api/Encoder.hh +++ b/contrib/libs/apache/avro/api/Encoder.hh @@ -20,18 +20,18 @@ #define avro_Encoder_hh__ #include "Config.hh" -#include <stdint.h> +#include <cstdint> +#include <memory> #include <string> #include <vector> -#include <memory> -#include "ValidSchema.hh" #include "Stream.hh" +#include "ValidSchema.hh" /// \file /// /// Low level support for encoding avro values. -/// This class has two types of funtions. One type of functions support +/// This class has two types of functions. One type of functions support /// the writing of leaf values (for example, encodeLong and /// encodeString). These functions have analogs in Decoder. /// @@ -46,16 +46,16 @@ namespace avro { /** * The abstract base class for all Avro encoders. The implementations - * differ in the method of encoding (binary vresus JSON) or in capabilities + * differ in the method of encoding (binary versus JSON) or in capabilities * such as ability to verify the order of invocation of different functions. */ class AVRO_DECL Encoder { public: - virtual ~Encoder() { }; + virtual ~Encoder() = default; /// All future encodings will go to os, which should be valid until /// it is reset with another call to init() or the encoder is /// destructed. - virtual void init(OutputStream& os) = 0; + virtual void init(OutputStream &os) = 0; /// Flushes any data in internal buffers. virtual void flush() = 0; @@ -83,10 +83,10 @@ public: virtual void encodeDouble(double d) = 0; /// Encodes a UTF-8 string to the current stream. - virtual void encodeString(const std::string& s) = 0; + virtual void encodeString(const std::string &s) = 0; /** - * Encodes aribtray binary data into tthe current stream as Avro "bytes" + * Encodes arbitrary binary data into the current stream as Avro "bytes" * data type. * \param bytes Where the data is * \param len Number of bytes at \p bytes. @@ -94,11 +94,11 @@ public: virtual void encodeBytes(const uint8_t *bytes, size_t len) = 0; /** - * Encodes aribtray binary data into tthe current stream as Avro "bytes" + * Encodes arbitrary binary data into the current stream as Avro "bytes" * data type. * \param bytes The data. */ - void encodeBytes(const std::vector<uint8_t>& bytes) { + void encodeBytes(const std::vector<uint8_t> &bytes) { uint8_t b = 0; encodeBytes(bytes.empty() ? &b : bytes.data(), bytes.size()); } @@ -111,7 +111,7 @@ public: * \param bytes The fixed, the length of which is taken as the size * of fixed. */ - void encodeFixed(const std::vector<uint8_t>& bytes) { + void encodeFixed(const std::vector<uint8_t> &bytes) { encodeFixed(bytes.data(), bytes.size()); } @@ -144,7 +144,7 @@ public: /** * Shared pointer to Encoder. */ -typedef std::shared_ptr<Encoder> EncoderPtr; +using EncoderPtr = std::shared_ptr<Encoder>; /** * Returns an encoder that can encode binary Avro standard. @@ -155,19 +155,19 @@ AVRO_DECL EncoderPtr binaryEncoder(); * Returns an encoder that validates sequence of calls to an underlying * Encoder against the given schema. */ -AVRO_DECL EncoderPtr validatingEncoder(const ValidSchema& schema, - const EncoderPtr& base); +AVRO_DECL EncoderPtr validatingEncoder(const ValidSchema &schema, + const EncoderPtr &base); /** * Returns an encoder that encodes Avro standard for JSON. */ -AVRO_DECL EncoderPtr jsonEncoder(const ValidSchema& schema); +AVRO_DECL EncoderPtr jsonEncoder(const ValidSchema &schema); /** * Returns an encoder that encodes Avro standard for pretty printed JSON. */ -AVRO_DECL EncoderPtr jsonPrettyEncoder(const ValidSchema& schema); +AVRO_DECL EncoderPtr jsonPrettyEncoder(const ValidSchema &schema); -} // namespace avro +} // namespace avro #endif diff --git a/contrib/libs/apache/avro/api/Exception.hh b/contrib/libs/apache/avro/api/Exception.hh index 7c5410f96be..691869bed8c 100644 --- a/contrib/libs/apache/avro/api/Exception.hh +++ b/contrib/libs/apache/avro/api/Exception.hh @@ -20,25 +20,19 @@ #define avro_Exception_hh__ #include "Config.hh" -#include <stdexcept> #include <boost/format.hpp> +#include <stdexcept> namespace avro { /// Wrapper for std::runtime_error that provides convenience constructor /// for boost::format objects -class AVRO_DECL Exception : public virtual std::runtime_error -{ - public: - - Exception(const std::string &msg) : - std::runtime_error(msg) - { } +class AVRO_DECL Exception : public virtual std::runtime_error { +public: + explicit Exception(const std::string &msg) : std::runtime_error(msg) {} - Exception(const boost::format &msg) : - std::runtime_error( boost::str(msg)) - { } + explicit Exception(const boost::format &msg) : std::runtime_error(boost::str(msg)) {} }; } // namespace avro diff --git a/contrib/libs/apache/avro/api/Generic.hh b/contrib/libs/apache/avro/api/Generic.hh index e1b3a8290c8..f35e8cf6896 100644 --- a/contrib/libs/apache/avro/api/Generic.hh +++ b/contrib/libs/apache/avro/api/Generic.hh @@ -22,10 +22,10 @@ #include <boost/utility.hpp> #include "Config.hh" -#include "Types.hh" -#include "Encoder.hh" #include "Decoder.hh" +#include "Encoder.hh" #include "GenericDatum.hh" +#include "Types.hh" namespace avro { /** @@ -36,25 +36,26 @@ class AVRO_DECL GenericReader : boost::noncopyable { const bool isResolving_; const DecoderPtr decoder_; - static void read(GenericDatum& datum, Decoder& d, bool isResolving); + static void read(GenericDatum &datum, Decoder &d, bool isResolving); + public: /** * Constructs a reader for the given schema using the given decoder. */ - GenericReader(const ValidSchema& s, const DecoderPtr& decoder); + GenericReader(ValidSchema s, const DecoderPtr &decoder); /** * Constructs a reader for the given reader's schema \c readerSchema * using the given * decoder which holds data matching writer's schema \c writerSchema. */ - GenericReader(const ValidSchema& writerSchema, - const ValidSchema& readerSchema, const DecoderPtr& decoder); + GenericReader(const ValidSchema &writerSchema, + const ValidSchema &readerSchema, const DecoderPtr &decoder); /** * Reads a value off the decoder. */ - void read(GenericDatum& datum) const; + void read(GenericDatum &datum) const; /** * Drains any residual bytes in the input stream (e.g. because @@ -67,15 +68,14 @@ public: /** * Reads a generic datum from the stream, using the given schema. */ - static void read(Decoder& d, GenericDatum& g); + static void read(Decoder &d, GenericDatum &g); /** * Reads a generic datum from the stream, using the given schema. */ - static void read(Decoder& d, GenericDatum& g, const ValidSchema& s); + static void read(Decoder &d, GenericDatum &g, const ValidSchema &s); }; - /** * A utility class to write generic datum to encoders. */ @@ -83,48 +83,51 @@ class AVRO_DECL GenericWriter : boost::noncopyable { const ValidSchema schema_; const EncoderPtr encoder_; - static void write(const GenericDatum& datum, Encoder& e); + static void write(const GenericDatum &datum, Encoder &e); + public: /** * Constructs a writer for the given schema using the given encoder. */ - GenericWriter(const ValidSchema& s, const EncoderPtr& encoder); + GenericWriter(ValidSchema s, EncoderPtr encoder); /** * Writes a value onto the encoder. */ - void write(const GenericDatum& datum) const; + void write(const GenericDatum &datum) const; /** * Writes a generic datum on to the stream. */ - static void write(Encoder& e, const GenericDatum& g); + static void write(Encoder &e, const GenericDatum &g); /** * Writes a generic datum on to the stream, using the given schema. * Retained for backward compatibility. */ - static void write(Encoder& e, const GenericDatum& g, const ValidSchema&) { + static void write(Encoder &e, const GenericDatum &g, const ValidSchema &) { write(e, g); } }; -template <typename T> struct codec_traits; +template<typename T> +struct codec_traits; /** * Specialization of codec_traits for Generic datum along with its schema. * This is maintained for compatibility with old code. Please use the * cleaner codec_traits<GenericDatum> instead. */ -template <> struct codec_traits<std::pair<ValidSchema, GenericDatum> > { +template<> +struct codec_traits<std::pair<ValidSchema, GenericDatum>> { /** Encodes */ - static void encode(Encoder& e, - const std::pair<ValidSchema, GenericDatum>& p) { + static void encode(Encoder &e, + const std::pair<ValidSchema, GenericDatum> &p) { GenericWriter::write(e, p.second, p.first); } /** Decodes */ - static void decode(Decoder& d, std::pair<ValidSchema, GenericDatum>& p) { + static void decode(Decoder &d, std::pair<ValidSchema, GenericDatum> &p) { GenericReader::read(d, p.second, p.first); } }; @@ -132,18 +135,18 @@ template <> struct codec_traits<std::pair<ValidSchema, GenericDatum> > { /** * Specialization of codec_traits for GenericDatum. */ -template <> struct codec_traits<GenericDatum> { +template<> +struct codec_traits<GenericDatum> { /** Encodes */ - static void encode(Encoder& e, const GenericDatum& g) { + static void encode(Encoder &e, const GenericDatum &g) { GenericWriter::write(e, g); } /** Decodes */ - static void decode(Decoder& d, GenericDatum& g) { + static void decode(Decoder &d, GenericDatum &g) { GenericReader::read(d, g); } }; -} // namespace avro +} // namespace avro #endif - diff --git a/contrib/libs/apache/avro/api/GenericDatum.hh b/contrib/libs/apache/avro/api/GenericDatum.hh index ac0e5e5e204..f58fd949950 100644 --- a/contrib/libs/apache/avro/api/GenericDatum.hh +++ b/contrib/libs/apache/avro/api/GenericDatum.hh @@ -19,10 +19,10 @@ #ifndef avro_GenericDatum_hh__ #define avro_GenericDatum_hh__ -#include <stdint.h> -#include <vector> +#include <cstdint> #include <map> #include <string> +#include <vector> #if __cplusplus >= 201703L #include <any> @@ -54,7 +54,7 @@ namespace avro { * \li Avro <tt>array</tt> maps to C++ class <tt>GenericArray</tt>. * \li Avro <tt>map</tt> maps to C++ class <tt>GenericMap</tt>. * \li There is no C++ type corresponding to Avro <tt>union</tt>. The - * object should have the C++ type corresponing to one of the constituent + * object should have the C++ type corresponding to one of the constituent * types of the union. * */ @@ -68,17 +68,18 @@ protected: boost::any value_; #endif - GenericDatum(Type t) - : type_(t), logicalType_(LogicalType::NONE) { } + explicit GenericDatum(Type t) + : type_(t), logicalType_(LogicalType::NONE) {} GenericDatum(Type t, LogicalType logicalType) - : type_(t), logicalType_(logicalType) { } + : type_(t), logicalType_(logicalType) {} + + template<typename T> + GenericDatum(Type t, LogicalType logicalType, const T &v) + : type_(t), logicalType_(logicalType), value_(v) {} - template <typename T> - GenericDatum(Type t, LogicalType logicalType, const T& v) - : type_(t), logicalType_(logicalType), value_(v) { } + void init(const NodePtr &schema); - void init(const NodePtr& schema); public: /** * The avro data type this datum holds. @@ -95,7 +96,8 @@ public: * T The type for the value. This must correspond to the * avro type returned by type(). */ - template<typename T> const T& value() const; + template<typename T> + const T &value() const; /** * Returns the reference to the value held by this datum, which @@ -106,7 +108,8 @@ public: * T The type for the value. This must correspond to the * avro type returned by type(). */ - template<typename T> T& value(); + template<typename T> + T &value(); /** * Returns true if and only if this datum is a union. @@ -126,36 +129,49 @@ public: void selectBranch(size_t branch); /// Makes a new AVRO_NULL datum. - GenericDatum() : type_(AVRO_NULL), logicalType_(LogicalType::NONE) { } + GenericDatum() : type_(AVRO_NULL), logicalType_(LogicalType::NONE) {} /// Makes a new AVRO_BOOL datum whose value is of type bool. + /// We don't make this explicit constructor because we want to allow automatic conversion + // NOLINTNEXTLINE(google-explicit-constructor) GenericDatum(bool v) - : type_(AVRO_BOOL), logicalType_(LogicalType::NONE), value_(v) { } + : type_(AVRO_BOOL), logicalType_(LogicalType::NONE), value_(v) {} /// Makes a new AVRO_INT datum whose value is of type int32_t. + /// We don't make this explicit constructor because we want to allow automatic conversion + // NOLINTNEXTLINE(google-explicit-constructor) GenericDatum(int32_t v) - : type_(AVRO_INT), logicalType_(LogicalType::NONE), value_(v) { } + : type_(AVRO_INT), logicalType_(LogicalType::NONE), value_(v) {} /// Makes a new AVRO_LONG datum whose value is of type int64_t. + /// We don't make this explicit constructor because we want to allow automatic conversion + // NOLINTNEXTLINE(google-explicit-constructor) GenericDatum(int64_t v) - : type_(AVRO_LONG), logicalType_(LogicalType::NONE), value_(v) { } + : type_(AVRO_LONG), logicalType_(LogicalType::NONE), value_(v) {} /// Makes a new AVRO_FLOAT datum whose value is of type float. + /// We don't make this explicit constructor because we want to allow automatic conversion + // NOLINTNEXTLINE(google-explicit-constructor) GenericDatum(float v) - : type_(AVRO_FLOAT), logicalType_(LogicalType::NONE), value_(v) { } + : type_(AVRO_FLOAT), logicalType_(LogicalType::NONE), value_(v) {} /// Makes a new AVRO_DOUBLE datum whose value is of type double. + /// We don't make this explicit constructor because we want to allow automatic conversion + // NOLINTNEXTLINE(google-explicit-constructor) GenericDatum(double v) - : type_(AVRO_DOUBLE), logicalType_(LogicalType::NONE), value_(v) { } + : type_(AVRO_DOUBLE), logicalType_(LogicalType::NONE), value_(v) {} /// Makes a new AVRO_STRING datum whose value is of type std::string. - GenericDatum(const std::string& v) - : type_(AVRO_STRING), logicalType_(LogicalType::NONE), value_(v) { } + /// We don't make this explicit constructor because we want to allow automatic conversion + // NOLINTNEXTLINE(google-explicit-constructor) + GenericDatum(const std::string &v) + : type_(AVRO_STRING), logicalType_(LogicalType::NONE), value_(v) {} /// Makes a new AVRO_BYTES datum whose value is of type /// std::vector<uint8_t>. - GenericDatum(const std::vector<uint8_t>& v) : - type_(AVRO_BYTES), logicalType_(LogicalType::NONE), value_(v) { } + /// We don't make this explicit constructor because we want to allow automatic conversion + // NOLINTNEXTLINE(google-explicit-constructor) + GenericDatum(const std::vector<uint8_t> &v) : type_(AVRO_BYTES), logicalType_(LogicalType::NONE), value_(v) {} /** * Constructs a datum corresponding to the given avro type. @@ -163,7 +179,9 @@ public: * data type. * \param schema The schema that defines the avro type. */ - GenericDatum(const NodePtr& schema); + /// We don't make this explicit constructor because we want to allow automatic conversion + // NOLINTNEXTLINE(google-explicit-constructor) + GenericDatum(const NodePtr &schema); /** * Constructs a datum corresponding to the given avro type and set @@ -172,8 +190,7 @@ public: * \param v The value for this type. */ template<typename T> - GenericDatum(const NodePtr& schema, const T& v) : - type_(schema->type()), logicalType_(schema->logicalType()) { + GenericDatum(const NodePtr &schema, const T &v) : type_(schema->type()), logicalType_(schema->logicalType()) { init(schema); #if __cplusplus >= 201703L *std::any_cast<T>(&value_) = v; @@ -188,7 +205,7 @@ public: * data type. * \param schema The schema that defines the avro type. */ - GenericDatum(const ValidSchema& schema); + explicit GenericDatum(const ValidSchema &schema); }; /** @@ -196,18 +213,19 @@ public: */ class AVRO_DECL GenericContainer { NodePtr schema_; - static void assertType(const NodePtr& schema, Type type); + static void assertType(const NodePtr &schema, Type type); + protected: /** * Constructs a container corresponding to the given schema. */ - GenericContainer(Type type, const NodePtr& s) : schema_(s) { + GenericContainer(Type type, const NodePtr &s) : schema_(s) { assertType(s, type); } public: /// Returns the schema for this object - const NodePtr& schema() const { + const NodePtr &schema() const { return schema_; } }; @@ -225,8 +243,7 @@ public: * and the given value. The schema should be of Avro type union * and the value should correspond to one of the branches of the union. */ - GenericUnion(const NodePtr& schema) : - GenericContainer(AVRO_UNION, schema), curBranch_(schema->leaves()) { + explicit GenericUnion(const NodePtr &schema) : GenericContainer(AVRO_UNION, schema), curBranch_(schema->leaves()) { selectBranch(0); } @@ -250,7 +267,7 @@ public: * Returns the datum corresponding to the currently selected branch * in this union. */ - GenericDatum& datum() { + GenericDatum &datum() { return datum_; } @@ -258,7 +275,7 @@ public: * Returns the datum corresponding to the currently selected branch * in this union. */ - const GenericDatum& datum() const { + const GenericDatum &datum() const { return datum_; } }; @@ -268,12 +285,13 @@ public: */ class AVRO_DECL GenericRecord : public GenericContainer { std::vector<GenericDatum> fields_; + public: /** * Constructs a generic record corresponding to the given schema \p schema, * which should be of Avro type record. */ - GenericRecord(const NodePtr& schema); + explicit GenericRecord(const NodePtr &schema); /** * Returns the number of fields in the current record. @@ -285,7 +303,7 @@ public: /** * Returns index of the field with the given name \p name */ - size_t fieldIndex(const std::string& name) const { + size_t fieldIndex(const std::string &name) const { size_t index = 0; if (!schema()->nameIndex(name, index)) { throw Exception("Invalid field name: " + name); @@ -297,7 +315,7 @@ public: * Returns true if a field with the given name \p name is located in this r * false otherwise */ - bool hasField(const std::string& name) const { + bool hasField(const std::string &name) const { size_t index = 0; return schema()->nameIndex(name, index); } @@ -305,7 +323,7 @@ public: /** * Returns the field with the given name \p name. */ - const GenericDatum& field(const std::string& name) const { + const GenericDatum &field(const std::string &name) const { return fieldAt(fieldIndex(name)); } @@ -313,14 +331,14 @@ public: * Returns the reference to the field with the given name \p name, * which can be used to change the contents. */ - GenericDatum& field(const std::string& name) { + GenericDatum &field(const std::string &name) { return fieldAt(fieldIndex(name)); } /** * Returns the field at the given position \p pos. */ - const GenericDatum& fieldAt(size_t pos) const { + const GenericDatum &fieldAt(size_t pos) const { return fields_[pos]; } @@ -328,14 +346,14 @@ public: * Returns the reference to the field at the given position \p pos, * which can be used to change the contents. */ - GenericDatum& fieldAt(size_t pos) { + GenericDatum &fieldAt(size_t pos) { return fields_[pos]; } /** * Replaces the field at the given position \p pos with \p v. */ - void setFieldAt(size_t pos, const GenericDatum& v) { + void setFieldAt(size_t pos, const GenericDatum &v) { // assertSameType(v, schema()->leafAt(pos)); fields_[pos] = v; } @@ -355,22 +373,23 @@ public: * Constructs a generic array corresponding to the given schema \p schema, * which should be of Avro type array. */ - GenericArray(const NodePtr& schema) : GenericContainer(AVRO_ARRAY, schema) { + explicit GenericArray(const NodePtr &schema) : GenericContainer(AVRO_ARRAY, schema) { } /** * Returns the contents of this array. */ - const Value& value() const { + const Value &value() const { return value_; } /** * Returns the reference to the contents of this array. */ - Value& value() { + Value &value() { return value_; } + private: Value value_; }; @@ -383,28 +402,29 @@ public: /** * The contents type for the map. */ - typedef std::vector<std::pair<std::string, GenericDatum> > Value; + typedef std::vector<std::pair<std::string, GenericDatum>> Value; /** * Constructs a generic map corresponding to the given schema \p schema, * which should be of Avro type map. */ - GenericMap(const NodePtr& schema) : GenericContainer(AVRO_MAP, schema) { + explicit GenericMap(const NodePtr &schema) : GenericContainer(AVRO_MAP, schema) { } /** * Returns the contents of this map. */ - const Value& value() const { + const Value &value() const { return value_; } /** * Returns the reference to the contents of this map. */ - Value& value() { + Value &value() { return value_; } + private: Value value_; }; @@ -415,7 +435,7 @@ private: class AVRO_DECL GenericEnum : public GenericContainer { size_t value_; - static size_t index(const NodePtr& schema, const std::string& symbol) { + static size_t index(const NodePtr &schema, const std::string &symbol) { size_t result; if (schema->nameIndex(symbol, result)) { return result; @@ -428,19 +448,17 @@ public: * Constructs a generic enum corresponding to the given schema \p schema, * which should be of Avro type enum. */ - GenericEnum(const NodePtr& schema) : - GenericContainer(AVRO_ENUM, schema), value_(0) { + explicit GenericEnum(const NodePtr &schema) : GenericContainer(AVRO_ENUM, schema), value_(0) { } - GenericEnum(const NodePtr& schema, const std::string& symbol) : - GenericContainer(AVRO_ENUM, schema), value_(index(schema, symbol)) { + GenericEnum(const NodePtr &schema, const std::string &symbol) : GenericContainer(AVRO_ENUM, schema), value_(index(schema, symbol)) { } /** * Returns the symbol corresponding to the cardinal \p n. If the * value for \p n is not within the limits an exception is thrown. */ - const std::string& symbol(size_t n) { + const std::string &symbol(size_t n) { if (n < schema()->names()) { return schema()->nameAt(n); } @@ -451,14 +469,14 @@ public: * Returns the cardinal for the given symbol \c symbol. If the symbol * is not defined for this enum and exception is thrown. */ - size_t index(const std::string& symbol) const { + size_t index(const std::string &symbol) const { return index(schema(), symbol); } /** * Set the value for this enum corresponding to the given symbol \c symbol. */ - size_t set(const std::string& symbol) { + size_t set(const std::string &symbol) { return value_ = index(symbol); } @@ -483,7 +501,7 @@ public: /** * Returns the symbol for the current value of this enum. */ - const std::string& symbol() const { + const std::string &symbol() const { return schema()->nameAt(value_); } }; @@ -493,29 +511,29 @@ public: */ class AVRO_DECL GenericFixed : public GenericContainer { std::vector<uint8_t> value_; + public: /** * Constructs a generic enum corresponding to the given schema \p schema, * which should be of Avro type fixed. */ - GenericFixed(const NodePtr& schema) : GenericContainer(AVRO_FIXED, schema) { + explicit GenericFixed(const NodePtr &schema) : GenericContainer(AVRO_FIXED, schema) { value_.resize(schema->fixedSize()); } - GenericFixed(const NodePtr& schema, const std::vector<uint8_t>& v) : - GenericContainer(AVRO_FIXED, schema), value_(v) { } + GenericFixed(const NodePtr &schema, const std::vector<uint8_t> &v); /** * Returns the contents of this fixed. */ - const std::vector<uint8_t>& value() const { + const std::vector<uint8_t> &value() const { return value_; } /** * Returns the reference to the contents of this fixed. */ - std::vector<uint8_t>& value() { + std::vector<uint8_t> &value() { return value_; } }; @@ -523,36 +541,46 @@ public: inline Type GenericDatum::type() const { return (type_ == AVRO_UNION) ? #if __cplusplus >= 201703L - std::any_cast<GenericUnion>(&value_)->datum().type() : + std::any_cast<GenericUnion>(&value_)->datum().type() + : #else - boost::any_cast<GenericUnion>(&value_)->datum().type() : + boost::any_cast<GenericUnion>(&value_)->datum().type() + : #endif - type_; + type_; } inline LogicalType GenericDatum::logicalType() const { - return logicalType_; + return (type_ == AVRO_UNION) ? +#if __cplusplus >= 201703L + std::any_cast<GenericUnion>(&value_)->datum().logicalType() : +#else + boost::any_cast<GenericUnion>(&value_)->datum().logicalType() : +#endif + logicalType_; } -template<typename T> T& GenericDatum::value() { +template<typename T> +T &GenericDatum::value() { return (type_ == AVRO_UNION) ? #if __cplusplus >= 201703L - std::any_cast<GenericUnion>(&value_)->datum().value<T>() : - *std::any_cast<T>(&value_); + std::any_cast<GenericUnion>(&value_)->datum().value<T>() + : *std::any_cast<T>(&value_); #else - boost::any_cast<GenericUnion>(&value_)->datum().value<T>() : - *boost::any_cast<T>(&value_); + boost::any_cast<GenericUnion>(&value_)->datum().value<T>() + : *boost::any_cast<T>(&value_); #endif } -template<typename T> const T& GenericDatum::value() const { +template<typename T> +const T &GenericDatum::value() const { return (type_ == AVRO_UNION) ? #if __cplusplus >= 201703L - std::any_cast<GenericUnion>(&value_)->datum().value<T>() : - *std::any_cast<T>(&value_); + std::any_cast<GenericUnion>(&value_)->datum().value<T>() + : *std::any_cast<T>(&value_); #else - boost::any_cast<GenericUnion>(&value_)->datum().value<T>() : - *boost::any_cast<T>(&value_); + boost::any_cast<GenericUnion>(&value_)->datum().value<T>() + : *boost::any_cast<T>(&value_); #endif } @@ -572,5 +600,5 @@ inline void GenericDatum::selectBranch(size_t branch) { #endif } -} // namespace avro +} // namespace avro #endif // avro_GenericDatum_hh__ diff --git a/contrib/libs/apache/avro/api/Layout.hh b/contrib/libs/apache/avro/api/Layout.hh index ffd810c8b06..56d2c1d9c74 100644 --- a/contrib/libs/apache/avro/api/Layout.hh +++ b/contrib/libs/apache/avro/api/Layout.hh @@ -19,8 +19,8 @@ #ifndef avro_Layout_hh__ #define avro_Layout_hh__ -#include <boost/noncopyable.hpp> #include "Config.hh" +#include <boost/noncopyable.hpp> /// \file Layout.hh /// @@ -28,54 +28,39 @@ namespace avro { class AVRO_DECL Layout : private boost::noncopyable { +protected: + explicit Layout(size_t offset = 0) : offset_(offset) {} - protected: - - Layout(size_t offset = 0) : - offset_(offset) - {} - - public: - +public: size_t offset() const { return offset_; } + virtual ~Layout() = default; - virtual ~Layout() {} - - private: - +private: const size_t offset_; }; class AVRO_DECL PrimitiveLayout : public Layout { - - public: - - PrimitiveLayout(size_t offset = 0) : - Layout(offset) - {} +public: + explicit PrimitiveLayout(size_t offset = 0) : Layout(offset) {} }; class AVRO_DECL CompoundLayout : public Layout { - public: - - CompoundLayout(size_t offset = 0) : - Layout(offset) - {} +public: + explicit CompoundLayout(size_t offset = 0) : Layout(offset) {} void add(std::unique_ptr<Layout> &layout) { layouts_.push_back(std::move(layout)); } - const Layout &at (size_t idx) const { + const Layout &at(size_t idx) const { return *layouts_.at(idx); } - private: - - std::vector<std::unique_ptr<Layout> > layouts_; +private: + std::vector<std::unique_ptr<Layout>> layouts_; }; } // namespace avro diff --git a/contrib/libs/apache/avro/api/LogicalType.hh b/contrib/libs/apache/avro/api/LogicalType.hh index 33972788fb2..4d06e74f635 100644 --- a/contrib/libs/apache/avro/api/LogicalType.hh +++ b/contrib/libs/apache/avro/api/LogicalType.hh @@ -26,7 +26,7 @@ namespace avro { class AVRO_DECL LogicalType { - public: +public: enum Type { NONE, DECIMAL, @@ -52,14 +52,14 @@ class AVRO_DECL LogicalType { void setScale(int scale); int scale() const { return scale_; } - void printJson(std::ostream& os) const; + void printJson(std::ostream &os) const; - private: +private: Type type_; int precision_; int scale_; }; -} // namespace avro +} // namespace avro #endif diff --git a/contrib/libs/apache/avro/api/Node.hh b/contrib/libs/apache/avro/api/Node.hh index 205b06f00eb..3c9389da50a 100644 --- a/contrib/libs/apache/avro/api/Node.hh +++ b/contrib/libs/apache/avro/api/Node.hh @@ -21,53 +21,54 @@ #include "Config.hh" +#include <boost/noncopyable.hpp> #include <cassert> #include <memory> -#include <boost/noncopyable.hpp> +#include <utility> #include "Exception.hh" #include "LogicalType.hh" -#include "Types.hh" #include "SchemaResolution.hh" +#include "Types.hh" namespace avro { class Node; class GenericDatum; -typedef std::shared_ptr<Node> NodePtr; +using NodePtr = std::shared_ptr<Node>; class AVRO_DECL Name { std::string ns_; std::string simpleName_; + public: - Name() { } - Name(const std::string& fullname); - Name(const std::string& simpleName, const std::string& ns) : ns_(ns), simpleName_(simpleName) { check(); } + Name() = default; + explicit Name(const std::string &fullname); + Name(std::string simpleName, std::string ns) : ns_(std::move(ns)), simpleName_(std::move(simpleName)) { check(); } - const std::string fullname() const; - const std::string& ns() const { return ns_; } - const std::string& simpleName() const { return simpleName_; } + std::string fullname() const; + const std::string &ns() const { return ns_; } + const std::string &simpleName() const { return simpleName_; } - void ns(const std::string& n) { ns_ = n; } - void simpleName(const std::string& n) { simpleName_ = n; } - void fullname(const std::string& n); + void ns(std::string n) { ns_ = std::move(n); } + void simpleName(std::string n) { simpleName_ = std::move(n); } + void fullname(const std::string &n); - bool operator < (const Name& n) const; + bool operator<(const Name &n) const; void check() const; - bool operator == (const Name& n) const; - bool operator != (const Name& n) const { return !((*this) == n); } + bool operator==(const Name &n) const; + bool operator!=(const Name &n) const { return !((*this) == n); } void clear() { ns_.clear(); simpleName_.clear(); } - operator std::string() const { + explicit operator std::string() const { return fullname(); } }; -inline -std::ostream& operator << (std::ostream& os, const Name& n) { +inline std::ostream &operator<<(std::ostream &os, const Name &n) { return os << n.fullname(); } @@ -86,15 +87,11 @@ std::ostream& operator << (std::ostream& os, const Name& n) { /// different node types. /// -class AVRO_DECL Node : private boost::noncopyable -{ - public: - - Node(Type type) : - type_(type), - logicalType_(LogicalType::NONE), - locked_(false) - {} +class AVRO_DECL Node : private boost::noncopyable { +public: + explicit Node(Type type) : type_(type), + logicalType_(LogicalType::NONE), + locked_(false) {} virtual ~Node(); @@ -136,45 +133,44 @@ class AVRO_DECL Node : private boost::noncopyable doAddLeaf(newLeaf); } virtual size_t leaves() const = 0; - virtual const NodePtr& leafAt(int index) const = 0; - virtual const GenericDatum& defaultValueAt(int index) { + virtual const NodePtr &leafAt(size_t index) const = 0; + virtual const GenericDatum &defaultValueAt(size_t index) { throw Exception(boost::format("No default value at: %1%") % index); } void addName(const std::string &name) { checkLock(); - checkName(name); + checkName(Name(name)); doAddName(name); } virtual size_t names() const = 0; - virtual const std::string &nameAt(int index) const = 0; + virtual const std::string &nameAt(size_t index) const = 0; virtual bool nameIndex(const std::string &name, size_t &index) const = 0; - void setFixedSize(int size) { + void setFixedSize(size_t size) { checkLock(); doSetFixedSize(size); } - virtual int fixedSize() const = 0; + virtual size_t fixedSize() const = 0; virtual bool isValid() const = 0; virtual SchemaResolution resolve(const Node &reader) const = 0; - virtual void printJson(std::ostream &os, int depth) const = 0; + virtual void printJson(std::ostream &os, size_t depth) const = 0; virtual void printBasicInfo(std::ostream &os) const = 0; - virtual void setLeafToSymbolic(int index, const NodePtr &node) = 0; + virtual void setLeafToSymbolic(size_t index, const NodePtr &node) = 0; // Serialize the default value GenericDatum g for the node contained // in a record node. - virtual void printDefaultToJson(const GenericDatum& g, std::ostream &os, - int depth) const = 0; - - protected: + virtual void printDefaultToJson(const GenericDatum &g, std::ostream &os, + size_t depth) const = 0; +protected: void checkLock() const { - if(locked()) { + if (locked()) { throw Exception("Cannot modify locked schema"); } } @@ -188,10 +184,9 @@ class AVRO_DECL Node : private boost::noncopyable virtual void doAddLeaf(const NodePtr &newLeaf) = 0; virtual void doAddName(const std::string &name) = 0; - virtual void doSetFixedSize(int size) = 0; - - private: + virtual void doSetFixedSize(size_t size) = 0; +private: const Type type_; LogicalType logicalType_; bool locked_; @@ -200,12 +195,10 @@ class AVRO_DECL Node : private boost::noncopyable } // namespace avro namespace std { -inline std::ostream& operator<<(std::ostream& os, const avro::Node& n) -{ +inline std::ostream &operator<<(std::ostream &os, const avro::Node &n) { n.printJson(os, 0); return os; } -} - +} // namespace std #endif diff --git a/contrib/libs/apache/avro/api/NodeConcepts.hh b/contrib/libs/apache/avro/api/NodeConcepts.hh index e914d925b60..155c8ed9193 100644 --- a/contrib/libs/apache/avro/api/NodeConcepts.hh +++ b/contrib/libs/apache/avro/api/NodeConcepts.hh @@ -21,13 +21,12 @@ #include "Config.hh" -#include <vector> -#include <map> #include "Exception.hh" +#include <map> +#include <vector> namespace avro { - /// /// The concept classes are used to simplify NodeImpl. Since different types /// of avro types carry different attributes, such as names, or field names for @@ -47,59 +46,48 @@ namespace avro { namespace concepts { -template <typename Attribute> -struct NoAttribute -{ +template<typename Attribute> +struct NoAttribute { static const bool hasAttribute = false; size_t size() const { return 0; } - void add( const Attribute &) { + void add(const Attribute & /* attr */) { // There must be an add function for the generic NodeImpl, but the // Node APIs ensure that it is never called, the throw here is // just in case throw Exception("This type does not have attribute"); } - const Attribute &get(size_t = 0) const { + const Attribute &get(size_t /* index */ = 0) const { // There must be an get function for the generic NodeImpl, but the // Node APIs ensure that it is never called, the throw here is // just in case throw Exception("This type does not have attribute"); - // even though this code is unreachable the compiler requires it - static const Attribute empty = Attribute(); - return empty; } - Attribute &get(size_t = 0) { + Attribute &get(size_t /* index */ = 0) { // There must be an get function for the generic NodeImpl, but the // Node APIs ensure that it is never called, the throw here is // just in case throw Exception("This type does not have attribute"); } - }; template<typename Attribute> -struct SingleAttribute -{ +struct SingleAttribute { static const bool hasAttribute = true; - SingleAttribute() : attr_() - { } + SingleAttribute() : attr_() {} - SingleAttribute(const Attribute& a) : attr_(a) { } + explicit SingleAttribute(const Attribute &a) : attr_(a) {} // copy constructing from another single attribute is allowed - SingleAttribute(const SingleAttribute<Attribute> &rhs) : - attr_(rhs.attr_) - { } + SingleAttribute(const SingleAttribute<Attribute> &rhs) : attr_(rhs.attr_) {} // copy constructing from a no attribute is allowed - SingleAttribute(const NoAttribute<Attribute> &) : - attr_() - { } + explicit SingleAttribute(const NoAttribute<Attribute> &rhs) : attr_() {} size_t size() const { return 1; @@ -124,34 +112,29 @@ struct SingleAttribute } private: - template<typename T> friend struct MultiAttribute; + template<typename T> + friend struct MultiAttribute; Attribute attr_; }; template<typename Attribute> -struct MultiAttribute -{ +struct MultiAttribute { static const bool hasAttribute = true; - MultiAttribute() - { } + MultiAttribute() = default; // copy constructing from another single attribute is allowed, it // pushes the attribute - MultiAttribute(const SingleAttribute<Attribute> &rhs) - { + explicit MultiAttribute(const SingleAttribute<Attribute> &rhs) { // since map is the only type that does this we know it's // final size will be two, so reserve attrs_.reserve(2); attrs_.push_back(rhs.attr_); } - MultiAttribute(const MultiAttribute<Attribute> &rhs) : - attrs_(rhs.attrs_) - { } + MultiAttribute(const MultiAttribute<Attribute> &rhs) : attrs_(rhs.attrs_) {} - MultiAttribute(const NoAttribute<Attribute> &) - {} + explicit MultiAttribute(const NoAttribute<Attribute> &rhs) {} size_t size() const { return attrs_.size(); @@ -169,52 +152,46 @@ struct MultiAttribute return attrs_.at(index); } - private: - +private: std::vector<Attribute> attrs_; }; - template<typename T> struct NameIndexConcept { - bool lookup(const std::string &, size_t &) const { + bool lookup(const std::string &name, size_t &index) const { throw Exception("Name index does not exist"); - return 0; } - bool add(const::std::string &, size_t) { + bool add(const ::std::string &name, size_t) { throw Exception("Name index does not exist"); - return false; } }; template<> -struct NameIndexConcept < MultiAttribute<std::string> > -{ - typedef std::map<std::string, size_t> IndexMap; +struct NameIndexConcept<MultiAttribute<std::string>> { + using IndexMap = std::map<std::string, size_t>; bool lookup(const std::string &name, size_t &index) const { - IndexMap::const_iterator iter = map_.find(name); - if(iter == map_.end()) { + auto iter = map_.find(name); + if (iter == map_.end()) { return false; } index = iter->second; return true; } - bool add(const::std::string &name, size_t index) { + bool add(const ::std::string &name, size_t index) { bool added = false; - IndexMap::iterator lb = map_.lower_bound(name); - if(lb == map_.end() || map_.key_comp()(name, lb->first)) { + auto lb = map_.lower_bound(name); + if (lb == map_.end() || map_.key_comp()(name, lb->first)) { map_.insert(lb, IndexMap::value_type(name, index)); added = true; } return added; } - private: - +private: IndexMap map_; }; diff --git a/contrib/libs/apache/avro/api/NodeImpl.hh b/contrib/libs/apache/avro/api/NodeImpl.hh index debce720a6c..c74d39e6b8b 100644 --- a/contrib/libs/apache/avro/api/NodeImpl.hh +++ b/contrib/libs/apache/avro/api/NodeImpl.hh @@ -22,12 +22,13 @@ #include "Config.hh" #include "GenericDatum.hh" -#include <limits> -#include <set> #include <iomanip> #include <iostream> -#include <sstream> +#include <limits> #include <memory> +#include <set> +#include <sstream> +#include <utility> #include "Node.hh" #include "NodeConcepts.hh" @@ -37,39 +38,31 @@ namespace avro { /// Implementation details for Node. NodeImpl represents all the avro types, /// whose properties are enabled and disabled by selecting concept classes. -template -< +template< class NameConcept, class LeavesConcept, class LeafNamesConcept, - class SizeConcept -> -class NodeImpl : public Node -{ - - protected: - - NodeImpl(Type type) : - Node(type), - nameAttribute_(), - docAttribute_(), - leafAttributes_(), - leafNameAttributes_(), - sizeAttribute_() - { } + class SizeConcept> +class NodeImpl : public Node { + +protected: + explicit NodeImpl(Type type) : Node(type), + nameAttribute_(), + docAttribute_(), + leafAttributes_(), + leafNameAttributes_(), + sizeAttribute_() {} NodeImpl(Type type, const NameConcept &name, const LeavesConcept &leaves, const LeafNamesConcept &leafNames, - const SizeConcept &size) : - Node(type), - nameAttribute_(name), - docAttribute_(), - leafAttributes_(leaves), - leafNameAttributes_(leafNames), - sizeAttribute_(size) - { } + const SizeConcept &size) : Node(type), + nameAttribute_(name), + docAttribute_(), + leafAttributes_(leaves), + leafNameAttributes_(leafNames), + sizeAttribute_(size) {} // Ctor with "doc" NodeImpl(Type type, @@ -77,16 +70,14 @@ class NodeImpl : public Node const concepts::SingleAttribute<std::string> &doc, const LeavesConcept &leaves, const LeafNamesConcept &leafNames, - const SizeConcept &size) : - Node(type), - nameAttribute_(name), - docAttribute_(doc), - leafAttributes_(leaves), - leafNameAttributes_(leafNames), - sizeAttribute_(size) - {} - - void swap(NodeImpl& impl) { + const SizeConcept &size) : Node(type), + nameAttribute_(name), + docAttribute_(doc), + leafAttributes_(leaves), + leafNameAttributes_(leafNames), + sizeAttribute_(size) {} + + void swap(NodeImpl &impl) { std::swap(nameAttribute_, impl.nameAttribute_); std::swap(docAttribute_, impl.docAttribute_); std::swap(leafAttributes_, impl.leafAttributes_); @@ -95,71 +86,71 @@ class NodeImpl : public Node std::swap(nameIndex_, impl.nameIndex_); } - bool hasName() const { - // e.g.: true for single and multiattributes, false for noattributes. + bool hasName() const override { + // e.g.: true for single and multi-attributes, false for no-attributes. return NameConcept::hasAttribute; } - void doSetName(const Name &name) { + void doSetName(const Name &name) override { nameAttribute_.add(name); } - const Name &name() const { + const Name &name() const override { return nameAttribute_.get(); } - void doSetDoc(const std::string &doc) { - docAttribute_.add(doc); + void doSetDoc(const std::string &doc) override { + docAttribute_.add(doc); } - const std::string &getDoc() const { - return docAttribute_.get(); + const std::string &getDoc() const override { + return docAttribute_.get(); } - void doAddLeaf(const NodePtr &newLeaf) { + void doAddLeaf(const NodePtr &newLeaf) final { leafAttributes_.add(newLeaf); } - size_t leaves() const { + size_t leaves() const override { return leafAttributes_.size(); } - const NodePtr &leafAt(int index) const { + const NodePtr &leafAt(size_t index) const override { return leafAttributes_.get(index); } - void doAddName(const std::string &name) { - if (! nameIndex_.add(name, leafNameAttributes_.size())) { + void doAddName(const std::string &name) override { + if (!nameIndex_.add(name, leafNameAttributes_.size())) { throw Exception(boost::format("Cannot add duplicate name: %1%") % name); } leafNameAttributes_.add(name); } - size_t names() const { + size_t names() const override { return leafNameAttributes_.size(); } - const std::string &nameAt(int index) const { + const std::string &nameAt(size_t index) const override { return leafNameAttributes_.get(index); } - bool nameIndex(const std::string &name, size_t &index) const { + bool nameIndex(const std::string &name, size_t &index) const override { return nameIndex_.lookup(name, index); } - void doSetFixedSize(int size) { + void doSetFixedSize(size_t size) override { sizeAttribute_.add(size); } - int fixedSize() const { + size_t fixedSize() const override { return sizeAttribute_.get(); } - virtual bool isValid() const = 0; + bool isValid() const override = 0; - void printBasicInfo(std::ostream &os) const; + void printBasicInfo(std::ostream &os) const override; - void setLeafToSymbolic(int index, const NodePtr &node); + void setLeafToSymbolic(size_t index, const NodePtr &node) override; SchemaResolution furtherResolution(const Node &reader) const { SchemaResolution match = RESOLVE_NO_MATCH; @@ -169,20 +160,19 @@ class NodeImpl : public Node // resolve the symbolic type, and check again const NodePtr &node = reader.leafAt(0); match = resolve(*node); - } - else if(reader.type() == AVRO_UNION) { + } else if (reader.type() == AVRO_UNION) { // in this case, need to see if there is an exact match for the // writer's type, or if not, the first one that can be promoted to a // match - for(size_t i= 0; i < reader.leaves(); ++i) { + for (size_t i = 0; i < reader.leaves(); ++i) { const NodePtr &node = reader.leafAt(i); SchemaResolution thisMatch = resolve(*node); // if matched then the search is done - if(thisMatch == RESOLVE_MATCH) { + if (thisMatch == RESOLVE_MATCH) { match = thisMatch; break; } @@ -209,84 +199,72 @@ class NodeImpl : public Node concepts::NameIndexConcept<LeafNamesConcept> nameIndex_; }; -typedef concepts::NoAttribute<Name> NoName; -typedef concepts::SingleAttribute<Name> HasName; - -typedef concepts::SingleAttribute<std::string> HasDoc; +using NoName = concepts::NoAttribute<Name>; +using HasName = concepts::SingleAttribute<Name>; -typedef concepts::NoAttribute<NodePtr> NoLeaves; -typedef concepts::SingleAttribute<NodePtr> SingleLeaf; -typedef concepts::MultiAttribute<NodePtr> MultiLeaves; +using HasDoc = concepts::SingleAttribute<std::string>; -typedef concepts::NoAttribute<std::string> NoLeafNames; -typedef concepts::MultiAttribute<std::string> LeafNames; +using NoLeaves = concepts::NoAttribute<NodePtr>; +using SingleLeaf = concepts::SingleAttribute<NodePtr>; +using MultiLeaves = concepts::MultiAttribute<NodePtr>; -typedef concepts::NoAttribute<int> NoSize; -typedef concepts::SingleAttribute<int> HasSize; +using NoLeafNames = concepts::NoAttribute<std::string>; +using LeafNames = concepts::MultiAttribute<std::string>; -typedef NodeImpl< NoName, NoLeaves, NoLeafNames, NoSize > NodeImplPrimitive; -typedef NodeImpl< HasName, NoLeaves, NoLeafNames, NoSize > NodeImplSymbolic; +using NoSize = concepts::NoAttribute<int>; +using HasSize = concepts::SingleAttribute<int>; -typedef NodeImpl< HasName, MultiLeaves, LeafNames, NoSize > NodeImplRecord; -typedef NodeImpl< HasName, NoLeaves, LeafNames, NoSize > NodeImplEnum; -typedef NodeImpl< NoName, SingleLeaf, NoLeafNames, NoSize > NodeImplArray; -typedef NodeImpl< NoName, MultiLeaves, NoLeafNames, NoSize > NodeImplMap; -typedef NodeImpl< NoName, MultiLeaves, NoLeafNames, NoSize > NodeImplUnion; -typedef NodeImpl< HasName, NoLeaves, NoLeafNames, HasSize > NodeImplFixed; +using NodeImplPrimitive = NodeImpl<NoName, NoLeaves, NoLeafNames, NoSize>; +using NodeImplSymbolic = NodeImpl<HasName, NoLeaves, NoLeafNames, NoSize>; -class AVRO_DECL NodePrimitive : public NodeImplPrimitive -{ - public: +using NodeImplRecord = NodeImpl<HasName, MultiLeaves, LeafNames, NoSize>; +using NodeImplEnum = NodeImpl<HasName, NoLeaves, LeafNames, NoSize>; +using NodeImplArray = NodeImpl<NoName, SingleLeaf, NoLeafNames, NoSize>; +using NodeImplMap = NodeImpl<NoName, MultiLeaves, NoLeafNames, NoSize>; +using NodeImplUnion = NodeImpl<NoName, MultiLeaves, NoLeafNames, NoSize>; +using NodeImplFixed = NodeImpl<HasName, NoLeaves, NoLeafNames, HasSize>; - explicit NodePrimitive(Type type) : - NodeImplPrimitive(type) - { } +class AVRO_DECL NodePrimitive : public NodeImplPrimitive { +public: + explicit NodePrimitive(Type type) : NodeImplPrimitive(type) {} - SchemaResolution resolve(const Node &reader) const; + SchemaResolution resolve(const Node &reader) const override; - void printJson(std::ostream &os, int depth) const; + void printJson(std::ostream &os, size_t depth) const override; - bool isValid() const { + bool isValid() const override { return true; } - void printDefaultToJson(const GenericDatum& g, std::ostream &os, int depth) const; + void printDefaultToJson(const GenericDatum &g, std::ostream &os, size_t depth) const override; }; -class AVRO_DECL NodeSymbolic : public NodeImplSymbolic -{ - typedef std::weak_ptr<Node> NodeWeakPtr; - - public: +class AVRO_DECL NodeSymbolic : public NodeImplSymbolic { + using NodeWeakPtr = std::weak_ptr<Node>; - NodeSymbolic() : - NodeImplSymbolic(AVRO_SYMBOLIC) - { } +public: + NodeSymbolic() : NodeImplSymbolic(AVRO_SYMBOLIC) {} - explicit NodeSymbolic(const HasName &name) : - NodeImplSymbolic(AVRO_SYMBOLIC, name, NoLeaves(), NoLeafNames(), NoSize()) - { } + explicit NodeSymbolic(const HasName &name) : NodeImplSymbolic(AVRO_SYMBOLIC, name, NoLeaves(), NoLeafNames(), NoSize()) {} - NodeSymbolic(const HasName &name, const NodePtr n) : - NodeImplSymbolic(AVRO_SYMBOLIC, name, NoLeaves(), NoLeafNames(), NoSize()), actualNode_(n) - { } - SchemaResolution resolve(const Node &reader) const; + NodeSymbolic(const HasName &name, const NodePtr &n) : NodeImplSymbolic(AVRO_SYMBOLIC, name, NoLeaves(), NoLeafNames(), NoSize()), actualNode_(n) {} + SchemaResolution resolve(const Node &reader) const override; - void printJson(std::ostream &os, int depth) const; + void printJson(std::ostream &os, size_t depth) const override; - bool isValid() const { + bool isValid() const override { return (nameAttribute_.size() == 1); } - void printDefaultToJson(const GenericDatum& g, std::ostream &os, int depth) const; + void printDefaultToJson(const GenericDatum &g, std::ostream &os, size_t depth) const override; bool isSet() const { - return (actualNode_.lock() != 0); + return (actualNode_.lock() != nullptr); } NodePtr getNode() const { NodePtr node = actualNode_.lock(); - if(!node) { + if (!node) { throw Exception(boost::format("Could not follow symbol %1%") % name()); } return node; @@ -296,134 +274,98 @@ class AVRO_DECL NodeSymbolic : public NodeImplSymbolic actualNode_ = node; } - protected: - +protected: NodeWeakPtr actualNode_; - }; class AVRO_DECL NodeRecord : public NodeImplRecord { std::vector<GenericDatum> defaultValues; + public: - NodeRecord() : NodeImplRecord(AVRO_RECORD) { } + NodeRecord() : NodeImplRecord(AVRO_RECORD) {} NodeRecord(const HasName &name, const MultiLeaves &fields, - const LeafNames &fieldsNames, - const std::vector<GenericDatum>& dv) : - NodeImplRecord(AVRO_RECORD, name, fields, fieldsNames, NoSize()), - defaultValues(dv) { - for (size_t i = 0; i < leafNameAttributes_.size(); ++i) { - if (!nameIndex_.add(leafNameAttributes_.get(i), i)) { - throw Exception(boost::format( - "Cannot add duplicate field: %1%") % - leafNameAttributes_.get(i)); - } - } - } + const LeafNames &fieldsNames, + std::vector<GenericDatum> dv); NodeRecord(const HasName &name, const HasDoc &doc, const MultiLeaves &fields, - const LeafNames &fieldsNames, - const std::vector<GenericDatum> &dv) : - NodeImplRecord(AVRO_RECORD, name, doc, fields, fieldsNames, NoSize()), - defaultValues(dv) { + const LeafNames &fieldsNames, + std::vector<GenericDatum> dv) : NodeImplRecord(AVRO_RECORD, name, doc, fields, fieldsNames, NoSize()), + defaultValues(std::move(dv)) { for (size_t i = 0; i < leafNameAttributes_.size(); ++i) { if (!nameIndex_.add(leafNameAttributes_.get(i), i)) { throw Exception(boost::format( - "Cannot add duplicate field: %1%") % - leafNameAttributes_.get(i)); + "Cannot add duplicate field: %1%") + % leafNameAttributes_.get(i)); } } } - void swap(NodeRecord& r) { + void swap(NodeRecord &r) { NodeImplRecord::swap(r); defaultValues.swap(r.defaultValues); } - SchemaResolution resolve(const Node &reader) const; + SchemaResolution resolve(const Node &reader) const override; - void printJson(std::ostream &os, int depth) const; + void printJson(std::ostream &os, size_t depth) const override; - bool isValid() const { - return ((nameAttribute_.size() == 1) && - (leafAttributes_.size() == leafNameAttributes_.size())); + bool isValid() const override { + return ((nameAttribute_.size() == 1) && (leafAttributes_.size() == leafNameAttributes_.size())); } - const GenericDatum& defaultValueAt(int index) { + const GenericDatum &defaultValueAt(size_t index) override { return defaultValues[index]; } - void printDefaultToJson(const GenericDatum& g, std::ostream &os, int depth) const; + void printDefaultToJson(const GenericDatum &g, std::ostream &os, size_t depth) const override; }; -class AVRO_DECL NodeEnum : public NodeImplEnum -{ - public: - - NodeEnum() : - NodeImplEnum(AVRO_ENUM) - { } +class AVRO_DECL NodeEnum : public NodeImplEnum { +public: + NodeEnum() : NodeImplEnum(AVRO_ENUM) {} - NodeEnum(const HasName &name, const LeafNames &symbols) : - NodeImplEnum(AVRO_ENUM, name, NoLeaves(), symbols, NoSize()) - { - for(size_t i=0; i < leafNameAttributes_.size(); ++i) { - if(!nameIndex_.add(leafNameAttributes_.get(i), i)) { - throw Exception(boost::format("Cannot add duplicate enum: %1%") % leafNameAttributes_.get(i)); + NodeEnum(const HasName &name, const LeafNames &symbols) : NodeImplEnum(AVRO_ENUM, name, NoLeaves(), symbols, NoSize()) { + for (size_t i = 0; i < leafNameAttributes_.size(); ++i) { + if (!nameIndex_.add(leafNameAttributes_.get(i), i)) { + throw Exception(boost::format("Cannot add duplicate enum: %1%") % leafNameAttributes_.get(i)); } } } - SchemaResolution resolve(const Node &reader) const; + SchemaResolution resolve(const Node &reader) const override; - void printJson(std::ostream &os, int depth) const; + void printJson(std::ostream &os, size_t depth) const override; - bool isValid() const { + bool isValid() const override { return ( - (nameAttribute_.size() == 1) && - (leafNameAttributes_.size() > 0) - ); + (nameAttribute_.size() == 1) && (leafNameAttributes_.size() > 0)); } - void printDefaultToJson(const GenericDatum& g, std::ostream &os, int depth) const; + void printDefaultToJson(const GenericDatum &g, std::ostream &os, size_t depth) const override; }; -class AVRO_DECL NodeArray : public NodeImplArray -{ - public: - - NodeArray() : - NodeImplArray(AVRO_ARRAY) - { } +class AVRO_DECL NodeArray : public NodeImplArray { +public: + NodeArray() : NodeImplArray(AVRO_ARRAY) {} - explicit NodeArray(const SingleLeaf &items) : - NodeImplArray(AVRO_ARRAY, NoName(), items, NoLeafNames(), NoSize()) - { } + explicit NodeArray(const SingleLeaf &items) : NodeImplArray(AVRO_ARRAY, NoName(), items, NoLeafNames(), NoSize()) {} - SchemaResolution resolve(const Node &reader) const; + SchemaResolution resolve(const Node &reader) const override; - void printJson(std::ostream &os, int depth) const; + void printJson(std::ostream &os, size_t depth) const override; - bool isValid() const { + bool isValid() const override { return (leafAttributes_.size() == 1); } - void printDefaultToJson(const GenericDatum& g, std::ostream &os, int depth) const; + void printDefaultToJson(const GenericDatum &g, std::ostream &os, size_t depth) const override; }; -class AVRO_DECL NodeMap : public NodeImplMap -{ - public: - - NodeMap() : - NodeImplMap(AVRO_MAP) - { - NodePtr key(new NodePrimitive(AVRO_STRING)); - doAddLeaf(key); - } +class AVRO_DECL NodeMap : public NodeImplMap { +public: + NodeMap(); - explicit NodeMap(const SingleLeaf &values) : - NodeImplMap(AVRO_MAP, NoName(), values, NoLeafNames(), NoSize()) - { + explicit NodeMap(const SingleLeaf &values) : NodeImplMap(AVRO_MAP, NoName(), MultiLeaves(values), NoLeafNames(), NoSize()) { // need to add the key for the map too NodePtr key(new NodePrimitive(AVRO_STRING)); doAddLeaf(key); @@ -432,79 +374,72 @@ class AVRO_DECL NodeMap : public NodeImplMap std::swap(leafAttributes_.get(0), leafAttributes_.get(1)); } - SchemaResolution resolve(const Node &reader) const; + SchemaResolution resolve(const Node &reader) const override; - void printJson(std::ostream &os, int depth) const; + void printJson(std::ostream &os, size_t depth) const override; - bool isValid() const { + bool isValid() const override { return (leafAttributes_.size() == 2); } - void printDefaultToJson(const GenericDatum& g, std::ostream &os, int depth) const; + void printDefaultToJson(const GenericDatum &g, std::ostream &os, size_t depth) const override; }; -class AVRO_DECL NodeUnion : public NodeImplUnion -{ - public: - - NodeUnion() : - NodeImplUnion(AVRO_UNION) - { } +class AVRO_DECL NodeUnion : public NodeImplUnion { +public: + NodeUnion() : NodeImplUnion(AVRO_UNION) {} - explicit NodeUnion(const MultiLeaves &types) : - NodeImplUnion(AVRO_UNION, NoName(), types, NoLeafNames(), NoSize()) - { } + explicit NodeUnion(const MultiLeaves &types) : NodeImplUnion(AVRO_UNION, NoName(), types, NoLeafNames(), NoSize()) {} - SchemaResolution resolve(const Node &reader) const; + SchemaResolution resolve(const Node &reader) const override; - void printJson(std::ostream &os, int depth) const; + void printJson(std::ostream &os, size_t depth) const override; - bool isValid() const { + bool isValid() const override { std::set<std::string> seen; if (leafAttributes_.size() >= 1) { for (size_t i = 0; i < leafAttributes_.size(); ++i) { std::string name; - const NodePtr& n = leafAttributes_.get(i); + const NodePtr &n = leafAttributes_.get(i); switch (n->type()) { - case AVRO_STRING: - name = "string"; - break; - case AVRO_BYTES: - name = "bytes"; - break; - case AVRO_INT: - name = "int"; - break; - case AVRO_LONG: - name = "long"; - break; - case AVRO_FLOAT: - name = "float"; - break; - case AVRO_DOUBLE: - name = "double"; - break; - case AVRO_BOOL: - name = "bool"; - break; - case AVRO_NULL: - name = "null"; - break; - case AVRO_ARRAY: - name = "array"; - break; - case AVRO_MAP: - name = "map"; - break; - case AVRO_RECORD: - case AVRO_ENUM: - case AVRO_UNION: - case AVRO_FIXED: - case AVRO_SYMBOLIC: - name = n->name().fullname(); - break; - default: - return false; + case AVRO_STRING: + name = "string"; + break; + case AVRO_BYTES: + name = "bytes"; + break; + case AVRO_INT: + name = "int"; + break; + case AVRO_LONG: + name = "long"; + break; + case AVRO_FLOAT: + name = "float"; + break; + case AVRO_DOUBLE: + name = "double"; + break; + case AVRO_BOOL: + name = "bool"; + break; + case AVRO_NULL: + name = "null"; + break; + case AVRO_ARRAY: + name = "array"; + break; + case AVRO_MAP: + name = "map"; + break; + case AVRO_RECORD: + case AVRO_ENUM: + case AVRO_UNION: + case AVRO_FIXED: + case AVRO_SYMBOLIC: + name = n->name().fullname(); + break; + default: return false; } if (seen.find(name) != seen.end()) { return false; @@ -516,102 +451,87 @@ class AVRO_DECL NodeUnion : public NodeImplUnion return false; } - void printDefaultToJson(const GenericDatum& g, std::ostream &os, int depth) const; + void printDefaultToJson(const GenericDatum &g, std::ostream &os, size_t depth) const override; }; -class AVRO_DECL NodeFixed : public NodeImplFixed -{ - public: - - NodeFixed() : - NodeImplFixed(AVRO_FIXED) - { } +class AVRO_DECL NodeFixed : public NodeImplFixed { +public: + NodeFixed() : NodeImplFixed(AVRO_FIXED) {} - NodeFixed(const HasName &name, const HasSize &size) : - NodeImplFixed(AVRO_FIXED, name, NoLeaves(), NoLeafNames(), size) - { } + NodeFixed(const HasName &name, const HasSize &size) : NodeImplFixed(AVRO_FIXED, name, NoLeaves(), NoLeafNames(), size) {} - SchemaResolution resolve(const Node &reader) const; + SchemaResolution resolve(const Node &reader) const override; - void printJson(std::ostream &os, int depth) const; + void printJson(std::ostream &os, size_t depth) const override; - bool isValid() const { + bool isValid() const override { return ( - (nameAttribute_.size() == 1) && - (sizeAttribute_.size() == 1) - ); + (nameAttribute_.size() == 1) && (sizeAttribute_.size() == 1)); } - void printDefaultToJson(const GenericDatum& g, std::ostream &os, int depth) const; + void printDefaultToJson(const GenericDatum &g, std::ostream &os, size_t depth) const override; }; -template < class A, class B, class C, class D > +template<class A, class B, class C, class D> inline void -NodeImpl<A,B,C,D>::setLeafToSymbolic(int index, const NodePtr &node) -{ - if(!B::hasAttribute) { +NodeImpl<A, B, C, D>::setLeafToSymbolic(size_t index, const NodePtr &node) { + if (!B::hasAttribute) { throw Exception("Cannot change leaf node for nonexistent leaf"); } - NodePtr &replaceNode = const_cast<NodePtr &>(leafAttributes_.get(index)); - if(replaceNode->name() != node->name()) { + auto &replaceNode = const_cast<NodePtr &>(leafAttributes_.get(index)); + if (replaceNode->name() != node->name()) { throw Exception("Symbolic name does not match the name of the schema it references"); } - NodePtr symbol(new NodeSymbolic); - NodeSymbolic *ptr = static_cast<NodeSymbolic *> (symbol.get()); - - ptr->setName(node->name()); - ptr->setNode(node); - replaceNode.swap(symbol); + auto symbol = std::make_shared<NodeSymbolic>(); + symbol->setName(node->name()); + symbol->setNode(node); + replaceNode = symbol; } -template < class A, class B, class C, class D > +template<class A, class B, class C, class D> inline void -NodeImpl<A,B,C,D>::printBasicInfo(std::ostream &os) const -{ +NodeImpl<A, B, C, D>::printBasicInfo(std::ostream &os) const { os << type(); - if(hasName()) { + if (hasName()) { os << ' ' << nameAttribute_.get(); } - if(D::hasAttribute) { + if (D::hasAttribute) { os << " " << sizeAttribute_.get(); } os << '\n'; int count = leaves(); count = count ? count : names(); - for(int i= 0; i < count; ++i) { - if( C::hasAttribute ) { + for (int i = 0; i < count; ++i) { + if (C::hasAttribute) { os << "name " << nameAt(i) << '\n'; } - if( type() != AVRO_SYMBOLIC && leafAttributes_.hasAttribute) { + if (type() != AVRO_SYMBOLIC && leafAttributes_.hasAttribute) { leafAt(i)->printBasicInfo(os); } } - if(isCompound(type())) { + if (isCompound(type())) { os << "end " << type() << '\n'; } } - -inline NodePtr resolveSymbol(const NodePtr &node) -{ - if(node->type() != AVRO_SYMBOLIC) { +inline NodePtr resolveSymbol(const NodePtr &node) { + if (node->type() != AVRO_SYMBOLIC) { throw Exception("Only symbolic nodes may be resolved"); } std::shared_ptr<NodeSymbolic> symNode = std::static_pointer_cast<NodeSymbolic>(node); return symNode->getNode(); } -template< typename T > -inline std::string intToHex(T i) -{ - std::stringstream stream; - stream << "\\u" - << std::setfill('0') << std::setw(sizeof(T)) - << std::hex << i; - return stream.str(); +template<typename T> +inline std::string intToHex(T i) { + std::stringstream stream; + stream << "\\u" + << std::setfill('0') << std::setw(sizeof(T)) + << std::hex << i; + return stream.str(); } } // namespace avro diff --git a/contrib/libs/apache/avro/api/Reader.hh b/contrib/libs/apache/avro/api/Reader.hh index 4f514fbbe93..ca6a719e31c 100644 --- a/contrib/libs/apache/avro/api/Reader.hh +++ b/contrib/libs/apache/avro/api/Reader.hh @@ -19,15 +19,15 @@ #ifndef avro_Reader_hh__ #define avro_Reader_hh__ -#include <stdint.h> -#include <vector> #include <array> #include <boost/noncopyable.hpp> +#include <cstdint> +#include <vector> #include "Config.hh" -#include "Zigzag.hh" #include "Types.hh" #include "Validator.hh" +#include "Zigzag.hh" #include "buffer/BufferReader.hh" namespace avro { @@ -38,19 +38,13 @@ namespace avro { /// template<class ValidatorType> -class ReaderImpl : private boost::noncopyable -{ +class ReaderImpl : private boost::noncopyable { - public: +public: + explicit ReaderImpl(const InputBuffer &buffer) : reader_(buffer) {} - explicit ReaderImpl(const InputBuffer &buffer) : - reader_(buffer) - {} - - ReaderImpl(const ValidSchema &schema, const InputBuffer &buffer) : - validator_(schema), - reader_(buffer) - {} + ReaderImpl(const ValidSchema &schema, const InputBuffer &buffer) : validator_(schema), + reader_(buffer) {} void readValue(Null &) { validator_.checkTypeExpected(AVRO_NULL); @@ -58,14 +52,14 @@ class ReaderImpl : private boost::noncopyable void readValue(bool &val) { validator_.checkTypeExpected(AVRO_BOOL); - uint8_t ival = 0; - reader_.read(ival); - val = (ival != 0); + uint8_t intVal = 0; + reader_.read(intVal); + val = (intVal != 0); } void readValue(int32_t &val) { validator_.checkTypeExpected(AVRO_INT); - uint32_t encoded = static_cast<uint32_t>(readVarInt()); + auto encoded = static_cast<uint32_t>(readVarInt()); val = decodeZigzag32(encoded); } @@ -97,13 +91,13 @@ class ReaderImpl : private boost::noncopyable void readValue(std::string &val) { validator_.checkTypeExpected(AVRO_STRING); - size_t size = static_cast<size_t>(readSize()); + auto size = static_cast<size_t>(readSize()); reader_.read(val, size); } void readBytes(std::vector<uint8_t> &val) { validator_.checkTypeExpected(AVRO_BYTES); - size_t size = static_cast<size_t>(readSize()); + auto size = static_cast<size_t>(readSize()); val.resize(size); reader_.read(reinterpret_cast<char *>(val.data()), size); } @@ -113,12 +107,12 @@ class ReaderImpl : private boost::noncopyable reader_.read(reinterpret_cast<char *>(val), size); } - template <size_t N> + template<size_t N> void readFixed(uint8_t (&val)[N]) { this->readFixed(val, N); } - template <size_t N> + template<size_t N> void readFixed(std::array<uint8_t, N> &val) { this->readFixed(val.data(), N); } @@ -167,16 +161,15 @@ class ReaderImpl : private boost::noncopyable return validator_.getNextFieldName(name); } - private: - +private: uint64_t readVarInt() { uint64_t encoded = 0; uint8_t val = 0; int shift = 0; do { reader_.read(val); - uint64_t newbits = static_cast<uint64_t>(val & 0x7f) << shift; - encoded |= newbits; + uint64_t newBits = static_cast<uint64_t>(val & 0x7f) << shift; + encoded |= newBits; shift += 7; } while (val & 0x80); @@ -197,12 +190,11 @@ class ReaderImpl : private boost::noncopyable } ValidatorType validator_; - BufferReader reader_; - + BufferReader reader_; }; -typedef ReaderImpl<NullValidator> Reader; -typedef ReaderImpl<Validator> ValidatingReader; +using Reader = ReaderImpl<NullValidator>; +using ValidatingReader = ReaderImpl<Validator>; } // namespace avro diff --git a/contrib/libs/apache/avro/api/Resolver.hh b/contrib/libs/apache/avro/api/Resolver.hh index a0ffcbeac77..06c33e76c68 100644 --- a/contrib/libs/apache/avro/api/Resolver.hh +++ b/contrib/libs/apache/avro/api/Resolver.hh @@ -19,9 +19,9 @@ #ifndef avro_Resolver_hh__ #define avro_Resolver_hh__ -#include <memory> #include <boost/noncopyable.hpp> -#include <stdint.h> +#include <cstdint> +#include <memory> #include "Config.hh" #include "Reader.hh" @@ -34,23 +34,16 @@ namespace avro { class ValidSchema; class Layout; -class AVRO_DECL Resolver : private boost::noncopyable -{ - - public: - +class AVRO_DECL Resolver : private boost::noncopyable { +public: virtual void parse(Reader &reader, uint8_t *address) const = 0; - virtual ~Resolver() {} - + virtual ~Resolver() = default; }; std::unique_ptr<Resolver> constructResolver( - const ValidSchema &rwriterSchema, - const ValidSchema &readerSchema, - const Layout &readerLayout - ); - - + const ValidSchema &writerSchema, + const ValidSchema &readerSchema, + const Layout &readerLayout); } // namespace avro diff --git a/contrib/libs/apache/avro/api/ResolverSchema.hh b/contrib/libs/apache/avro/api/ResolverSchema.hh index 9048a22b9a2..d641d08f8c9 100644 --- a/contrib/libs/apache/avro/api/ResolverSchema.hh +++ b/contrib/libs/apache/avro/api/ResolverSchema.hh @@ -20,7 +20,7 @@ #define avro_ResolverSchema_hh__ #include <boost/noncopyable.hpp> -#include <stdint.h> +#include <cstdint> #include <memory> #include "Config.hh" @@ -36,19 +36,13 @@ class Layout; class Resolver; class AVRO_DECL ResolverSchema { - - public: - +public: ResolverSchema(const ValidSchema &writer, const ValidSchema &reader, const Layout &readerLayout); - private: - +private: friend class ResolvingReader; - void parse(Reader &reader, uint8_t *address); - std::shared_ptr<Resolver> resolver_; - }; } // namespace avro diff --git a/contrib/libs/apache/avro/api/Schema.hh b/contrib/libs/apache/avro/api/Schema.hh index b8ad92c8256..abd646f9fc7 100644 --- a/contrib/libs/apache/avro/api/Schema.hh +++ b/contrib/libs/apache/avro/api/Schema.hh @@ -31,13 +31,11 @@ namespace avro { - /// The root Schema object is a base class. Nobody constructs this class directly. class AVRO_DECL Schema { public: - - virtual ~Schema(); + virtual ~Schema() = default; Type type() const { return node_->type(); @@ -51,57 +49,56 @@ public: return node_; } - protected: - Schema(); - explicit Schema(const NodePtr &node); - explicit Schema(Node *node); +protected: + explicit Schema(NodePtr node) : node_(std::move(node)) {} + explicit Schema(Node *node) : node_(node) {} NodePtr node_; }; class AVRO_DECL NullSchema : public Schema { public: - NullSchema(): Schema(new NodePrimitive(AVRO_NULL)) {} + NullSchema() : Schema(new NodePrimitive(AVRO_NULL)) {} }; class AVRO_DECL BoolSchema : public Schema { public: - BoolSchema(): Schema(new NodePrimitive(AVRO_BOOL)) {} + BoolSchema() : Schema(new NodePrimitive(AVRO_BOOL)) {} }; class AVRO_DECL IntSchema : public Schema { public: - IntSchema(): Schema(new NodePrimitive(AVRO_INT)) {} + IntSchema() : Schema(new NodePrimitive(AVRO_INT)) {} }; class AVRO_DECL LongSchema : public Schema { public: - LongSchema(): Schema(new NodePrimitive(AVRO_LONG)) {} + LongSchema() : Schema(new NodePrimitive(AVRO_LONG)) {} }; class AVRO_DECL FloatSchema : public Schema { public: - FloatSchema(): Schema(new NodePrimitive(AVRO_FLOAT)) {} + FloatSchema() : Schema(new NodePrimitive(AVRO_FLOAT)) {} }; class AVRO_DECL DoubleSchema : public Schema { public: - DoubleSchema(): Schema(new NodePrimitive(AVRO_DOUBLE)) {} + DoubleSchema() : Schema(new NodePrimitive(AVRO_DOUBLE)) {} }; class AVRO_DECL StringSchema : public Schema { public: - StringSchema(): Schema(new NodePrimitive(AVRO_STRING)) {} + StringSchema() : Schema(new NodePrimitive(AVRO_STRING)) {} }; class AVRO_DECL BytesSchema : public Schema { public: - BytesSchema(): Schema(new NodePrimitive(AVRO_BYTES)) {} + BytesSchema() : Schema(new NodePrimitive(AVRO_BYTES)) {} }; class AVRO_DECL RecordSchema : public Schema { public: - RecordSchema(const std::string &name); + explicit RecordSchema(const std::string &name); void addField(const std::string &name, const Schema &fieldSchema); std::string getDoc() const; @@ -110,19 +107,19 @@ public: class AVRO_DECL EnumSchema : public Schema { public: - EnumSchema(const std::string &name); + explicit EnumSchema(const std::string &name); void addSymbol(const std::string &symbol); }; class AVRO_DECL ArraySchema : public Schema { public: - ArraySchema(const Schema &itemsSchema); + explicit ArraySchema(const Schema &itemsSchema); ArraySchema(const ArraySchema &itemsSchema); }; class AVRO_DECL MapSchema : public Schema { public: - MapSchema(const Schema &valuesSchema); + explicit MapSchema(const Schema &valuesSchema); MapSchema(const MapSchema &itemsSchema); }; @@ -139,7 +136,7 @@ public: class AVRO_DECL SymbolicSchema : public Schema { public: - SymbolicSchema(const Name& name, const NodePtr& link); + SymbolicSchema(const Name &name, const NodePtr &link); }; } // namespace avro diff --git a/contrib/libs/apache/avro/api/SchemaResolution.hh b/contrib/libs/apache/avro/api/SchemaResolution.hh index 765347d9ded..905d1b49cf0 100644 --- a/contrib/libs/apache/avro/api/SchemaResolution.hh +++ b/contrib/libs/apache/avro/api/SchemaResolution.hh @@ -23,7 +23,6 @@ namespace avro { - enum SchemaResolution { /// The schemas definitely do not match diff --git a/contrib/libs/apache/avro/api/Specific.hh b/contrib/libs/apache/avro/api/Specific.hh index 53741be4808..247d86da720 100644 --- a/contrib/libs/apache/avro/api/Specific.hh +++ b/contrib/libs/apache/avro/api/Specific.hh @@ -19,18 +19,18 @@ #ifndef avro_Codec_hh__ #define avro_Codec_hh__ +#include "array" +#include <algorithm> +#include <map> #include <string> #include <vector> -#include <map> -#include <algorithm> -#include "array" #include "boost/blank.hpp" #include "AvroTraits.hh" #include "Config.hh" -#include "Encoder.hh" #include "Decoder.hh" +#include "Encoder.hh" /** * A bunch of templates and specializations for encoding and decoding @@ -39,7 +39,7 @@ * Primitive AVRO types BOOLEAN, INT, LONG, FLOAT, DOUBLE, STRING and BYTES * get decoded to and encoded from C++ types bool, int32_t, int64_t, float, * double, std::string and std::vector<uint8_t> respectively. In addition, - * std::vector<T> for aribtrary type T gets encoded as an Avro array of T. + * std::vector<T> for arbitrary type T gets encoded as an Avro array of T. * Similarly, std::map<std::string, T> for arbitrary type T gets encoded * as an Avro map with value type T. * @@ -50,8 +50,10 @@ namespace avro { typedef boost::blank null; -template <typename T> void encode(Encoder& e, const T& t); -template <typename T> void decode(Decoder& d, T& t); +template<typename T> +void encode(Encoder &e, const T &t); +template<typename T> +void decode(Decoder &d, T &t); /** * Codec_traits tells avro how to encode and decode an object of given type. @@ -61,24 +63,25 @@ template <typename T> void decode(Decoder& d, T& t); * \li static void decode(Decoder& e, T& value); * The default is empty. */ -template <typename T> +template<typename T> struct codec_traits; /** * codec_traits for Avro boolean. */ -template <> struct codec_traits<bool> { +template<> +struct codec_traits<bool> { /** * Encodes a given value. */ - static void encode(Encoder& e, bool b) { + static void encode(Encoder &e, bool b) { e.encodeBool(b); } /** * Decodes into a given value. */ - static void decode(Decoder& d, bool& b) { + static void decode(Decoder &d, bool &b) { b = d.decodeBool(); } }; @@ -86,18 +89,19 @@ template <> struct codec_traits<bool> { /** * codec_traits for Avro int. */ -template <> struct codec_traits<int32_t> { +template<> +struct codec_traits<int32_t> { /** * Encodes a given value. */ - static void encode(Encoder& e, int32_t i) { + static void encode(Encoder &e, int32_t i) { e.encodeInt(i); } /** * Decodes into a given value. */ - static void decode(Decoder& d, int32_t& i) { + static void decode(Decoder &d, int32_t &i) { i = d.decodeInt(); } }; @@ -105,18 +109,19 @@ template <> struct codec_traits<int32_t> { /** * codec_traits for Avro long. */ -template <> struct codec_traits<int64_t> { +template<> +struct codec_traits<int64_t> { /** * Encodes a given value. */ - static void encode(Encoder& e, int64_t l) { + static void encode(Encoder &e, int64_t l) { e.encodeLong(l); } /** * Decodes into a given value. */ - static void decode(Decoder& d, int64_t& l) { + static void decode(Decoder &d, int64_t &l) { l = d.decodeLong(); } }; @@ -124,18 +129,19 @@ template <> struct codec_traits<int64_t> { /** * codec_traits for Avro float. */ -template <> struct codec_traits<float> { +template<> +struct codec_traits<float> { /** * Encodes a given value. */ - static void encode(Encoder& e, float f) { + static void encode(Encoder &e, float f) { e.encodeFloat(f); } /** * Decodes into a given value. */ - static void decode(Decoder& d, float& f) { + static void decode(Decoder &d, float &f) { f = d.decodeFloat(); } }; @@ -143,18 +149,19 @@ template <> struct codec_traits<float> { /** * codec_traits for Avro double. */ -template <> struct codec_traits<double> { +template<> +struct codec_traits<double> { /** * Encodes a given value. */ - static void encode(Encoder& e, double d) { + static void encode(Encoder &e, double d) { e.encodeDouble(d); } /** * Decodes into a given value. */ - static void decode(Decoder& d, double& dbl) { + static void decode(Decoder &d, double &dbl) { dbl = d.decodeDouble(); } }; @@ -162,18 +169,19 @@ template <> struct codec_traits<double> { /** * codec_traits for Avro string. */ -template <> struct codec_traits<std::string> { +template<> +struct codec_traits<std::string> { /** * Encodes a given value. */ - static void encode(Encoder& e, const std::string& s) { + static void encode(Encoder &e, const std::string &s) { e.encodeString(s); } /** * Decodes into a given value. */ - static void decode(Decoder& d, std::string& s) { + static void decode(Decoder &d, std::string &s) { s = d.decodeString(); } }; @@ -181,18 +189,19 @@ template <> struct codec_traits<std::string> { /** * codec_traits for Avro bytes. */ -template <> struct codec_traits<std::vector<uint8_t> > { +template<> +struct codec_traits<std::vector<uint8_t>> { /** * Encodes a given value. */ - static void encode(Encoder& e, const std::vector<uint8_t>& b) { + static void encode(Encoder &e, const std::vector<uint8_t> &b) { e.encodeBytes(b); } /** * Decodes into a given value. */ - static void decode(Decoder& d, std::vector<uint8_t>& s) { + static void decode(Decoder &d, std::vector<uint8_t> &s) { d.decodeBytes(s); } }; @@ -200,18 +209,19 @@ template <> struct codec_traits<std::vector<uint8_t> > { /** * codec_traits for Avro fixed. */ -template <size_t N> struct codec_traits<std::array<uint8_t, N> > { +template<size_t N> +struct codec_traits<std::array<uint8_t, N>> { /** * Encodes a given value. */ - static void encode(Encoder& e, const std::array<uint8_t, N>& b) { + static void encode(Encoder &e, const std::array<uint8_t, N> &b) { e.encodeFixed(b.data(), N); } /** * Decodes into a given value. */ - static void decode(Decoder& d, std::array<uint8_t, N>& s) { + static void decode(Decoder &d, std::array<uint8_t, N> &s) { std::vector<uint8_t> v(N); d.decodeFixed(N, v); std::copy(v.data(), v.data() + N, s.data()); @@ -221,16 +231,17 @@ template <size_t N> struct codec_traits<std::array<uint8_t, N> > { /** * codec_traits for Avro arrays. */ -template <typename T> struct codec_traits<std::vector<T> > { +template<typename T> +struct codec_traits<std::vector<T>> { /** * Encodes a given value. */ - static void encode(Encoder& e, const std::vector<T>& b) { + static void encode(Encoder &e, const std::vector<T> &b) { e.arrayStart(); - if (! b.empty()) { + if (!b.empty()) { e.setItemCount(b.size()); for (typename std::vector<T>::const_iterator it = b.begin(); - it != b.end(); ++it) { + it != b.end(); ++it) { e.startItem(); avro::encode(e, *it); } @@ -241,7 +252,7 @@ template <typename T> struct codec_traits<std::vector<T> > { /** * Decodes into a given value. */ - static void decode(Decoder& d, std::vector<T>& s) { + static void decode(Decoder &d, std::vector<T> &s) { s.clear(); for (size_t n = d.arrayStart(); n != 0; n = d.arrayNext()) { for (size_t i = 0; i < n; ++i) { @@ -255,12 +266,13 @@ template <typename T> struct codec_traits<std::vector<T> > { typedef codec_traits<std::vector<bool>::const_reference> bool_codec_traits; -template <> struct codec_traits<std::conditional<avro::is_not_defined<bool_codec_traits>::value, - std::vector<bool>::const_reference, void>::type> { - /** - * Encodes a given value. - */ - static void encode(Encoder& e, std::vector<bool>::const_reference b) { +template<> +struct codec_traits<std::conditional<avro::is_not_defined<bool_codec_traits>::value, + std::vector<bool>::const_reference, void>::type> { + /** + * Encodes a given value. + */ + static void encode(Encoder &e, std::vector<bool>::const_reference b) { e.encodeBool(b); } }; @@ -268,17 +280,18 @@ template <> struct codec_traits<std::conditional<avro::is_not_defined<bool_codec /** * codec_traits for Avro maps. */ -template <typename T> struct codec_traits<std::map<std::string, T> > { +template<typename T> +struct codec_traits<std::map<std::string, T>> { /** * Encodes a given value. */ - static void encode(Encoder& e, const std::map<std::string, T>& b) { + static void encode(Encoder &e, const std::map<std::string, T> &b) { e.mapStart(); - if (! b.empty()) { + if (!b.empty()) { e.setItemCount(b.size()); for (typename std::map<std::string, T>::const_iterator - it = b.begin(); - it != b.end(); ++it) { + it = b.begin(); + it != b.end(); ++it) { e.startItem(); avro::encode(e, it->first); avro::encode(e, it->second); @@ -290,13 +303,13 @@ template <typename T> struct codec_traits<std::map<std::string, T> > { /** * Decodes into a given value. */ - static void decode(Decoder& d, std::map<std::string, T>& s) { + static void decode(Decoder &d, std::map<std::string, T> &s) { s.clear(); for (size_t n = d.mapStart(); n != 0; n = d.mapNext()) { for (size_t i = 0; i < n; ++i) { std::string k; avro::decode(d, k); - T& t = s[std::move(k)]; + T &t = s[std::move(k)]; avro::decode(d, t); } } @@ -306,43 +319,39 @@ template <typename T> struct codec_traits<std::map<std::string, T> > { /** * codec_traits for Avro null. */ -template <> struct codec_traits<avro::null> { - /** - * Encodes a given value. - */ - static void encode(Encoder& e, const avro::null&) { - e.encodeNull(); - } - - /** - * Decodes into a given value. - */ - static void decode(Decoder& d, avro::null&) { - d.decodeNull(); - } -}; - +template<> +struct codec_traits<avro::null> { + /** + * Encodes a given value. + */ + static void encode(Encoder &e, const avro::null &) { + e.encodeNull(); + } + /** + * Decodes into a given value. + */ + static void decode(Decoder &d, avro::null &) { + d.decodeNull(); + } +}; /** * Generic encoder function that makes use of the codec_traits. */ -template <typename T> -void encode(Encoder& e, const T& t) { +template<typename T> +void encode(Encoder &e, const T &t) { codec_traits<T>::encode(e, t); } /** * Generic decoder function that makes use of the codec_traits. */ -template <typename T> -void decode(Decoder& d, T& t) { +template<typename T> +void decode(Decoder &d, T &t) { codec_traits<T>::decode(d, t); } -} // namespace avro +} // namespace avro #endif // avro_Codec_hh__ - - - diff --git a/contrib/libs/apache/avro/api/Stream.hh b/contrib/libs/apache/avro/api/Stream.hh index 508cb039807..fe2c97ee2dd 100644 --- a/contrib/libs/apache/avro/api/Stream.hh +++ b/contrib/libs/apache/avro/api/Stream.hh @@ -19,9 +19,9 @@ #ifndef avro_Stream_hh__ #define avro_Stream_hh__ +#include <cstdint> +#include <cstring> #include <memory> -#include <string.h> -#include <stdint.h> #include "boost/utility.hpp" @@ -35,17 +35,16 @@ namespace avro { */ class AVRO_DECL InputStream : boost::noncopyable { protected: - /** - * An empty constuctor. + * An empty constructor. */ - InputStream() { } + InputStream() = default; public: /** * Destructor. */ - virtual ~InputStream() { } + virtual ~InputStream() = default; /** * Returns some of available data. @@ -53,7 +52,7 @@ public: * Returns true if some data is available, false if no more data is * available or an error has occurred. */ - virtual bool next(const uint8_t** data, size_t* len) = 0; + virtual bool next(const uint8_t **data, size_t *len) = 0; /** * "Returns" back some of the data to the stream. The returned @@ -70,7 +69,7 @@ public: /** * Returns the number of bytes read from this stream so far. * All the bytes made available through next are considered - * to be used unless, retutned back using backup. + * to be used unless, returned back using backup. */ virtual size_t byteCount() const = 0; }; @@ -82,17 +81,16 @@ typedef std::unique_ptr<InputStream> InputStreamPtr; */ class AVRO_DECL SeekableInputStream : public InputStream { protected: - /** - * An empty constuctor. + * An empty constructor. */ - SeekableInputStream() { } + SeekableInputStream() = default; public: /** * Destructor. */ - virtual ~SeekableInputStream() { } + ~SeekableInputStream() override = default; /** * Seek to a specific position in the stream. This may invalidate pointers @@ -109,24 +107,23 @@ typedef std::unique_ptr<SeekableInputStream> SeekableInputStreamPtr; */ class AVRO_DECL OutputStream : boost::noncopyable { protected: - /** - * An empty constuctor. + * An empty constructor. */ - OutputStream() { } -public: + OutputStream() = default; +public: /** * Destructor. */ - virtual ~OutputStream() { } + virtual ~OutputStream() = default; /** * Returns a buffer that can be written into. * On successful return, data has the pointer to the buffer * and len has the number of bytes available at data. */ - virtual bool next(uint8_t** data, size_t* len) = 0; + virtual bool next(uint8_t **data, size_t *len) = 0; /** * "Returns" back to the stream some of the buffer obtained @@ -137,7 +134,7 @@ public: /** * Number of bytes written so far into this stream. The whole buffer * returned by next() is assumed to be written unless some of - * it was retutned using backup(). + * it was returned using backup(). */ virtual uint64_t byteCount() const = 0; @@ -160,23 +157,23 @@ AVRO_DECL OutputStreamPtr memoryOutputStream(size_t chunkSize = 4 * 1024); * It does not copy the data, the byte array should remain valid * until the InputStream is used. */ -AVRO_DECL InputStreamPtr memoryInputStream(const uint8_t* data, size_t len); +AVRO_DECL InputStreamPtr memoryInputStream(const uint8_t *data, size_t len); /** * Returns a new InputStream with the contents written into an - * outputstream. The output stream must have been returned by + * OutputStream. The output stream must have been returned by * an earlier call to memoryOutputStream(). The contents for the new - * input stream are the snapshot of the outputstream. One can construct + * InputStream are the snapshot of the output stream. One can construct * any number of memory input stream from a single memory output stream. */ -AVRO_DECL InputStreamPtr memoryInputStream(const OutputStream& source); +AVRO_DECL InputStreamPtr memoryInputStream(const OutputStream &source); /** * Returns the contents written so far into the output stream, which should - * be a memory output stream. That is it must have been returned by a pervious + * be a memory output stream. That is it must have been returned by a previous * call to memoryOutputStream(). */ -AVRO_DECL std::shared_ptr<std::vector<uint8_t> > snapshot(const OutputStream& source); +AVRO_DECL std::shared_ptr<std::vector<uint8_t>> snapshot(const OutputStream &source); /** * Returns a new OutputStream whose contents would be stored in a file. @@ -185,8 +182,8 @@ AVRO_DECL std::shared_ptr<std::vector<uint8_t> > snapshot(const OutputStream& so * If there is a file with the given name, it is truncated and overwritten. * If there is no file with the given name, it is created. */ -AVRO_DECL OutputStreamPtr fileOutputStream(const char* filename, - size_t bufferSize = 8 * 1024); +AVRO_DECL OutputStreamPtr fileOutputStream(const char *filename, + size_t bufferSize = 8 * 1024); /** * Returns a new InputStream whose contents come from the given file. @@ -202,8 +199,8 @@ AVRO_DECL SeekableInputStreamPtr fileSeekableInputStream( * std::ostream. The std::ostream object should outlive the returned * OutputStream. */ -AVRO_DECL OutputStreamPtr ostreamOutputStream(std::ostream& os, - size_t bufferSize = 8 * 1024); +AVRO_DECL OutputStreamPtr ostreamOutputStream(std::ostream &os, + size_t bufferSize = 8 * 1024); /** * Returns a new InputStream whose contents come from the given @@ -224,46 +221,45 @@ AVRO_DECL InputStreamPtr istreamInputStream( * InputStream. */ AVRO_DECL InputStreamPtr nonSeekableIstreamInputStream( - std::istream& is, size_t bufferSize = 8 * 1024); - + std::istream &is, size_t bufferSize = 8 * 1024); /** A convenience class for reading from an InputStream */ struct StreamReader { /** * The underlying input stream. */ - InputStream* in_; + InputStream *in_; /** * The next location to read from. */ - const uint8_t* next_; + const uint8_t *next_; /** * One past the last valid location. */ - const uint8_t* end_; + const uint8_t *end_; /** * Constructs an empty reader. */ - StreamReader() : in_(0), next_(0), end_(0) { } + StreamReader() : in_(nullptr), next_(nullptr), end_(nullptr) {} /** * Constructs a reader with the given underlying stream. */ - StreamReader(InputStream& in) : in_(0), next_(0), end_(0) { reset(in); } + explicit StreamReader(InputStream &in) : in_(nullptr), next_(nullptr), end_(nullptr) { reset(in); } /** * Replaces the current input stream with the given one after backing up * the original one if required. */ - void reset(InputStream& is) { - if (in_ != 0 && end_ != next_) { + void reset(InputStream &is) { + if (in_ != nullptr && end_ != next_) { in_->backup(end_ - next_); } in_ = &is; - next_ = end_ = 0; + next_ = end_ = nullptr; } /** @@ -281,7 +277,7 @@ struct StreamReader { * Reads the given number of bytes from the underlying stream. * If there are not that many bytes, throws an exception. */ - void readBytes(uint8_t* b, size_t n) { + void readBytes(uint8_t *b, size_t n) { while (n > 0) { if (next_ == end_) { more(); @@ -332,7 +328,7 @@ struct StreamReader { * Tries to get more data and if it cannot, throws an exception. */ void more() { - if (! fill()) { + if (!fill()) { throw Exception("EOF reached"); } } @@ -341,7 +337,7 @@ struct StreamReader { * Returns true if and only if the end of stream is not reached. */ bool hasMore() { - return (next_ == end_) ? fill() : true; + return next_ != end_ || fill(); } /** @@ -358,40 +354,40 @@ struct StreamReader { }; /** - * A convinience class to write data into an OutputStream. + * A convenience class to write data into an OutputStream. */ struct StreamWriter { /** * The underlying output stream for this writer. */ - OutputStream* out_; + OutputStream *out_; /** * The next location to write to. */ - uint8_t* next_; + uint8_t *next_; /** * One past the last location one can write to. */ - uint8_t* end_; + uint8_t *end_; /** * Constructs a writer with no underlying stream. */ - StreamWriter() : out_(0), next_(0), end_(0) { } + StreamWriter() : out_(nullptr), next_(nullptr), end_(nullptr) {} /** * Constructs a new writer with the given underlying stream. */ - StreamWriter(OutputStream& out) : out_(0), next_(0), end_(0) { reset(out); } + explicit StreamWriter(OutputStream &out) : out_(nullptr), next_(nullptr), end_(nullptr) { reset(out); } /** * Replaces the current underlying stream with a new one. * If required, it backs up unused bytes in the previous stream. */ - void reset(OutputStream& os) { - if (out_ != 0 && end_ != next_) { + void reset(OutputStream &os) { + if (out_ != nullptr && end_ != next_) { out_->backup(end_ - next_); } out_ = &os; @@ -411,7 +407,7 @@ struct StreamWriter { /** * Writes the specified number of bytes starting at \p b. */ - void writeBytes(const uint8_t* b, size_t n) { + void writeBytes(const uint8_t *b, size_t n) { while (n > 0) { if (next_ == end_) { more(); @@ -466,9 +462,8 @@ struct StreamWriter { * A convenience function to copy all the contents of an input stream into * an output stream. */ -inline void copy(InputStream& in, OutputStream& out) -{ - const uint8_t *p = 0; +inline void copy(InputStream &in, OutputStream &out) { + const uint8_t *p = nullptr; size_t n = 0; StreamWriter w(out); while (in.next(&p, &n)) { @@ -477,7 +472,5 @@ inline void copy(InputStream& in, OutputStream& out) w.flush(); } -} // namespace avro +} // namespace avro #endif - - diff --git a/contrib/libs/apache/avro/api/Types.hh b/contrib/libs/apache/avro/api/Types.hh index f42399e96b5..e3296ae0d00 100644 --- a/contrib/libs/apache/avro/api/Types.hh +++ b/contrib/libs/apache/avro/api/Types.hh @@ -30,29 +30,28 @@ namespace avro { */ enum Type { - AVRO_STRING, /*!< String */ - AVRO_BYTES, /*!< Sequence of variable length bytes data */ - AVRO_INT, /*!< 32-bit integer */ - AVRO_LONG, /*!< 64-bit integer */ - AVRO_FLOAT, /*!< Floating point number */ - AVRO_DOUBLE, /*!< Double precision floating point number */ - AVRO_BOOL, /*!< Boolean value */ - AVRO_NULL, /*!< Null */ - - AVRO_RECORD, /*!< Record, a sequence of fields */ - AVRO_ENUM, /*!< Enumeration */ - AVRO_ARRAY, /*!< Homogeneous array of some specific type */ - AVRO_MAP, /*!< Homogeneous map from string to some specific type */ - AVRO_UNION, /*!< Union of one or more types */ - AVRO_FIXED, /*!< Fixed number of bytes */ + AVRO_STRING, /*!< String */ + AVRO_BYTES, /*!< Sequence of variable length bytes data */ + AVRO_INT, /*!< 32-bit integer */ + AVRO_LONG, /*!< 64-bit integer */ + AVRO_FLOAT, /*!< Floating point number */ + AVRO_DOUBLE, /*!< Double precision floating point number */ + AVRO_BOOL, /*!< Boolean value */ + AVRO_NULL, /*!< Null */ + + AVRO_RECORD, /*!< Record, a sequence of fields */ + AVRO_ENUM, /*!< Enumeration */ + AVRO_ARRAY, /*!< Homogeneous array of some specific type */ + AVRO_MAP, /*!< Homogeneous map from string to some specific type */ + AVRO_UNION, /*!< Union of one or more types */ + AVRO_FIXED, /*!< Fixed number of bytes */ AVRO_NUM_TYPES, /*!< Marker */ // The following is a pseudo-type used in implementation AVRO_SYMBOLIC = AVRO_NUM_TYPES, /*!< User internally to avoid circular references. */ - AVRO_UNKNOWN = -1 /*!< Used internally. */ - + AVRO_UNKNOWN = -1 /*!< Used internally. */ }; /** @@ -60,7 +59,7 @@ enum Type { * Primitive types are: string, bytes, int, long, float, double, boolean * and null */ -inline bool isPrimitive(Type t) { +inline constexpr bool isPrimitive(Type t) noexcept { return (t >= AVRO_STRING) && (t < AVRO_RECORD); } @@ -69,14 +68,14 @@ inline bool isPrimitive(Type t) { * Primitive types are: string, bytes, int, long, float, double, boolean * and null */ -inline bool isCompound(Type t) { - return (t>= AVRO_RECORD) && (t < AVRO_NUM_TYPES); +inline constexpr bool isCompound(Type t) noexcept { + return (t >= AVRO_RECORD) && (t < AVRO_NUM_TYPES); } /** * Returns true if and only if the given type is a valid avro type. */ -inline bool isAvroType(Type t) { +inline constexpr bool isAvroType(Type t) noexcept { return (t >= AVRO_STRING) && (t < AVRO_NUM_TYPES); } @@ -84,31 +83,30 @@ inline bool isAvroType(Type t) { * Returns true if and only if the given type is within the valid range * of enumeration. */ -inline bool isAvroTypeOrPseudoType(Type t) { +inline constexpr bool isAvroTypeOrPseudoType(Type t) noexcept { return (t >= AVRO_STRING) && (t <= AVRO_NUM_TYPES); } /** * Converts the given type into a string. Useful for generating messages. */ -AVRO_DECL const std::string& toString(Type type); +AVRO_DECL const std::string &toString(Type type) noexcept; /** * Writes a string form of the given type into the given ostream. */ -AVRO_DECL std::ostream &operator<< (std::ostream &os, avro::Type type); +AVRO_DECL std::ostream &operator<<(std::ostream &os, avro::Type type); -/// define a type to identify Null in template functions -struct AVRO_DECL Null { }; +/// define a type to represent Avro Null in template functions +struct AVRO_DECL Null {}; /** * Writes schema for null \p null type to \p os. * \param os The ostream to write to. * \param null The value to be written. */ -std::ostream& operator<< (std::ostream &os, const Null &null); +std::ostream &operator<<(std::ostream &os, const Null &null); } // namespace avro - #endif diff --git a/contrib/libs/apache/avro/api/ValidSchema.hh b/contrib/libs/apache/avro/api/ValidSchema.hh index 7b0ec28bed1..718c8f47333 100644 --- a/contrib/libs/apache/avro/api/ValidSchema.hh +++ b/contrib/libs/apache/avro/api/ValidSchema.hh @@ -39,7 +39,7 @@ class AVRO_DECL Schema; class AVRO_DECL ValidSchema { public: - explicit ValidSchema(const NodePtr &root); + explicit ValidSchema(NodePtr root); explicit ValidSchema(const Schema &schema); ValidSchema(); @@ -54,10 +54,10 @@ public: void toFlatList(std::ostream &os) const; - protected: +protected: NodePtr root_; - private: +private: static std::string compactSchema(const std::string &schema); }; diff --git a/contrib/libs/apache/avro/api/Validator.hh b/contrib/libs/apache/avro/api/Validator.hh index 3f542d611a4..ab5d068df0b 100644 --- a/contrib/libs/apache/avro/api/Validator.hh +++ b/contrib/libs/apache/avro/api/Validator.hh @@ -20,8 +20,9 @@ #define avro_Validating_hh__ #include <boost/noncopyable.hpp> +#include <cstdint> +#include <utility> #include <vector> -#include <stdint.h> #include "Config.hh" #include "Types.hh" @@ -29,39 +30,35 @@ namespace avro { -class AVRO_DECL NullValidator : private boost::noncopyable -{ - public: - - explicit NullValidator(const ValidSchema &) {} - NullValidator() {} +class AVRO_DECL NullValidator : private boost::noncopyable { +public: + explicit NullValidator(const ValidSchema &schema) {} + NullValidator() = default; void setCount(int64_t) {} - bool typeIsExpected(Type) const { + static bool typeIsExpected(Type) { return true; } - Type nextTypeExpected() const { + static Type nextTypeExpected() { return AVRO_UNKNOWN; } - int nextSizeExpected() const { + static int nextSizeExpected() { return 0; } - bool getCurrentRecordName(std::string &) const { + static bool getCurrentRecordName(std::string &name) { return true; } - bool getNextFieldName(std::string &) const { + static bool getNextFieldName(std::string &name) { return true; } - void checkTypeExpected(Type) { } - void checkFixedSizeExpected(int) { } - - + void checkTypeExpected(Type) {} + void checkFixedSizeExpected(int) {} }; /// This class is used by both the ValidatingSerializer and ValidationParser @@ -70,11 +67,9 @@ class AVRO_DECL NullValidator : private boost::noncopyable /// through all leaf nodes but a union only skips to one), and reports which /// type is next. -class AVRO_DECL Validator : private boost::noncopyable -{ - public: - - explicit Validator(const ValidSchema &schema); +class AVRO_DECL Validator : private boost::noncopyable { +public: + explicit Validator(ValidSchema schema); void setCount(int64_t val); @@ -92,30 +87,27 @@ class AVRO_DECL Validator : private boost::noncopyable bool getNextFieldName(std::string &name) const; void checkTypeExpected(Type type) { - if(! typeIsExpected(type)) { + if (!typeIsExpected(type)) { throw Exception( boost::format("Type %1% does not match schema %2%") - % type % nextType_ - ); + % type % nextType_); } advance(); } void checkFixedSizeExpected(int size) { - if( nextSizeExpected() != size) { + if (nextSizeExpected() != size) { throw Exception( boost::format("Wrong size for fixed, got %1%, expected %2%") - % size % nextSizeExpected() - ); + % size % nextSizeExpected()); } checkTypeExpected(AVRO_FIXED); } - private: - - typedef uint32_t flag_t; +private: + using flag_t = uint32_t; - flag_t typeToFlag(Type type) const { + static flag_t typeToFlag(Type type) { flag_t flag = (1L << type); return flag; } @@ -144,16 +136,13 @@ class AVRO_DECL Validator : private boost::noncopyable int64_t count_; struct CompoundType { - explicit CompoundType(const NodePtr &n) : - node(n), pos(0) - {} - NodePtr node; ///< save the node - size_t pos; ///< track the leaf position to visit + explicit CompoundType(NodePtr n) : node(std::move(n)), pos(0) {} + NodePtr node; ///< save the node + size_t pos; ///< track the leaf position to visit }; std::vector<CompoundType> compoundStack_; std::vector<size_t> counters_; - }; } // namespace avro diff --git a/contrib/libs/apache/avro/api/Writer.hh b/contrib/libs/apache/avro/api/Writer.hh index 74b057ce65b..930ea398c44 100644 --- a/contrib/libs/apache/avro/api/Writer.hh +++ b/contrib/libs/apache/avro/api/Writer.hh @@ -23,26 +23,22 @@ #include <boost/noncopyable.hpp> #include "Config.hh" -#include "buffer/Buffer.hh" -#include "Zigzag.hh" #include "Types.hh" #include "Validator.hh" +#include "Zigzag.hh" +#include "buffer/Buffer.hh" namespace avro { /// Class for writing avro data to a stream. template<class ValidatorType> -class WriterImpl : private boost::noncopyable -{ +class WriterImpl : private boost::noncopyable { - public: +public: + WriterImpl() = default; - WriterImpl() {} - - explicit WriterImpl(const ValidSchema &schema) : - validator_(schema) - {} + explicit WriterImpl(const ValidSchema &schema) : validator_(schema) {} void writeValue(const Null &) { validator_.checkTypeExpected(AVRO_NULL); @@ -56,6 +52,7 @@ class WriterImpl : private boost::noncopyable void writeValue(int32_t val) { validator_.checkTypeExpected(AVRO_INT); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) std::array<uint8_t, 5> bytes; size_t size = encodeInt32(val, bytes); buffer_.writeTo(reinterpret_cast<const char *>(bytes.data()), size); @@ -98,13 +95,13 @@ class WriterImpl : private boost::noncopyable putBytes(val, size); } - template <size_t N> + template<size_t N> void writeFixed(const uint8_t (&val)[N]) { validator_.checkFixedSizeExpected(N); buffer_.writeTo(reinterpret_cast<const char *>(val), N); } - template <size_t N> + template<size_t N> void writeFixed(const std::array<uint8_t, N> &val) { validator_.checkFixedSizeExpected(val.size()); buffer_.writeTo(reinterpret_cast<const char *>(val.data()), val.size()); @@ -154,9 +151,9 @@ class WriterImpl : private boost::noncopyable return buffer_; } - private: - +private: void putLong(int64_t val) { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) std::array<uint8_t, 10> bytes; size_t size = encodeInt64(val, bytes); buffer_.writeTo(reinterpret_cast<const char *>(bytes.data()), size); @@ -175,11 +172,10 @@ class WriterImpl : private boost::noncopyable ValidatorType validator_; OutputBuffer buffer_; - }; -typedef WriterImpl<NullValidator> Writer; -typedef WriterImpl<Validator> ValidatingWriter; +using Writer = WriterImpl<NullValidator>; +using ValidatingWriter = WriterImpl<Validator>; } // namespace avro diff --git a/contrib/libs/apache/avro/api/Zigzag.hh b/contrib/libs/apache/avro/api/Zigzag.hh index d0259b8d50c..fefdc3f32e7 100644 --- a/contrib/libs/apache/avro/api/Zigzag.hh +++ b/contrib/libs/apache/avro/api/Zigzag.hh @@ -19,9 +19,9 @@ #ifndef avro_Encoding_hh__ #define avro_Encoding_hh__ -#include <stdint.h> #include <array> #include <cstddef> +#include <cstdint> #include "Config.hh" /// \file @@ -29,14 +29,24 @@ namespace avro { -AVRO_DECL uint64_t encodeZigzag64(int64_t input); -AVRO_DECL int64_t decodeZigzag64(uint64_t input); - -AVRO_DECL uint32_t encodeZigzag32(int32_t input); -AVRO_DECL int32_t decodeZigzag32(uint32_t input); - -AVRO_DECL size_t encodeInt32(int32_t input, std::array<uint8_t, 5> &output); -AVRO_DECL size_t encodeInt64(int64_t input, std::array<uint8_t, 10> &output); +AVRO_DECL constexpr uint64_t encodeZigzag64(int64_t input) noexcept { + // cppcheck-suppress shiftTooManyBitsSigned + return ((input << 1) ^ (input >> 63)); +} +AVRO_DECL constexpr int64_t decodeZigzag64(uint64_t input) noexcept { + return static_cast<int64_t>(((input >> 1) ^ -(static_cast<int64_t>(input) & 1))); +} + +AVRO_DECL constexpr uint32_t encodeZigzag32(int32_t input) noexcept { + // cppcheck-suppress shiftTooManyBitsSigned + return ((input << 1) ^ (input >> 31)); +} +AVRO_DECL constexpr int32_t decodeZigzag32(uint32_t input) noexcept { + return static_cast<int32_t>(((input >> 1) ^ -(static_cast<int64_t>(input) & 1))); +} + +AVRO_DECL size_t encodeInt32(int32_t input, std::array<uint8_t, 5> &output) noexcept; +AVRO_DECL size_t encodeInt64(int64_t input, std::array<uint8_t, 10> &output) noexcept; } // namespace avro diff --git a/contrib/libs/apache/avro/api/buffer/Buffer.hh b/contrib/libs/apache/avro/api/buffer/Buffer.hh index 7d7aaf8679c..bc3baf12330 100644 --- a/contrib/libs/apache/avro/api/buffer/Buffer.hh +++ b/contrib/libs/apache/avro/api/buffer/Buffer.hh @@ -22,6 +22,7 @@ #ifndef _WIN32 #include <sys/uio.h> #endif +#include <utility> #include <vector> #include "../Config.hh" @@ -40,7 +41,6 @@ namespace avro { class OutputBuffer; class InputBuffer; - /** * The OutputBuffer (write-only buffer) * @@ -61,11 +61,9 @@ class InputBuffer; * modifying one will modify both. **/ -class AVRO_DECL OutputBuffer -{ - - public: +class AVRO_DECL OutputBuffer { +public: typedef detail::size_type size_type; typedef detail::data_type data_type; @@ -95,10 +93,8 @@ class AVRO_DECL OutputBuffer * **/ - OutputBuffer(size_type reserveSize = 0) : - pimpl_(new detail::BufferImpl) - { - if(reserveSize) { + explicit OutputBuffer(size_type reserveSize = 0) : pimpl_(new detail::BufferImpl) { + if (reserveSize) { reserve(reserveSize); } } @@ -111,8 +107,7 @@ class AVRO_DECL OutputBuffer * the write operation. **/ - void reserve(size_type reserveSize) - { + void reserve(size_type reserveSize) { pimpl_->reserveFreeSpace(reserveSize); } @@ -149,11 +144,10 @@ class AVRO_DECL OutputBuffer * throw a std::length_error exception. **/ - size_type wroteTo(size_type size) - { + size_type wroteTo(size_type size) { int wrote = 0; - if(size) { - if(size > freeSpace()) { + if (size) { + if (size > freeSpace()) { throw std::length_error("Impossible to write more data than free space"); } wrote = pimpl_->wroteTo(size); @@ -166,7 +160,7 @@ class AVRO_DECL OutputBuffer **/ bool empty() const { - return (pimpl_->size()==0); + return (pimpl_->size() == 0); } /** @@ -174,7 +168,7 @@ class AVRO_DECL OutputBuffer */ size_type size() const { - return pimpl_->size(); + return pimpl_->size(); } /** @@ -184,7 +178,7 @@ class AVRO_DECL OutputBuffer **/ size_type freeSpace() const { - return pimpl_->freeSpace(); + return pimpl_->freeSpace(); } /** @@ -193,10 +187,10 @@ class AVRO_DECL OutputBuffer * **/ - template <class BufferType> + template<class BufferType> void append(const BufferType &buf) { // don't append an empty buffer - if(buf.size()) { + if (buf.size()) { pimpl_->append(*(buf.pimpl_.get())); } } @@ -222,8 +216,7 @@ class AVRO_DECL OutputBuffer * Discard any data in this buffer. **/ - void discardData() - { + void discardData() { pimpl_->discardData(); } @@ -232,16 +225,13 @@ class AVRO_DECL OutputBuffer * Throws if the size is greater than the number of bytes. **/ - void discardData(size_t bytes) - { - if(bytes > 0) { - if(bytes < pimpl_->size()) { + void discardData(size_t bytes) { + if (bytes > 0) { + if (bytes < pimpl_->size()) { pimpl_->discardData(bytes); - } - else if(bytes == pimpl_->size()) { + } else if (bytes == pimpl_->size()) { pimpl_->discardData(); - } - else { + } else { throw std::out_of_range("trying to discard more data than exists"); } } @@ -267,8 +257,7 @@ class AVRO_DECL OutputBuffer * Clone this buffer, creating a copy that contains the same data. **/ - OutputBuffer clone() const - { + OutputBuffer clone() const { detail::BufferImpl::SharedPtr newImpl(new detail::BufferImpl(*pimpl_)); return OutputBuffer(newImpl); } @@ -288,7 +277,7 @@ class AVRO_DECL OutputBuffer **/ int numChunks() const { - return pimpl_->numFreeChunks(); + return pimpl_->numFreeChunks(); } /** @@ -296,17 +285,14 @@ class AVRO_DECL OutputBuffer **/ int numDataChunks() const { - return pimpl_->numDataChunks(); + return pimpl_->numDataChunks(); } - private: - +private: friend class InputBuffer; friend class BufferReader; - explicit OutputBuffer(const detail::BufferImpl::SharedPtr &pimpl) : - pimpl_(pimpl) - { } + explicit OutputBuffer(detail::BufferImpl::SharedPtr pimpl) : pimpl_(std::move(pimpl)) {} detail::BufferImpl::SharedPtr pimpl_; ///< Must never be null. }; @@ -325,11 +311,9 @@ class AVRO_DECL OutputBuffer * **/ -class AVRO_DECL InputBuffer -{ - - public: +class AVRO_DECL InputBuffer { +public: typedef detail::size_type size_type; typedef detail::data_type data_type; @@ -347,9 +331,7 @@ class AVRO_DECL InputBuffer * deleting the underlying data if no other copies of exist. **/ - InputBuffer() : - pimpl_(new detail::BufferImpl) - { } + InputBuffer() : pimpl_(new detail::BufferImpl) {} /** * Construct an InputBuffer that contains the contents of an OutputBuffer. @@ -362,10 +344,8 @@ class AVRO_DECL InputBuffer * * Implicit conversion is allowed. **/ - - InputBuffer(const OutputBuffer &src) : - pimpl_(new detail::BufferImpl(*src.pimpl_)) - { } + // NOLINTNEXTLINE(google-explicit-constructor) + InputBuffer(const OutputBuffer &src) : pimpl_(new detail::BufferImpl(*src.pimpl_)) {} /** * Does the buffer have any data? @@ -408,16 +388,12 @@ class AVRO_DECL InputBuffer return pimpl_->numDataChunks(); } - - private: - +private: friend class OutputBuffer; // for append function friend class istreambuf; friend class BufferReader; - explicit InputBuffer(const detail::BufferImpl::SharedPtr &pimpl) : - pimpl_(pimpl) - { } + explicit InputBuffer(const detail::BufferImpl::SharedPtr &pimpl) : pimpl_(pimpl) {} /** * Class to indicate that a copy of a OutputBuffer to InputBuffer should be @@ -436,9 +412,7 @@ class AVRO_DECL InputBuffer * Make a shallow copy of an OutputBuffer in order to read it without * causing conversion overhead. **/ - InputBuffer(const OutputBuffer &src, const ShallowCopy &) : - pimpl_(src.pimpl_) - { } + InputBuffer(const OutputBuffer &src, const ShallowCopy &) : pimpl_(src.pimpl_) {} /** * Make a shallow copy of an InputBuffer. The default copy constructor @@ -447,41 +421,34 @@ class AVRO_DECL InputBuffer * manner. **/ - InputBuffer(const InputBuffer &src, const ShallowCopy &) : - pimpl_(src.pimpl_) - { } - + InputBuffer(const InputBuffer &src, const ShallowCopy &) : pimpl_(src.pimpl_) {} detail::BufferImpl::ConstSharedPtr pimpl_; ///< Must never be null. }; - /* * Implementations of some OutputBuffer functions are inlined here * because InputBuffer definition was required before. */ -inline InputBuffer OutputBuffer::extractData() -{ +inline InputBuffer OutputBuffer::extractData() { detail::BufferImpl::SharedPtr newImpl(new detail::BufferImpl); - if(pimpl_->size()) { + if (pimpl_->size()) { pimpl_->extractData(*newImpl); } return InputBuffer(newImpl); } -inline InputBuffer OutputBuffer::extractData(size_type bytes) -{ - if(bytes > pimpl_->size()) { +inline InputBuffer OutputBuffer::extractData(size_type bytes) { + if (bytes > pimpl_->size()) { throw std::out_of_range("trying to extract more data than exists"); } detail::BufferImpl::SharedPtr newImpl(new detail::BufferImpl); - if(bytes > 0) { - if(bytes < pimpl_->size()) { + if (bytes > 0) { + if (bytes < pimpl_->size()) { pimpl_->extractData(*newImpl, bytes); - } - else { + } else { pimpl_->extractData(*newImpl); } } @@ -508,8 +475,7 @@ inline InputBuffer OutputBuffer::extractData(size_type bytes) **/ template<class BufferType> -inline void toIovec(BufferType &buf, std::vector<struct iovec> &iov) -{ +inline void toIovec(BufferType &buf, std::vector<struct iovec> &iov) { const int chunks = buf.numChunks(); iov.resize(chunks); typename BufferType::const_iterator iter = buf.begin(); @@ -521,6 +487,6 @@ inline void toIovec(BufferType &buf, std::vector<struct iovec> &iov) } #endif -} // namespace +} // namespace avro #endif diff --git a/contrib/libs/apache/avro/api/buffer/BufferReader.hh b/contrib/libs/apache/avro/api/buffer/BufferReader.hh index 83b6b4b3242..7f49518e64b 100644 --- a/contrib/libs/apache/avro/api/buffer/BufferReader.hh +++ b/contrib/libs/apache/avro/api/buffer/BufferReader.hh @@ -19,8 +19,8 @@ #ifndef avro_BufferReader_hh__ #define avro_BufferReader_hh__ -#include <type_traits> #include "Buffer.hh" +#include <type_traits> #ifdef min #undef min @@ -40,24 +40,21 @@ namespace avro { * chunk boundaries. May read from an InputBuffer or OutputBuffer. * **/ -class AVRO_DECL BufferReader : private boost::noncopyable -{ - - public: +class AVRO_DECL BufferReader : private boost::noncopyable { +public: typedef detail::data_type data_type; typedef detail::size_type size_type; - private: - +private: size_type chunkRemaining() const { return iter_->dataSize() - chunkPos_; } - void incrementChunk(size_type howmuch) { - bytesRemaining_ -= howmuch; - chunkPos_ += howmuch; - if(chunkPos_ == iter_->dataSize()) { + void incrementChunk(size_type howMuch) { + bytesRemaining_ -= howMuch; + chunkPos_ += howMuch; + if (chunkPos_ == iter_->dataSize()) { chunkPos_ = 0; ++iter_; } @@ -73,23 +70,18 @@ class AVRO_DECL BufferReader : private boost::noncopyable return iter_->tellReadPos() + chunkPos_; } - public: - - BufferReader(const InputBuffer &buf) : - bufferImpl_(buf.pimpl_), - iter_(bufferImpl_->beginRead()), - bytes_(bufferImpl_->size()), - bytesRemaining_(bytes_), - chunkPos_(0) - { } +public: + explicit BufferReader(const InputBuffer &buf) : bufferImpl_(buf.pimpl_), + iter_(bufferImpl_->beginRead()), + bytes_(bufferImpl_->size()), + bytesRemaining_(bytes_), + chunkPos_(0) {} - BufferReader(const OutputBuffer &buf) : - bufferImpl_(buf.pimpl_), - iter_(bufferImpl_->beginRead()), - bytes_(bufferImpl_->size()), - bytesRemaining_(bytes_), - chunkPos_(0) - { } + explicit BufferReader(const OutputBuffer &buf) : bufferImpl_(buf.pimpl_), + iter_(bufferImpl_->beginRead()), + bytes_(bufferImpl_->size()), + bytesRemaining_(bytes_), + chunkPos_(0) {} /** * How many bytes are still not read from this buffer. @@ -113,12 +105,12 @@ class AVRO_DECL BufferReader : private boost::noncopyable size_type read(data_type *data, size_type size) { - if(size > bytesRemaining_) { + if (size > bytesRemaining_) { size = bytesRemaining_; } size_type sizeToRead = size; - while(sizeToRead) { + while (sizeToRead) { const size_type toRead = std::min(sizeToRead, chunkRemaining()); memcpy(data, addr(), toRead); sizeToRead -= toRead; @@ -134,21 +126,19 @@ class AVRO_DECL BufferReader : private boost::noncopyable **/ bool read(std::string &str, size_type size) { - if(size > bytesRemaining_) { + if (size > bytesRemaining_) { return false; } - if(size <= chunkRemaining()) { + if (size <= chunkRemaining()) { fastStringRead(str, size); - } - else { + } else { slowStringRead(str, size); } return true; } - /** * Read a single value from the buffer. The value must be a "fundamental" * type, e.g. int, float, etc. (otherwise use the other writeTo tests). @@ -156,7 +146,7 @@ class AVRO_DECL BufferReader : private boost::noncopyable **/ template<typename T> - bool read(T &val) { + bool read(T &val) { return read(val, std::is_fundamental<T>()); } @@ -166,7 +156,7 @@ class AVRO_DECL BufferReader : private boost::noncopyable bool skip(size_type bytes) { bool skipped = false; - if(bytes <= bytesRemaining_) { + if (bytes <= bytesRemaining_) { doSkip(bytes); skipped = true; } @@ -178,25 +168,24 @@ class AVRO_DECL BufferReader : private boost::noncopyable **/ bool seek(size_type pos) { - if(pos > bytes_) { + if (pos > bytes_) { return false; } size_type toSkip = pos; size_type curPos = bytesRead(); // if the seek position is ahead, we can use skip to get there - if(pos >= curPos) { + if (pos >= curPos) { toSkip -= curPos; } // if the seek position is ahead of the start of the chunk we can back up to // start of the chunk - else if(pos >= (curPos - chunkPos_)) { + else if (pos >= (curPos - chunkPos_)) { curPos -= chunkPos_; bytesRemaining_ += chunkPos_; chunkPos_ = 0; toSkip -= curPos; - } - else { + } else { rewind(); } doSkip(toSkip); @@ -205,30 +194,29 @@ class AVRO_DECL BufferReader : private boost::noncopyable bool peek(char &val) { bool ret = (bytesRemaining_ > 0); - if(ret) { + if (ret) { val = *(addr()); } return ret; } InputBuffer copyData(size_type bytes) { - if(bytes > bytesRemaining_) { + if (bytes > bytesRemaining_) { // force no copy bytes = 0; } detail::BufferImpl::SharedPtr newImpl(new detail::BufferImpl); - if(bytes) { + if (bytes) { bufferImpl_->copyData(*newImpl, iter_, chunkPos_, bytes); doSkip(bytes); } return InputBuffer(newImpl); } - private: - +private: void doSkip(size_type sizeToSkip) { - while(sizeToSkip) { + while (sizeToSkip) { const size_type toSkip = std::min(sizeToSkip, chunkRemaining()); sizeToSkip -= toSkip; incrementChunk(toSkip); @@ -236,17 +224,15 @@ class AVRO_DECL BufferReader : private boost::noncopyable } template<typename T> - bool read(T &val, const std::true_type&) - { - if(sizeof(T) > bytesRemaining_) { + bool read(T &val, const std::true_type &) { + if (sizeof(T) > bytesRemaining_) { return false; } if (sizeof(T) <= chunkRemaining()) { - val = *(reinterpret_cast<const T*> (addr())); + val = *(reinterpret_cast<const T *>(addr())); incrementChunk(sizeof(T)); - } - else { + } else { read(reinterpret_cast<data_type *>(&val), sizeof(T)); } return true; @@ -254,8 +240,7 @@ class AVRO_DECL BufferReader : private boost::noncopyable /// An uninstantiable function, that is if boost::is_fundamental check fails template<typename T> - bool read(T &, const std::false_type&) - { + bool read(T &val, const std::false_type &) { static_assert(sizeof(T) == 0, "Not a valid type to read"); return false; } @@ -268,7 +253,7 @@ class AVRO_DECL BufferReader : private boost::noncopyable void slowStringRead(std::string &str, size_type sizeToCopy) { str.clear(); str.reserve(sizeToCopy); - while(sizeToCopy) { + while (sizeToCopy) { const size_type toCopy = std::min(sizeToCopy, chunkRemaining()); str.append(addr(), toCopy); sizeToCopy -= toCopy; @@ -283,7 +268,6 @@ class AVRO_DECL BufferReader : private boost::noncopyable size_type chunkPos_; }; - -} // namespace +} // namespace avro #endif diff --git a/contrib/libs/apache/avro/api/buffer/detail/BufferDetail.hh b/contrib/libs/apache/avro/api/buffer/detail/BufferDetail.hh index 29a2e00b4e0..b487cdb3935 100644 --- a/contrib/libs/apache/avro/api/buffer/detail/BufferDetail.hh +++ b/contrib/libs/apache/avro/api/buffer/detail/BufferDetail.hh @@ -19,17 +19,18 @@ #ifndef avro_BufferDetail_hh__ #define avro_BufferDetail_hh__ -#include <boost/shared_ptr.hpp> +#include <boost/function.hpp> #include <boost/shared_array.hpp> +#include <boost/shared_ptr.hpp> #include <boost/static_assert.hpp> -#include <boost/function.hpp> #include <boost/utility.hpp> +#include <utility> #ifdef HAVE_BOOST_ASIO #include <boost/asio/buffer.hpp> #endif -#include <exception> #include <cassert> #include <deque> +#include <exception> /** * \file BufferDetail.hh @@ -42,10 +43,10 @@ namespace avro { namespace detail { -typedef char data_type; -typedef size_t size_type; +typedef char data_type; +typedef size_t size_type; #ifdef HAVE_BOOST_ASIO -typedef boost::asio::const_buffer ConstAsioBuffer; +typedef boost::asio::const_buffer ConstAsioBuffer; typedef boost::asio::mutable_buffer MutableAsioBuffer; #endif @@ -54,21 +55,21 @@ const size_type kMinBlockSize = 4096; const size_type kMaxBlockSize = 16384; const size_type kDefaultBlockSize = kMinBlockSize; -typedef boost::function<void(void)> free_func; +typedef boost::function<void(void)> free_func; /** * Simple class to hold a functor that executes on delete **/ class CallOnDestroy { - public: - CallOnDestroy(const free_func &func) : func_(func) - { } +public: + explicit CallOnDestroy(free_func func) : func_(std::move(func)) {} ~CallOnDestroy() { if (func_) { func_(); } } - private: + +private: free_func func_; }; @@ -89,36 +90,27 @@ class CallOnDestroy { * **/ -class Chunk -{ - - public: - - typedef boost::shared_ptr<Chunk> SharedPtr; +class Chunk { +public: /// Default constructor, allocates a new underlying block for this chunk. - Chunk(size_type size) : - underlyingBlock_(new data_type[size]), - readPos_(underlyingBlock_.get()), - writePos_(readPos_), - endPos_(readPos_ + size) - { } + explicit Chunk(size_type size) : underlyingBlock_(new data_type[size]), + readPos_(underlyingBlock_.get()), + writePos_(readPos_), + endPos_(readPos_ + size) {} /// Foreign buffer constructor, uses the supplied data for this chunk, and /// only for reading. - Chunk(const data_type *data, size_type size, const free_func &func) : - callOnDestroy_(new CallOnDestroy(func)), - readPos_(const_cast<data_type *>(data)), - writePos_(readPos_ + size), - endPos_(writePos_) - { } - - private: + Chunk(const data_type *data, size_type size, const free_func &func) : callOnDestroy_(new CallOnDestroy(func)), + readPos_(const_cast<data_type *>(data)), + writePos_(readPos_ + size), + endPos_(writePos_) {} + +private: // reference counted object will call a functor when it's destroyed boost::shared_ptr<CallOnDestroy> callOnDestroy_; - public: - +public: /// Remove readable bytes from the front of the chunk by advancing the /// chunk start position. void truncateFront(size_type howMuch) { @@ -145,7 +137,7 @@ class Chunk /// After a write operation, increment the write position. void incrementCursor(size_type howMuch) { - writePos_ += howMuch; + writePos_ += howMuch; assert(writePos_ <= endPos_); } @@ -164,8 +156,7 @@ class Chunk return (endPos_ - readPos_); } - private: - +private: friend bool operator==(const Chunk &lhs, const Chunk &rhs); friend bool operator!=(const Chunk &lhs, const Chunk &rhs); @@ -191,7 +182,6 @@ inline bool operator!=(const Chunk &lhs, const Chunk &rhs) { return lhs.underlyingBlock_ != rhs.underlyingBlock_; } - /** * \brief Implementation details for Buffer class * @@ -201,13 +191,11 @@ inline bool operator!=(const Chunk &lhs, const Chunk &rhs) { * */ -class BufferImpl : boost::noncopyable -{ +class BufferImpl : boost::noncopyable { /// Add a new chunk to the list of chunks for this buffer, growing the /// buffer by the default block size. - void allocChunkChecked(size_type size = kDefaultBlockSize) - { + void allocChunkChecked(size_type size = kDefaultBlockSize) { writeChunks_.push_back(Chunk(size)); freeSpace_ += writeChunks_.back().freeSize(); } @@ -215,12 +203,10 @@ class BufferImpl : boost::noncopyable /// Add a new chunk to the list of chunks for this buffer, growing the /// buffer by the requested size, but within the range of a minimum and /// maximum. - void allocChunk(size_type size) - { - if(size < kMinBlockSize) { + void allocChunk(size_type size) { + if (size < kMinBlockSize) { size = kMinBlockSize; - } - else if (size > kMaxBlockSize) { + } else if (size > kMaxBlockSize) { size = kMaxBlockSize; } allocChunkChecked(size); @@ -228,8 +214,7 @@ class BufferImpl : boost::noncopyable /// Update the state of the chunks after a write operation. This function /// ensures the chunk states are consistent with the write. - void postWrite(size_type size) - { + void postWrite(size_type size) { // precondition to this function is that the writeChunk_.front() // contains the data that was just written, so make sure writeChunks_ @@ -257,7 +242,7 @@ class BufferImpl : boost::noncopyable // if readChunks_ is not the same as writeChunks_.front(), make a copy // of it there - if(readChunks_.empty() || (readChunks_.back() != writeChunks_.front())) { + if (readChunks_.empty() || (readChunks_.back() != writeChunks_.front())) { const Chunk &curChunk = writeChunks_.front(); readChunks_.push_back(curChunk); @@ -266,7 +251,7 @@ class BufferImpl : boost::noncopyable // added to the readChunk_ list). Here, adjust the start of the // readChunk to begin after any data already existing in curChunk - readChunks_.back().truncateFront( curChunk.dataSize()); + readChunks_.back().truncateFront(curChunk.dataSize()); } assert(readChunks_.back().freeSize() == writeChunks_.front().freeSize()); @@ -282,29 +267,24 @@ class BufferImpl : boost::noncopyable // if there is no more free space in writeChunks_, the next write cannot use // it, so dispose of it now - if(writeChunks_.front().freeSize() == 0) { + if (writeChunks_.front().freeSize() == 0) { writeChunks_.pop_front(); } } - public: - +public: typedef std::deque<Chunk> ChunkList; typedef boost::shared_ptr<BufferImpl> SharedPtr; typedef boost::shared_ptr<const BufferImpl> ConstSharedPtr; /// Default constructor, creates a buffer without any chunks - BufferImpl() : - freeSpace_(0), - size_(0) - { } + BufferImpl() : freeSpace_(0), + size_(0) {} /// Copy constructor, gets a copy of all the chunks with data. - explicit BufferImpl(const BufferImpl &src) : - readChunks_(src.readChunks_), - freeSpace_(0), - size_(src.size_) - { } + BufferImpl(const BufferImpl &src) : readChunks_(src.readChunks_), + freeSpace_(0), + size_(src.size_) {} /// Amount of data held in this buffer. size_type size() const { @@ -319,7 +299,7 @@ class BufferImpl : boost::noncopyable /// Add enough free chunks to make the reservation size available. /// Actual amount may be more (rounded up to next chunk). void reserveFreeSpace(size_type reserveSize) { - while(freeSpace_ < reserveSize) { + while (freeSpace_ < reserveSize) { allocChunk(reserveSize - freeSpace_); } } @@ -346,36 +326,32 @@ class BufferImpl : boost::noncopyable /// Write a single value to buffer, add a new chunk if necessary. template<typename T> - void writeTo(T val, const std::true_type&) - { - if(freeSpace_ && (sizeof(T) <= writeChunks_.front().freeSize())) { + void writeTo(T val, const std::true_type &) { + if (freeSpace_ && (sizeof(T) <= writeChunks_.front().freeSize())) { // fast path, there's enough room in the writeable chunk to just // straight out copy it - *(reinterpret_cast <T*> ( writeChunks_.front().tellWritePos()) ) = val; + *(reinterpret_cast<T *>(writeChunks_.front().tellWritePos())) = val; postWrite(sizeof(T)); - } - else { + } else { // need to fixup chunks first, so use the regular memcpy // writeTo method - writeTo(reinterpret_cast<data_type*>(&val), sizeof(T)); + writeTo(reinterpret_cast<data_type *>(&val), sizeof(T)); } } /// An uninstantiable function, this is if boost::is_fundamental check fails, /// and will compile-time assert. template<typename T> - void writeTo(T, const std::false_type&) - { - BOOST_STATIC_ASSERT(sizeof(T)==0); + void writeTo(T /*val*/, const std::false_type &) { + BOOST_STATIC_ASSERT(sizeof(T) == 0); } /// Write a block of data to the buffer, adding new chunks if necessary. - size_type writeTo(const data_type *data, size_type size) - { + size_type writeTo(const data_type *data, size_type size) { size_type bytesLeft = size; - while(bytesLeft) { + while (bytesLeft) { - if(freeSpace_ == 0) { + if (freeSpace_ == 0) { allocChunkChecked(); } @@ -384,15 +360,14 @@ class BufferImpl : boost::noncopyable assert(toCopy); memcpy(chunk.tellWritePos(), data, toCopy); postWrite(toCopy); - data += toCopy; + data += toCopy; bytesLeft -= toCopy; } return size; } /// Update internal status of chunks after data is written using iterator. - size_type wroteTo(size_type size) - { + size_type wroteTo(size_type size) { assert(size <= freeSpace_); size_type bytesLeft = size; while (bytesLeft) { @@ -419,21 +394,19 @@ class BufferImpl : boost::noncopyable } /// Remove the specified amount of data from the chunks, starting at the front. - void discardData(size_type bytes) - { + void discardData(size_type bytes) { assert(bytes && bytes <= size_); size_type bytesToDiscard = bytes; - while( bytesToDiscard ) { + while (bytesToDiscard) { size_t currentSize = readChunks_.front().dataSize(); // see if entire chunk is discarded - if(currentSize <= bytesToDiscard) { + if (currentSize <= bytesToDiscard) { readChunks_.pop_front(); bytesToDiscard -= currentSize; - } - else { + } else { readChunks_.front().truncateFront(bytesToDiscard); bytesToDiscard = 0; } @@ -444,22 +417,20 @@ class BufferImpl : boost::noncopyable /// Remove the specified amount of data from the chunks, moving the /// data to dest's chunks - void extractData(BufferImpl &dest, size_type bytes) - { + void extractData(BufferImpl &dest, size_type bytes) { assert(bytes && bytes <= size_); size_type bytesToExtract = bytes; - while( bytesToExtract ) { + while (bytesToExtract) { size_t currentSize = readChunks_.front().dataSize(); dest.readChunks_.push_back(readChunks_.front()); // see if entire chunk was extracted - if(currentSize <= bytesToExtract) { + if (currentSize <= bytesToExtract) { readChunks_.pop_front(); bytesToExtract -= currentSize; - } - else { + } else { readChunks_.front().truncateFront(bytesToExtract); size_t excess = currentSize - bytesToExtract; dest.readChunks_.back().truncateBack(excess); @@ -472,8 +443,7 @@ class BufferImpl : boost::noncopyable } /// Move data from this to the destination, leaving this buffer without data - void extractData(BufferImpl &dest) - { + void extractData(BufferImpl &dest) { assert(dest.readChunks_.empty()); dest.readChunks_.swap(readChunks_); dest.size_ = size_; @@ -482,16 +452,15 @@ class BufferImpl : boost::noncopyable /// Copy data to a different buffer by copying the chunks. It's /// a bit like extract, but without modifying the source buffer. - void copyData(BufferImpl &dest, - ChunkList::const_iterator iter, - size_type offset, - size_type bytes) const - { + static void copyData(BufferImpl &dest, + ChunkList::const_iterator iter, + size_type offset, + size_type bytes) { // now we are positioned to start the copying, copy as many // chunks as we need, the first chunk may have a non-zero offset // if the data to copy is not at the start of the chunk size_type copied = 0; - while(copied < bytes) { + while (copied < bytes) { dest.readChunks_.push_back(*iter); @@ -522,34 +491,25 @@ class BufferImpl : boost::noncopyable return writeChunks_.size(); } - /// Add unmanaged data to the buffer. The buffer will not automatically - /// free the data, but it will call the supplied function when the data is - /// no longer referenced by the buffer (or copies of the buffer). + /// Add unmanaged data to the buffer. The buffer will not automatically + /// free the data, but it will call the supplied function when the data is + /// no longer referenced by the buffer (or copies of the buffer). void appendForeignData(const data_type *data, size_type size, const free_func &func) { readChunks_.push_back(Chunk(data, size, func)); size_ += size; } + BufferImpl &operator=(const BufferImpl &src) = delete; - private: - - /// Assignment not allowed - BufferImpl& operator=(const BufferImpl &src); - /* { - readChunks_.assign(src.readChunks_.begin(), src.readChunks_.end()); - size_ = src.size(); - return *this; - } */ - - ChunkList readChunks_; ///< chunks of this buffer containing data - ChunkList writeChunks_; ///< chunks of this buffer containing free space - - size_type freeSpace_; ///< capacity of buffer before allocation required - size_type size_; ///< amount of data in buffer +private: + ChunkList readChunks_; ///< chunks of this buffer containing data + ChunkList writeChunks_; ///< chunks of this buffer containing free space + size_type freeSpace_; ///< capacity of buffer before allocation required + size_type size_; ///< amount of data in buffer }; -} // detail namespace +} // namespace detail -} // namespace +} // namespace avro #endif diff --git a/contrib/libs/apache/avro/api/buffer/detail/BufferDetailIterator.hh b/contrib/libs/apache/avro/api/buffer/detail/BufferDetailIterator.hh index c05f219a64c..44e35dd0138 100644 --- a/contrib/libs/apache/avro/api/buffer/detail/BufferDetailIterator.hh +++ b/contrib/libs/apache/avro/api/buffer/detail/BufferDetailIterator.hh @@ -40,17 +40,12 @@ namespace detail { * wraps the iterator with a cast operator to do this conversion. **/ -struct InputIteratorHelper -{ +struct InputIteratorHelper { /// Construct a helper with an unnassigned iterator. - InputIteratorHelper() : - iter_() - {} + InputIteratorHelper() : iter_() {} /// Construct a helper with an iterator. - InputIteratorHelper(const BufferImpl::ChunkList::const_iterator &iter) : - iter_(iter) - {} + explicit InputIteratorHelper(const BufferImpl::ChunkList::const_iterator &iter) : iter_(iter) {} /// The location of valid data in this chunk. const data_type *data() const { @@ -83,17 +78,12 @@ struct InputIteratorHelper * wraps the iterator with a cast operator to do this conversion. */ -struct OutputIteratorHelper -{ +struct OutputIteratorHelper { /// Construct a helper with an unnassigned iterator. - OutputIteratorHelper() : - iter_() - {} + OutputIteratorHelper() : iter_() {} /// Construct a helper with an iterator. - OutputIteratorHelper(const BufferImpl::ChunkList::const_iterator &iter) : - iter_(iter) - {} + explicit OutputIteratorHelper(const BufferImpl::ChunkList::const_iterator &iter) : iter_(iter) {} /// The location of the first writable byte in this chunk. data_type *data() const { @@ -123,11 +113,9 @@ struct OutputIteratorHelper **/ template<typename Helper> -class BufferIterator -{ - - public: +class BufferIterator { +public: typedef BufferIterator<Helper> this_type; /** @@ -144,14 +132,12 @@ class BufferIterator typedef std::forward_iterator_tag iterator_category; // this is a lie to appease asio typedef Helper value_type; typedef std::ptrdiff_t difference_type; - typedef value_type* pointer; - typedef value_type& reference; + typedef value_type *pointer; + typedef value_type &reference; //@} /// Construct an unitialized iterator. - BufferIterator() : - helper_() - { } + BufferIterator() : helper_() {} /* The default implementations are good here /// Copy constructor. @@ -166,17 +152,15 @@ class BufferIterator */ /// Construct iterator at the position in the buffer's chunk list. - explicit BufferIterator(BufferImpl::ChunkList::const_iterator iter) : - helper_(iter) - { } + explicit BufferIterator(BufferImpl::ChunkList::const_iterator iter) : helper_(iter) {} /// Dereference iterator, returns InputIteratorHelper or OutputIteratorHelper wrapper. - reference operator *() { + reference operator*() { return helper_; } /// Dereference iterator, returns const InputIteratorHelper or OutputIteratorHelper wrapper. - const value_type &operator *() const { + const value_type &operator*() const { return helper_; } @@ -191,15 +175,13 @@ class BufferIterator } /// Increment to next chunk in list, or to end() iterator. - this_type& operator++() - { + this_type &operator++() { ++helper_.iter_; return *this; } /// Increment to next chunk in list, or to end() iterator. - this_type operator++(int) - { + this_type operator++(int) { this_type ret = *this; ++helper_.iter_; return ret; @@ -215,16 +197,15 @@ class BufferIterator return (helper_.iter_ != rhs.helper_.iter_); } - private: - +private: Helper helper_; }; typedef BufferIterator<InputIteratorHelper> InputBufferIterator; typedef BufferIterator<OutputIteratorHelper> OutputBufferIterator; -} // detail namespace +} // namespace detail -} // namespace +} // namespace avro #endif diff --git a/contrib/libs/apache/avro/impl/BinaryDecoder.cc b/contrib/libs/apache/avro/impl/BinaryDecoder.cc index 71cbf9f1078..248b503342a 100644 --- a/contrib/libs/apache/avro/impl/BinaryDecoder.cc +++ b/contrib/libs/apache/avro/impl/BinaryDecoder.cc @@ -16,12 +16,10 @@ * limitations under the License. */ -#define __STDC_LIMIT_MACROS - -#include <memory> #include "Decoder.hh" -#include "Zigzag.hh" #include "Exception.hh" +#include "Zigzag.hh" +#include <memory> namespace avro { @@ -29,66 +27,58 @@ using std::make_shared; class BinaryDecoder : public Decoder { StreamReader in_; - const uint8_t* next_; - const uint8_t* end_; - - void init(InputStream& ib); - void decodeNull(); - bool decodeBool(); - int32_t decodeInt(); - int64_t decodeLong(); - float decodeFloat(); - double decodeDouble(); - void decodeString(std::string& value); - void skipString(); - void decodeBytes(std::vector<uint8_t>& value); - void skipBytes(); - void decodeFixed(size_t n, std::vector<uint8_t>& value); - void skipFixed(size_t n); - size_t decodeEnum(); - size_t arrayStart(); - size_t arrayNext(); - size_t skipArray(); - size_t mapStart(); - size_t mapNext(); - size_t skipMap(); - size_t decodeUnionIndex(); + + void init(InputStream &is) final; + void decodeNull() final; + bool decodeBool() final; + int32_t decodeInt() final; + int64_t decodeLong() final; + float decodeFloat() final; + double decodeDouble() final; + void decodeString(std::string &value) final; + void skipString() final; + void decodeBytes(std::vector<uint8_t> &value) final; + void skipBytes() final; + void decodeFixed(size_t n, std::vector<uint8_t> &value) final; + void skipFixed(size_t n) final; + size_t decodeEnum() final; + size_t arrayStart() final; + size_t arrayNext() final; + size_t skipArray() final; + size_t mapStart() final; + size_t mapNext() final; + size_t skipMap() final; + size_t decodeUnionIndex() final; int64_t doDecodeLong(); size_t doDecodeItemCount(); size_t doDecodeLength(); - void drain(); - void more(); + void drain() final; }; -DecoderPtr binaryDecoder() -{ +DecoderPtr binaryDecoder() { return make_shared<BinaryDecoder>(); } -void BinaryDecoder::init(InputStream& is) -{ +void BinaryDecoder::init(InputStream &is) { in_.reset(is); } -void BinaryDecoder::decodeNull() -{ +void BinaryDecoder::decodeNull() { } -bool BinaryDecoder::decodeBool() -{ - uint8_t v = in_.read(); +bool BinaryDecoder::decodeBool() { + auto v = in_.read(); if (v == 0) { return false; } else if (v == 1) { return true; } - throw Exception("Invalid value for bool"); + throw Exception(boost::format("Invalid value for bool: %1%") % v); } -int32_t BinaryDecoder::decodeInt() -{ - int64_t val = doDecodeLong(); +int32_t BinaryDecoder::decodeInt() { + auto val = doDecodeLong(); if (val < INT32_MIN || val > INT32_MAX) { throw Exception( boost::format("Value out of range for Avro int: %1%") % val); @@ -96,27 +86,23 @@ int32_t BinaryDecoder::decodeInt() return static_cast<int32_t>(val); } -int64_t BinaryDecoder::decodeLong() -{ +int64_t BinaryDecoder::decodeLong() { return doDecodeLong(); } -float BinaryDecoder::decodeFloat() -{ +float BinaryDecoder::decodeFloat() { float result; in_.readBytes(reinterpret_cast<uint8_t *>(&result), sizeof(float)); return result; } -double BinaryDecoder::decodeDouble() -{ +double BinaryDecoder::decodeDouble() { double result; in_.readBytes(reinterpret_cast<uint8_t *>(&result), sizeof(double)); return result; } -size_t BinaryDecoder::doDecodeLength() -{ +size_t BinaryDecoder::doDecodeLength() { ssize_t len = decodeInt(); if (len < 0) { throw Exception( @@ -125,29 +111,26 @@ size_t BinaryDecoder::doDecodeLength() return len; } -void BinaryDecoder::drain() -{ +void BinaryDecoder::drain() { in_.drain(false); } -void BinaryDecoder::decodeString(std::string& value) -{ +void BinaryDecoder::decodeString(std::string &value) { size_t len = doDecodeLength(); value.resize(len); if (len > 0) { - in_.readBytes(const_cast<uint8_t*>( - reinterpret_cast<const uint8_t*>(value.c_str())), len); + in_.readBytes(const_cast<uint8_t *>( + reinterpret_cast<const uint8_t *>(value.c_str())), + len); } } -void BinaryDecoder::skipString() -{ +void BinaryDecoder::skipString() { size_t len = doDecodeLength(); in_.skipBytes(len); } -void BinaryDecoder::decodeBytes(std::vector<uint8_t>& value) -{ +void BinaryDecoder::decodeBytes(std::vector<uint8_t> &value) { size_t len = doDecodeLength(); value.resize(len); if (len > 0) { @@ -155,38 +138,32 @@ void BinaryDecoder::decodeBytes(std::vector<uint8_t>& value) } } -void BinaryDecoder::skipBytes() -{ +void BinaryDecoder::skipBytes() { size_t len = doDecodeLength(); in_.skipBytes(len); } -void BinaryDecoder::decodeFixed(size_t n, std::vector<uint8_t>& value) -{ +void BinaryDecoder::decodeFixed(size_t n, std::vector<uint8_t> &value) { value.resize(n); if (n > 0) { in_.readBytes(value.data(), n); } } -void BinaryDecoder::skipFixed(size_t n) -{ +void BinaryDecoder::skipFixed(size_t n) { in_.skipBytes(n); } -size_t BinaryDecoder::decodeEnum() -{ +size_t BinaryDecoder::decodeEnum() { return static_cast<size_t>(doDecodeLong()); } -size_t BinaryDecoder::arrayStart() -{ +size_t BinaryDecoder::arrayStart() { return doDecodeItemCount(); } -size_t BinaryDecoder::doDecodeItemCount() -{ - int64_t result = doDecodeLong(); +size_t BinaryDecoder::doDecodeItemCount() { + auto result = doDecodeLong(); if (result < 0) { doDecodeLong(); return static_cast<size_t>(-result); @@ -194,17 +171,15 @@ size_t BinaryDecoder::doDecodeItemCount() return static_cast<size_t>(result); } -size_t BinaryDecoder::arrayNext() -{ +size_t BinaryDecoder::arrayNext() { return static_cast<size_t>(doDecodeLong()); } -size_t BinaryDecoder::skipArray() -{ - for (; ;) { - int64_t r = doDecodeLong(); +size_t BinaryDecoder::skipArray() { + for (;;) { + auto r = doDecodeLong(); if (r < 0) { - size_t n = static_cast<size_t>(doDecodeLong()); + auto n = static_cast<size_t>(doDecodeLong()); in_.skipBytes(n); } else { return static_cast<size_t>(r); @@ -212,23 +187,19 @@ size_t BinaryDecoder::skipArray() } } -size_t BinaryDecoder::mapStart() -{ +size_t BinaryDecoder::mapStart() { return doDecodeItemCount(); } -size_t BinaryDecoder::mapNext() -{ +size_t BinaryDecoder::mapNext() { return doDecodeItemCount(); } -size_t BinaryDecoder::skipMap() -{ +size_t BinaryDecoder::skipMap() { return skipArray(); } -size_t BinaryDecoder::decodeUnionIndex() -{ +size_t BinaryDecoder::decodeUnionIndex() { return static_cast<size_t>(doDecodeLong()); } @@ -248,5 +219,4 @@ int64_t BinaryDecoder::doDecodeLong() { return decodeZigzag64(encoded); } -} // namespace avro - +} // namespace avro diff --git a/contrib/libs/apache/avro/impl/BinaryEncoder.cc b/contrib/libs/apache/avro/impl/BinaryEncoder.cc index 5ceb872f8cd..fcb80bd43d4 100644 --- a/contrib/libs/apache/avro/impl/BinaryEncoder.cc +++ b/contrib/libs/apache/avro/impl/BinaryEncoder.cc @@ -27,130 +27,110 @@ using std::make_shared; class BinaryEncoder : public Encoder { StreamWriter out_; - void init(OutputStream& os); - void flush(); - int64_t byteCount() const; - void encodeNull(); - void encodeBool(bool b); - void encodeInt(int32_t i); - void encodeLong(int64_t l); - void encodeFloat(float f); - void encodeDouble(double d); - void encodeString(const std::string& s); - void encodeBytes(const uint8_t *bytes, size_t len); - void encodeFixed(const uint8_t *bytes, size_t len); - void encodeEnum(size_t e); - void arrayStart(); - void arrayEnd(); - void mapStart(); - void mapEnd(); - void setItemCount(size_t count); - void startItem(); - void encodeUnionIndex(size_t e); + void init(OutputStream &os) final; + void flush() final; + int64_t byteCount() const final; + void encodeNull() final; + void encodeBool(bool b) final; + void encodeInt(int32_t i) final; + void encodeLong(int64_t l) final; + void encodeFloat(float f) final; + void encodeDouble(double d) final; + void encodeString(const std::string &s) final; + void encodeBytes(const uint8_t *bytes, size_t len) final; + void encodeFixed(const uint8_t *bytes, size_t len) final; + void encodeEnum(size_t e) final; + void arrayStart() final; + void arrayEnd() final; + void mapStart() final; + void mapEnd() final; + void setItemCount(size_t count) final; + void startItem() final; + void encodeUnionIndex(size_t e) final; void doEncodeLong(int64_t l); }; -EncoderPtr binaryEncoder() -{ +EncoderPtr binaryEncoder() { return make_shared<BinaryEncoder>(); } -void BinaryEncoder::init(OutputStream& os) -{ +void BinaryEncoder::init(OutputStream &os) { out_.reset(os); } -void BinaryEncoder::flush() -{ +void BinaryEncoder::flush() { out_.flush(); } -void BinaryEncoder::encodeNull() -{ +void BinaryEncoder::encodeNull() { } -void BinaryEncoder::encodeBool(bool b) -{ +void BinaryEncoder::encodeBool(bool b) { out_.write(b ? 1 : 0); } -void BinaryEncoder::encodeInt(int32_t i) -{ +void BinaryEncoder::encodeInt(int32_t i) { doEncodeLong(i); } -void BinaryEncoder::encodeLong(int64_t l) -{ +void BinaryEncoder::encodeLong(int64_t l) { doEncodeLong(l); } -void BinaryEncoder::encodeFloat(float f) -{ - const uint8_t* p = reinterpret_cast<const uint8_t*>(&f); +void BinaryEncoder::encodeFloat(float f) { + const auto *p = reinterpret_cast<const uint8_t *>(&f); out_.writeBytes(p, sizeof(float)); } -void BinaryEncoder::encodeDouble(double d) -{ - const uint8_t* p = reinterpret_cast<const uint8_t*>(&d); +void BinaryEncoder::encodeDouble(double d) { + const auto *p = reinterpret_cast<const uint8_t *>(&d); out_.writeBytes(p, sizeof(double)); } -void BinaryEncoder::encodeString(const std::string& s) -{ +void BinaryEncoder::encodeString(const std::string &s) { doEncodeLong(s.size()); - out_.writeBytes(reinterpret_cast<const uint8_t*>(s.c_str()), s.size()); + out_.writeBytes(reinterpret_cast<const uint8_t *>(s.c_str()), s.size()); } -void BinaryEncoder::encodeBytes(const uint8_t *bytes, size_t len) -{ +void BinaryEncoder::encodeBytes(const uint8_t *bytes, size_t len) { doEncodeLong(len); out_.writeBytes(bytes, len); } -void BinaryEncoder::encodeFixed(const uint8_t *bytes, size_t len) -{ +void BinaryEncoder::encodeFixed(const uint8_t *bytes, size_t len) { out_.writeBytes(bytes, len); } -void BinaryEncoder::encodeEnum(size_t e) -{ +void BinaryEncoder::encodeEnum(size_t e) { doEncodeLong(e); } -void BinaryEncoder::arrayStart() -{ +void BinaryEncoder::arrayStart() { } -void BinaryEncoder::arrayEnd() -{ +void BinaryEncoder::arrayEnd() { doEncodeLong(0); } -void BinaryEncoder::mapStart() -{ +void BinaryEncoder::mapStart() { } -void BinaryEncoder::mapEnd() -{ +void BinaryEncoder::mapEnd() { doEncodeLong(0); } -void BinaryEncoder::setItemCount(size_t count) -{ +void BinaryEncoder::setItemCount(size_t count) { if (count == 0) { throw Exception("Count cannot be zero"); } doEncodeLong(count); } -void BinaryEncoder::startItem() -{ +void BinaryEncoder::startItem() { } -void BinaryEncoder::encodeUnionIndex(size_t e) -{ +void BinaryEncoder::encodeUnionIndex(size_t e) { doEncodeLong(e); } @@ -158,11 +138,10 @@ int64_t BinaryEncoder::byteCount() const { return out_.byteCount(); } - -void BinaryEncoder::doEncodeLong(int64_t l) -{ +void BinaryEncoder::doEncodeLong(int64_t l) { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) std::array<uint8_t, 10> bytes; - size_t size = encodeInt64(l, bytes); + auto size = encodeInt64(l, bytes); out_.writeBytes(bytes.data(), size); } -} // namespace avro +} // namespace avro diff --git a/contrib/libs/apache/avro/impl/Compiler.cc b/contrib/libs/apache/avro/impl/Compiler.cc index 6453db8f17f..d76546f317d 100644 --- a/contrib/libs/apache/avro/impl/Compiler.cc +++ b/contrib/libs/apache/avro/impl/Compiler.cc @@ -17,34 +17,33 @@ */ #include <boost/algorithm/string/replace.hpp> #include <sstream> +#include <utility> #include "Compiler.hh" -#include "Types.hh" #include "Schema.hh" -#include "ValidSchema.hh" #include "Stream.hh" +#include "Types.hh" +#include "ValidSchema.hh" #include "json/JsonDom.hh" -using std::string; +using std::make_pair; using std::map; -using std::vector; using std::pair; -using std::make_pair; +using std::string; +using std::vector; namespace avro { -using json::Entity; -using json::Object; using json::Array; +using json::Entity; using json::EntityType; +using json::Object; -typedef map<Name, NodePtr> SymbolTable; - +using SymbolTable = map<Name, NodePtr>; // #define DEBUG_VERBOSE -static NodePtr makePrimitive(const string& t) -{ +static NodePtr makePrimitive(const string &t) { if (t == "null") { return NodePtr(new NodePrimitive(AVRO_NULL)); } else if (t == "boolean") { @@ -66,35 +65,31 @@ static NodePtr makePrimitive(const string& t) } } -static NodePtr makeNode(const json::Entity& e, SymbolTable& st, const string &ns); +static NodePtr makeNode(const json::Entity &e, SymbolTable &st, const string &ns); -template <typename T> -concepts::SingleAttribute<T> asSingleAttribute(const T& t) -{ +template<typename T> +concepts::SingleAttribute<T> asSingleAttribute(const T &t) { concepts::SingleAttribute<T> n; n.add(t); return n; } -static bool isFullName(const string &s) -{ +static bool isFullName(const string &s) { return s.find('.') != string::npos; } -static Name getName(const string &name, const string &ns) -{ +static Name getName(const string &name, const string &ns) { return (isFullName(name)) ? Name(name) : Name(name, ns); } -static NodePtr makeNode(const string &t, SymbolTable &st, const string &ns) -{ +static NodePtr makeNode(const string &t, SymbolTable &st, const string &ns) { NodePtr result = makePrimitive(t); if (result) { return result; } Name n = getName(t, ns); - SymbolTable::const_iterator it = st.find(n); + auto it = st.find(n); if (it != st.end()) { return NodePtr(new NodeSymbolic(asSingleAttribute(n), it->second)); } @@ -103,63 +98,45 @@ static NodePtr makeNode(const string &t, SymbolTable &st, const string &ns) /** Returns "true" if the field is in the container */ // e.g.: can be false for non-mandatory fields -bool containsField(const Object& m, const string& fieldName) { - Object::const_iterator it = m.find(fieldName); +bool containsField(const Object &m, const string &fieldName) { + auto it = m.find(fieldName); return (it != m.end()); } -const json::Object::const_iterator findField(const Entity& e, - const Object& m, const string& fieldName) -{ - Object::const_iterator it = m.find(fieldName); - if (it == m.end()) { - throw Exception(boost::format("Missing Json field \"%1%\": %2%") % - fieldName % e.toString()); - } else { - return it; - } -} +json::Object::const_iterator findField(const Entity &e, + const Object &m, const string &fieldName); -template <typename T> void ensureType(const Entity &e, const string &name) -{ +template<typename T> +void ensureType(const Entity &e, const string &name) { if (e.type() != json::type_traits<T>::type()) { - throw Exception(boost::format("Json field \"%1%\" is not a %2%: %3%") % - name % json::type_traits<T>::name() % e.toString()); + throw Exception(boost::format("Json field \"%1%\" is not a %2%: %3%") % name % json::type_traits<T>::name() % e.toString()); } } string getStringField(const Entity &e, const Object &m, - const string &fieldName) -{ - Object::const_iterator it = findField(e, m, fieldName); + const string &fieldName) { + auto it = findField(e, m, fieldName); ensureType<string>(it->second, fieldName); return it->second.stringValue(); } -const Array& getArrayField(const Entity& e, const Object& m, - const string& fieldName) -{ - Object::const_iterator it = findField(e, m, fieldName); - ensureType<Array >(it->second, fieldName); - return it->second.arrayValue(); -} +const Array &getArrayField(const Entity &e, const Object &m, + const string &fieldName); -const int64_t getLongField(const Entity& e, const Object& m, - const string& fieldName) -{ - Object::const_iterator it = findField(e, m, fieldName); +int64_t getLongField(const Entity &e, const Object &m, + const string &fieldName) { + auto it = findField(e, m, fieldName); ensureType<int64_t>(it->second, fieldName); return it->second.longValue(); } // Unescape double quotes (") for de-serialization. This method complements the // method NodeImpl::escape() which is used for serialization. -static void unescape(string& s) { +static void unescape(string &s) { boost::replace_all(s, "\\\"", "\""); } -const string getDocField(const Entity& e, const Object& m) -{ +string getDocField(const Entity &e, const Object &m) { string doc = getStringField(e, m, "doc"); unescape(doc); return doc; @@ -169,32 +146,27 @@ struct Field { const string name; const NodePtr schema; const GenericDatum defaultValue; - Field(const string& n, const NodePtr& v, GenericDatum dv) : - name(n), schema(v), defaultValue(dv) { } + Field(string n, NodePtr v, GenericDatum dv) : name(std::move(n)), schema(std::move(v)), defaultValue(std::move(dv)) {} }; -static void assertType(const Entity& e, EntityType et) -{ +static void assertType(const Entity &e, EntityType et) { if (e.type() != et) { throw Exception(boost::format("Unexpected type for default value: " - "Expected %1%, but found %2% in line %3%") % - json::typeToString(et) % json::typeToString(e.type()) % - e.line()); + "Expected %1%, but found %2% in line %3%") + % json::typeToString(et) % json::typeToString(e.type()) % e.line()); } } -static vector<uint8_t> toBin(const string& s) -{ +static vector<uint8_t> toBin(const string &s) { vector<uint8_t> result(s.size()); - if (s.size() > 0) { + if (!s.empty()) { std::copy(s.c_str(), s.c_str() + s.size(), result.data()); } return result; } static GenericDatum makeGenericDatum(NodePtr n, - const Entity& e, const SymbolTable& st) -{ + const Entity &e, const SymbolTable &st) { Type t = n->type(); EntityType dt = e.type(); @@ -203,127 +175,117 @@ static GenericDatum makeGenericDatum(NodePtr n, t = n->type(); } switch (t) { - case AVRO_STRING: - assertType(e, json::etString); - return GenericDatum(e.stringValue()); - case AVRO_BYTES: - assertType(e, json::etString); - return GenericDatum(toBin(e.bytesValue())); - case AVRO_INT: - assertType(e, json::etLong); - return GenericDatum(static_cast<int32_t>(e.longValue())); - case AVRO_LONG: - assertType(e, json::etLong); - return GenericDatum(e.longValue()); - case AVRO_FLOAT: - if (dt == json::etLong) { - return GenericDatum(static_cast<float>(e.longValue())); - } - assertType(e, json::etDouble); - return GenericDatum(static_cast<float>(e.doubleValue())); - case AVRO_DOUBLE: - if (dt == json::etLong) { - return GenericDatum(static_cast<double>(e.longValue())); + case AVRO_STRING: + assertType(e, json::EntityType::String); + return GenericDatum(e.stringValue()); + case AVRO_BYTES: + assertType(e, json::EntityType::String); + return GenericDatum(toBin(e.bytesValue())); + case AVRO_INT: + assertType(e, json::EntityType::Long); + return GenericDatum(static_cast<int32_t>(e.longValue())); + case AVRO_LONG: + assertType(e, json::EntityType::Long); + return GenericDatum(e.longValue()); + case AVRO_FLOAT: + if (dt == json::EntityType::Long) { + return GenericDatum(static_cast<float>(e.longValue())); + } + assertType(e, json::EntityType::Double); + return GenericDatum(static_cast<float>(e.doubleValue())); + case AVRO_DOUBLE: + if (dt == json::EntityType::Long) { + return GenericDatum(static_cast<double>(e.longValue())); + } + assertType(e, json::EntityType::Double); + return GenericDatum(e.doubleValue()); + case AVRO_BOOL: + assertType(e, json::EntityType::Bool); + return GenericDatum(e.boolValue()); + case AVRO_NULL: + assertType(e, json::EntityType::Null); + return GenericDatum(); + case AVRO_RECORD: { + assertType(e, json::EntityType::Obj); + GenericRecord result(n); + const map<string, Entity> &v = e.objectValue(); + for (size_t i = 0; i < n->leaves(); ++i) { + auto it = v.find(n->nameAt(i)); + if (it == v.end()) { + throw Exception(boost::format( + "No value found in default for %1%") + % n->nameAt(i)); + } + result.setFieldAt(i, + makeGenericDatum(n->leafAt(i), it->second, st)); + } + return GenericDatum(n, result); } - assertType(e, json::etDouble); - return GenericDatum(e.doubleValue()); - case AVRO_BOOL: - assertType(e, json::etBool); - return GenericDatum(e.boolValue()); - case AVRO_NULL: - assertType(e, json::etNull); - return GenericDatum(); - case AVRO_RECORD: - { - assertType(e, json::etObject); - GenericRecord result(n); - const map<string, Entity>& v = e.objectValue(); - for (size_t i = 0; i < n->leaves(); ++i) { - map<string, Entity>::const_iterator it = v.find(n->nameAt(i)); - if (it == v.end()) { - throw Exception(boost::format( - "No value found in default for %1%") % n->nameAt(i)); + case AVRO_ENUM: + assertType(e, json::EntityType::String); + return GenericDatum(n, GenericEnum(n, e.stringValue())); + case AVRO_ARRAY: { + assertType(e, json::EntityType::Arr); + GenericArray result(n); + const vector<Entity> &elements = e.arrayValue(); + for (const auto &element : elements) { + result.value().push_back(makeGenericDatum(n->leafAt(0), element, st)); } - result.setFieldAt(i, - makeGenericDatum(n->leafAt(i), it->second, st)); + return GenericDatum(n, result); } - return GenericDatum(n, result); - } - case AVRO_ENUM: - assertType(e, json::etString); - return GenericDatum(n, GenericEnum(n, e.stringValue())); - case AVRO_ARRAY: - { - assertType(e, json::etArray); - GenericArray result(n); - const vector<Entity>& elements = e.arrayValue(); - for (vector<Entity>::const_iterator it = elements.begin(); - it != elements.end(); ++it) { - result.value().push_back(makeGenericDatum(n->leafAt(0), *it, st)); + case AVRO_MAP: { + assertType(e, json::EntityType::Obj); + GenericMap result(n); + const map<string, Entity> &v = e.objectValue(); + for (const auto &it : v) { + result.value().push_back(make_pair(it.first, + makeGenericDatum(n->leafAt(1), it.second, st))); + } + return GenericDatum(n, result); } - return GenericDatum(n, result); - } - case AVRO_MAP: - { - assertType(e, json::etObject); - GenericMap result(n); - const map<string, Entity>& v = e.objectValue(); - for (map<string, Entity>::const_iterator it = v.begin(); - it != v.end(); ++it) { - result.value().push_back(make_pair(it->first, - makeGenericDatum(n->leafAt(1), it->second, st))); + case AVRO_UNION: { + GenericUnion result(n); + result.selectBranch(0); + result.datum() = makeGenericDatum(n->leafAt(0), e, st); + return GenericDatum(n, result); } - return GenericDatum(n, result); - } - case AVRO_UNION: - { - GenericUnion result(n); - result.selectBranch(0); - result.datum() = makeGenericDatum(n->leafAt(0), e, st); - return GenericDatum(n, result); + case AVRO_FIXED: + assertType(e, json::EntityType::String); + return GenericDatum(n, GenericFixed(n, toBin(e.bytesValue()))); + default: throw Exception(boost::format("Unknown type: %1%") % t); } - case AVRO_FIXED: - assertType(e, json::etString); - return GenericDatum(n, GenericFixed(n, toBin(e.bytesValue()))); - default: - throw Exception(boost::format("Unknown type: %1%") % t); - } - return GenericDatum(); } - -static Field makeField(const Entity& e, SymbolTable& st, const string& ns) -{ - const Object& m = e.objectValue(); - const string& n = getStringField(e, m, "name"); - Object::const_iterator it = findField(e, m, "type"); - map<string, Entity>::const_iterator it2 = m.find("default"); +static Field makeField(const Entity &e, SymbolTable &st, const string &ns) { + const Object &m = e.objectValue(); + const string &n = getStringField(e, m, "name"); + auto it = findField(e, m, "type"); + auto it2 = m.find("default"); NodePtr node = makeNode(it->second, st, ns); if (containsField(m, "doc")) { node->setDoc(getDocField(e, m)); } - GenericDatum d = (it2 == m.end()) ? GenericDatum() : - makeGenericDatum(node, it2->second, st); + GenericDatum d = (it2 == m.end()) ? GenericDatum() : makeGenericDatum(node, it2->second, st); return Field(n, node, d); } // Extended makeRecordNode (with doc). -static NodePtr makeRecordNode(const Entity& e, const Name& name, - const string* doc, const Object& m, - SymbolTable& st, const string& ns) { - const Array& v = getArrayField(e, m, "fields"); +static NodePtr makeRecordNode(const Entity &e, const Name &name, + const string *doc, const Object &m, + SymbolTable &st, const string &ns) { + const Array &v = getArrayField(e, m, "fields"); concepts::MultiAttribute<string> fieldNames; concepts::MultiAttribute<NodePtr> fieldValues; vector<GenericDatum> defaultValues; - for (Array::const_iterator it = v.begin(); it != v.end(); ++it) { - Field f = makeField(*it, st, ns); + for (const auto &it : v) { + Field f = makeField(it, st, ns); fieldNames.add(f.name); fieldValues.add(f.schema); defaultValues.push_back(f.defaultValue); } - NodeRecord* node; - if (doc == NULL) { + NodeRecord *node; + if (doc == nullptr) { node = new NodeRecord(asSingleAttribute(name), fieldValues, fieldNames, defaultValues); } else { @@ -333,12 +295,12 @@ static NodePtr makeRecordNode(const Entity& e, const Name& name, return NodePtr(node); } -static LogicalType makeLogicalType(const Entity& e, const Object& m) { +static LogicalType makeLogicalType(const Entity &e, const Object &m) { if (!containsField(m, "logicalType")) { return LogicalType(LogicalType::NONE); } - const std::string& typeField = getStringField(e, m, "logicalType"); + const std::string &typeField = getStringField(e, m, "logicalType"); if (typeField == "decimal") { LogicalType decimalType(LogicalType::DECIMAL); @@ -347,7 +309,7 @@ static LogicalType makeLogicalType(const Entity& e, const Object& m) { if (containsField(m, "scale")) { decimalType.setScale(getLongField(e, m, "scale")); } - } catch (Exception& ex) { + } catch (Exception &ex) { // If any part of the logical type is malformed, per the standard we // must ignore the whole attribute. return LogicalType(LogicalType::NONE); @@ -373,17 +335,15 @@ static LogicalType makeLogicalType(const Entity& e, const Object& m) { return LogicalType(t); } -static NodePtr makeEnumNode(const Entity& e, - const Name& name, const Object& m) -{ - const Array& v = getArrayField(e, m, "symbols"); +static NodePtr makeEnumNode(const Entity &e, + const Name &name, const Object &m) { + const Array &v = getArrayField(e, m, "symbols"); concepts::MultiAttribute<string> symbols; - for (Array::const_iterator it = v.begin(); it != v.end(); ++it) { - if (it->type() != json::etString) { - throw Exception(boost::format("Enum symbol not a string: %1%") % - it->toString()); + for (const auto &it : v) { + if (it.type() != json::EntityType::String) { + throw Exception(boost::format("Enum symbol not a string: %1%") % it.toString()); } - symbols.add(it->stringValue()); + symbols.add(it.stringValue()); } NodePtr node = NodePtr(new NodeEnum(asSingleAttribute(name), symbols)); if (containsField(m, "doc")) { @@ -392,13 +352,11 @@ static NodePtr makeEnumNode(const Entity& e, return node; } -static NodePtr makeFixedNode(const Entity& e, - const Name& name, const Object& m) -{ +static NodePtr makeFixedNode(const Entity &e, + const Name &name, const Object &m) { int v = static_cast<int>(getLongField(e, m, "size")); if (v <= 0) { - throw Exception(boost::format("Size for fixed is not positive: %1%") % - e.toString()); + throw Exception(boost::format("Size for fixed is not positive: %1%") % e.toString()); } NodePtr node = NodePtr(new NodeFixed(asSingleAttribute(name), asSingleAttribute(v))); @@ -408,10 +366,9 @@ static NodePtr makeFixedNode(const Entity& e, return node; } -static NodePtr makeArrayNode(const Entity& e, const Object& m, - SymbolTable& st, const string& ns) -{ - Object::const_iterator it = findField(e, m, "items"); +static NodePtr makeArrayNode(const Entity &e, const Object &m, + SymbolTable &st, const string &ns) { + auto it = findField(e, m, "items"); NodePtr node = NodePtr(new NodeArray( asSingleAttribute(makeNode(it->second, st, ns)))); if (containsField(m, "doc")) { @@ -420,10 +377,9 @@ static NodePtr makeArrayNode(const Entity& e, const Object& m, return node; } -static NodePtr makeMapNode(const Entity& e, const Object& m, - SymbolTable& st, const string& ns) -{ - Object::const_iterator it = findField(e, m, "values"); +static NodePtr makeMapNode(const Entity &e, const Object &m, + SymbolTable &st, const string &ns) { + auto it = findField(e, m, "values"); NodePtr node = NodePtr(new NodeMap( asSingleAttribute(makeNode(it->second, st, ns)))); @@ -433,20 +389,18 @@ static NodePtr makeMapNode(const Entity& e, const Object& m, return node; } -static Name getName(const Entity& e, const Object& m, const string& ns) -{ - const string& name = getStringField(e, m, "name"); +static Name getName(const Entity &e, const Object &m, const string &ns) { + const string &name = getStringField(e, m, "name"); if (isFullName(name)) { return Name(name); } else { - Object::const_iterator it = m.find("namespace"); + auto it = m.find("namespace"); if (it != m.end()) { if (it->second.type() != json::type_traits<string>::type()) { throw Exception(boost::format( - "Json field \"%1%\" is not a %2%: %3%") % - "namespace" % json::type_traits<string>::name() % - it->second.toString()); + "Json field \"%1%\" is not a %2%: %3%") + % "namespace" % json::type_traits<string>::name() % it->second.toString()); } Name result = Name(name, it->second.stringValue()); return result; @@ -455,13 +409,11 @@ static Name getName(const Entity& e, const Object& m, const string& ns) } } -static NodePtr makeNode(const Entity& e, const Object& m, - SymbolTable& st, const string& ns) -{ - const string& type = getStringField(e, m, "type"); +static NodePtr makeNode(const Entity &e, const Object &m, + SymbolTable &st, const string &ns) { + const string &type = getStringField(e, m, "type"); NodePtr result; - if (type == "record" || type == "error" || - type == "enum" || type == "fixed") { + if (type == "record" || type == "error" || type == "enum" || type == "fixed") { Name nm = getName(e, m, ns); if (type == "record" || type == "error") { result = NodePtr(new NodeRecord()); @@ -471,17 +423,15 @@ static NodePtr makeNode(const Entity& e, const Object& m, string doc = getDocField(e, m); NodePtr r = makeRecordNode(e, nm, &doc, m, st, nm.ns()); - (std::dynamic_pointer_cast<NodeRecord>(r))->swap( - *std::dynamic_pointer_cast<NodeRecord>(result)); - } else { // No doc + (std::dynamic_pointer_cast<NodeRecord>(r))->swap(*std::dynamic_pointer_cast<NodeRecord>(result)); + } else { // No doc NodePtr r = - makeRecordNode(e, nm, NULL, m, st, nm.ns()); + makeRecordNode(e, nm, nullptr, m, st, nm.ns()); (std::dynamic_pointer_cast<NodeRecord>(r)) ->swap(*std::dynamic_pointer_cast<NodeRecord>(result)); } } else { - result = (type == "enum") ? makeEnumNode(e, nm, m) : - makeFixedNode(e, nm, m); + result = (type == "enum") ? makeEnumNode(e, nm, m) : makeFixedNode(e, nm, m); st[nm] = result; } } else if (type == "array") { @@ -495,7 +445,7 @@ static NodePtr makeNode(const Entity& e, const Object& m, if (result) { try { result->setLogicalType(makeLogicalType(e, m)); - } catch (Exception& ex) { + } catch (Exception &ex) { // Per the standard we must ignore the logical type attribute if it // is malformed. } @@ -503,72 +453,72 @@ static NodePtr makeNode(const Entity& e, const Object& m, } throw Exception(boost::format("Unknown type definition: %1%") - % e.toString()); + % e.toString()); } -static NodePtr makeNode(const Entity& e, const Array& m, - SymbolTable& st, const string& ns) -{ +static NodePtr makeNode(const Entity &e, const Array &m, + SymbolTable &st, const string &ns) { concepts::MultiAttribute<NodePtr> mm; - for (Array::const_iterator it = m.begin(); it != m.end(); ++it) { - mm.add(makeNode(*it, st, ns)); + for (const auto &it : m) { + mm.add(makeNode(it, st, ns)); } return NodePtr(new NodeUnion(mm)); } -static NodePtr makeNode(const json::Entity& e, SymbolTable& st, const string& ns) -{ +static NodePtr makeNode(const json::Entity &e, SymbolTable &st, const string &ns) { switch (e.type()) { - case json::etString: - return makeNode(e.stringValue(), st, ns); - case json::etObject: - return makeNode(e, e.objectValue(), st, ns); - case json::etArray: - return makeNode(e, e.arrayValue(), st, ns); - default: - throw Exception(boost::format("Invalid Avro type: %1%") % e.toString()); + case json::EntityType::String: return makeNode(e.stringValue(), st, ns); + case json::EntityType::Obj: return makeNode(e, e.objectValue(), st, ns); + case json::EntityType::Arr: return makeNode(e, e.arrayValue(), st, ns); + default: throw Exception(boost::format("Invalid Avro type: %1%") % e.toString()); + } +} +json::Object::const_iterator findField(const Entity &e, const Object &m, const string &fieldName) { + auto it = m.find(fieldName); + if (it == m.end()) { + throw Exception(boost::format("Missing Json field \"%1%\": %2%") % fieldName % e.toString()); + } else { + return it; } } +const Array &getArrayField(const Entity &e, const Object &m, const string &fieldName) { + auto it = findField(e, m, fieldName); + ensureType<Array>(it->second, fieldName); + return it->second.arrayValue(); +} -ValidSchema compileJsonSchemaFromStream(InputStream& is) -{ +ValidSchema compileJsonSchemaFromStream(InputStream &is) { json::Entity e = json::loadEntity(is); SymbolTable st; NodePtr n = makeNode(e, st, ""); return ValidSchema(n); } -AVRO_DECL ValidSchema compileJsonSchemaFromFile(const char* filename) -{ +AVRO_DECL ValidSchema compileJsonSchemaFromFile(const char *filename) { std::unique_ptr<InputStream> s = fileInputStream(filename); return compileJsonSchemaFromStream(*s); } -AVRO_DECL ValidSchema compileJsonSchemaFromMemory(const uint8_t* input, size_t len) -{ +AVRO_DECL ValidSchema compileJsonSchemaFromMemory(const uint8_t *input, size_t len) { return compileJsonSchemaFromStream(*memoryInputStream(input, len)); } -AVRO_DECL ValidSchema compileJsonSchemaFromString(const char* input) -{ - return compileJsonSchemaFromMemory(reinterpret_cast<const uint8_t*>(input), - ::strlen(input)); +AVRO_DECL ValidSchema compileJsonSchemaFromString(const char *input) { + return compileJsonSchemaFromMemory(reinterpret_cast<const uint8_t *>(input), + ::strlen(input)); } -AVRO_DECL ValidSchema compileJsonSchemaFromString(const string& input) -{ +AVRO_DECL ValidSchema compileJsonSchemaFromString(const string &input) { return compileJsonSchemaFromMemory( - reinterpret_cast<const uint8_t*>(input.data()), input.size()); + reinterpret_cast<const uint8_t *>(input.data()), input.size()); } -static ValidSchema compile(std::istream& is) -{ +static ValidSchema compile(std::istream &is) { std::unique_ptr<InputStream> in = istreamInputStream(is); return compileJsonSchemaFromStream(*in); } -void compileJsonSchema(std::istream &is, ValidSchema &schema) -{ +void compileJsonSchema(std::istream &is, ValidSchema &schema) { if (!is.good()) { throw Exception("Input stream is not good"); } @@ -576,8 +526,7 @@ void compileJsonSchema(std::istream &is, ValidSchema &schema) schema = compile(is); } -AVRO_DECL bool compileJsonSchema(std::istream &is, ValidSchema &schema, string &error) -{ +AVRO_DECL bool compileJsonSchema(std::istream &is, ValidSchema &schema, string &error) { try { compileJsonSchema(is, schema); return true; @@ -585,7 +534,6 @@ AVRO_DECL bool compileJsonSchema(std::istream &is, ValidSchema &schema, string & error = e.what(); return false; } - } } // namespace avro diff --git a/contrib/libs/apache/avro/impl/DataFile.cc b/contrib/libs/apache/avro/impl/DataFile.cc index e20e6058276..18fb3f61a68 100644 --- a/contrib/libs/apache/avro/impl/DataFile.cc +++ b/contrib/libs/apache/avro/impl/DataFile.cc @@ -22,23 +22,23 @@ #include <sstream> -#include <boost/random/mersenne_twister.hpp> +#include <boost/crc.hpp> // for boost::crc_32_type #include <boost/iostreams/device/file.hpp> #include <boost/iostreams/filter/gzip.hpp> #include <boost/iostreams/filter/zlib.hpp> -#include <boost/crc.hpp> // for boost::crc_32_type +#include <boost/random/mersenne_twister.hpp> #ifdef SNAPPY_CODEC_AVAILABLE #include <snappy.h> #endif namespace avro { -using std::unique_ptr; -using std::ostringstream; -using std::istringstream; -using std::vector; using std::copy; +using std::istringstream; +using std::ostringstream; using std::string; +using std::unique_ptr; +using std::vector; using std::array; @@ -56,63 +56,59 @@ const size_t minSyncInterval = 32; const size_t maxSyncInterval = 1u << 30; boost::iostreams::zlib_params get_zlib_params() { - boost::iostreams::zlib_params ret; - ret.method = boost::iostreams::zlib::deflated; - ret.noheader = true; - return ret; -} -} - -DataFileWriterBase::DataFileWriterBase(const char* filename, const ValidSchema& schema, size_t syncInterval, - Codec codec) : - filename_(filename), - schema_(schema), - encoderPtr_(binaryEncoder()), - syncInterval_(syncInterval), - codec_(codec), - stream_(fileOutputStream(filename)), - buffer_(memoryOutputStream()), - sync_(makeSync()), - objectCount_(0), - lastSync_(0) -{ + boost::iostreams::zlib_params ret; + ret.method = boost::iostreams::zlib::deflated; + ret.noheader = true; + return ret; +} +} // namespace + +DataFileWriterBase::DataFileWriterBase(const char *filename, const ValidSchema &schema, size_t syncInterval, + Codec codec) : filename_(filename), + schema_(schema), + encoderPtr_(binaryEncoder()), + syncInterval_(syncInterval), + codec_(codec), + stream_(fileOutputStream(filename)), + buffer_(memoryOutputStream()), + sync_(makeSync()), + objectCount_(0), + lastSync_(0) { init(schema, syncInterval, codec); } DataFileWriterBase::DataFileWriterBase(std::unique_ptr<OutputStream> outputStream, - const ValidSchema& schema, size_t syncInterval, Codec codec) : - filename_(), - schema_(schema), - encoderPtr_(binaryEncoder()), - syncInterval_(syncInterval), - codec_(codec), - stream_(std::move(outputStream)), - buffer_(memoryOutputStream()), - sync_(makeSync()), - objectCount_(0), - lastSync_(0) -{ + const ValidSchema &schema, size_t syncInterval, Codec codec) : filename_(), + schema_(schema), + encoderPtr_(binaryEncoder()), + syncInterval_(syncInterval), + codec_(codec), + stream_(std::move(outputStream)), + buffer_(memoryOutputStream()), + sync_(makeSync()), + objectCount_(0), + lastSync_(0) { init(schema, syncInterval, codec); } void DataFileWriterBase::init(const ValidSchema &schema, size_t syncInterval, const Codec &codec) { if (syncInterval < minSyncInterval || syncInterval > maxSyncInterval) { throw Exception(boost::format("Invalid sync interval: %1%. " - "Should be between %2% and %3%") % syncInterval % - minSyncInterval % maxSyncInterval); + "Should be between %2% and %3%") + % syncInterval % minSyncInterval % maxSyncInterval); } setMetadata(AVRO_CODEC_KEY, AVRO_NULL_CODEC); if (codec_ == NULL_CODEC) { - setMetadata(AVRO_CODEC_KEY, AVRO_NULL_CODEC); + setMetadata(AVRO_CODEC_KEY, AVRO_NULL_CODEC); } else if (codec_ == DEFLATE_CODEC) { - setMetadata(AVRO_CODEC_KEY, AVRO_DEFLATE_CODEC); + setMetadata(AVRO_CODEC_KEY, AVRO_DEFLATE_CODEC); #ifdef SNAPPY_CODEC_AVAILABLE } else if (codec_ == SNAPPY_CODEC) { - setMetadata(AVRO_CODEC_KEY, AVRO_SNAPPY_CODEC); + setMetadata(AVRO_CODEC_KEY, AVRO_SNAPPY_CODEC); #endif } else { - throw Exception(boost::format("Unknown codec: %1%") % codec); + throw Exception(boost::format("Unknown codec: %1%") % codec); } setMetadata(AVRO_SCHEMA_KEY, schema.toJson(false)); @@ -122,22 +118,18 @@ void DataFileWriterBase::init(const ValidSchema &schema, size_t syncInterval, co lastSync_ = stream_->byteCount(); } - -DataFileWriterBase::~DataFileWriterBase() -{ - if (stream_.get()) { +DataFileWriterBase::~DataFileWriterBase() { + if (stream_) { close(); } } -void DataFileWriterBase::close() -{ +void DataFileWriterBase::close() { flush(); stream_.reset(); } -void DataFileWriterBase::sync() -{ +void DataFileWriterBase::sync() { encoderPtr_->flush(); encoderPtr_->init(*stream_); @@ -154,16 +146,16 @@ void DataFileWriterBase::sync() boost::iostreams::filtering_ostream os; os.push(boost::iostreams::zlib_compressor(get_zlib_params())); os.push(boost::iostreams::back_inserter(buf)); - const uint8_t* data; + const uint8_t *data; size_t len; std::unique_ptr<InputStream> input = memoryInputStream(*buffer_); while (input->next(&data, &len)) { - boost::iostreams::write(os, reinterpret_cast<const char*>(data), len); + boost::iostreams::write(os, reinterpret_cast<const char *>(data), len); } } // make sure all is flushed std::unique_ptr<InputStream> in = memoryInputStream( - reinterpret_cast<const uint8_t*>(buf.data()), buf.size()); + reinterpret_cast<const uint8_t *>(buf.data()), buf.size()); int64_t byteCount = buf.size(); avro::encode(*encoderPtr_, byteCount); encoderPtr_->flush(); @@ -176,25 +168,25 @@ void DataFileWriterBase::sync() { boost::iostreams::filtering_ostream os; os.push(boost::iostreams::back_inserter(temp)); - const uint8_t* data; + const uint8_t *data; size_t len; std::unique_ptr<InputStream> input = memoryInputStream(*buffer_); while (input->next(&data, &len)) { - boost::iostreams::write(os, reinterpret_cast<const char*>(data), - len); + boost::iostreams::write(os, reinterpret_cast<const char *>(data), + len); } } // make sure all is flushed - crc.process_bytes(reinterpret_cast<const char*>(temp.data()), - temp.size()); + crc.process_bytes(reinterpret_cast<const char *>(temp.data()), + temp.size()); // For Snappy, add the CRC32 checksum int32_t checksum = crc(); // Now compress size_t compressed_size = snappy::Compress( - reinterpret_cast<const char*>(temp.data()), temp.size(), - &compressed); + reinterpret_cast<const char *>(temp.data()), temp.size(), + &compressed); temp.clear(); { boost::iostreams::filtering_ostream os; @@ -206,7 +198,7 @@ void DataFileWriterBase::sync() temp.push_back((checksum >> 8) & 0xFF); temp.push_back(checksum & 0xFF); std::unique_ptr<InputStream> in = memoryInputStream( - reinterpret_cast<const uint8_t*>(temp.data()), temp.size()); + reinterpret_cast<const uint8_t *>(temp.data()), temp.size()); int64_t byteCount = temp.size(); avro::encode(*encoderPtr_, byteCount); encoderPtr_->flush(); @@ -225,40 +217,33 @@ void DataFileWriterBase::sync() objectCount_ = 0; } -void DataFileWriterBase::syncIfNeeded() -{ +void DataFileWriterBase::syncIfNeeded() { encoderPtr_->flush(); if (buffer_->byteCount() >= syncInterval_) { sync(); } } -uint64_t DataFileWriterBase::getCurrentBlockStart() -{ +uint64_t DataFileWriterBase::getCurrentBlockStart() const { return lastSync_; } -void DataFileWriterBase::flush() -{ +void DataFileWriterBase::flush() { sync(); } -boost::mt19937 random(static_cast<uint32_t>(time(0))); +boost::mt19937 random(static_cast<uint32_t>(time(nullptr))); -DataFileSync DataFileWriterBase::makeSync() -{ +DataFileSync DataFileWriterBase::makeSync() { DataFileSync sync; - for (size_t i = 0; i < sync.size(); ++i) { - sync[i] = random(); - } + std::generate(sync.begin(), sync.end(), random); return sync; } typedef array<uint8_t, 4> Magic; -static Magic magic = { { 'O', 'b', 'j', '\x01' } }; +static Magic magic = {{'O', 'b', 'j', '\x01'}}; -void DataFileWriterBase::writeHeader() -{ +void DataFileWriterBase::writeHeader() { encoderPtr_->init(*stream_); avro::encode(*encoderPtr_, magic); avro::encode(*encoderPtr_, metadata_); @@ -266,69 +251,56 @@ void DataFileWriterBase::writeHeader() encoderPtr_->flush(); } -void DataFileWriterBase::setMetadata(const string& key, const string& value) -{ +void DataFileWriterBase::setMetadata(const string &key, const string &value) { vector<uint8_t> v(value.size()); copy(value.begin(), value.end(), v.begin()); metadata_[key] = v; } -DataFileReaderBase::DataFileReaderBase(const char* filename) : - filename_(filename), stream_(fileSeekableInputStream(filename)), - decoder_(binaryDecoder()), objectCount_(0), eof_(false), blockStart_(-1), - blockEnd_(-1) -{ +DataFileReaderBase::DataFileReaderBase(const char *filename) : filename_(filename), codec_(NULL_CODEC), stream_(fileSeekableInputStream(filename)), + decoder_(binaryDecoder()), objectCount_(0), eof_(false), blockStart_(-1), + blockEnd_(-1) { readHeader(); } -DataFileReaderBase::DataFileReaderBase(std::unique_ptr<InputStream> inputStream) : - filename_(""), stream_(std::move(inputStream)), - decoder_(binaryDecoder()), objectCount_(0), eof_(false) -{ +DataFileReaderBase::DataFileReaderBase(std::unique_ptr<InputStream> inputStream) : codec_(NULL_CODEC), stream_(std::move(inputStream)), + decoder_(binaryDecoder()), objectCount_(0), eof_(false) { readHeader(); } -void DataFileReaderBase::init() -{ +void DataFileReaderBase::init() { readerSchema_ = dataSchema_; - dataDecoder_ = binaryDecoder(); + dataDecoder_ = binaryDecoder(); readDataBlock(); } -void DataFileReaderBase::init(const ValidSchema& readerSchema) -{ +void DataFileReaderBase::init(const ValidSchema &readerSchema) { readerSchema_ = readerSchema; - dataDecoder_ = (readerSchema_.toJson(true) != dataSchema_.toJson(true)) ? - resolvingDecoder(dataSchema_, readerSchema_, binaryDecoder()) : - binaryDecoder(); + dataDecoder_ = (readerSchema_.toJson(true) != dataSchema_.toJson(true)) ? resolvingDecoder(dataSchema_, readerSchema_, binaryDecoder()) : binaryDecoder(); readDataBlock(); } -static void drain(InputStream& in) -{ - const uint8_t *p = 0; +static void drain(InputStream &in) { + const uint8_t *p = nullptr; size_t n = 0; - while (in.next(&p, &n)); + while (in.next(&p, &n)) + ; } -char hex(unsigned int x) -{ - return x + (x < 10 ? '0' : ('a' - 10)); +char hex(unsigned int x) { + return static_cast<char>(x + (x < 10 ? '0' : ('a' - 10))); } -std::ostream& operator << (std::ostream& os, const DataFileSync& s) -{ - for (size_t i = 0; i < s.size(); ++i) { - os << hex(s[i] / 16) << hex(s[i] % 16) << ' '; +std::ostream &operator<<(std::ostream &os, const DataFileSync &s) { + for (uint8_t i : s) { + os << hex(i / 16) << hex(i % 16) << ' '; } os << std::endl; return os; } - -bool DataFileReaderBase::hasMore() -{ - for (; ;) { +bool DataFileReaderBase::hasMore() { + for (;;) { if (eof_) { return false; } else if (objectCount_ != 0) { @@ -348,10 +320,10 @@ bool DataFileReaderBase::hasMore() } class BoundedInputStream : public InputStream { - InputStream& in_; + InputStream &in_; size_t limit_; - bool next(const uint8_t** data, size_t* len) { + bool next(const uint8_t **data, size_t *len) final { if (limit_ != 0 && in_.next(data, len)) { if (*len > limit_) { in_.backup(*len - limit_); @@ -363,12 +335,12 @@ class BoundedInputStream : public InputStream { return false; } - void backup(size_t len) { + void backup(size_t len) final { in_.backup(len); limit_ += len; } - void skip(size_t len) { + void skip(size_t len) final { if (len > limit_) { len = limit_; } @@ -376,27 +348,24 @@ class BoundedInputStream : public InputStream { limit_ -= len; } - size_t byteCount() const { + size_t byteCount() const final { return in_.byteCount(); } public: - BoundedInputStream(InputStream& in, size_t limit) : - in_(in), limit_(limit) { } + BoundedInputStream(InputStream &in, size_t limit) : in_(in), limit_(limit) {} }; -unique_ptr<InputStream> boundedInputStream(InputStream& in, size_t limit) -{ +unique_ptr<InputStream> boundedInputStream(InputStream &in, size_t limit) { return unique_ptr<InputStream>(new BoundedInputStream(in, limit)); } -void DataFileReaderBase::readDataBlock() -{ +void DataFileReaderBase::readDataBlock() { decoder_->init(*stream_); blockStart_ = stream_->byteCount(); - const uint8_t* p = 0; + const uint8_t *p = nullptr; size_t n = 0; - if (! stream_->next(&p, &n)) { + if (!stream_->next(&p, &n)) { eof_ = true; return; } @@ -417,7 +386,7 @@ void DataFileReaderBase::readDataBlock() uint32_t checksum = 0; compressed_.clear(); uncompressed.clear(); - const uint8_t* data; + const uint8_t *data; size_t len; while (st->next(&data, &len)) { compressed_.insert(compressed_.end(), data, data + len); @@ -429,20 +398,22 @@ void DataFileReaderBase::readDataBlock() int b4 = compressed_[len - 1] & 0xFF; checksum = (b1 << 24) + (b2 << 16) + (b3 << 8) + (b4); - if (!snappy::Uncompress(reinterpret_cast<const char*>(compressed_.data()), - len - 4, &uncompressed)) { + if (!snappy::Uncompress(reinterpret_cast<const char *>(compressed_.data()), + len - 4, &uncompressed)) { throw Exception( - "Snappy Compression reported an error when decompressing"); + "Snappy Compression reported an error when decompressing"); } crc.process_bytes(uncompressed.c_str(), uncompressed.size()); uint32_t c = crc(); if (checksum != c) { - throw Exception(boost::format("Checksum did not match for Snappy compression: Expected: %1%, computed: %2%") % checksum % c); + throw Exception( + boost::format("Checksum did not match for Snappy compression: Expected: %1%, computed: %2%") % checksum + % c); } os_.reset(new boost::iostreams::filtering_istream()); os_->push( - boost::iostreams::basic_array_source<char>(uncompressed.c_str(), - uncompressed.size())); + boost::iostreams::basic_array_source<char>(uncompressed.c_str(), + uncompressed.size())); std::unique_ptr<InputStream> in = istreamInputStream(*os_); dataDecoder_->init(*in); @@ -450,16 +421,15 @@ void DataFileReaderBase::readDataBlock() #endif } else { compressed_.clear(); - const uint8_t* data; + const uint8_t *data; size_t len; while (st->next(&data, &len)) { compressed_.insert(compressed_.end(), data, data + len); } - // boost::iostreams::write(os, reinterpret_cast<const char*>(data), len); os_.reset(new boost::iostreams::filtering_istream()); os_->push(boost::iostreams::zlib_decompressor(get_zlib_params())); os_->push(boost::iostreams::basic_array_source<char>( - compressed_.data(), compressed_.size())); + compressed_.data(), compressed_.size())); std::unique_ptr<InputStream> in = nonSeekableIstreamInputStream(*os_); dataDecoder_->init(*in); @@ -467,34 +437,30 @@ void DataFileReaderBase::readDataBlock() } } -void DataFileReaderBase::close() -{ +void DataFileReaderBase::close() { } -static string toString(const vector<uint8_t>& v) -{ +static string toString(const vector<uint8_t> &v) { string result; result.resize(v.size()); copy(v.begin(), v.end(), result.begin()); return result; } -static ValidSchema makeSchema(const vector<uint8_t>& v) -{ +static ValidSchema makeSchema(const vector<uint8_t> &v) { istringstream iss(toString(v)); ValidSchema vs; compileJsonSchema(iss, vs); return ValidSchema(vs); } -void DataFileReaderBase::readHeader() -{ +void DataFileReaderBase::readHeader() { decoder_->init(*stream_); Magic m; avro::decode(*decoder_, m); if (magic != m) { throw Exception("Invalid data file. Magic does not match: " - + filename_); + + filename_); } avro::decode(*decoder_, metadata_); Metadata::const_iterator it = metadata_.find(AVRO_SCHEMA_KEY); @@ -503,7 +469,7 @@ void DataFileReaderBase::readHeader() } dataSchema_ = makeSchema(it->second); - if (! readerSchema_.root()) { + if (!readerSchema_.root()) { readerSchema_ = dataSchema(); } @@ -512,7 +478,7 @@ void DataFileReaderBase::readHeader() codec_ = DEFLATE_CODEC; #ifdef SNAPPY_CODEC_AVAILABLE } else if (it != metadata_.end() - && toString(it->second) == AVRO_SNAPPY_CODEC) { + && toString(it->second) == AVRO_SNAPPY_CODEC) { codec_ = SNAPPY_CODEC; #endif } else { @@ -527,9 +493,8 @@ void DataFileReaderBase::readHeader() blockStart_ = stream_->byteCount(); } -void DataFileReaderBase::doSeek(int64_t position) -{ - if (SeekableInputStream *ss = dynamic_cast<SeekableInputStream *>(stream_.get())) { +void DataFileReaderBase::doSeek(int64_t position) { + if (auto *ss = dynamic_cast<SeekableInputStream *>(stream_.get())) { if (!eof_) { dataDecoder_->init(*dataStream_); drain(*dataStream_); @@ -542,17 +507,15 @@ void DataFileReaderBase::doSeek(int64_t position) } } -void DataFileReaderBase::seek(int64_t position) -{ +void DataFileReaderBase::seek(int64_t position) { doSeek(position); readDataBlock(); } -void DataFileReaderBase::sync(int64_t position) -{ +void DataFileReaderBase::sync(int64_t position) { doSeek(position); DataFileSync sync_buffer; - const uint8_t *p = 0; + const uint8_t *p = nullptr; size_t n = 0; size_t i = 0; while (i < SyncSize) { @@ -590,11 +553,11 @@ void DataFileReaderBase::sync(int64_t position) } bool DataFileReaderBase::pastSync(int64_t position) { - return !hasMore() || blockStart_ >= position + SyncSize; + return !hasMore() || blockStart_ >= position + SyncSize; } -int64_t DataFileReaderBase::previousSync() { - return blockStart_; +int64_t DataFileReaderBase::previousSync() const { + return blockStart_; } -} // namespace avro +} // namespace avro diff --git a/contrib/libs/apache/avro/impl/FileStream.cc b/contrib/libs/apache/avro/impl/FileStream.cc index ed601b4c6fa..749fd835abd 100644 --- a/contrib/libs/apache/avro/impl/FileStream.cc +++ b/contrib/libs/apache/avro/impl/FileStream.cc @@ -16,12 +16,12 @@ * limitations under the License. */ -#include <fstream> #include "Stream.hh" +#include <fstream> #ifndef _WIN32 -#include "unistd.h" #include "fcntl.h" -#include "errno.h" +#include "unistd.h" +#include <cerrno> #ifndef O_BINARY #define O_BINARY 0 @@ -34,24 +34,22 @@ #endif #endif -using std::unique_ptr; using std::istream; using std::ostream; +using std::unique_ptr; namespace avro { namespace { struct BufferCopyIn { - virtual ~BufferCopyIn() { } + virtual ~BufferCopyIn() = default; virtual void seek(size_t len) = 0; - virtual bool read(uint8_t* b, size_t toRead, size_t& actual) = 0; - + virtual bool read(uint8_t *b, size_t toRead, size_t &actual) = 0; }; struct FileBufferCopyIn : public BufferCopyIn { #ifdef _WIN32 HANDLE h_; - FileBufferCopyIn(const char* filename) : - h_(::CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) { + FileBufferCopyIn(const char *filename) : h_(::CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) { if (h_ == INVALID_HANDLE_VALUE) { throw Exception(boost::format("Cannot open file: %1%") % ::GetLastError()); } @@ -67,9 +65,9 @@ struct FileBufferCopyIn : public BufferCopyIn { } } - bool read(uint8_t* b, size_t toRead, size_t& actual) { + bool read(uint8_t *b, size_t toRead, size_t &actual) { DWORD dw = 0; - if (! ::ReadFile(h_, b, toRead, &dw, NULL)) { + if (!::ReadFile(h_, b, toRead, &dw, NULL)) { throw Exception(boost::format("Cannot read file: %1%") % ::GetLastError()); } actual = static_cast<size_t>(dw); @@ -78,27 +76,24 @@ struct FileBufferCopyIn : public BufferCopyIn { #else const int fd_; - FileBufferCopyIn(const char* filename) : - fd_(open(filename, O_RDONLY | O_BINARY)) { + explicit FileBufferCopyIn(const char *filename) : fd_(open(filename, O_RDONLY | O_BINARY)) { if (fd_ < 0) { - throw Exception(boost::format("Cannot open file: %1%") % - ::strerror(errno)); + throw Exception(boost::format("Cannot open file: %1%") % ::strerror(errno)); } } - ~FileBufferCopyIn() { + ~FileBufferCopyIn() override { ::close(fd_); } - void seek(size_t len) { + void seek(size_t len) final { off_t r = ::lseek(fd_, len, SEEK_CUR); if (r == static_cast<off_t>(-1)) { - throw Exception(boost::format("Cannot skip file: %1%") % - strerror(errno)); + throw Exception(boost::format("Cannot skip file: %1%") % strerror(errno)); } } - bool read(uint8_t* b, size_t toRead, size_t& actual) { + bool read(uint8_t *b, size_t toRead, size_t &actual) final { int n = ::read(fd_, b, toRead); if (n > 0) { actual = n; @@ -107,45 +102,43 @@ struct FileBufferCopyIn : public BufferCopyIn { return false; } #endif - }; struct IStreamBufferCopyIn : public BufferCopyIn { - istream& is_; + istream &is_; - IStreamBufferCopyIn(istream& is) : is_(is) { + explicit IStreamBufferCopyIn(istream &is) : is_(is) { } - void seek(size_t len) { - if (! is_.seekg(len, std::ios_base::cur)) { + void seek(size_t len) override { + if (!is_.seekg(len, std::ios_base::cur)) { throw Exception("Cannot skip stream"); } } - bool read(uint8_t* b, size_t toRead, size_t& actual) { - is_.read(reinterpret_cast<char*>(b), toRead); + bool read(uint8_t *b, size_t toRead, size_t &actual) override { + is_.read(reinterpret_cast<char *>(b), toRead); if (is_.bad()) { return false; } actual = static_cast<size_t>(is_.gcount()); - return (! is_.eof() || actual != 0); + return (!is_.eof() || actual != 0); } - }; struct NonSeekableIStreamBufferCopyIn : public IStreamBufferCopyIn { - NonSeekableIStreamBufferCopyIn(istream& is) : IStreamBufferCopyIn(is) { } + explicit NonSeekableIStreamBufferCopyIn(istream &is) : IStreamBufferCopyIn(is) {} - void seek(size_t len) { + void seek(size_t len) final { const size_t bufSize = 4096; uint8_t buf[bufSize]; while (len > 0) { size_t n = std::min(len, bufSize); - is_.read(reinterpret_cast<char*>(buf), n); + is_.read(reinterpret_cast<char *>(buf), n); if (is_.bad()) { throw Exception("Cannot skip stream"); } - size_t actual = static_cast<size_t>(is_.gcount()); + auto actual = static_cast<size_t>(is_.gcount()); if (is_.eof() && actual == 0) { throw Exception("Cannot skip stream"); } @@ -154,18 +147,18 @@ struct NonSeekableIStreamBufferCopyIn : public IStreamBufferCopyIn { } }; -} +} // namespace class BufferCopyInInputStream : public SeekableInputStream { const size_t bufferSize_; - uint8_t* const buffer_; + uint8_t *const buffer_; unique_ptr<BufferCopyIn> in_; size_t byteCount_; - uint8_t* next_; + uint8_t *next_; size_t available_; - bool next(const uint8_t** data, size_t *size) { - if (available_ == 0 && ! fill()) { + bool next(const uint8_t **data, size_t *size) final { + if (available_ == 0 && !fill()) { return false; } *data = next_; @@ -176,13 +169,13 @@ class BufferCopyInInputStream : public SeekableInputStream { return true; } - void backup(size_t len) { + void backup(size_t len) final { next_ -= len; available_ += len; byteCount_ -= len; } - void skip(size_t len) { + void skip(size_t len) final { while (len > 0) { if (available_ == 0) { in_->seek(len); @@ -197,7 +190,7 @@ class BufferCopyInInputStream : public SeekableInputStream { } } - size_t byteCount() const { return byteCount_; } + size_t byteCount() const final { return byteCount_; } bool fill() { size_t n = 0; @@ -209,39 +202,37 @@ class BufferCopyInInputStream : public SeekableInputStream { return false; } - void seek(int64_t position) { - // BufferCopyIn::seek is relative to byteCount_, whereas position is - // absolute. - in_->seek(position - byteCount_ - available_); - byteCount_ = position; - available_ = 0; + void seek(int64_t position) final { + // BufferCopyIn::seek is relative to byteCount_, whereas position is + // absolute. + in_->seek(position - byteCount_ - available_); + byteCount_ = position; + available_ = 0; } public: - BufferCopyInInputStream(unique_ptr<BufferCopyIn> in, size_t bufferSize) : - bufferSize_(bufferSize), - buffer_(new uint8_t[bufferSize]), - in_(std::move(in)), - byteCount_(0), - next_(buffer_), - available_(0) { } - - ~BufferCopyInInputStream() { + BufferCopyInInputStream(unique_ptr<BufferCopyIn> in, size_t bufferSize) : bufferSize_(bufferSize), + buffer_(new uint8_t[bufferSize]), + in_(std::move(in)), + byteCount_(0), + next_(buffer_), + available_(0) {} + + ~BufferCopyInInputStream() override { delete[] buffer_; } }; namespace { struct BufferCopyOut { - virtual ~BufferCopyOut() { } - virtual void write(const uint8_t* b, size_t len) = 0; + virtual ~BufferCopyOut() = default; + virtual void write(const uint8_t *b, size_t len) = 0; }; struct FileBufferCopyOut : public BufferCopyOut { #ifdef _WIN32 HANDLE h_; - FileBufferCopyOut(const char* filename) : - h_(::CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) { + FileBufferCopyOut(const char *filename) : h_(::CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) { if (h_ == INVALID_HANDLE_VALUE) { throw Exception(boost::format("Cannot open file: %1%") % ::GetLastError()); } @@ -251,10 +242,10 @@ struct FileBufferCopyOut : public BufferCopyOut { ::CloseHandle(h_); } - void write(const uint8_t* b, size_t len) { + void write(const uint8_t *b, size_t len) { while (len > 0) { DWORD dw = 0; - if (! ::WriteFile(h_, b, len, &dw, NULL)) { + if (!::WriteFile(h_, b, len, &dw, NULL)) { throw Exception(boost::format("Cannot read file: %1%") % ::GetLastError()); } b += dw; @@ -264,53 +255,48 @@ struct FileBufferCopyOut : public BufferCopyOut { #else const int fd_; - FileBufferCopyOut(const char* filename) : - fd_(::open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644)) { + explicit FileBufferCopyOut(const char *filename) : fd_(::open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644)) { if (fd_ < 0) { - throw Exception(boost::format("Cannot open file: %1%") % - ::strerror(errno)); + throw Exception(boost::format("Cannot open file: %1%") % ::strerror(errno)); } } - ~FileBufferCopyOut() { + ~FileBufferCopyOut() override { ::close(fd_); } - void write(const uint8_t* b, size_t len) { + void write(const uint8_t *b, size_t len) final { if (::write(fd_, b, len) < 0) { - throw Exception(boost::format("Cannot write file: %1%") % - ::strerror(errno)); + throw Exception(boost::format("Cannot write file: %1%") % ::strerror(errno)); } } #endif - }; struct OStreamBufferCopyOut : public BufferCopyOut { - ostream& os_; + ostream &os_; - OStreamBufferCopyOut(ostream& os) : os_(os) { + explicit OStreamBufferCopyOut(ostream &os) : os_(os) { } - void write(const uint8_t* b, size_t len) { - os_.write(reinterpret_cast<const char*>(b), len); + void write(const uint8_t *b, size_t len) final { + os_.write(reinterpret_cast<const char *>(b), len); } - }; -} +} // namespace class BufferCopyOutputStream : public OutputStream { size_t bufferSize_; - uint8_t* const buffer_; + uint8_t *const buffer_; unique_ptr<BufferCopyOut> out_; - uint8_t* next_; + uint8_t *next_; size_t available_; size_t byteCount_; - // Invaiant: byteCount_ == byteswritten + bufferSize_ - available_; - bool next(uint8_t** data, size_t* len) { + // Invariant: byteCount_ == bytesWritten + bufferSize_ - available_; + bool next(uint8_t **data, size_t *len) final { if (available_ == 0) { flush(); } @@ -322,76 +308,68 @@ class BufferCopyOutputStream : public OutputStream { return true; } - void backup(size_t len) { + void backup(size_t len) final { available_ += len; next_ -= len; byteCount_ -= len; } - uint64_t byteCount() const { + uint64_t byteCount() const final { return byteCount_; } - void flush() { + void flush() final { out_->write(buffer_, bufferSize_ - available_); next_ = buffer_; available_ = bufferSize_; } public: - BufferCopyOutputStream(unique_ptr<BufferCopyOut> out, size_t bufferSize) : - bufferSize_(bufferSize), - buffer_(new uint8_t[bufferSize]), - out_(std::move(out)), - next_(buffer_), - available_(bufferSize_), byteCount_(0) { } - - ~BufferCopyOutputStream() { + BufferCopyOutputStream(unique_ptr<BufferCopyOut> out, size_t bufferSize) : bufferSize_(bufferSize), + buffer_(new uint8_t[bufferSize]), + out_(std::move(out)), + next_(buffer_), + available_(bufferSize_), byteCount_(0) {} + + ~BufferCopyOutputStream() override { delete[] buffer_; } }; -unique_ptr<InputStream> fileInputStream(const char* filename, - size_t bufferSize) -{ +unique_ptr<InputStream> fileInputStream(const char *filename, + size_t bufferSize) { unique_ptr<BufferCopyIn> in(new FileBufferCopyIn(filename)); - return unique_ptr<InputStream>( new BufferCopyInInputStream(std::move(in), bufferSize)); + return unique_ptr<InputStream>(new BufferCopyInInputStream(std::move(in), bufferSize)); } -unique_ptr<SeekableInputStream> fileSeekableInputStream(const char* filename, - size_t bufferSize) -{ +unique_ptr<SeekableInputStream> fileSeekableInputStream(const char *filename, + size_t bufferSize) { unique_ptr<BufferCopyIn> in(new FileBufferCopyIn(filename)); - return unique_ptr<SeekableInputStream>( new BufferCopyInInputStream(std::move(in), - bufferSize)); + return unique_ptr<SeekableInputStream>(new BufferCopyInInputStream(std::move(in), + bufferSize)); } -unique_ptr<InputStream> istreamInputStream(istream& is, size_t bufferSize) -{ +unique_ptr<InputStream> istreamInputStream(istream &is, size_t bufferSize) { unique_ptr<BufferCopyIn> in(new IStreamBufferCopyIn(is)); - return unique_ptr<InputStream>( new BufferCopyInInputStream(std::move(in), bufferSize)); + return unique_ptr<InputStream>(new BufferCopyInInputStream(std::move(in), bufferSize)); } unique_ptr<InputStream> nonSeekableIstreamInputStream( - istream& is, size_t bufferSize) -{ + istream &is, size_t bufferSize) { unique_ptr<BufferCopyIn> in(new NonSeekableIStreamBufferCopyIn(is)); - return unique_ptr<InputStream>( new BufferCopyInInputStream(std::move(in), bufferSize)); + return unique_ptr<InputStream>(new BufferCopyInInputStream(std::move(in), bufferSize)); } -unique_ptr<OutputStream> fileOutputStream(const char* filename, - size_t bufferSize) -{ +unique_ptr<OutputStream> fileOutputStream(const char *filename, + size_t bufferSize) { unique_ptr<BufferCopyOut> out(new FileBufferCopyOut(filename)); return unique_ptr<OutputStream>(new BufferCopyOutputStream(std::move(out), bufferSize)); } -unique_ptr<OutputStream> ostreamOutputStream(ostream& os, - size_t bufferSize) -{ +unique_ptr<OutputStream> ostreamOutputStream(ostream &os, + size_t bufferSize) { unique_ptr<BufferCopyOut> out(new OStreamBufferCopyOut(os)); return unique_ptr<OutputStream>(new BufferCopyOutputStream(std::move(out), bufferSize)); } - -} // namespace avro +} // namespace avro diff --git a/contrib/libs/apache/avro/impl/Generic.cc b/contrib/libs/apache/avro/impl/Generic.cc index 8efb7e9ac46..6e0436ae344 100644 --- a/contrib/libs/apache/avro/impl/Generic.cc +++ b/contrib/libs/apache/avro/impl/Generic.cc @@ -17,86 +17,76 @@ */ #include "Generic.hh" -#include <sstream> +#include <utility> namespace avro { +using std::ostringstream; using std::string; using std::vector; -using std::ostringstream; typedef vector<uint8_t> bytes; -void GenericContainer::assertType(const NodePtr& schema, Type type) { +void GenericContainer::assertType(const NodePtr &schema, Type type) { if (schema->type() != type) { - throw Exception(boost::format("Schema type %1 expected %2") % - toString(schema->type()) % toString(type)); + throw Exception(boost::format("Schema type %1 expected %2") % toString(schema->type()) % toString(type)); } } -GenericReader::GenericReader(const ValidSchema& s, const DecoderPtr& decoder) : - schema_(s), isResolving_(dynamic_cast<ResolvingDecoder*>(&(*decoder)) != 0), - decoder_(decoder) -{ +GenericReader::GenericReader(ValidSchema s, const DecoderPtr &decoder) : schema_(std::move(s)), isResolving_(dynamic_cast<ResolvingDecoder *>(&(*decoder)) != nullptr), + decoder_(decoder) { } -GenericReader::GenericReader(const ValidSchema& writerSchema, - const ValidSchema& readerSchema, const DecoderPtr& decoder) : - schema_(readerSchema), - isResolving_(true), - decoder_(resolvingDecoder(writerSchema, readerSchema, decoder)) -{ +GenericReader::GenericReader(const ValidSchema &writerSchema, + const ValidSchema &readerSchema, const DecoderPtr &decoder) : schema_(readerSchema), + isResolving_(true), + decoder_(resolvingDecoder(writerSchema, readerSchema, decoder)) { } -void GenericReader::read(GenericDatum& datum) const -{ +void GenericReader::read(GenericDatum &datum) const { datum = GenericDatum(schema_.root()); read(datum, *decoder_, isResolving_); } -void GenericReader::read(GenericDatum& datum, Decoder& d, bool isResolving) -{ +void GenericReader::read(GenericDatum &datum, Decoder &d, bool isResolving) { if (datum.isUnion()) { datum.selectBranch(d.decodeUnionIndex()); } switch (datum.type()) { - case AVRO_NULL: - d.decodeNull(); - break; - case AVRO_BOOL: - datum.value<bool>() = d.decodeBool(); - break; - case AVRO_INT: - datum.value<int32_t>() = d.decodeInt(); - break; - case AVRO_LONG: - datum.value<int64_t>() = d.decodeLong(); - break; - case AVRO_FLOAT: - datum.value<float>() = d.decodeFloat(); - break; - case AVRO_DOUBLE: - datum.value<double>() = d.decodeDouble(); - break; - case AVRO_STRING: - d.decodeString(datum.value<string>()); - break; - case AVRO_BYTES: - d.decodeBytes(datum.value<bytes>()); - break; - case AVRO_FIXED: - { - GenericFixed& f = datum.value<GenericFixed>(); + case AVRO_NULL: + d.decodeNull(); + break; + case AVRO_BOOL: + datum.value<bool>() = d.decodeBool(); + break; + case AVRO_INT: + datum.value<int32_t>() = d.decodeInt(); + break; + case AVRO_LONG: + datum.value<int64_t>() = d.decodeLong(); + break; + case AVRO_FLOAT: + datum.value<float>() = d.decodeFloat(); + break; + case AVRO_DOUBLE: + datum.value<double>() = d.decodeDouble(); + break; + case AVRO_STRING: + d.decodeString(datum.value<string>()); + break; + case AVRO_BYTES: + d.decodeBytes(datum.value<bytes>()); + break; + case AVRO_FIXED: { + auto &f = datum.value<GenericFixed>(); d.decodeFixed(f.schema()->fixedSize(), f.value()); - } - break; - case AVRO_RECORD: - { - GenericRecord& r = datum.value<GenericRecord>(); + } break; + case AVRO_RECORD: { + auto &r = datum.value<GenericRecord>(); size_t c = r.schema()->leaves(); if (isResolving) { std::vector<size_t> fo = - static_cast<ResolvingDecoder&>(d).fieldOrder(); + static_cast<ResolvingDecoder &>(d).fieldOrder(); for (size_t i = 0; i < c; ++i) { read(r.fieldAt(fo[i]), d, isResolving); } @@ -105,16 +95,14 @@ void GenericReader::read(GenericDatum& datum, Decoder& d, bool isResolving) read(r.fieldAt(i), d, isResolving); } } - } - break; - case AVRO_ENUM: - datum.value<GenericEnum>().set(d.decodeEnum()); - break; - case AVRO_ARRAY: - { - GenericArray& v = datum.value<GenericArray>(); - vector<GenericDatum>& r = v.value(); - const NodePtr& nn = v.schema()->leafAt(0); + } break; + case AVRO_ENUM: + datum.value<GenericEnum>().set(d.decodeEnum()); + break; + case AVRO_ARRAY: { + auto &v = datum.value<GenericArray>(); + vector<GenericDatum> &r = v.value(); + const NodePtr &nn = v.schema()->leafAt(0); r.resize(0); size_t start = 0; for (size_t m = d.arrayStart(); m != 0; m = d.arrayNext()) { @@ -124,13 +112,11 @@ void GenericReader::read(GenericDatum& datum, Decoder& d, bool isResolving) read(r[start], d, isResolving); } } - } - break; - case AVRO_MAP: - { - GenericMap& v = datum.value<GenericMap>(); - GenericMap::Value& r = v.value(); - const NodePtr& nn = v.schema()->leafAt(1); + } break; + case AVRO_MAP: { + auto &v = datum.value<GenericMap>(); + GenericMap::Value &r = v.value(); + const NodePtr &nn = v.schema()->leafAt(1); r.resize(0); size_t start = 0; for (size_t m = d.mapStart(); m != 0; m = d.mapNext()) { @@ -141,120 +127,102 @@ void GenericReader::read(GenericDatum& datum, Decoder& d, bool isResolving) read(r[start].second, d, isResolving); } } - } - break; - default: - throw Exception(boost::format("Unknown schema type %1%") % - toString(datum.type())); + } break; + default: + throw Exception(boost::format("Unknown schema type %1%") % toString(datum.type())); } } -void GenericReader::read(Decoder& d, GenericDatum& g, const ValidSchema& s) -{ +void GenericReader::read(Decoder &d, GenericDatum &g, const ValidSchema &s) { g = GenericDatum(s); read(d, g); } -void GenericReader::read(Decoder& d, GenericDatum& g) -{ - read(g, d, dynamic_cast<ResolvingDecoder*>(&d) != 0); +void GenericReader::read(Decoder &d, GenericDatum &g) { + read(g, d, dynamic_cast<ResolvingDecoder *>(&d) != nullptr); } -GenericWriter::GenericWriter(const ValidSchema& s, const EncoderPtr& encoder) : - schema_(s), encoder_(encoder) -{ +GenericWriter::GenericWriter(ValidSchema s, EncoderPtr encoder) : schema_(std::move(s)), encoder_(std::move(encoder)) { } -void GenericWriter::write(const GenericDatum& datum) const -{ +void GenericWriter::write(const GenericDatum &datum) const { write(datum, *encoder_); } -void GenericWriter::write(const GenericDatum& datum, Encoder& e) -{ +void GenericWriter::write(const GenericDatum &datum, Encoder &e) { if (datum.isUnion()) { e.encodeUnionIndex(datum.unionBranch()); } switch (datum.type()) { - case AVRO_NULL: - e.encodeNull(); - break; - case AVRO_BOOL: - e.encodeBool(datum.value<bool>()); - break; - case AVRO_INT: - e.encodeInt(datum.value<int32_t>()); - break; - case AVRO_LONG: - e.encodeLong(datum.value<int64_t>()); - break; - case AVRO_FLOAT: - e.encodeFloat(datum.value<float>()); - break; - case AVRO_DOUBLE: - e.encodeDouble(datum.value<double>()); - break; - case AVRO_STRING: - e.encodeString(datum.value<string>()); - break; - case AVRO_BYTES: - e.encodeBytes(datum.value<bytes>()); - break; - case AVRO_FIXED: - e.encodeFixed(datum.value<GenericFixed>().value()); - break; - case AVRO_RECORD: - { - const GenericRecord& r = datum.value<GenericRecord>(); + case AVRO_NULL: + e.encodeNull(); + break; + case AVRO_BOOL: + e.encodeBool(datum.value<bool>()); + break; + case AVRO_INT: + e.encodeInt(datum.value<int32_t>()); + break; + case AVRO_LONG: + e.encodeLong(datum.value<int64_t>()); + break; + case AVRO_FLOAT: + e.encodeFloat(datum.value<float>()); + break; + case AVRO_DOUBLE: + e.encodeDouble(datum.value<double>()); + break; + case AVRO_STRING: + e.encodeString(datum.value<string>()); + break; + case AVRO_BYTES: + e.encodeBytes(datum.value<bytes>()); + break; + case AVRO_FIXED: + e.encodeFixed(datum.value<GenericFixed>().value()); + break; + case AVRO_RECORD: { + const auto &r = datum.value<GenericRecord>(); size_t c = r.schema()->leaves(); for (size_t i = 0; i < c; ++i) { write(r.fieldAt(i), e); } - } - break; - case AVRO_ENUM: - e.encodeEnum(datum.value<GenericEnum>().value()); - break; - case AVRO_ARRAY: - { - const GenericArray::Value& r = datum.value<GenericArray>().value(); + } break; + case AVRO_ENUM: + e.encodeEnum(datum.value<GenericEnum>().value()); + break; + case AVRO_ARRAY: { + const GenericArray::Value &r = datum.value<GenericArray>().value(); e.arrayStart(); - if (! r.empty()) { + if (!r.empty()) { e.setItemCount(r.size()); - for (GenericArray::Value::const_iterator it = r.begin(); - it != r.end(); ++it) { + for (const auto &it : r) { e.startItem(); - write(*it, e); + write(it, e); } } e.arrayEnd(); - } - break; - case AVRO_MAP: - { - const GenericMap::Value& r = datum.value<GenericMap>().value(); + } break; + case AVRO_MAP: { + const GenericMap::Value &r = datum.value<GenericMap>().value(); e.mapStart(); - if (! r.empty()) { + if (!r.empty()) { e.setItemCount(r.size()); - for (GenericMap::Value::const_iterator it = r.begin(); - it != r.end(); ++it) { + for (const auto &it : r) { e.startItem(); - e.encodeString(it->first); - write(it->second, e); + e.encodeString(it.first); + write(it.second, e); } } e.mapEnd(); - } - break; - default: - throw Exception(boost::format("Unknown schema type %1%") % - toString(datum.type())); + } break; + default: + throw Exception(boost::format("Unknown schema type %1%") % toString(datum.type())); } } -void GenericWriter::write(Encoder& e, const GenericDatum& g) -{ +void GenericWriter::write(Encoder &e, const GenericDatum &g) { write(g, e); } -} // namespace avro +} // namespace avro diff --git a/contrib/libs/apache/avro/impl/GenericDatum.cc b/contrib/libs/apache/avro/impl/GenericDatum.cc index cdf9006eef2..7b2bf93bca9 100644 --- a/contrib/libs/apache/avro/impl/GenericDatum.cc +++ b/contrib/libs/apache/avro/impl/GenericDatum.cc @@ -24,22 +24,17 @@ using std::vector; namespace avro { -GenericDatum::GenericDatum(const ValidSchema& schema) : - type_(schema.root()->type()), - logicalType_(schema.root()->logicalType()) -{ +GenericDatum::GenericDatum(const ValidSchema &schema) : type_(schema.root()->type()), + logicalType_(schema.root()->logicalType()) { init(schema.root()); } -GenericDatum::GenericDatum(const NodePtr& schema) : - type_(schema->type()), - logicalType_(schema->logicalType()) -{ +GenericDatum::GenericDatum(const NodePtr &schema) : type_(schema->type()), + logicalType_(schema->logicalType()) { init(schema); } -void GenericDatum::init(const NodePtr& schema) -{ +void GenericDatum::init(const NodePtr &schema) { NodePtr sc = schema; if (type_ == AVRO_SYMBOLIC) { sc = resolveSymbol(schema); @@ -47,59 +42,57 @@ void GenericDatum::init(const NodePtr& schema) logicalType_ = sc->logicalType(); } switch (type_) { - case AVRO_NULL: - break; - case AVRO_BOOL: - value_ = bool(); - break; - case AVRO_INT: - value_ = int32_t(); - break; - case AVRO_LONG: - value_ = int64_t(); - break; - case AVRO_FLOAT: - value_ = float(); - break; - case AVRO_DOUBLE: - value_ = double(); - break; - case AVRO_STRING: - value_ = string(); - break; - case AVRO_BYTES: - value_ = vector<uint8_t>(); - break; - case AVRO_FIXED: - value_ = GenericFixed(sc); - break; - case AVRO_RECORD: - value_ = GenericRecord(sc); - break; - case AVRO_ENUM: - value_ = GenericEnum(sc); - break; - case AVRO_ARRAY: - value_ = GenericArray(sc); - break; - case AVRO_MAP: - value_ = GenericMap(sc); - break; - case AVRO_UNION: - value_ = GenericUnion(sc); - break; - default: - throw Exception(boost::format("Unknown schema type %1%") % - toString(type_)); + case AVRO_NULL: break; + case AVRO_BOOL: + value_ = bool(); + break; + case AVRO_INT: + value_ = int32_t(); + break; + case AVRO_LONG: + value_ = int64_t(); + break; + case AVRO_FLOAT: + value_ = float(); + break; + case AVRO_DOUBLE: + value_ = double(); + break; + case AVRO_STRING: + value_ = string(); + break; + case AVRO_BYTES: + value_ = vector<uint8_t>(); + break; + case AVRO_FIXED: + value_ = GenericFixed(sc); + break; + case AVRO_RECORD: + value_ = GenericRecord(sc); + break; + case AVRO_ENUM: + value_ = GenericEnum(sc); + break; + case AVRO_ARRAY: + value_ = GenericArray(sc); + break; + case AVRO_MAP: + value_ = GenericMap(sc); + break; + case AVRO_UNION: + value_ = GenericUnion(sc); + break; + default: + throw Exception(boost::format("Unknown schema type %1%") % toString(type_)); } } -GenericRecord::GenericRecord(const NodePtr& schema) : - GenericContainer(AVRO_RECORD, schema) { +GenericRecord::GenericRecord(const NodePtr &schema) : GenericContainer(AVRO_RECORD, schema) { fields_.resize(schema->leaves()); for (size_t i = 0; i < schema->leaves(); ++i) { fields_[i] = GenericDatum(schema->leafAt(i)); } } -} // namespace avro +GenericFixed::GenericFixed(const NodePtr &schema, const vector<uint8_t> &v) : GenericContainer(AVRO_FIXED, schema), value_(v) {} +} // namespace avro diff --git a/contrib/libs/apache/avro/impl/LogicalType.cc b/contrib/libs/apache/avro/impl/LogicalType.cc index a0d9cc3b6fe..1aa24bf20de 100644 --- a/contrib/libs/apache/avro/impl/LogicalType.cc +++ b/contrib/libs/apache/avro/impl/LogicalType.cc @@ -16,8 +16,8 @@ * limitations under the License. */ -#include "Exception.hh" #include "LogicalType.hh" +#include "Exception.hh" namespace avro { @@ -48,37 +48,36 @@ void LogicalType::setScale(int scale) { scale_ = scale; } -void LogicalType::printJson(std::ostream& os) const { +void LogicalType::printJson(std::ostream &os) const { switch (type_) { - case LogicalType::NONE: - break; - case LogicalType::DECIMAL: - os << "\"logicalType\": \"decimal\""; - os << ", \"precision\": " << precision_; - os << ", \"scale\": " << scale_; - break; - case DATE: - os << "\"logicalType\": \"date\""; - break; - case TIME_MILLIS: - os << "\"logicalType\": \"time-millis\""; - break; - case TIME_MICROS: - os << "\"logicalType\": \"time-micros\""; - break; - case TIMESTAMP_MILLIS: - os << "\"logicalType\": \"timestamp-millis\""; - break; - case TIMESTAMP_MICROS: - os << "\"logicalType\": \"timestamp-micros\""; - break; - case DURATION: - os << "\"logicalType\": \"duration\""; - break; - case UUID: - os << "\"logicalType\": \"uuid\""; - break; + case LogicalType::NONE: break; + case LogicalType::DECIMAL: + os << R"("logicalType": "decimal")"; + os << ", \"precision\": " << precision_; + os << ", \"scale\": " << scale_; + break; + case DATE: + os << R"("logicalType": "date")"; + break; + case TIME_MILLIS: + os << R"("logicalType": "time-millis")"; + break; + case TIME_MICROS: + os << R"("logicalType": "time-micros")"; + break; + case TIMESTAMP_MILLIS: + os << R"("logicalType": "timestamp-millis")"; + break; + case TIMESTAMP_MICROS: + os << R"("logicalType": "timestamp-micros")"; + break; + case DURATION: + os << R"("logicalType": "duration")"; + break; + case UUID: + os << R"("logicalType": "uuid")"; + break; } } -} // namespace avro +} // namespace avro diff --git a/contrib/libs/apache/avro/impl/Node.cc b/contrib/libs/apache/avro/impl/Node.cc index bb510cc1471..46310d0f9ef 100644 --- a/contrib/libs/apache/avro/impl/Node.cc +++ b/contrib/libs/apache/avro/impl/Node.cc @@ -24,21 +24,17 @@ namespace avro { using std::string; -Node::~Node() -{ } +Node::~Node() = default; -Name::Name(const std::string& name) -{ +Name::Name(const std::string &name) { fullname(name); } -const string Name::fullname() const -{ +string Name::fullname() const { return (ns_.empty()) ? simpleName_ : ns_ + "." + simpleName_; } -void Name::fullname(const string& name) -{ +void Name::fullname(const string &name) { string::size_type n = name.find_last_of('.'); if (n == string::npos) { simpleName_ = name; @@ -50,35 +46,29 @@ void Name::fullname(const string& name) check(); } -bool Name::operator < (const Name& n) const -{ - return (ns_ < n.ns_) ? true : - (n.ns_ < ns_) ? false : - (simpleName_ < n.simpleName_); +bool Name::operator<(const Name &n) const { + return (ns_ < n.ns_) || (!(n.ns_ < ns_) && (simpleName_ < n.simpleName_)); } -static bool invalidChar1(char c) -{ +static bool invalidChar1(char c) { return !isalnum(c) && c != '_' && c != '.' && c != '$'; } -static bool invalidChar2(char c) -{ +static bool invalidChar2(char c) { return !isalnum(c) && c != '_'; } -void Name::check() const -{ - if (! ns_.empty() && (ns_[0] == '.' || ns_[ns_.size() - 1] == '.' || std::find_if(ns_.begin(), ns_.end(), invalidChar1) != ns_.end())) { +void Name::check() const { + if (!ns_.empty() && (ns_[0] == '.' || ns_[ns_.size() - 1] == '.' || std::find_if(ns_.begin(), ns_.end(), invalidChar1) != ns_.end())) { throw Exception("Invalid namespace: " + ns_); } - if (simpleName_.empty() || std::find_if(simpleName_.begin(), simpleName_.end(), invalidChar2) != simpleName_.end()) { + if (simpleName_.empty() + || std::find_if(simpleName_.begin(), simpleName_.end(), invalidChar2) != simpleName_.end()) { throw Exception("Invalid name: " + simpleName_); } } -bool Name::operator == (const Name& n) const -{ +bool Name::operator==(const Name &n) const { return ns_ == n.ns_ && simpleName_ == n.simpleName_; } @@ -87,72 +77,71 @@ void Node::setLogicalType(LogicalType logicalType) { // Check that the logical type is applicable to the node type. switch (logicalType.type()) { - case LogicalType::NONE: - break; - case LogicalType::DECIMAL: { - if (type_ != AVRO_BYTES && type_ != AVRO_FIXED) { - throw Exception("DECIMAL logical type can annotate " - "only BYTES or FIXED type"); - } - if (type_ == AVRO_FIXED) { - // Max precision that can be supported by the current size of - // the FIXED type. - long maxPrecision = floor(log10(2.0) * (8.0 * fixedSize() - 1)); - if (logicalType.precision() > maxPrecision) { - throw Exception( - boost::format( - "DECIMAL precision %1% is too large for the " - "FIXED type of size %2%, precision cannot be " - "larget than %3%") % logicalType.precision() % - fixedSize() % maxPrecision); + case LogicalType::NONE: break; + case LogicalType::DECIMAL: { + if (type_ != AVRO_BYTES && type_ != AVRO_FIXED) { + throw Exception("DECIMAL logical type can annotate " + "only BYTES or FIXED type"); } + if (type_ == AVRO_FIXED) { + // Max precision that can be supported by the current size of + // the FIXED type. + long maxPrecision = floor(log10(2.0) * (8.0 * fixedSize() - 1)); + if (logicalType.precision() > maxPrecision) { + throw Exception( + boost::format( + "DECIMAL precision %1% is too large for the " + "FIXED type of size %2%, precision cannot be " + "larger than %3%") + % logicalType.precision() % fixedSize() % maxPrecision); + } + } + if (logicalType.scale() > logicalType.precision()) { + throw Exception("DECIMAL scale cannot exceed precision"); + } + break; } - if (logicalType.scale() > logicalType.precision()) { - throw Exception("DECIMAL scale cannot exceed precision"); - } - break; - } - case LogicalType::DATE: - if (type_ != AVRO_INT) { - throw Exception("DATE logical type can only annotate INT type"); - } - break; - case LogicalType::TIME_MILLIS: - if (type_ != AVRO_INT) { - throw Exception("TIME-MILLIS logical type can only annotate " - "INT type"); - } - break; - case LogicalType::TIME_MICROS: - if (type_ != AVRO_LONG) { - throw Exception("TIME-MICROS logical type can only annotate " - "LONG type"); - } - break; - case LogicalType::TIMESTAMP_MILLIS: - if (type_ != AVRO_LONG) { - throw Exception("TIMESTAMP-MILLIS logical type can only annotate " - "LONG type"); - } - break; - case LogicalType::TIMESTAMP_MICROS: - if (type_ != AVRO_LONG) { - throw Exception("TIMESTAMP-MICROS logical type can only annotate " - "LONG type"); - } - break; - case LogicalType::DURATION: - if (type_ != AVRO_FIXED || fixedSize() != 12) { - throw Exception("DURATION logical type can only annotate " - "FIXED type of size 12"); - } - break; - case LogicalType::UUID: - if (type_ != AVRO_STRING) { - throw Exception("UUID logical type can only annotate " - "STRING type"); - } - break; + case LogicalType::DATE: + if (type_ != AVRO_INT) { + throw Exception("DATE logical type can only annotate INT type"); + } + break; + case LogicalType::TIME_MILLIS: + if (type_ != AVRO_INT) { + throw Exception("TIME-MILLIS logical type can only annotate " + "INT type"); + } + break; + case LogicalType::TIME_MICROS: + if (type_ != AVRO_LONG) { + throw Exception("TIME-MICROS logical type can only annotate " + "LONG type"); + } + break; + case LogicalType::TIMESTAMP_MILLIS: + if (type_ != AVRO_LONG) { + throw Exception("TIMESTAMP-MILLIS logical type can only annotate " + "LONG type"); + } + break; + case LogicalType::TIMESTAMP_MICROS: + if (type_ != AVRO_LONG) { + throw Exception("TIMESTAMP-MICROS logical type can only annotate " + "LONG type"); + } + break; + case LogicalType::DURATION: + if (type_ != AVRO_FIXED || fixedSize() != 12) { + throw Exception("DURATION logical type can only annotate " + "FIXED type of size 12"); + } + break; + case LogicalType::UUID: + if (type_ != AVRO_STRING) { + throw Exception("UUID logical type can only annotate " + "STRING type"); + } + break; } logicalType_ = logicalType; diff --git a/contrib/libs/apache/avro/impl/NodeImpl.cc b/contrib/libs/apache/avro/impl/NodeImpl.cc index 4a0acb92c0b..810e1641ed9 100644 --- a/contrib/libs/apache/avro/impl/NodeImpl.cc +++ b/contrib/libs/apache/avro/impl/NodeImpl.cc @@ -16,12 +16,9 @@ * limitations under the License. */ - -#include <sstream> -#include <iomanip> -#include <boost/algorithm/string/replace.hpp> #include "NodeImpl.hh" - +#include <sstream> +#include <utility> using std::string; namespace avro { @@ -30,59 +27,55 @@ namespace { // Escape string for serialization. string escape(const string &unescaped) { - string s; - s.reserve(unescaped.length()); - for (std::string::const_iterator it = unescaped.begin(); it != unescaped.end(); ++it) { - char c = *it; - switch (c) { - case '\\': - case '"': - case '/': - s += '\\'; - s += c; - break; - case '\b': - s += '\\'; - s += 'b'; - break; - case '\f': - s += '\f'; - break; - case '\n': - s += '\\'; - s += 'n'; - break; - case '\r': - s += '\\'; - s += 'r'; - break; - case '\t': - s += '\\'; - s += 't'; - break; - default: - if (!std::iscntrl(c, std::locale::classic())) { - s += c; - continue; + string s; + s.reserve(unescaped.length()); + for (char c : unescaped) { + switch (c) { + case '\\': + case '"': + case '/': + s += '\\'; + s += c; + break; + case '\b': + s += '\\'; + s += 'b'; + break; + case '\f': + s += '\f'; + break; + case '\n': + s += '\\'; + s += 'n'; + break; + case '\r': + s += '\\'; + s += 'r'; + break; + case '\t': + s += '\\'; + s += 't'; + break; + default: + if (!std::iscntrl(c, std::locale::classic())) { + s += c; + continue; + } + s += intToHex(static_cast<unsigned int>(c)); + break; } - s += intToHex(static_cast<unsigned int>(c)); - break; } - } - return s; + return s; } // Wrap an indentation in a struct for ostream operator<< struct indent { - indent(int depth) : - d(depth) - { } + explicit indent(size_t depth) : d(depth) {} int d; }; /// ostream operator for indent -std::ostream& operator <<(std::ostream &os, indent x) -{ +std::ostream &operator<<(std::ostream &os, indent x) { static const string spaces(" "); while (x.d--) { os << spaces; @@ -95,46 +88,43 @@ std::ostream& operator <<(std::ostream &os, indent x) const int kByteStringSize = 6; SchemaResolution -NodePrimitive::resolve(const Node &reader) const -{ +NodePrimitive::resolve(const Node &reader) const { if (type() == reader.type()) { return RESOLVE_MATCH; } - switch ( type() ) { + switch (type()) { - case AVRO_INT: + case AVRO_INT: - if ( reader.type() == AVRO_LONG ) { - return RESOLVE_PROMOTABLE_TO_LONG; - } + if (reader.type() == AVRO_LONG) { + return RESOLVE_PROMOTABLE_TO_LONG; + } - // fall-through intentional + // fall-through intentional - case AVRO_LONG: + case AVRO_LONG: - if (reader.type() == AVRO_FLOAT) { - return RESOLVE_PROMOTABLE_TO_FLOAT; - } + if (reader.type() == AVRO_FLOAT) { + return RESOLVE_PROMOTABLE_TO_FLOAT; + } - // fall-through intentional + // fall-through intentional - case AVRO_FLOAT: + case AVRO_FLOAT: - if (reader.type() == AVRO_DOUBLE) { - return RESOLVE_PROMOTABLE_TO_DOUBLE; - } + if (reader.type() == AVRO_DOUBLE) { + return RESOLVE_PROMOTABLE_TO_DOUBLE; + } - default: - break; + default: break; } return furtherResolution(reader); } SchemaResolution -NodeRecord::resolve(const Node &reader) const -{ +NodeRecord::resolve(const Node &reader) const { if (reader.type() == AVRO_RECORD) { if (name() == reader.name()) { return RESOLVE_MATCH; @@ -144,8 +134,7 @@ NodeRecord::resolve(const Node &reader) const } SchemaResolution -NodeEnum::resolve(const Node &reader) const -{ +NodeEnum::resolve(const Node &reader) const { if (reader.type() == AVRO_ENUM) { return (name() == reader.name()) ? RESOLVE_MATCH : RESOLVE_NO_MATCH; } @@ -153,8 +142,7 @@ NodeEnum::resolve(const Node &reader) const } SchemaResolution -NodeArray::resolve(const Node &reader) const -{ +NodeArray::resolve(const Node &reader) const { if (reader.type() == AVRO_ARRAY) { const NodePtr &arrayType = leafAt(0); return arrayType->resolve(*reader.leafAt(0)); @@ -163,8 +151,7 @@ NodeArray::resolve(const Node &reader) const } SchemaResolution -NodeMap::resolve(const Node &reader) const -{ +NodeMap::resolve(const Node &reader) const { if (reader.type() == AVRO_MAP) { const NodePtr &mapType = leafAt(1); return mapType->resolve(*reader.leafAt(1)); @@ -173,8 +160,7 @@ NodeMap::resolve(const Node &reader) const } SchemaResolution -NodeUnion::resolve(const Node &reader) const -{ +NodeUnion::resolve(const Node &reader) const { // If the writer is union, resolution only needs to occur when the selected // type of the writer is known, so this function is not very helpful. @@ -184,7 +170,7 @@ NodeUnion::resolve(const Node &reader) const // found. SchemaResolution match = RESOLVE_NO_MATCH; - for (size_t i=0; i < leaves(); ++i) { + for (size_t i = 0; i < leaves(); ++i) { const NodePtr &node = leafAt(i); SchemaResolution thisMatch = node->resolve(reader); if (thisMatch == RESOLVE_MATCH) { @@ -199,73 +185,67 @@ NodeUnion::resolve(const Node &reader) const } SchemaResolution -NodeFixed::resolve(const Node &reader) const -{ +NodeFixed::resolve(const Node &reader) const { if (reader.type() == AVRO_FIXED) { return ( - (reader.fixedSize() == fixedSize()) && - (reader.name() == name()) - ) ? - RESOLVE_MATCH : RESOLVE_NO_MATCH; + (reader.fixedSize() == fixedSize()) && (reader.name() == name())) + ? RESOLVE_MATCH + : RESOLVE_NO_MATCH; } return furtherResolution(reader); } SchemaResolution -NodeSymbolic::resolve(const Node &reader) const -{ +NodeSymbolic::resolve(const Node &reader) const { const NodePtr &node = leafAt(0); return node->resolve(reader); } -void -NodePrimitive::printJson(std::ostream &os, int depth) const -{ +void NodePrimitive::printJson(std::ostream &os, size_t depth) const { bool hasLogicalType = logicalType().type() != LogicalType::NONE; if (hasLogicalType) { - os << "{\n" << indent(depth) << "\"type\": "; + os << "{\n" + << indent(depth) << "\"type\": "; } os << '\"' << type() << '\"'; if (hasLogicalType) { - os << ",\n" << indent(depth); + os << ",\n" + << indent(depth); logicalType().printJson(os); os << "\n}"; } - if (getDoc().size()) { - os << ",\n" << indent(depth) << "\"doc\": \"" + if (!getDoc().empty()) { + os << ",\n" + << indent(depth) << R"("doc": ")" << escape(getDoc()) << "\""; } } -void -NodeSymbolic::printJson(std::ostream &os, int depth) const -{ +void NodeSymbolic::printJson(std::ostream &os, size_t depth) const { os << '\"' << nameAttribute_.get() << '\"'; - if (getDoc().size()) { - os << ",\n" << indent(depth) << "\"doc\": \"" + if (!getDoc().empty()) { + os << ",\n" + << indent(depth) << R"("doc": ")" << escape(getDoc()) << "\""; } } -static void printName(std::ostream& os, const Name& n, int depth) -{ +static void printName(std::ostream &os, const Name &n, size_t depth) { if (!n.ns().empty()) { - os << indent(depth) << "\"namespace\": \"" << n.ns() << "\",\n"; + os << indent(depth) << R"("namespace": ")" << n.ns() << "\",\n"; } - os << indent(depth) << "\"name\": \"" << n.simpleName() << "\",\n"; + os << indent(depth) << R"("name": ")" << n.simpleName() << "\",\n"; } -void -NodeRecord::printJson(std::ostream &os, int depth) const -{ +void NodeRecord::printJson(std::ostream &os, size_t depth) const { os << "{\n"; os << indent(++depth) << "\"type\": \"record\",\n"; printName(os, nameAttribute_.get(), depth); - if (getDoc().size()) { - os << indent(depth) << "\"doc\": \"" + if (!getDoc().empty()) { + os << indent(depth) << R"("doc": ")" << escape(getDoc()) << "\",\n"; } os << indent(depth) << "\"fields\": ["; @@ -278,187 +258,201 @@ NodeRecord::printJson(std::ostream &os, int depth) const if (i > 0) { os << ','; } - os << '\n' << indent(depth) << "{\n"; - os << indent(++depth) << "\"name\": \"" << leafNameAttributes_.get(i) << "\",\n"; + os << '\n' + << indent(depth) << "{\n"; + os << indent(++depth) << R"("name": ")" << leafNameAttributes_.get(i) << "\",\n"; os << indent(depth) << "\"type\": "; leafAttributes_.get(i)->printJson(os, depth); if (!defaultValues.empty()) { - if (!defaultValues[i].isUnion() && - defaultValues[i].type() == AVRO_NULL) { - // No "default" field. - } else { - os << ",\n" << indent(depth) << "\"default\": "; - leafAttributes_.get(i)->printDefaultToJson(defaultValues[i], os, - depth); - } + if (!defaultValues[i].isUnion() && defaultValues[i].type() == AVRO_NULL) { + // No "default" field. + } else { + os << ",\n" + << indent(depth) << "\"default\": "; + leafAttributes_.get(i)->printDefaultToJson(defaultValues[i], os, + depth); + } } os << '\n'; os << indent(--depth) << '}'; } - os << '\n' << indent(--depth) << "]\n"; + os << '\n' + << indent(--depth) << "]\n"; os << indent(--depth) << '}'; } void NodePrimitive::printDefaultToJson(const GenericDatum &g, std::ostream &os, - int depth) const { - assert(isPrimitive(g.type())); - - switch (g.type()) { - case AVRO_NULL: - os << "null"; - break; - case AVRO_BOOL: - os << (g.value<bool>() ? "true" : "false"); - break; - case AVRO_INT: - os << g.value<int32_t>(); - break; - case AVRO_LONG: - os << g.value<int64_t>(); - break; - case AVRO_FLOAT: - os << g.value<float>(); - break; - case AVRO_DOUBLE: - os << g.value<double>(); - break; - case AVRO_STRING: - os << "\"" << escape(g.value<string>()) << "\""; - break; - case AVRO_BYTES: { - // Convert to a string: - const std::vector<uint8_t> &vg = g.value<std::vector<uint8_t> >(); - string s; - s.resize(vg.size() * kByteStringSize); - for (unsigned int i = 0; i < vg.size(); i++) { - string hex_string = intToHex(static_cast<int>(vg[i])); - s.replace(i*kByteStringSize, kByteStringSize, hex_string); - } - os << "\"" << s << "\""; - } break; - default: - break; - } + size_t depth) const { + assert(isPrimitive(g.type())); + + switch (g.type()) { + case AVRO_NULL: + os << "null"; + break; + case AVRO_BOOL: + os << (g.value<bool>() ? "true" : "false"); + break; + case AVRO_INT: + os << g.value<int32_t>(); + break; + case AVRO_LONG: + os << g.value<int64_t>(); + break; + case AVRO_FLOAT: + os << g.value<float>(); + break; + case AVRO_DOUBLE: + os << g.value<double>(); + break; + case AVRO_STRING: + os << "\"" << escape(g.value<string>()) << "\""; + break; + case AVRO_BYTES: { + // Convert to a string: + const auto &vg = g.value<std::vector<uint8_t>>(); + string s; + s.resize(vg.size() * kByteStringSize); + for (unsigned int i = 0; i < vg.size(); i++) { + string hex_string = intToHex(static_cast<int>(vg[i])); + s.replace(i * kByteStringSize, kByteStringSize, hex_string); + } + os << "\"" << s << "\""; + } break; + default: break; + } } void NodeEnum::printDefaultToJson(const GenericDatum &g, std::ostream &os, - int depth) const { - assert(g.type() == AVRO_ENUM); - os << "\"" << g.value<GenericEnum>().symbol() << "\""; + size_t depth) const { + assert(g.type() == AVRO_ENUM); + os << "\"" << g.value<GenericEnum>().symbol() << "\""; } void NodeFixed::printDefaultToJson(const GenericDatum &g, std::ostream &os, - int depth) const { - assert(g.type() == AVRO_FIXED); - // ex: "\uOOff" - // Convert to a string - const std::vector<uint8_t> &vg = g.value<GenericFixed>().value(); - string s; - s.resize(vg.size() * kByteStringSize); - for (unsigned int i = 0; i < vg.size(); i++) { - string hex_string = intToHex(static_cast<int>(vg[i])); - s.replace(i*kByteStringSize, kByteStringSize, hex_string); - } - os << "\"" << s << "\""; + size_t depth) const { + assert(g.type() == AVRO_FIXED); + // ex: "\uOOff" + // Convert to a string + const std::vector<uint8_t> &vg = g.value<GenericFixed>().value(); + string s; + s.resize(vg.size() * kByteStringSize); + for (unsigned int i = 0; i < vg.size(); i++) { + string hex_string = intToHex(static_cast<int>(vg[i])); + s.replace(i * kByteStringSize, kByteStringSize, hex_string); + } + os << "\"" << s << "\""; } void NodeUnion::printDefaultToJson(const GenericDatum &g, std::ostream &os, - int depth) const { - leafAt(0)->printDefaultToJson(g, os, depth); + size_t depth) const { + leafAt(0)->printDefaultToJson(g, os, depth); } void NodeArray::printDefaultToJson(const GenericDatum &g, std::ostream &os, - int depth) const { - assert(g.type() == AVRO_ARRAY); - // ex: "default": [1] - if (g.value<GenericArray>().value().empty()) { - os << "[]"; - } else { - os << "[\n"; - depth++; - - // Serialize all values of the array with recursive calls: - for (unsigned int i = 0; i < g.value<GenericArray>().value().size(); i++) { - if (i > 0) { - os << ",\n"; - } - os << indent(depth); - leafAt(0)->printDefaultToJson(g.value<GenericArray>().value()[i], os, - depth); + size_t depth) const { + assert(g.type() == AVRO_ARRAY); + // ex: "default": [1] + if (g.value<GenericArray>().value().empty()) { + os << "[]"; + } else { + os << "[\n"; + depth++; + + // Serialize all values of the array with recursive calls: + for (unsigned int i = 0; i < g.value<GenericArray>().value().size(); i++) { + if (i > 0) { + os << ",\n"; + } + os << indent(depth); + leafAt(0)->printDefaultToJson(g.value<GenericArray>().value()[i], os, + depth); + } + os << "\n" + << indent(--depth) << "]"; } - os << "\n" << indent(--depth) << "]"; - } } void NodeSymbolic::printDefaultToJson(const GenericDatum &g, std::ostream &os, - int depth) const { - getNode()->printDefaultToJson(g, os, depth); + size_t depth) const { + getNode()->printDefaultToJson(g, os, depth); } void NodeRecord::printDefaultToJson(const GenericDatum &g, std::ostream &os, - int depth) const { - assert(g.type() == AVRO_RECORD); - if (g.value<GenericRecord>().fieldCount() == 0) { - os << "{}"; - } else { - os << "{\n"; - - // Serialize all fields of the record with recursive calls: - for (unsigned int i = 0; i < g.value<GenericRecord>().fieldCount(); i++) { - if (i == 0) { - ++depth; - } else { // i > 0 - os << ",\n"; - } - - os << indent(depth) << "\""; - assert(i < leaves()); - os << leafNameAttributes_.get(i); - os << "\": "; - - // Recursive call on child node to be able to get the name attribute - // (In case of a record we need the name of the leaves (contained in - // 'this')) - leafAt(i)->printDefaultToJson(g.value<GenericRecord>().fieldAt(i), os, - depth); + size_t depth) const { + assert(g.type() == AVRO_RECORD); + if (g.value<GenericRecord>().fieldCount() == 0) { + os << "{}"; + } else { + os << "{\n"; + + // Serialize all fields of the record with recursive calls: + for (size_t i = 0; i < g.value<GenericRecord>().fieldCount(); i++) { + if (i == 0) { + ++depth; + } else { // i > 0 + os << ",\n"; + } + + os << indent(depth) << "\""; + assert(i < leaves()); + os << leafNameAttributes_.get(i); + os << "\": "; + + // Recursive call on child node to be able to get the name attribute + // (In case of a record we need the name of the leaves (contained in + // 'this')) + leafAt(i)->printDefaultToJson(g.value<GenericRecord>().fieldAt(i), os, + depth); + } + os << "\n" + << indent(--depth) << "}"; + } +} +NodeRecord::NodeRecord(const HasName &name, + const MultiLeaves &fields, + const LeafNames &fieldsNames, + std::vector<GenericDatum> dv) : NodeImplRecord(AVRO_RECORD, name, fields, fieldsNames, NoSize()), + defaultValues(std::move(dv)) { + for (size_t i = 0; i < leafNameAttributes_.size(); ++i) { + if (!nameIndex_.add(leafNameAttributes_.get(i), i)) { + throw Exception(boost::format( + "Cannot add duplicate field: %1%") + % leafNameAttributes_.get(i)); + } } - os << "\n" << indent(--depth) << "}"; - } } void NodeMap::printDefaultToJson(const GenericDatum &g, std::ostream &os, - int depth) const { - assert(g.type() == AVRO_MAP); - //{"a": 1} - if (g.value<GenericMap>().value().empty()) { - os << "{}"; - } else { - os << "{\n"; - - for (unsigned int i = 0; i < g.value<GenericMap>().value().size(); i++) { - if (i == 0) { - ++depth; - } else { - os << ",\n"; - } - os << indent(depth) << "\"" << g.value<GenericMap>().value()[i].first - << "\": "; - - leafAt(i)->printDefaultToJson(g.value<GenericMap>().value()[i].second, os, - depth); + size_t depth) const { + assert(g.type() == AVRO_MAP); + if (g.value<GenericMap>().value().empty()) { + os << "{}"; + } else { + os << "{\n"; + + for (size_t i = 0; i < g.value<GenericMap>().value().size(); i++) { + if (i == 0) { + ++depth; + } else { + os << ",\n"; + } + os << indent(depth) << "\"" << g.value<GenericMap>().value()[i].first + << "\": "; + + leafAt(i)->printDefaultToJson(g.value<GenericMap>().value()[i].second, os, + depth); + } + os << "\n" + << indent(--depth) << "}"; } - os << "\n" << indent(--depth) << "}"; - } } -void -NodeEnum::printJson(std::ostream &os, int depth) const -{ +void NodeEnum::printJson(std::ostream &os, size_t depth) const { os << "{\n"; os << indent(++depth) << "\"type\": \"enum\",\n"; - if (getDoc().size()) { - os << indent(depth) << "\"doc\": \"" + if (!getDoc().empty()) { + os << indent(depth) << R"("doc": ")" << escape(getDoc()) << "\",\n"; } printName(os, nameAttribute_.get(), depth); @@ -477,39 +471,38 @@ NodeEnum::printJson(std::ostream &os, int depth) const os << indent(--depth) << '}'; } -void -NodeArray::printJson(std::ostream &os, int depth) const -{ +void NodeArray::printJson(std::ostream &os, size_t depth) const { os << "{\n"; - os << indent(depth+1) << "\"type\": \"array\",\n"; - if (getDoc().size()) { - os << indent(depth+1) << "\"doc\": \"" + os << indent(depth + 1) << "\"type\": \"array\",\n"; + if (!getDoc().empty()) { + os << indent(depth + 1) << R"("doc": ")" << escape(getDoc()) << "\",\n"; } - os << indent(depth+1) << "\"items\": "; - leafAttributes_.get()->printJson(os, depth+1); + os << indent(depth + 1) << "\"items\": "; + leafAttributes_.get()->printJson(os, depth + 1); os << '\n'; os << indent(depth) << '}'; } -void -NodeMap::printJson(std::ostream &os, int depth) const -{ +void NodeMap::printJson(std::ostream &os, size_t depth) const { os << "{\n"; - os << indent(depth+1) <<"\"type\": \"map\",\n"; - if (getDoc().size()) { - os << indent(depth+1) << "\"doc\": \"" + os << indent(depth + 1) << "\"type\": \"map\",\n"; + if (!getDoc().empty()) { + os << indent(depth + 1) << R"("doc": ")" << escape(getDoc()) << "\",\n"; } - os << indent(depth+1) << "\"values\": "; - leafAttributes_.get(1)->printJson(os, depth+1); + os << indent(depth + 1) << "\"values\": "; + leafAttributes_.get(1)->printJson(os, depth + 1); os << '\n'; os << indent(depth) << '}'; } -void -NodeUnion::printJson(std::ostream &os, int depth) const -{ +NodeMap::NodeMap() : NodeImplMap(AVRO_MAP) { + NodePtr key(new NodePrimitive(AVRO_STRING)); + doAddLeaf(key); +} + +void NodeUnion::printJson(std::ostream &os, size_t depth) const { os << "[\n"; int fields = leafAttributes_.size(); ++depth; @@ -524,24 +517,24 @@ NodeUnion::printJson(std::ostream &os, int depth) const os << indent(--depth) << ']'; } -void -NodeFixed::printJson(std::ostream &os, int depth) const -{ +void NodeFixed::printJson(std::ostream &os, size_t depth) const { os << "{\n"; os << indent(++depth) << "\"type\": \"fixed\",\n"; - if (getDoc().size()) { - os << indent(depth) << "\"doc\": \"" + if (!getDoc().empty()) { + os << indent(depth) << R"("doc": ")" << escape(getDoc()) << "\",\n"; } printName(os, nameAttribute_.get(), depth); os << indent(depth) << "\"size\": " << sizeAttribute_.get(); if (logicalType().type() != LogicalType::NONE) { - os << ",\n" << indent(depth); - logicalType().printJson(os); + os << ",\n" + << indent(depth); + logicalType().printJson(os); } - os << "\n" << indent(--depth) << '}'; + os << "\n" + << indent(--depth) << '}'; } } // namespace avro diff --git a/contrib/libs/apache/avro/impl/Resolver.cc b/contrib/libs/apache/avro/impl/Resolver.cc index 43467c028dd..919345e8a2d 100644 --- a/contrib/libs/apache/avro/impl/Resolver.cc +++ b/contrib/libs/apache/avro/impl/Resolver.cc @@ -17,20 +17,20 @@ * limitations under the License. */ -#include <memory> #include "Resolver.hh" +#include "AvroTraits.hh" #include "Layout.hh" #include "NodeImpl.hh" -#include "ValidSchema.hh" #include "Reader.hh" -#include "AvroTraits.hh" +#include "ValidSchema.hh" +#include <memory> namespace avro { using std::unique_ptr; class ResolverFactory; typedef std::shared_ptr<Resolver> ResolverPtr; -typedef std::vector<std::unique_ptr<Resolver> > ResolverPtrVector; +typedef std::vector<std::unique_ptr<Resolver>> ResolverPtrVector; // #define DEBUG_VERBOSE @@ -38,7 +38,8 @@ typedef std::vector<std::unique_ptr<Resolver> > ResolverPtrVector; #define DEBUG_OUT(str) std::cout << str << '\n' #else class NoOp {}; -template<typename T> NoOp& operator<<(NoOp &noOp, const T&) { +template<typename T> +NoOp &operator<<(NoOp &noOp, const T &) { return noOp; } NoOp noop; @@ -46,16 +47,11 @@ NoOp noop; #endif template<typename T> -class PrimitiveSkipper : public Resolver -{ - public: - - PrimitiveSkipper() : - Resolver() - {} +class PrimitiveSkipper : public Resolver { +public: + PrimitiveSkipper() : Resolver() {} - virtual void parse(Reader &reader, uint8_t *address) const - { + void parse(Reader &reader, uint8_t *address) const final { T val; reader.readValue(val); DEBUG_OUT("Skipping " << val); @@ -63,199 +59,154 @@ class PrimitiveSkipper : public Resolver }; template<typename T> -class PrimitiveParser : public Resolver -{ - public: - - PrimitiveParser(const PrimitiveLayout &offset) : - Resolver(), - offset_(offset.offset()) - {} - - virtual void parse(Reader &reader, uint8_t *address) const - { - T* location = reinterpret_cast<T *> (address + offset_); +class PrimitiveParser : public Resolver { +public: + explicit PrimitiveParser(const PrimitiveLayout &offset) : Resolver(), + offset_(offset.offset()) {} + + void parse(Reader &reader, uint8_t *address) const final { + T *location = reinterpret_cast<T *>(address + offset_); reader.readValue(*location); DEBUG_OUT("Reading " << *location); } - private: - +private: size_t offset_; }; template<typename WT, typename RT> -class PrimitivePromoter : public Resolver -{ - public: - - PrimitivePromoter(const PrimitiveLayout &offset) : - Resolver(), - offset_(offset.offset()) - {} +class PrimitivePromoter : public Resolver { +public: + explicit PrimitivePromoter(const PrimitiveLayout &offset) : Resolver(), + offset_(offset.offset()) {} - virtual void parse(Reader &reader, uint8_t *address) const - { + void parse(Reader &reader, uint8_t *address) const final { parseIt<WT>(reader, address); } - private: - - void parseIt(Reader &reader, uint8_t *address, const std::true_type &) const - { +private: + void parseIt(Reader &reader, uint8_t *address, const std::true_type &) const { WT val; reader.readValue(val); - RT *location = reinterpret_cast<RT *> (address + offset_); + RT *location = reinterpret_cast<RT *>(address + offset_); *location = static_cast<RT>(val); DEBUG_OUT("Promoting " << val); } - void parseIt(Reader &reader, uint8_t *address, const std::false_type &) const - { } + void parseIt(Reader &reader, uint8_t *, const std::false_type &) const {} template<typename T> - void parseIt(Reader &reader, uint8_t *address) const - { + void parseIt(Reader &reader, uint8_t *address) const { parseIt(reader, address, is_promotable<T>()); } size_t offset_; }; -template <> -class PrimitiveSkipper<std::vector<uint8_t> > : public Resolver -{ - public: - - PrimitiveSkipper() : - Resolver() - {} +template<> +class PrimitiveSkipper<std::vector<uint8_t>> : public Resolver { +public: + PrimitiveSkipper() : Resolver() {} - virtual void parse(Reader &reader, uint8_t *address) const - { + void parse(Reader &reader, uint8_t *address) const final { std::vector<uint8_t> val; reader.readBytes(val); DEBUG_OUT("Skipping bytes"); } }; -template <> -class PrimitiveParser<std::vector<uint8_t> > : public Resolver -{ - public: - - PrimitiveParser(const PrimitiveLayout &offset) : - Resolver(), - offset_(offset.offset()) - {} +template<> +class PrimitiveParser<std::vector<uint8_t>> : public Resolver { +public: + explicit PrimitiveParser(const PrimitiveLayout &offset) : Resolver(), + offset_(offset.offset()) {} - virtual void parse(Reader &reader, uint8_t *address) const - { - std::vector<uint8_t> *location = reinterpret_cast<std::vector<uint8_t> *> (address + offset_); + void parse(Reader &reader, uint8_t *address) const final { + auto *location = reinterpret_cast<std::vector<uint8_t> *>(address + offset_); reader.readBytes(*location); DEBUG_OUT("Reading bytes"); } - private: - +private: size_t offset_; }; -class RecordSkipper : public Resolver -{ - public: - +class RecordSkipper : public Resolver { +public: RecordSkipper(ResolverFactory &factory, const NodePtr &writer); - virtual void parse(Reader &reader, uint8_t *address) const - { + void parse(Reader &reader, uint8_t *address) const final { DEBUG_OUT("Skipping record"); reader.readRecord(); size_t steps = resolvers_.size(); - for(size_t i = 0; i < steps; ++i) { + for (size_t i = 0; i < steps; ++i) { resolvers_[i]->parse(reader, address); } } - protected: - +protected: ResolverPtrVector resolvers_; - }; -class RecordParser : public Resolver -{ - public: - - virtual void parse(Reader &reader, uint8_t *address) const - { +class RecordParser : public Resolver { +public: + void parse(Reader &reader, uint8_t *address) const final { DEBUG_OUT("Reading record"); reader.readRecord(); size_t steps = resolvers_.size(); - for(size_t i = 0; i < steps; ++i) { + for (size_t i = 0; i < steps; ++i) { resolvers_[i]->parse(reader, address); } } RecordParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets); - protected: - +protected: ResolverPtrVector resolvers_; - }; - -class MapSkipper : public Resolver -{ - public: - +class MapSkipper : public Resolver { +public: MapSkipper(ResolverFactory &factory, const NodePtr &writer); - virtual void parse(Reader &reader, uint8_t *address) const - { + void parse(Reader &reader, uint8_t *address) const final { DEBUG_OUT("Skipping map"); std::string key; - int64_t size = 0; + int64_t size; do { size = reader.readMapBlockSize(); - for(int64_t i = 0; i < size; ++i) { + for (auto i = 0; i < size; ++i) { reader.readValue(key); resolver_->parse(reader, address); } } while (size != 0); } - protected: - +protected: ResolverPtr resolver_; }; - -class MapParser : public Resolver -{ - public: - +class MapParser : public Resolver { +public: typedef uint8_t *(*GenericMapSetter)(uint8_t *map, const std::string &key); MapParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets); - virtual void parse(Reader &reader, uint8_t *address) const - { + void parse(Reader &reader, uint8_t *address) const final { DEBUG_OUT("Reading map"); uint8_t *mapAddress = address + offset_; std::string key; - GenericMapSetter* setter = reinterpret_cast<GenericMapSetter *> (address + setFuncOffset_); + auto *setter = reinterpret_cast<GenericMapSetter *>(address + setFuncOffset_); - int64_t size = 0; + int64_t size; do { size = reader.readMapBlockSize(); - for(int64_t i = 0; i < size; ++i) { + for (auto i = 0; i < size; ++i) { reader.readValue(key); // create a new map entry and get the address @@ -265,57 +216,49 @@ class MapParser : public Resolver } while (size != 0); } - protected: - - ResolverPtr resolver_; - size_t offset_; - size_t setFuncOffset_; +protected: + ResolverPtr resolver_; + size_t offset_; + size_t setFuncOffset_; }; -class ArraySkipper : public Resolver -{ - public: - +class ArraySkipper : public Resolver { +public: ArraySkipper(ResolverFactory &factory, const NodePtr &writer); - virtual void parse(Reader &reader, uint8_t *address) const - { + void parse(Reader &reader, uint8_t *address) const final { DEBUG_OUT("Skipping array"); - int64_t size = 0; + int64_t size; do { size = reader.readArrayBlockSize(); - for(int64_t i = 0; i < size; ++i) { + for (auto i = 0; i < size; ++i) { resolver_->parse(reader, address); } } while (size != 0); } - protected: - +protected: ResolverPtr resolver_; }; typedef uint8_t *(*GenericArraySetter)(uint8_t *array); -class ArrayParser : public Resolver -{ - public: - +class ArrayParser : public Resolver { +public: ArrayParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets); - virtual void parse(Reader &reader, uint8_t *address) const - { + void parse(Reader &reader, uint8_t *address) const final { DEBUG_OUT("Reading array"); uint8_t *arrayAddress = address + offset_; - GenericArraySetter* setter = reinterpret_cast<GenericArraySetter *> (address + setFuncOffset_); + auto *setter = reinterpret_cast<GenericArraySetter *>(address + setFuncOffset_); - int64_t size = 0; + int64_t size; do { size = reader.readArrayBlockSize(); - for(int64_t i = 0; i < size; ++i) { + for (auto i = 0; i < size; ++i) { // create a new map entry and get the address uint8_t *location = (*setter)(arrayAddress); resolver_->parse(reader, location); @@ -323,50 +266,38 @@ class ArrayParser : public Resolver } while (size != 0); } - protected: - - ArrayParser() : - Resolver() - {} +protected: + ArrayParser() : Resolver(), offset_(0), setFuncOffset_(0) {} ResolverPtr resolver_; - size_t offset_; - size_t setFuncOffset_; + size_t offset_; + size_t setFuncOffset_; }; -class EnumSkipper : public Resolver -{ - public: - - EnumSkipper(ResolverFactory &factory, const NodePtr &writer) : - Resolver() - { } +class EnumSkipper : public Resolver { +public: + EnumSkipper(ResolverFactory &factory, const NodePtr &writer) : Resolver() {} - virtual void parse(Reader &reader, uint8_t *address) const - { + void parse(Reader &reader, uint8_t *address) const final { int64_t val = reader.readEnum(); DEBUG_OUT("Skipping enum" << val); } }; -class EnumParser : public Resolver -{ - public: - +class EnumParser : public Resolver { +public: enum EnumRepresentation { VAL }; - EnumParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets) : - Resolver(), - offset_(offsets.at(0).offset()), - readerSize_(reader->names()) - { + EnumParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets) : Resolver(), + offset_(offsets.at(0).offset()), + readerSize_(reader->names()) { const size_t writerSize = writer->names(); mapping_.reserve(writerSize); - for(size_t i = 0; i < writerSize; ++i) { + for (size_t i = 0; i < writerSize; ++i) { const std::string &name = writer->nameAt(i); size_t readerIndex = readerSize_; reader->nameIndex(name, readerIndex); @@ -374,69 +305,57 @@ class EnumParser : public Resolver } } - virtual void parse(Reader &reader, uint8_t *address) const - { - size_t val = static_cast<size_t>(reader.readEnum()); + void parse(Reader &reader, uint8_t *address) const final { + auto val = static_cast<size_t>(reader.readEnum()); assert(static_cast<size_t>(val) < mapping_.size()); - if(mapping_[val] < readerSize_) { - EnumRepresentation* location = reinterpret_cast<EnumRepresentation *> (address + offset_); + if (mapping_[val] < readerSize_) { + auto *location = reinterpret_cast<EnumRepresentation *>(address + offset_); *location = static_cast<EnumRepresentation>(mapping_[val]); DEBUG_OUT("Setting enum" << *location); } } protected: - size_t offset_; size_t readerSize_; std::vector<size_t> mapping_; - }; -class UnionSkipper : public Resolver -{ - public: - +class UnionSkipper : public Resolver { +public: UnionSkipper(ResolverFactory &factory, const NodePtr &writer); - virtual void parse(Reader &reader, uint8_t *address) const - { + void parse(Reader &reader, uint8_t *address) const final { DEBUG_OUT("Skipping union"); - size_t choice = static_cast<size_t>(reader.readUnion()); + auto choice = static_cast<size_t>(reader.readUnion()); resolvers_[choice]->parse(reader, address); } - protected: - +protected: ResolverPtrVector resolvers_; }; - -class UnionParser : public Resolver -{ - public: - +class UnionParser : public Resolver { +public: typedef uint8_t *(*GenericUnionSetter)(uint8_t *, int64_t); UnionParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets); - virtual void parse(Reader &reader, uint8_t *address) const - { + void parse(Reader &reader, uint8_t *address) const final { DEBUG_OUT("Reading union"); - size_t writerChoice = static_cast<size_t>(reader.readUnion()); - int64_t *readerChoice = reinterpret_cast<int64_t *>(address + choiceOffset_); + auto writerChoice = static_cast<size_t>(reader.readUnion()); + auto *readerChoice = reinterpret_cast<int64_t *>(address + choiceOffset_); *readerChoice = choiceMapping_[writerChoice]; - GenericUnionSetter* setter = reinterpret_cast<GenericUnionSetter *> (address + setFuncOffset_); - uint8_t *value = reinterpret_cast<uint8_t *> (address + offset_); + auto *setter = reinterpret_cast<GenericUnionSetter *>(address + setFuncOffset_); + auto *value = reinterpret_cast<uint8_t *>(address + offset_); uint8_t *location = (*setter)(value, *readerChoice); resolvers_[writerChoice]->parse(reader, location); } - protected: - +protected: ResolverPtrVector resolvers_; std::vector<int64_t> choiceMapping_; size_t offset_; @@ -444,49 +363,47 @@ class UnionParser : public Resolver size_t setFuncOffset_; }; -class UnionToNonUnionParser : public Resolver -{ - public: - +class UnionToNonUnionParser : public Resolver { +public: typedef uint8_t *(*GenericUnionSetter)(uint8_t *, int64_t); - UnionToNonUnionParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const Layout &offsets); + UnionToNonUnionParser(ResolverFactory &factory, + const NodePtr &writer, + const NodePtr &reader, + const Layout &offsets); - virtual void parse(Reader &reader, uint8_t *address) const - { + void parse(Reader &reader, uint8_t *address) const final { DEBUG_OUT("Reading union to non-union"); - size_t choice = static_cast<size_t>(reader.readUnion()); + auto choice = static_cast<size_t>(reader.readUnion()); resolvers_[choice]->parse(reader, address); } - protected: - +protected: ResolverPtrVector resolvers_; }; -class NonUnionToUnionParser : public Resolver -{ - public: - +class NonUnionToUnionParser : public Resolver { +public: typedef uint8_t *(*GenericUnionSetter)(uint8_t *, int64_t); - NonUnionToUnionParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets); + NonUnionToUnionParser(ResolverFactory &factory, + const NodePtr &writer, + const NodePtr &reader, + const CompoundLayout &offsets); - virtual void parse(Reader &reader, uint8_t *address) const - { + void parse(Reader &reader, uint8_t *address) const final { DEBUG_OUT("Reading non-union to union"); - int64_t *choice = reinterpret_cast<int64_t *>(address + choiceOffset_); + auto *choice = reinterpret_cast<int64_t *>(address + choiceOffset_); *choice = choice_; - GenericUnionSetter* setter = reinterpret_cast<GenericUnionSetter *> (address + setFuncOffset_); - uint8_t *value = reinterpret_cast<uint8_t *> (address + offset_); + auto *setter = reinterpret_cast<GenericUnionSetter *>(address + setFuncOffset_); + auto *value = reinterpret_cast<uint8_t *>(address + offset_); uint8_t *location = (*setter)(value, choice_); resolver_->parse(reader, location); } - protected: - +protected: ResolverPtr resolver_; size_t choice_; size_t offset_; @@ -494,96 +411,73 @@ class NonUnionToUnionParser : public Resolver size_t setFuncOffset_; }; -class FixedSkipper : public Resolver -{ - public: - - FixedSkipper(ResolverFactory &factory, const NodePtr &writer) : - Resolver() - { +class FixedSkipper : public Resolver { +public: + FixedSkipper(ResolverFactory &factory, const NodePtr &writer) : Resolver() { size_ = writer->fixedSize(); } - virtual void parse(Reader &reader, uint8_t *address) const - { + void parse(Reader &reader, uint8_t *address) const final { DEBUG_OUT("Skipping fixed"); std::unique_ptr<uint8_t[]> val(new uint8_t[size_]); reader.readFixed(&val[0], size_); } - protected: - +protected: int size_; - }; -class FixedParser : public Resolver -{ - public: - - FixedParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets) : - Resolver() - { +class FixedParser : public Resolver { +public: + FixedParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets) : Resolver() { size_ = writer->fixedSize(); offset_ = offsets.at(0).offset(); } - virtual void parse(Reader &reader, uint8_t *address) const - { + void parse(Reader &reader, uint8_t *address) const final { DEBUG_OUT("Reading fixed"); - uint8_t *location = reinterpret_cast<uint8_t *> (address + offset_); + auto *location = reinterpret_cast<uint8_t *>(address + offset_); reader.readFixed(location, size_); } - protected: - +protected: int size_; size_t offset_; - }; - class ResolverFactory : private boost::noncopyable { template<typename T> unique_ptr<Resolver> - constructPrimitiveSkipper(const NodePtr &writer) - { + constructPrimitiveSkipper(const NodePtr &writer) { return unique_ptr<Resolver>(new PrimitiveSkipper<T>()); } template<typename T> unique_ptr<Resolver> - constructPrimitive(const NodePtr &writer, const NodePtr &reader, const Layout &offset) - { + constructPrimitive(const NodePtr &writer, const NodePtr &reader, const Layout &offset) { unique_ptr<Resolver> instruction; SchemaResolution match = writer->resolve(*reader); if (match == RESOLVE_NO_MATCH) { instruction = unique_ptr<Resolver>(new PrimitiveSkipper<T>()); - } - else if (reader->type() == AVRO_UNION) { - const CompoundLayout &compoundLayout = static_cast<const CompoundLayout &>(offset); + } else if (reader->type() == AVRO_UNION) { + const auto &compoundLayout = static_cast<const CompoundLayout &>(offset); instruction = unique_ptr<Resolver>(new NonUnionToUnionParser(*this, writer, reader, compoundLayout)); - } - else if (match == RESOLVE_MATCH) { - const PrimitiveLayout &primitiveLayout = static_cast<const PrimitiveLayout &>(offset); + } else if (match == RESOLVE_MATCH) { + const auto &primitiveLayout = static_cast<const PrimitiveLayout &>(offset); instruction = unique_ptr<Resolver>(new PrimitiveParser<T>(primitiveLayout)); - } - else if(match == RESOLVE_PROMOTABLE_TO_LONG) { - const PrimitiveLayout &primitiveLayout = static_cast<const PrimitiveLayout &>(offset); + } else if (match == RESOLVE_PROMOTABLE_TO_LONG) { + const auto &primitiveLayout = static_cast<const PrimitiveLayout &>(offset); instruction = unique_ptr<Resolver>(new PrimitivePromoter<T, int64_t>(primitiveLayout)); - } - else if(match == RESOLVE_PROMOTABLE_TO_FLOAT) { - const PrimitiveLayout &primitiveLayout = static_cast<const PrimitiveLayout &>(offset); + } else if (match == RESOLVE_PROMOTABLE_TO_FLOAT) { + const auto &primitiveLayout = static_cast<const PrimitiveLayout &>(offset); instruction = unique_ptr<Resolver>(new PrimitivePromoter<T, float>(primitiveLayout)); - } - else if(match == RESOLVE_PROMOTABLE_TO_DOUBLE) { - const PrimitiveLayout &primitiveLayout = static_cast<const PrimitiveLayout &>(offset); + } else if (match == RESOLVE_PROMOTABLE_TO_DOUBLE) { + const auto &primitiveLayout = static_cast<const PrimitiveLayout &>(offset); instruction = unique_ptr<Resolver>(new PrimitivePromoter<T, double>(primitiveLayout)); - } - else { + } else { assert(0); } return instruction; @@ -591,57 +485,45 @@ class ResolverFactory : private boost::noncopyable { template<typename Skipper> unique_ptr<Resolver> - constructCompoundSkipper(const NodePtr &writer) - { + constructCompoundSkipper(const NodePtr &writer) { return unique_ptr<Resolver>(new Skipper(*this, writer)); } - template<typename Parser, typename Skipper> unique_ptr<Resolver> - constructCompound(const NodePtr &writer, const NodePtr &reader, const Layout &offset) - { + constructCompound(const NodePtr &writer, const NodePtr &reader, const Layout &offset) { unique_ptr<Resolver> instruction; - SchemaResolution match = RESOLVE_NO_MATCH; - - match = writer->resolve(*reader); + avro::SchemaResolution match = writer->resolve(*reader); if (match == RESOLVE_NO_MATCH) { instruction = unique_ptr<Resolver>(new Skipper(*this, writer)); - } - else if(writer->type() != AVRO_UNION && reader->type() == AVRO_UNION) { - const CompoundLayout &compoundLayout = dynamic_cast<const CompoundLayout &>(offset); + } else if (writer->type() != AVRO_UNION && reader->type() == AVRO_UNION) { + const auto &compoundLayout = dynamic_cast<const CompoundLayout &>(offset); instruction = unique_ptr<Resolver>(new NonUnionToUnionParser(*this, writer, reader, compoundLayout)); - } - else if(writer->type() == AVRO_UNION && reader->type() != AVRO_UNION) { + } else if (writer->type() == AVRO_UNION && reader->type() != AVRO_UNION) { instruction = unique_ptr<Resolver>(new UnionToNonUnionParser(*this, writer, reader, offset)); - } - else { - const CompoundLayout &compoundLayout = dynamic_cast<const CompoundLayout &>(offset); + } else { + const auto &compoundLayout = dynamic_cast<const CompoundLayout &>(offset); instruction = unique_ptr<Resolver>(new Parser(*this, writer, reader, compoundLayout)); } return instruction; } - public: - +public: unique_ptr<Resolver> - construct(const NodePtr &writer, const NodePtr &reader, const Layout &offset) - { + construct(const NodePtr &writer, const NodePtr &reader, const Layout &offset) { typedef unique_ptr<Resolver> (ResolverFactory::*BuilderFunc)(const NodePtr &writer, const NodePtr &reader, const Layout &offset); - NodePtr currentWriter = (writer->type() == AVRO_SYMBOLIC) ? - resolveSymbol(writer) : writer; + NodePtr currentWriter = (writer->type() == AVRO_SYMBOLIC) ? resolveSymbol(writer) : writer; - NodePtr currentReader = (reader->type() == AVRO_SYMBOLIC) ? - resolveSymbol(reader) : reader; + NodePtr currentReader = (reader->type() == AVRO_SYMBOLIC) ? resolveSymbol(reader) : reader; static const BuilderFunc funcs[] = { &ResolverFactory::constructPrimitive<std::string>, - &ResolverFactory::constructPrimitive<std::vector<uint8_t> >, + &ResolverFactory::constructPrimitive<std::vector<uint8_t>>, &ResolverFactory::constructPrimitive<int32_t>, &ResolverFactory::constructPrimitive<int64_t>, &ResolverFactory::constructPrimitive<float>, @@ -653,30 +535,27 @@ class ResolverFactory : private boost::noncopyable { &ResolverFactory::constructCompound<ArrayParser, ArraySkipper>, &ResolverFactory::constructCompound<MapParser, MapSkipper>, &ResolverFactory::constructCompound<UnionParser, UnionSkipper>, - &ResolverFactory::constructCompound<FixedParser, FixedSkipper> - }; + &ResolverFactory::constructCompound<FixedParser, FixedSkipper>}; - static_assert((sizeof(funcs)/sizeof(BuilderFunc)) == (AVRO_NUM_TYPES), - "Invalid number of builder functions"); + static_assert((sizeof(funcs) / sizeof(BuilderFunc)) == (AVRO_NUM_TYPES), + "Invalid number of builder functions"); BuilderFunc func = funcs[currentWriter->type()]; assert(func); - return ((this)->*(func))(currentWriter, currentReader, offset); + return ((this)->*(func))(currentWriter, currentReader, offset); } unique_ptr<Resolver> - skipper(const NodePtr &writer) - { + skipper(const NodePtr &writer) { typedef unique_ptr<Resolver> (ResolverFactory::*BuilderFunc)(const NodePtr &writer); - NodePtr currentWriter = (writer->type() == AVRO_SYMBOLIC) ? - writer->leafAt(0) : writer; + NodePtr currentWriter = (writer->type() == AVRO_SYMBOLIC) ? writer->leafAt(0) : writer; static const BuilderFunc funcs[] = { &ResolverFactory::constructPrimitiveSkipper<std::string>, - &ResolverFactory::constructPrimitiveSkipper<std::vector<uint8_t> >, + &ResolverFactory::constructPrimitiveSkipper<std::vector<uint8_t>>, &ResolverFactory::constructPrimitiveSkipper<int32_t>, &ResolverFactory::constructPrimitiveSkipper<int64_t>, &ResolverFactory::constructPrimitiveSkipper<float>, @@ -688,37 +567,34 @@ class ResolverFactory : private boost::noncopyable { &ResolverFactory::constructCompoundSkipper<ArraySkipper>, &ResolverFactory::constructCompoundSkipper<MapSkipper>, &ResolverFactory::constructCompoundSkipper<UnionSkipper>, - &ResolverFactory::constructCompoundSkipper<FixedSkipper> - }; + &ResolverFactory::constructCompoundSkipper<FixedSkipper>}; - static_assert((sizeof(funcs)/sizeof(BuilderFunc)) == (AVRO_NUM_TYPES), - "Invalid number of builder functions"); + static_assert((sizeof(funcs) / sizeof(BuilderFunc)) == (AVRO_NUM_TYPES), + "Invalid number of builder functions"); BuilderFunc func = funcs[currentWriter->type()]; assert(func); - return ((this)->*(func))(currentWriter); + return ((this)->*(func))(currentWriter); } }; - -RecordSkipper::RecordSkipper(ResolverFactory &factory, const NodePtr &writer) : - Resolver() -{ +RecordSkipper::RecordSkipper(ResolverFactory &factory, const NodePtr &writer) : Resolver() { size_t leaves = writer->leaves(); resolvers_.reserve(leaves); - for(size_t i = 0; i < leaves; ++i) { + for (size_t i = 0; i < leaves; ++i) { const NodePtr &w = writer->leafAt(i); resolvers_.push_back(factory.skipper(w)); } } -RecordParser::RecordParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets) : - Resolver() -{ +RecordParser::RecordParser(ResolverFactory &factory, + const NodePtr &writer, + const NodePtr &reader, + const CompoundLayout &offsets) : Resolver() { size_t leaves = writer->leaves(); resolvers_.reserve(leaves); - for(size_t i = 0; i < leaves; ++i) { + for (size_t i = 0; i < leaves; ++i) { const NodePtr &w = writer->leafAt(i); @@ -727,74 +603,68 @@ RecordParser::RecordParser(ResolverFactory &factory, const NodePtr &writer, cons size_t readerIndex = 0; bool found = reader->nameIndex(name, readerIndex); - if(found) { + if (found) { const NodePtr &r = reader->leafAt(readerIndex); resolvers_.push_back(factory.construct(w, r, offsets.at(readerIndex))); - } - else { + } else { resolvers_.push_back(factory.skipper(w)); } } } -MapSkipper::MapSkipper(ResolverFactory &factory, const NodePtr &writer) : - Resolver(), - resolver_(factory.skipper(writer->leafAt(1))) -{ } - -MapParser::MapParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets) : - Resolver(), - resolver_(factory.construct(writer->leafAt(1), reader->leafAt(1), offsets.at(1))), - offset_(offsets.offset()), - setFuncOffset_( offsets.at(0).offset()) -{ } - -ArraySkipper::ArraySkipper(ResolverFactory &factory, const NodePtr &writer) : - Resolver(), - resolver_(factory.skipper(writer->leafAt(0))) -{ } - -ArrayParser::ArrayParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets) : - Resolver(), - resolver_(factory.construct(writer->leafAt(0), reader->leafAt(0), offsets.at(1))), - offset_(offsets.offset()), - setFuncOffset_(offsets.at(0).offset()) -{ } - -UnionSkipper::UnionSkipper(ResolverFactory &factory, const NodePtr &writer) : - Resolver() -{ +MapSkipper::MapSkipper(ResolverFactory &factory, const NodePtr &writer) : Resolver(), + resolver_(factory.skipper(writer->leafAt(1))) {} + +MapParser::MapParser(ResolverFactory &factory, + const NodePtr &writer, + const NodePtr &reader, + const CompoundLayout &offsets) : Resolver(), + resolver_(factory.construct(writer->leafAt(1), reader->leafAt(1), offsets.at(1))), + offset_(offsets.offset()), + setFuncOffset_(offsets.at(0).offset()) {} + +ArraySkipper::ArraySkipper(ResolverFactory &factory, const NodePtr &writer) : Resolver(), + resolver_(factory.skipper(writer->leafAt(0))) {} + +ArrayParser::ArrayParser(ResolverFactory &factory, + const NodePtr &writer, + const NodePtr &reader, + const CompoundLayout &offsets) : Resolver(), + resolver_(factory.construct(writer->leafAt(0), reader->leafAt(0), offsets.at(1))), + offset_(offsets.offset()), + setFuncOffset_(offsets.at(0).offset()) {} + +UnionSkipper::UnionSkipper(ResolverFactory &factory, const NodePtr &writer) : Resolver() { size_t leaves = writer->leaves(); resolvers_.reserve(leaves); - for(size_t i = 0; i < leaves; ++i) { - const NodePtr &w = writer->leafAt(i); + for (size_t i = 0; i < leaves; ++i) { + const NodePtr &w = writer->leafAt(i); resolvers_.push_back(factory.skipper(w)); } } namespace { -// asumes the writer is NOT a union, and the reader IS a union +// assumes the writer is NOT a union, and the reader IS a union SchemaResolution -checkUnionMatch(const NodePtr &writer, const NodePtr &reader, size_t &index) -{ +checkUnionMatch(const NodePtr &writer, const NodePtr &reader, size_t &index) { SchemaResolution bestMatch = RESOLVE_NO_MATCH; index = 0; size_t leaves = reader->leaves(); - for(size_t i=0; i < leaves; ++i) { + for (size_t i = 0; i < leaves; ++i) { const NodePtr &leaf = reader->leafAt(i); SchemaResolution newMatch = writer->resolve(*leaf); - if(newMatch == RESOLVE_MATCH) { + if (newMatch == RESOLVE_MATCH) { bestMatch = newMatch; index = i; break; } - if(bestMatch == RESOLVE_NO_MATCH) { + if (bestMatch == RESOLVE_NO_MATCH) { bestMatch = newMatch; index = i; } @@ -803,19 +673,20 @@ checkUnionMatch(const NodePtr &writer, const NodePtr &reader, size_t &index) return bestMatch; } -}; +} // namespace -UnionParser::UnionParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets) : - Resolver(), - offset_(offsets.offset()), - choiceOffset_(offsets.at(0).offset()), - setFuncOffset_(offsets.at(1).offset()) -{ +UnionParser::UnionParser(ResolverFactory &factory, + const NodePtr &writer, + const NodePtr &reader, + const CompoundLayout &offsets) : Resolver(), + offset_(offsets.offset()), + choiceOffset_(offsets.at(0).offset()), + setFuncOffset_(offsets.at(1).offset()) { size_t leaves = writer->leaves(); resolvers_.reserve(leaves); choiceMapping_.reserve(leaves); - for(size_t i = 0; i < leaves; ++i) { + for (size_t i = 0; i < leaves; ++i) { // for each writer, we need a schema match for the reader const NodePtr &w = writer->leafAt(i); @@ -823,48 +694,49 @@ UnionParser::UnionParser(ResolverFactory &factory, const NodePtr &writer, const SchemaResolution match = checkUnionMatch(w, reader, index); - if(match == RESOLVE_NO_MATCH) { + if (match == RESOLVE_NO_MATCH) { resolvers_.push_back(factory.skipper(w)); - // push back a non-sensical number + // push back a non-sense number choiceMapping_.push_back(reader->leaves()); - } - else { + } else { const NodePtr &r = reader->leafAt(index); - resolvers_.push_back(factory.construct(w, r, offsets.at(index+2))); + resolvers_.push_back(factory.construct(w, r, offsets.at(index + 2))); choiceMapping_.push_back(index); } } } -NonUnionToUnionParser::NonUnionToUnionParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets) : - Resolver(), - offset_(offsets.offset()), - choiceOffset_(offsets.at(0).offset()), - setFuncOffset_(offsets.at(1).offset()) -{ +NonUnionToUnionParser::NonUnionToUnionParser(ResolverFactory &factory, + const NodePtr &writer, + const NodePtr &reader, + const CompoundLayout &offsets) : Resolver(), + offset_(offsets.offset()), + choice_(0), + choiceOffset_(offsets.at(0).offset()), + setFuncOffset_(offsets.at(1).offset()) { #ifndef NDEBUG SchemaResolution bestMatch = #endif - checkUnionMatch(writer, reader, choice_); + checkUnionMatch(writer, reader, choice_); assert(bestMatch != RESOLVE_NO_MATCH); - resolver_ = factory.construct(writer, reader->leafAt(choice_), offsets.at(choice_+2)); + resolver_ = factory.construct(writer, reader->leafAt(choice_), offsets.at(choice_ + 2)); } -UnionToNonUnionParser::UnionToNonUnionParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const Layout &offsets) : - Resolver() -{ +UnionToNonUnionParser::UnionToNonUnionParser(ResolverFactory &factory, + const NodePtr &writer, + const NodePtr &reader, + const Layout &offsets) : Resolver() { size_t leaves = writer->leaves(); resolvers_.reserve(leaves); - for(size_t i = 0; i < leaves; ++i) { + for (size_t i = 0; i < leaves; ++i) { const NodePtr &w = writer->leafAt(i); resolvers_.push_back(factory.construct(w, reader, offsets)); } } unique_ptr<Resolver> constructResolver(const ValidSchema &writerSchema, - const ValidSchema &readerSchema, - const Layout &readerLayout) -{ + const ValidSchema &readerSchema, + const Layout &readerLayout) { ResolverFactory factory; return factory.construct(writerSchema.root(), readerSchema.root(), readerLayout); } diff --git a/contrib/libs/apache/avro/impl/ResolverSchema.cc b/contrib/libs/apache/avro/impl/ResolverSchema.cc index f42946d6929..7ddea33d91b 100644 --- a/contrib/libs/apache/avro/impl/ResolverSchema.cc +++ b/contrib/libs/apache/avro/impl/ResolverSchema.cc @@ -24,15 +24,11 @@ namespace avro { ResolverSchema::ResolverSchema( - const ValidSchema &writerSchema, - const ValidSchema &readerSchema, - const Layout &readerLayout) : - resolver_(constructResolver(writerSchema, readerSchema, readerLayout)) -{ } + const ValidSchema &writerSchema, + const ValidSchema &readerSchema, + const Layout &readerLayout) : resolver_(constructResolver(writerSchema, readerSchema, readerLayout)) {} -void -ResolverSchema::parse(Reader &reader, uint8_t *address) -{ +void ResolverSchema::parse(Reader &reader, uint8_t *address) { resolver_->parse(reader, address); } diff --git a/contrib/libs/apache/avro/impl/Schema.cc b/contrib/libs/apache/avro/impl/Schema.cc index e6cfa45c2e8..42245292e67 100644 --- a/contrib/libs/apache/avro/impl/Schema.cc +++ b/contrib/libs/apache/avro/impl/Schema.cc @@ -16,34 +16,17 @@ * limitations under the License. */ +#include <utility> #include "Schema.hh" namespace avro { -Schema::Schema() -{ } - -Schema::~Schema() -{ } - -Schema::Schema(const NodePtr &node) : - node_(node) -{ } - -Schema::Schema(Node *node) : - node_(node) -{ } - -RecordSchema::RecordSchema(const std::string &name) : - Schema(new NodeRecord) -{ - node_->setName(name); +RecordSchema::RecordSchema(const std::string &name) : Schema(new NodeRecord) { + node_->setName(Name(name)); } -void -RecordSchema::addField(const std::string &name, const Schema &fieldSchema) -{ +void RecordSchema::addField(const std::string &name, const Schema &fieldSchema) { // add the name first. it will throw if the name is a duplicate, preventing // the leaf from being added node_->addName(name); @@ -51,69 +34,51 @@ RecordSchema::addField(const std::string &name, const Schema &fieldSchema) node_->addLeaf(fieldSchema.root()); } -std::string RecordSchema::getDoc() const -{ +std::string RecordSchema::getDoc() const { return node_->getDoc(); } -void RecordSchema::setDoc(const std::string& doc) -{ +void RecordSchema::setDoc(const std::string &doc) { node_->setDoc(doc); } -EnumSchema::EnumSchema(const std::string &name) : - Schema(new NodeEnum) -{ - node_->setName(name); +EnumSchema::EnumSchema(const std::string &name) : Schema(new NodeEnum) { + node_->setName(Name(name)); } -void -EnumSchema::addSymbol(const std::string &symbol) -{ +void EnumSchema::addSymbol(const std::string &symbol) { node_->addName(symbol); } -ArraySchema::ArraySchema(const Schema &itemsSchema) : - Schema(new NodeArray) -{ +ArraySchema::ArraySchema(const Schema &itemsSchema) : Schema(new NodeArray) { node_->addLeaf(itemsSchema.root()); } -ArraySchema::ArraySchema(const ArraySchema &itemsSchema) : - Schema(new NodeArray) -{ +ArraySchema::ArraySchema(const ArraySchema &itemsSchema) : Schema(new NodeArray) { node_->addLeaf(itemsSchema.root()); } -MapSchema::MapSchema(const Schema &valuesSchema) : - Schema(new NodeMap) -{ +MapSchema::MapSchema(const Schema &valuesSchema) : Schema(new NodeMap) { node_->addLeaf(valuesSchema.root()); } -MapSchema::MapSchema(const MapSchema &valuesSchema) : - Schema(new NodeMap) -{ +MapSchema::MapSchema(const MapSchema &valuesSchema) : Schema(new NodeMap) { node_->addLeaf(valuesSchema.root()); } -UnionSchema::UnionSchema() : - Schema(new NodeUnion) -{ } +UnionSchema::UnionSchema() : Schema(new NodeUnion) {} -void -UnionSchema::addType(const Schema &typeSchema) -{ - if(typeSchema.type() == AVRO_UNION) { +void UnionSchema::addType(const Schema &typeSchema) { + if (typeSchema.type() == AVRO_UNION) { throw Exception("Cannot add unions to unions"); } - if(typeSchema.type() == AVRO_RECORD) { + if (typeSchema.type() == AVRO_RECORD) { // check for duplicate records size_t types = node_->leaves(); - for(size_t i = 0; i < types; ++i) { + for (size_t i = 0; i < types; ++i) { const NodePtr &leaf = node_->leafAt(i); // TODO, more checks? - if(leaf->type() == AVRO_RECORD && leaf->name() == typeSchema.root()->name()) { + if (leaf->type() == AVRO_RECORD && leaf->name() == typeSchema.root()->name()) { throw Exception("Records in unions cannot have duplicate names"); } } @@ -122,18 +87,12 @@ UnionSchema::addType(const Schema &typeSchema) node_->addLeaf(typeSchema.root()); } -FixedSchema::FixedSchema(int size, const std::string &name) : - Schema(new NodeFixed) -{ +FixedSchema::FixedSchema(int size, const std::string &name) : Schema(new NodeFixed) { node_->setFixedSize(size); - node_->setName(name); + node_->setName(Name(name)); } -SymbolicSchema::SymbolicSchema(const Name &name, const NodePtr& link) : - Schema(new NodeSymbolic(HasName(name), link)) -{ +SymbolicSchema::SymbolicSchema(const Name &name, const NodePtr &link) : Schema(new NodeSymbolic(HasName(name), link)) { } - - } // namespace avro diff --git a/contrib/libs/apache/avro/impl/Stream.cc b/contrib/libs/apache/avro/impl/Stream.cc index 7023f3f2136..63a8b4e8fc5 100644 --- a/contrib/libs/apache/avro/impl/Stream.cc +++ b/contrib/libs/apache/avro/impl/Stream.cc @@ -24,7 +24,7 @@ namespace avro { using std::vector; class MemoryInputStream : public InputStream { - const std::vector<uint8_t*>& data_; + const std::vector<uint8_t *> &data_; const size_t chunkSize_; const size_t size_; const size_t available_; @@ -45,12 +45,11 @@ class MemoryInputStream : public InputStream { } public: - MemoryInputStream(const std::vector<uint8_t*>& b, - size_t chunkSize, size_t available) : - data_(b), chunkSize_(chunkSize), size_(b.size()), - available_(available), cur_(0), curLen_(0) { } + MemoryInputStream(const std::vector<uint8_t *> &b, + size_t chunkSize, size_t available) : data_(b), chunkSize_(chunkSize), size_(b.size()), + available_(available), cur_(0), curLen_(0) {} - bool next(const uint8_t** data, size_t* len) { + bool next(const uint8_t **data, size_t *len) final { if (size_t n = maxLen()) { *data = data_[cur_] + curLen_; *len = n - curLen_; @@ -60,11 +59,11 @@ public: return false; } - void backup(size_t len) { + void backup(size_t len) final { curLen_ -= len; } - void skip(size_t len) { + void skip(size_t len) final { while (len > 0) { if (size_t n = maxLen()) { if ((curLen_ + len) < n) { @@ -78,20 +77,21 @@ public: } } - size_t byteCount() const { + size_t byteCount() const final { return cur_ * chunkSize_ + curLen_; } }; class MemoryInputStream2 : public InputStream { - const uint8_t* const data_; + const uint8_t *const data_; const size_t size_; size_t curLen_; + public: MemoryInputStream2(const uint8_t *data, size_t len) - : data_(data), size_(len), curLen_(0) { } + : data_(data), size_(len), curLen_(0) {} - bool next(const uint8_t** data, size_t* len) { + bool next(const uint8_t **data, size_t *len) final { if (curLen_ == size_) { return false; } @@ -101,18 +101,18 @@ public: return true; } - void backup(size_t len) { + void backup(size_t len) final { curLen_ -= len; } - void skip(size_t len) { + void skip(size_t len) final { if (len > (size_ - curLen_)) { len = size_ - curLen_; } curLen_ += len; } - size_t byteCount() const { + size_t byteCount() const final { return curLen_; } }; @@ -120,20 +120,20 @@ public: class MemoryOutputStream : public OutputStream { public: const size_t chunkSize_; - std::vector<uint8_t*> data_; + std::vector<uint8_t *> data_; size_t available_; size_t byteCount_; - MemoryOutputStream(size_t chunkSize) : chunkSize_(chunkSize), - available_(0), byteCount_(0) { } - ~MemoryOutputStream() { - for (std::vector<uint8_t*>::const_iterator it = data_.begin(); - it != data_.end(); ++it) { - delete[] *it; + explicit MemoryOutputStream(size_t chunkSize) : chunkSize_(chunkSize), + available_(0), byteCount_(0) {} + ~MemoryOutputStream() final { + for (std::vector<uint8_t *>::const_iterator it = data_.begin(); + it != data_.end(); ++it) { + delete[] * it; } } - bool next(uint8_t** data, size_t* len) { + bool next(uint8_t **data, size_t *len) final { if (available_ == 0) { data_.push_back(new uint8_t[chunkSize_]); available_ = chunkSize_; @@ -145,48 +145,40 @@ public: return true; } - void backup(size_t len) { + void backup(size_t len) final { available_ += len; byteCount_ -= len; } - uint64_t byteCount() const { + uint64_t byteCount() const final { return byteCount_; } - void flush() { } + void flush() final {} }; -std::unique_ptr<OutputStream> memoryOutputStream(size_t chunkSize) -{ +std::unique_ptr<OutputStream> memoryOutputStream(size_t chunkSize) { return std::unique_ptr<OutputStream>(new MemoryOutputStream(chunkSize)); } -std::unique_ptr<InputStream> memoryInputStream(const uint8_t* data, size_t len) -{ +std::unique_ptr<InputStream> memoryInputStream(const uint8_t *data, size_t len) { return std::unique_ptr<InputStream>(new MemoryInputStream2(data, len)); } -std::unique_ptr<InputStream> memoryInputStream(const OutputStream& source) -{ - const MemoryOutputStream& mos = - dynamic_cast<const MemoryOutputStream&>(source); - return (mos.data_.empty()) ? - std::unique_ptr<InputStream>(new MemoryInputStream2(0, 0)) : - std::unique_ptr<InputStream>(new MemoryInputStream(mos.data_, - mos.chunkSize_, - (mos.chunkSize_ - mos.available_))); +std::unique_ptr<InputStream> memoryInputStream(const OutputStream &source) { + const auto &mos = + dynamic_cast<const MemoryOutputStream &>(source); + return (mos.data_.empty()) ? std::unique_ptr<InputStream>(new MemoryInputStream2(nullptr, 0)) : std::unique_ptr<InputStream>(new MemoryInputStream(mos.data_, mos.chunkSize_, (mos.chunkSize_ - mos.available_))); } -std::shared_ptr<std::vector<uint8_t> > snapshot(const OutputStream& source) -{ - const MemoryOutputStream& mos = - dynamic_cast<const MemoryOutputStream&>(source); - std::shared_ptr<std::vector<uint8_t> > result(new std::vector<uint8_t>()); +std::shared_ptr<std::vector<uint8_t>> snapshot(const OutputStream &source) { + const auto &mos = + dynamic_cast<const MemoryOutputStream &>(source); + std::shared_ptr<std::vector<uint8_t>> result(new std::vector<uint8_t>()); size_t c = mos.byteCount_; result->reserve(mos.byteCount_); - for (vector<uint8_t*>::const_iterator it = mos.data_.begin(); - it != mos.data_.end(); ++it) { + for (auto it = mos.data_.begin(); + it != mos.data_.end(); ++it) { size_t n = std::min(c, mos.chunkSize_); std::copy(*it, *it + n, std::back_inserter(*result)); c -= n; @@ -194,5 +186,4 @@ std::shared_ptr<std::vector<uint8_t> > snapshot(const OutputStream& source) return result; } -} // namespace avro - +} // namespace avro diff --git a/contrib/libs/apache/avro/impl/Types.cc b/contrib/libs/apache/avro/impl/Types.cc index 6c9d702b831..754e584e46a 100644 --- a/contrib/libs/apache/avro/impl/Types.cc +++ b/contrib/libs/apache/avro/impl/Types.cc @@ -16,12 +16,11 @@ * limitations under the License. */ +#include "Types.hh" #include <iostream> #include <string> -#include "Types.hh" namespace avro { - namespace strings { const std::string typeToString[] = { "string", @@ -38,21 +37,18 @@ const std::string typeToString[] = { "map", "union", "fixed", - "symbolic" -}; + "symbolic"}; -static_assert((sizeof(typeToString)/sizeof(std::string)) == (AVRO_NUM_TYPES+1), - "Incorrect Avro typeToString"); +static_assert((sizeof(typeToString) / sizeof(std::string)) == (AVRO_NUM_TYPES + 1), + "Incorrect Avro typeToString"); } // namespace strings - // this static assert exists because a 32 bit integer is used as a bit-flag for each type, // and it would be a problem for this flag if we ever supported more than 32 types static_assert(AVRO_NUM_TYPES < 32, "Too many Avro types"); -const std::string& toString(Type type) -{ +const std::string &toString(Type type) noexcept { static std::string undefinedType = "Undefined type"; if (isAvroTypeOrPseudoType(type)) { return strings::typeToString[type]; @@ -61,22 +57,18 @@ const std::string& toString(Type type) } } -std::ostream &operator<< (std::ostream &os, Type type) -{ - if(isAvroTypeOrPseudoType(type)) { +std::ostream &operator<<(std::ostream &os, Type type) { + if (isAvroTypeOrPseudoType(type)) { os << strings::typeToString[type]; - } - else { + } else { os << static_cast<int>(type); } return os; } -std::ostream &operator<< (std::ostream &os, const Null &) -{ +std::ostream &operator<<(std::ostream &os, const Null &) { os << "(null value)"; return os; } } // namespace avro - diff --git a/contrib/libs/apache/avro/impl/ValidSchema.cc b/contrib/libs/apache/avro/impl/ValidSchema.cc index 74a3f845c5e..63a3bbee919 100644 --- a/contrib/libs/apache/avro/impl/ValidSchema.cc +++ b/contrib/libs/apache/avro/impl/ValidSchema.cc @@ -19,37 +19,39 @@ #include <boost/format.hpp> #include <cctype> #include <sstream> +#include <utility> -#include "ValidSchema.hh" -#include "Schema.hh" #include "Node.hh" +#include "Schema.hh" +#include "ValidSchema.hh" -using std::string; -using std::ostringstream; -using std::make_pair; using boost::format; +using std::make_pair; +using std::ostringstream; using std::shared_ptr; using std::static_pointer_cast; +using std::string; namespace avro { -typedef std::map<Name, NodePtr> SymbolMap; +using SymbolMap = std::map<Name, NodePtr>; -static bool validate(const NodePtr &node, SymbolMap &symbolMap) -{ - if (! node->isValid()) { +static bool validate(const NodePtr &node, SymbolMap &symbolMap) { + if (!node->isValid()) { throw Exception(format("Schema is invalid, due to bad node of type %1%") - % node->type()); + % node->type()); } if (node->hasName()) { - const Name& nm = node->name(); - SymbolMap::iterator it = symbolMap.find(nm); - bool found = it != symbolMap.end() && nm == it->first; + const Name &nm = node->name(); + // FIXME: replace "find" with "lower_bound". The author seems to have intended + // "lower_bound" here because of (1) the check for the contents of the iterator + // that follows and (2) use of the iterator in insert later in the code. + auto it = symbolMap.find(nm); + auto found = it != symbolMap.end() && nm == it->first; if (node->type() == AVRO_SYMBOLIC) { - if (! found) { - throw Exception(format("Symbolic name \"%1%\" is unknown") % - node->name()); + if (!found) { + throw Exception(format("Symbolic name \"%1%\" is unknown") % node->name()); } shared_ptr<NodeSymbolic> symNode = @@ -67,16 +69,16 @@ static bool validate(const NodePtr &node, SymbolMap &symbolMap) } node->lock(); - size_t leaves = node->leaves(); - for (size_t i = 0; i < leaves; ++i) { + auto leaves = node->leaves(); + for (auto i = 0; i < leaves; ++i) { const NodePtr &leaf(node->leafAt(i)); - if (! validate(leaf, symbolMap)) { + if (!validate(leaf, symbolMap)) { // if validate returns false it means a node with this name already // existed in the map, instead of keeping this node twice in the // map (which could potentially create circular shared pointer - // links that could not be easily freed), replace this node with a + // links that would not be freed), replace this node with a // symbolic link to the original one. node->setLeafToSymbolic(i, symbolMap.find(leaf->name())->second); @@ -86,44 +88,35 @@ static bool validate(const NodePtr &node, SymbolMap &symbolMap) return true; } -static void validate(const NodePtr& p) -{ +static void validate(const NodePtr &p) { SymbolMap m; validate(p, m); } -ValidSchema::ValidSchema(const NodePtr &root) : root_(root) -{ +ValidSchema::ValidSchema(NodePtr root) : root_(std::move(root)) { validate(root_); } -ValidSchema::ValidSchema(const Schema &schema) : root_(schema.root()) -{ +ValidSchema::ValidSchema(const Schema &schema) : root_(schema.root()) { validate(root_); } -ValidSchema::ValidSchema() : root_(NullSchema().root()) -{ +ValidSchema::ValidSchema() : root_(NullSchema().root()) { validate(root_); } -void -ValidSchema::setSchema(const Schema &schema) -{ +void ValidSchema::setSchema(const Schema &schema) { root_ = schema.root(); validate(root_); } -void -ValidSchema::toJson(std::ostream &os) const -{ +void ValidSchema::toJson(std::ostream &os) const { root_->printJson(os, 0); os << '\n'; } string -ValidSchema::toJson(bool prettyPrint) const -{ +ValidSchema::toJson(bool prettyPrint) const { ostringstream oss; toJson(oss); if (!prettyPrint) { @@ -132,9 +125,7 @@ ValidSchema::toJson(bool prettyPrint) const return oss.str(); } -void -ValidSchema::toFlatList(std::ostream &os) const -{ +void ValidSchema::toFlatList(std::ostream &os) const { root_->printBasicInfo(os); } @@ -145,25 +136,25 @@ ValidSchema::toFlatList(std::ostream &os) const * in UTF-8 format. Note that this method is not responsible for validating * the schema. */ -string ValidSchema::compactSchema(const string& schema) { - bool insideQuote = false; +string ValidSchema::compactSchema(const string &schema) { + auto insideQuote = false; size_t newPos = 0; - string data(schema.data()); + string data = schema; - for (size_t currentPos = 0; currentPos < schema.size(); currentPos++) { - if (!insideQuote && std::isspace(data[currentPos])) { + for (auto c : schema) { + if (!insideQuote && std::isspace(c)) { // Skip the white spaces outside quotes. continue; } - if (data[currentPos] == '\"') { + if (c == '\"') { // It is valid for a quote to be part of the value for some fields, // e.g., the "doc" field. In that case, the quote is expected to be // escaped inside the schema. Since the escape character '\\' could // be escaped itself, we need to check whether there are an even // number of consecutive slashes prior to the quote. - int leadingSlashes = 0; - for (int i = newPos - 1; i >= 0; i--) { + auto leadingSlashes = 0; + for (int i = static_cast<int>(newPos) - 1; i >= 0; i--) { if (data[i] == '\\') { leadingSlashes++; } else { @@ -176,13 +167,11 @@ string ValidSchema::compactSchema(const string& schema) { insideQuote = !insideQuote; } } - data[newPos++] = data[currentPos]; + data[newPos++] = c; } - if (insideQuote) { throw Exception("Schema is not well formed with mismatched quotes"); } - if (newPos < schema.size()) { data.resize(newPos); } @@ -190,4 +179,3 @@ string ValidSchema::compactSchema(const string& schema) { } } // namespace avro - diff --git a/contrib/libs/apache/avro/impl/Validator.cc b/contrib/libs/apache/avro/impl/Validator.cc index 2e74b06b66b..0e5fd8bedad 100644 --- a/contrib/libs/apache/avro/impl/Validator.cc +++ b/contrib/libs/apache/avro/impl/Validator.cc @@ -16,61 +16,52 @@ * limitations under the License. */ -#include "Validator.hh" -#include "ValidSchema.hh" +#include <utility> + #include "NodeImpl.hh" +#include "ValidSchema.hh" +#include "Validator.hh" namespace avro { -Validator::Validator(const ValidSchema &schema) : - schema_(schema), - nextType_(AVRO_NULL), - expectedTypesFlag_(0), - compoundStarted_(false), - waitingForCount_(false), - count_(0) -{ +Validator::Validator(ValidSchema schema) : schema_(std::move(schema)), + nextType_(AVRO_NULL), + expectedTypesFlag_(0), + compoundStarted_(false), + waitingForCount_(false), + count_(0) { setupOperation(schema_.root()); } -void -Validator::setWaitingForCount() -{ +void Validator::setWaitingForCount() { waitingForCount_ = true; count_ = 0; expectedTypesFlag_ = typeToFlag(AVRO_INT) | typeToFlag(AVRO_LONG); nextType_ = AVRO_LONG; } -void -Validator::enumAdvance() -{ - if(compoundStarted_) { +void Validator::enumAdvance() { + if (compoundStarted_) { setWaitingForCount(); compoundStarted_ = false; - } - else { + } else { waitingForCount_ = false; compoundStack_.pop_back(); } } -bool -Validator::countingSetup() -{ - bool proceed = true; - if(compoundStarted_) { +bool Validator::countingSetup() { + auto proceed = true; + if (compoundStarted_) { setWaitingForCount(); compoundStarted_ = false; proceed = false; - } - else if(waitingForCount_) { + } else if (waitingForCount_) { waitingForCount_ = false; - if(count_ == 0) { + if (count_ == 0) { compoundStack_.pop_back(); proceed = false; - } - else { + } else { counters_.push_back(static_cast<size_t>(count_)); } } @@ -78,100 +69,83 @@ Validator::countingSetup() return proceed; } -void -Validator::countingAdvance() -{ - if(countingSetup()) { - - size_t index = (compoundStack_.back().pos)++; +void Validator::countingAdvance() { + if (countingSetup()) { + auto index = (compoundStack_.back().pos)++; const NodePtr &node = compoundStack_.back().node; - if(index < node->leaves() ) { + if (index < node->leaves()) { setupOperation(node->leafAt(index)); - } - else { + } else { compoundStack_.back().pos = 0; int count = --counters_.back(); - if(count == 0) { + if (count == 0) { counters_.pop_back(); compoundStarted_ = true; nextType_ = node->type(); expectedTypesFlag_ = typeToFlag(nextType_); - } - else { - size_t index = (compoundStack_.back().pos)++; + } else { + index = (compoundStack_.back().pos)++; setupOperation(node->leafAt(index)); } } } } -void -Validator::unionAdvance() -{ - if(compoundStarted_) { +void Validator::unionAdvance() { + if (compoundStarted_) { setWaitingForCount(); compoundStarted_ = false; - } - else { + } else { waitingForCount_ = false; NodePtr node = compoundStack_.back().node; - if(count_ < static_cast<int64_t>(node->leaves())) { + if (count_ < static_cast<int64_t>(node->leaves())) { compoundStack_.pop_back(); setupOperation(node->leafAt(static_cast<int>(count_))); - } - else { + } else { throw Exception( - boost::format("Union selection out of range, got %1%," \ - " expecting 0-%2%") - % count_ % (node->leaves() -1) - ); + boost::format("Union selection out of range, got %1%," + " expecting 0-%2%") + % count_ % (node->leaves() - 1)); } } } -void -Validator::fixedAdvance() -{ +void Validator::fixedAdvance() { compoundStarted_ = false; compoundStack_.pop_back(); } -int -Validator::nextSizeExpected() const -{ +int Validator::nextSizeExpected() const { return compoundStack_.back().node->fixedSize(); } -void -Validator::doAdvance() -{ - typedef void (Validator::*AdvanceFunc)(); +void Validator::doAdvance() { + using AdvanceFunc = void (Validator::*)(); // only the compound types need advance functions here static const AdvanceFunc funcs[] = { - 0, // string - 0, // bytes - 0, // int - 0, // long - 0, // float - 0, // double - 0, // bool - 0, // null + nullptr, // string + nullptr, // bytes + nullptr, // int + nullptr, // long + nullptr, // float + nullptr, // double + nullptr, // bool + nullptr, // null &Validator::countingAdvance, // Record is treated like counting with count == 1 &Validator::enumAdvance, &Validator::countingAdvance, &Validator::countingAdvance, &Validator::unionAdvance, - &Validator::fixedAdvance - }; - static_assert((sizeof(funcs)/sizeof(AdvanceFunc)) == (AVRO_NUM_TYPES), - "Invalid number of advance functions"); + &Validator::fixedAdvance}; + static_assert((sizeof(funcs) / sizeof(AdvanceFunc)) == (AVRO_NUM_TYPES), + "Invalid number of advance functions"); expectedTypesFlag_ = 0; // loop until we encounter a next expected type, or we've exited all compound types - while(!expectedTypesFlag_ && !compoundStack_.empty() ) { + while (!expectedTypesFlag_ && !compoundStack_.empty()) { Type type = compoundStack_.back().node->type(); @@ -184,25 +158,21 @@ Validator::doAdvance() ((this)->*(func))(); } - if(compoundStack_.empty()) { + if (compoundStack_.empty()) { nextType_ = AVRO_NULL; } } -void Validator::advance() -{ - if(!waitingForCount_) { +void Validator::advance() { + if (!waitingForCount_) { doAdvance(); } } -void -Validator::setCount(int64_t count) -{ - if(!waitingForCount_) { +void Validator::setCount(int64_t count) { + if (!waitingForCount_) { throw Exception("Not expecting count"); - } - else if(count_ < 0) { + } else if (count_ < 0) { throw Exception("Count cannot be negative"); } count_ = count; @@ -210,9 +180,7 @@ Validator::setCount(int64_t count) doAdvance(); } -void -Validator::setupFlag(Type type) -{ +void Validator::setupFlag(Type type) { // use flags instead of strictly types, so that we can be more lax about the type // (for example, a long should be able to accept an int type, but not vice versa) static const flag_t flags[] = { @@ -229,20 +197,17 @@ Validator::setupFlag(Type type) typeToFlag(AVRO_ARRAY), typeToFlag(AVRO_MAP), typeToFlag(AVRO_UNION), - typeToFlag(AVRO_FIXED) - }; - static_assert((sizeof(flags)/sizeof(flag_t)) == (AVRO_NUM_TYPES), - "Invalid number of avro type flags"); + typeToFlag(AVRO_FIXED)}; + static_assert((sizeof(flags) / sizeof(flag_t)) == (AVRO_NUM_TYPES), + "Invalid number of avro type flags"); expectedTypesFlag_ = flags[type]; } -void -Validator::setupOperation(const NodePtr &node) -{ +void Validator::setupOperation(const NodePtr &node) { nextType_ = node->type(); - if(nextType_ == AVRO_SYMBOLIC) { + if (nextType_ == AVRO_SYMBOLIC) { NodePtr actualNode = resolveSymbol(node); assert(actualNode); setupOperation(actualNode); @@ -253,44 +218,34 @@ Validator::setupOperation(const NodePtr &node) setupFlag(nextType_); - if(!isPrimitive(nextType_)) { - compoundStack_.push_back(CompoundType(node)); + if (!isPrimitive(nextType_)) { + compoundStack_.emplace_back(node); compoundStarted_ = true; } } -bool -Validator::getCurrentRecordName(std::string &name) const -{ - bool found = false; +bool Validator::getCurrentRecordName(std::string &name) const { + auto found = false; name.clear(); - int idx = -1; // if the top of the stack is a record I want this record name - if(!compoundStack_.empty() && (isPrimitive(nextType_) || nextType_ == AVRO_RECORD)) { - idx = compoundStack_.size() -1; - } - else { - idx = compoundStack_.size() -2; - } + auto idx = static_cast<int>(compoundStack_.size() - ((!compoundStack_.empty() && (isPrimitive(nextType_) || nextType_ == AVRO_RECORD)) ? 1 : 2)); - if(idx >= 0 && compoundStack_[idx].node->type() == AVRO_RECORD) { + if (idx >= 0 && compoundStack_[idx].node->type() == AVRO_RECORD) { name = compoundStack_[idx].node->name().simpleName(); found = true; } return found; } -bool -Validator::getNextFieldName(std::string &name) const -{ - bool found = false; +bool Validator::getNextFieldName(std::string &name) const { + auto found = false; name.clear(); - int idx = isCompound(nextType_) ? compoundStack_.size()-2 : compoundStack_.size()-1; - if(idx >= 0 && compoundStack_[idx].node->type() == AVRO_RECORD) { - size_t pos = compoundStack_[idx].pos-1; + auto idx = static_cast<int>(compoundStack_.size() - (isCompound(nextType_) ? 2 : 1)); + if (idx >= 0 && compoundStack_[idx].node->type() == AVRO_RECORD) { + size_t pos = compoundStack_[idx].pos - 1; const NodePtr &node = compoundStack_[idx].node; - if(pos < node->leaves()) { + if (pos < node->leaves()) { name = node->nameAt(pos); found = true; } diff --git a/contrib/libs/apache/avro/impl/Zigzag.cc b/contrib/libs/apache/avro/impl/Zigzag.cc index 06db5b4e7ba..538a89cbaa7 100644 --- a/contrib/libs/apache/avro/impl/Zigzag.cc +++ b/contrib/libs/apache/avro/impl/Zigzag.cc @@ -16,70 +16,41 @@ * limitations under the License. */ - #include "Zigzag.hh" namespace avro { - -uint64_t -encodeZigzag64(int64_t input) -{ - // cppcheck-suppress shiftTooManyBitsSigned - return ((input << 1) ^ (input >> 63)); -} - -int64_t -decodeZigzag64(uint64_t input) -{ - return static_cast<int64_t>(((input >> 1) ^ -(static_cast<int64_t>(input) & 1))); -} - -uint32_t -encodeZigzag32(int32_t input) -{ - // cppcheck-suppress shiftTooManyBitsSigned - return ((input << 1) ^ (input >> 31)); -} - -int32_t -decodeZigzag32(uint32_t input) -{ - return static_cast<int32_t>(((input >> 1) ^ -(static_cast<int64_t>(input) & 1))); -} - +// TODO: The following two functions have exactly the same code except for the type. +// They should be implemented as a template. size_t -encodeInt64(int64_t input, std::array<uint8_t, 10> &output) -{ - // get the zigzag encoding - uint64_t val = encodeZigzag64(input); +encodeInt64(int64_t input, std::array<uint8_t, 10> &output) noexcept { + auto val = encodeZigzag64(input); // put values in an array of bytes with variable length encoding - const int mask = 0x7F; - output[0] = val & mask; - size_t bytesOut = 1; - while( val >>=7 ) { - output[bytesOut-1] |= 0x80; - output[bytesOut++] = (val & mask); + const int mask = 0x7F; + auto v = val & mask; + size_t bytesOut = 0; + while (val >>= 7) { + output[bytesOut++] = (v | 0x80); + v = val & mask; } + output[bytesOut++] = v; return bytesOut; } - size_t -encodeInt32(int32_t input, std::array<uint8_t, 5> &output) -{ - // get the zigzag encoding - uint32_t val = encodeZigzag32(input); +encodeInt32(int32_t input, std::array<uint8_t, 5> &output) noexcept { + auto val = encodeZigzag32(input); // put values in an array of bytes with variable length encoding - const int mask = 0x7F; - output[0] = val & mask; - size_t bytesOut = 1; - while( val >>=7 ) { - output[bytesOut-1] |= 0x80; - output[bytesOut++] = (val & mask); + const int mask = 0x7F; + auto v = val & mask; + size_t bytesOut = 0; + while (val >>= 7) { + output[bytesOut++] = (v | 0x80); + v = val & mask; } + output[bytesOut++] = v; return bytesOut; } diff --git a/contrib/libs/apache/avro/impl/json/JsonDom.cc b/contrib/libs/apache/avro/impl/json/JsonDom.cc index ac4d8c9bfca..5bffda2559c 100644 --- a/contrib/libs/apache/avro/impl/json/JsonDom.cc +++ b/contrib/libs/apache/avro/impl/json/JsonDom.cc @@ -20,65 +20,61 @@ #include <stdexcept> -#include <string.h> +#include <cstring> -#include "Stream.hh" #include "JsonIO.hh" +#include "Stream.hh" -using std::string; using boost::format; +using std::string; namespace avro { namespace json { -const char* typeToString(EntityType t) -{ +const char *typeToString(EntityType t) { switch (t) { - case etNull: return "null"; - case etBool: return "bool"; - case etLong: return "long"; - case etDouble: return "double"; - case etString: return "string"; - case etArray: return "array"; - case etObject: return "object"; - default: return "unknown"; + case EntityType::Null: return "null"; + case EntityType::Bool: return "bool"; + case EntityType::Long: return "long"; + case EntityType::Double: return "double"; + case EntityType::String: return "string"; + case EntityType::Arr: return "array"; + case EntityType::Obj: return "object"; + default: return "unknown"; } } -Entity readEntity(JsonParser& p) -{ +Entity readEntity(JsonParser &p) { switch (p.peek()) { - case JsonParser::tkNull: - p.advance(); - return Entity(p.line()); - case JsonParser::tkBool: - p.advance(); - return Entity(p.boolValue(), p.line()); - case JsonParser::tkLong: - p.advance(); - return Entity(p.longValue(), p.line()); - case JsonParser::tkDouble: - p.advance(); - return Entity(p.doubleValue(), p.line()); - case JsonParser::tkString: - p.advance(); - return Entity(std::make_shared<String>(p.rawString()), p.line()); - case JsonParser::tkArrayStart: - { + case JsonParser::Token::Null: + p.advance(); + return Entity(p.line()); + case JsonParser::Token::Bool: + p.advance(); + return Entity(p.boolValue(), p.line()); + case JsonParser::Token::Long: + p.advance(); + return Entity(p.longValue(), p.line()); + case JsonParser::Token::Double: + p.advance(); + return Entity(p.doubleValue(), p.line()); + case JsonParser::Token::String: + p.advance(); + return Entity(std::make_shared<String>(p.rawString()), p.line()); + case JsonParser::Token::ArrayStart: { size_t l = p.line(); p.advance(); std::shared_ptr<Array> v = std::make_shared<Array>(); - while (p.peek() != JsonParser::tkArrayEnd) { + while (p.peek() != JsonParser::Token::ArrayEnd) { v->push_back(readEntity(p)); } p.advance(); return Entity(v, l); } - case JsonParser::tkObjectStart: - { + case JsonParser::Token::ObjectStart: { size_t l = p.line(); p.advance(); std::shared_ptr<Object> v = std::make_shared<Object>(); - while (p.peek() != JsonParser::tkObjectEnd) { + while (p.peek() != JsonParser::Token::ObjectEnd) { p.advance(); std::string k = p.stringValue(); Entity n = readEntity(p); @@ -87,101 +83,88 @@ Entity readEntity(JsonParser& p) p.advance(); return Entity(v, l); } - default: - throw std::domain_error(JsonParser::toString(p.peek())); + default: + throw std::domain_error(JsonParser::toString(p.peek())); } - } -Entity loadEntity(const char* text) -{ - return loadEntity(reinterpret_cast<const uint8_t*>(text), ::strlen(text)); +Entity loadEntity(const char *text) { + return loadEntity(reinterpret_cast<const uint8_t *>(text), ::strlen(text)); } -Entity loadEntity(InputStream& in) -{ +Entity loadEntity(InputStream &in) { JsonParser p; p.init(in); return readEntity(p); } -Entity loadEntity(const uint8_t* text, size_t len) -{ +Entity loadEntity(const uint8_t *text, size_t len) { std::unique_ptr<InputStream> in = memoryInputStream(text, len); return loadEntity(*in); } -void writeEntity(JsonGenerator<JsonNullFormatter>& g, const Entity& n) -{ +void writeEntity(JsonGenerator<JsonNullFormatter> &g, const Entity &n) { switch (n.type()) { - case etNull: - g.encodeNull(); - break; - case etBool: - g.encodeBool(n.boolValue()); - break; - case etLong: - g.encodeNumber(n.longValue()); - break; - case etDouble: - g.encodeNumber(n.doubleValue()); - break; - case etString: - g.encodeString(n.stringValue()); - break; - case etArray: - { + case EntityType::Null: + g.encodeNull(); + break; + case EntityType::Bool: + g.encodeBool(n.boolValue()); + break; + case EntityType::Long: + g.encodeNumber(n.longValue()); + break; + case EntityType::Double: + g.encodeNumber(n.doubleValue()); + break; + case EntityType::String: + g.encodeString(n.stringValue()); + break; + case EntityType::Arr: { g.arrayStart(); - const Array& v = n.arrayValue(); - for (Array::const_iterator it = v.begin(); - it != v.end(); ++it) { - writeEntity(g, *it); + const Array &v = n.arrayValue(); + for (const auto &it : v) { + writeEntity(g, it); } g.arrayEnd(); - } - break; - case etObject: - { + } break; + case EntityType::Obj: { g.objectStart(); - const Object& v = n.objectValue(); - for (Object::const_iterator it = v.begin(); it != v.end(); ++it) { - g.encodeString(it->first); - writeEntity(g, it->second); + const Object &v = n.objectValue(); + for (const auto &it : v) { + g.encodeString(it.first); + writeEntity(g, it.second); } g.objectEnd(); - } - break; + } break; } } -void Entity::ensureType(EntityType type) const -{ +void Entity::ensureType(EntityType type) const { if (type_ != type) { - format msg = format("Invalid type. Expected \"%1%\" actual %2%") % - typeToString(type) % typeToString(type_); + format msg = format("Invalid type. Expected \"%1%\" actual %2%") % typeToString(type) % typeToString(type_); throw Exception(msg); } } String Entity::stringValue() const { - ensureType(etString); - return JsonParser::toStringValue(**boost::any_cast<std::shared_ptr<String> >(&value_)); + ensureType(EntityType::String); + return JsonParser::toStringValue(**boost::any_cast<std::shared_ptr<String>>(&value_)); } String Entity::bytesValue() const { - ensureType(etString); - return JsonParser::toBytesValue(**boost::any_cast<std::shared_ptr<String> >(&value_)); + ensureType(EntityType::String); + return JsonParser::toBytesValue(**boost::any_cast<std::shared_ptr<String>>(&value_)); } -std::string Entity::toString() const -{ +std::string Entity::toString() const { std::unique_ptr<OutputStream> out = memoryOutputStream(); JsonGenerator<JsonNullFormatter> g; g.init(*out); writeEntity(g, *this); g.flush(); std::unique_ptr<InputStream> in = memoryInputStream(*out); - const uint8_t *p = 0; + const uint8_t *p = nullptr; size_t n = 0; size_t c = 0; while (in->next(&p, &n)) { @@ -198,6 +181,5 @@ std::string Entity::toString() const return result; } -} -} - +} // namespace json +} // namespace avro diff --git a/contrib/libs/apache/avro/impl/json/JsonDom.hh b/contrib/libs/apache/avro/impl/json/JsonDom.hh index e1f549dfeab..3fb5670b70b 100644 --- a/contrib/libs/apache/avro/impl/json/JsonDom.hh +++ b/contrib/libs/apache/avro/impl/json/JsonDom.hh @@ -19,15 +19,15 @@ #ifndef avro_json_JsonDom_hh__ #define avro_json_JsonDom_hh__ +#include <cstdint> #include <iostream> -#include <stdint.h> #include <map> +#include <memory> #include <string> #include <vector> -#include <memory> -#include "boost/any.hpp" #include "Config.hh" +#include "boost/any.hpp" namespace avro { @@ -49,17 +49,21 @@ class JsonNullFormatter; template<typename F = JsonNullFormatter> class AVRO_DECL JsonGenerator; -enum EntityType { - etNull, - etBool, - etLong, - etDouble, - etString, - etArray, - etObject +enum class EntityType { + Null, + Bool, + Long, + Double, + String, + Arr, + Obj }; -const char* typeToString(EntityType t); +const char *typeToString(EntityType t); + +inline std::ostream &operator<<(std::ostream &os, EntityType et) { + return os << typeToString(et); +} class AVRO_DECL Entity { EntityType type_; @@ -67,31 +71,44 @@ class AVRO_DECL Entity { size_t line_; // can't be const else noncopyable... void ensureType(EntityType) const; + public: - Entity(size_t line = 0) : type_(etNull), line_(line) { } - Entity(Bool v, size_t line = 0) : type_(etBool), value_(v), line_(line) { } - Entity(Long v, size_t line = 0) : type_(etLong), value_(v), line_(line) { } - Entity(Double v, size_t line = 0) : type_(etDouble), value_(v), line_(line) { } - Entity(const std::shared_ptr<String>& v, size_t line = 0) : type_(etString), value_(v), line_(line) { } - Entity(const std::shared_ptr<Array>& v, size_t line = 0) : type_(etArray), value_(v), line_(line) { } - Entity(const std::shared_ptr<Object>& v, size_t line = 0) : type_(etObject), value_(v), line_(line) { } + explicit Entity(size_t line = 0) : type_(EntityType::Null), line_(line) {} + // Not explicit because do want implicit conversion + // NOLINTNEXTLINE(google-explicit-constructor) + Entity(Bool v, size_t line = 0) : type_(EntityType::Bool), value_(v), line_(line) {} + // Not explicit because do want implicit conversion + // NOLINTNEXTLINE(google-explicit-constructor) + Entity(Long v, size_t line = 0) : type_(EntityType::Long), value_(v), line_(line) {} + // Not explicit because do want implicit conversion + // NOLINTNEXTLINE(google-explicit-constructor) + Entity(Double v, size_t line = 0) : type_(EntityType::Double), value_(v), line_(line) {} + // Not explicit because do want implicit conversion + // NOLINTNEXTLINE(google-explicit-constructor) + Entity(const std::shared_ptr<String> &v, size_t line = 0) : type_(EntityType::String), value_(v), line_(line) {} + // Not explicit because do want implicit conversion + // NOLINTNEXTLINE(google-explicit-constructor) + Entity(const std::shared_ptr<Array> &v, size_t line = 0) : type_(EntityType::Arr), value_(v), line_(line) {} + // Not explicit because do want implicit conversion + // NOLINTNEXTLINE(google-explicit-constructor) + Entity(const std::shared_ptr<Object> &v, size_t line = 0) : type_(EntityType::Obj), value_(v), line_(line) {} EntityType type() const { return type_; } size_t line() const { return line_; } Bool boolValue() const { - ensureType(etBool); + ensureType(EntityType::Bool); return boost::any_cast<Bool>(value_); } Long longValue() const { - ensureType(etLong); + ensureType(EntityType::Long); return boost::any_cast<Long>(value_); } Double doubleValue() const { - ensureType(etDouble); + ensureType(EntityType::Double); return boost::any_cast<Double>(value_); } @@ -99,64 +116,68 @@ public: String bytesValue() const; - const Array& arrayValue() const { - ensureType(etArray); - return **boost::any_cast<std::shared_ptr<Array> >(&value_); + const Array &arrayValue() const { + ensureType(EntityType::Arr); + return **boost::any_cast<std::shared_ptr<Array>>(&value_); } - const Object& objectValue() const { - ensureType(etObject); - return **boost::any_cast<std::shared_ptr<Object> >(&value_); + const Object &objectValue() const { + ensureType(EntityType::Obj); + return **boost::any_cast<std::shared_ptr<Object>>(&value_); } std::string toString() const; }; -template <typename T> +template<typename T> struct type_traits { }; -template <> struct type_traits<bool> { - static EntityType type() { return etBool; } - static const char* name() { return "bool"; } +template<> +struct type_traits<bool> { + static EntityType type() { return EntityType::Bool; } + static const char *name() { return "bool"; } }; -template <> struct type_traits<int64_t> { - static EntityType type() { return etLong; } - static const char* name() { return "long"; } +template<> +struct type_traits<int64_t> { + static EntityType type() { return EntityType::Long; } + static const char *name() { return "long"; } }; -template <> struct type_traits<double> { - static EntityType type() { return etDouble; } - static const char* name() { return "double"; } +template<> +struct type_traits<double> { + static EntityType type() { return EntityType::Double; } + static const char *name() { return "double"; } }; -template <> struct type_traits<std::string> { - static EntityType type() { return etString; } - static const char* name() { return "string"; } +template<> +struct type_traits<std::string> { + static EntityType type() { return EntityType::String; } + static const char *name() { return "string"; } }; -template <> struct type_traits<std::vector<Entity> > { - static EntityType type() { return etArray; } - static const char* name() { return "array"; } +template<> +struct type_traits<std::vector<Entity>> { + static EntityType type() { return EntityType::Arr; } + static const char *name() { return "array"; } }; -template <> struct type_traits<std::map<std::string, Entity> > { - static EntityType type() { return etObject; } - static const char* name() { return "object"; } +template<> +struct type_traits<std::map<std::string, Entity>> { + static EntityType type() { return EntityType::Obj; } + static const char *name() { return "object"; } }; -AVRO_DECL Entity readEntity(JsonParser& p); +AVRO_DECL Entity readEntity(JsonParser &p); -AVRO_DECL Entity loadEntity(InputStream& in); -AVRO_DECL Entity loadEntity(const char* text); -AVRO_DECL Entity loadEntity(const uint8_t* text, size_t len); +AVRO_DECL Entity loadEntity(InputStream &in); +AVRO_DECL Entity loadEntity(const char *text); +AVRO_DECL Entity loadEntity(const uint8_t *text, size_t len); -void writeEntity(JsonGenerator<JsonNullFormatter>& g, const Entity& n); +void writeEntity(JsonGenerator<JsonNullFormatter> &g, const Entity &n); -} -} +} // namespace json +} // namespace avro #endif - - diff --git a/contrib/libs/apache/avro/impl/json/JsonIO.cc b/contrib/libs/apache/avro/impl/json/JsonIO.cc index c11a722ad4d..62549484a92 100644 --- a/contrib/libs/apache/avro/impl/json/JsonIO.cc +++ b/contrib/libs/apache/avro/impl/json/JsonIO.cc @@ -24,21 +24,20 @@ namespace json { using std::ostringstream; using std::string; -const char* const -JsonParser::tokenNames[] = { - "Null", - "Bool", - "Integer", - "Double", - "String", - "Array start", - "Array end", - "Object start", - "Object end", +const char *const + JsonParser::tokenNames[] = { + "Null", + "Bool", + "Integer", + "Double", + "String", + "Array start", + "Array end", + "Object start", + "Object end", }; -char JsonParser::next() -{ +char JsonParser::next() { char ch = hasNext ? nextChar : ' '; while (isspace(ch)) { if (ch == '\n') { @@ -50,20 +49,15 @@ char JsonParser::next() return ch; } -void JsonParser::expectToken(Token tk) -{ +void JsonParser::expectToken(Token tk) { if (advance() != tk) { - if (tk == tkDouble) { - if(cur() == tkString + if (tk == Token::Double) { + if (cur() == Token::String && (sv == "Infinity" || sv == "-Infinity" || sv == "NaN")) { - curToken = tkDouble; - dv = sv == "Infinity" ? - std::numeric_limits<double>::infinity() : - sv == "-Infinity" ? - -std::numeric_limits<double>::infinity() : - std::numeric_limits<double>::quiet_NaN(); + curToken = Token::Double; + dv = sv == "Infinity" ? std::numeric_limits<double>::infinity() : sv == "-Infinity" ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::quiet_NaN(); return; - } else if (cur() == tkLong) { + } else if (cur() == Token::Long) { dv = double(lv); return; } @@ -76,14 +70,13 @@ void JsonParser::expectToken(Token tk) } } -JsonParser::Token JsonParser::doAdvance() -{ +JsonParser::Token JsonParser::doAdvance() { char ch = next(); if (ch == ']') { if (curState == stArray0 || curState == stArrayN) { curState = stateStack.top(); stateStack.pop(); - return tkArrayEnd; + return Token::ArrayEnd; } else { throw unexpected(ch); } @@ -91,7 +84,7 @@ JsonParser::Token JsonParser::doAdvance() if (curState == stObject0 || curState == stObjectN) { curState = stateStack.top(); stateStack.pop(); - return tkObjectEnd; + return Token::ObjectEnd; } else { throw unexpected(ch); } @@ -121,137 +114,138 @@ JsonParser::Token JsonParser::doAdvance() } switch (ch) { - case '[': - stateStack.push(curState); - curState = stArray0; - return tkArrayStart; - case '{': - stateStack.push(curState); - curState = stObject0; - return tkObjectStart; - case '"': - return tryString(); - case 't': - bv = true; - return tryLiteral("rue", 3, tkBool); - case 'f': - bv = false; - return tryLiteral("alse", 4, tkBool); - case 'n': - return tryLiteral("ull", 3, tkNull); - default: - if (isdigit(ch) || ch == '-') { - return tryNumber(ch); - } else { - throw unexpected(ch); - } + case '[': + stateStack.push(curState); + curState = stArray0; + return Token::ArrayStart; + case '{': + stateStack.push(curState); + curState = stObject0; + return Token::ObjectStart; + case '"': + return tryString(); + case 't': + bv = true; + return tryLiteral("rue", 3, Token::Bool); + case 'f': + bv = false; + return tryLiteral("alse", 4, Token::Bool); + case 'n': + return tryLiteral("ull", 3, Token::Null); + default: + if (isdigit(ch) || ch == '-') { + return tryNumber(ch); + } else { + throw unexpected(ch); + } } } -JsonParser::Token JsonParser::tryNumber(char ch) -{ +JsonParser::Token JsonParser::tryNumber(char ch) { sv.clear(); sv.push_back(ch); hasNext = false; int state = (ch == '-') ? 0 : (ch == '0') ? 1 : 2; - for (; ;) { + for (;;) { switch (state) { - case 0: - if (in_.hasMore()) { - ch = in_.read(); - if (isdigit(ch)) { - state = (ch == '0') ? 1 : 2; - sv.push_back(ch); - continue; + case 0: + if (in_.hasMore()) { + ch = in_.read(); + if (isdigit(ch)) { + state = (ch == '0') ? 1 : 2; + sv.push_back(ch); + continue; + } + hasNext = true; } - hasNext = true; - } - break; - case 1: - if (in_.hasMore()) { - ch = in_.read(); - if (ch == '.') { - state = 3; - sv.push_back(ch); - continue; - } else if (ch == 'e' || ch == 'E') { - sv.push_back(ch); - state = 5; - continue; + break; + case 1: + if (in_.hasMore()) { + ch = in_.read(); + if (ch == '.') { + state = 3; + sv.push_back(ch); + continue; + } else if (ch == 'e' || ch == 'E') { + sv.push_back(ch); + state = 5; + continue; + } + hasNext = true; } - hasNext = true; - } - break; - case 2: - if (in_.hasMore()) { - ch = in_.read(); - if (isdigit(ch)) { - sv.push_back(ch); - continue; - } else if (ch == '.') { - state = 3; - sv.push_back(ch); - continue; - } else if (ch == 'e' || ch == 'E') { - sv.push_back(ch); - state = 5; - continue; + break; + case 2: + if (in_.hasMore()) { + ch = in_.read(); + if (isdigit(ch)) { + sv.push_back(ch); + continue; + } else if (ch == '.') { + state = 3; + sv.push_back(ch); + continue; + } else if (ch == 'e' || ch == 'E') { + sv.push_back(ch); + state = 5; + continue; + } + hasNext = true; } - hasNext = true; - } - break; - case 3: - case 6: - if (in_.hasMore()) { - ch = in_.read(); - if (isdigit(ch)) { - sv.push_back(ch); - state++; - continue; + break; + case 3: + case 6: + if (in_.hasMore()) { + ch = in_.read(); + if (isdigit(ch)) { + sv.push_back(ch); + state++; + continue; + } + hasNext = true; } - hasNext = true; - } - break; - case 4: - if (in_.hasMore()) { - ch = in_.read(); - if (isdigit(ch)) { - sv.push_back(ch); - continue; - } else if (ch == 'e' || ch == 'E') { - sv.push_back(ch); - state = 5; - continue; + break; + case 4: + if (in_.hasMore()) { + ch = in_.read(); + if (isdigit(ch)) { + sv.push_back(ch); + continue; + } else if (ch == 'e' || ch == 'E') { + sv.push_back(ch); + state = 5; + continue; + } + hasNext = true; } - hasNext = true; - } - break; - case 5: - if (in_.hasMore()) { - ch = in_.read(); - if (ch == '+' || ch == '-') { - sv.push_back(ch); - state = 6; - continue; - } else if (isdigit(ch)) { - sv.push_back(ch); - state = 7; - continue; + break; + case 5: + if (in_.hasMore()) { + ch = in_.read(); + if (ch == '+' || ch == '-') { + sv.push_back(ch); + state = 6; + continue; + } else if (isdigit(ch)) { + sv.push_back(ch); + state = 7; + continue; + } + hasNext = true; } - hasNext = true; - } - break; - case 7: - if (in_.hasMore()) { - ch = in_.read(); - if (isdigit(ch)) { - sv.push_back(ch); - continue; + break; + case 7: + if (in_.hasMore()) { + ch = in_.read(); + if (isdigit(ch)) { + sv.push_back(ch); + continue; + } + hasNext = true; } - hasNext = true; - } - break; + break; + default: + throw Exception("Unexpected JSON parse state"); } if (state == 1 || state == 2 || state == 4 || state == 7) { if (hasNext) { @@ -260,10 +254,10 @@ JsonParser::Token JsonParser::tryNumber(char ch) std::istringstream iss(sv); if (state == 1 || state == 2) { iss >> lv; - return tkLong; + return Token::Long; } else { iss >> dv; - return tkDouble; + return Token::Double; } } else { if (hasNext) { @@ -275,50 +269,44 @@ JsonParser::Token JsonParser::tryNumber(char ch) } } -JsonParser::Token JsonParser::tryString() -{ +JsonParser::Token JsonParser::tryString() { sv.clear(); - for ( ; ;) { + for (;;) { char ch = in_.read(); if (ch == '"') { - return tkString; + return Token::String; } else if (ch == '\\') { ch = in_.read(); switch (ch) { - case '"': - case '\\': - case '/': - case 'b': - case 'f': - case 'n': - case 'r': - case 't': - sv.push_back('\\'); - sv.push_back(ch); - break; - case 'u': - case 'U': - { + case '"': + case '\\': + case '/': + case 'b': + case 'f': + case 'n': + case 'r': + case 't': + sv.push_back('\\'); + sv.push_back(ch); + break; + case 'u': + case 'U': { uint32_t n = 0; char e[4]; - in_.readBytes(reinterpret_cast<uint8_t*>(e), 4); + in_.readBytes(reinterpret_cast<uint8_t *>(e), 4); sv.push_back('\\'); sv.push_back(ch); - for (int i = 0; i < 4; i++) { + for (char c : e) { n *= 16; - char c = e[i]; - if (isdigit(c) || - (c >= 'a' && c <= 'f') || - (c >= 'A' && c <= 'F')) { + if (isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) { sv.push_back(c); } else { throw unexpected(c); } } - } - break; - default: - throw unexpected(ch); + } break; + default: + throw unexpected(ch); } } else { sv.push_back(ch); @@ -326,44 +314,41 @@ JsonParser::Token JsonParser::tryString() } } - -string JsonParser::decodeString(const string& s, bool binary) -{ +string JsonParser::decodeString(const string &s, bool binary) { string result; for (string::const_iterator it = s.begin(); it != s.end(); ++it) { char ch = *it; if (ch == '\\') { ch = *++it; switch (ch) { - case '"': - case '\\': - case '/': - result.push_back(ch); - continue; - case 'b': - result.push_back('\b'); - continue; - case 'f': - result.push_back('\f'); - continue; - case 'n': - result.push_back('\n'); - continue; - case 'r': - result.push_back('\r'); - continue; - case 't': - result.push_back('\t'); - continue; - case 'u': - case 'U': - { + case '"': + case '\\': + case '/': + result.push_back(ch); + continue; + case 'b': + result.push_back('\b'); + continue; + case 'f': + result.push_back('\f'); + continue; + case 'n': + result.push_back('\n'); + continue; + case 'r': + result.push_back('\r'); + continue; + case 't': + result.push_back('\t'); + continue; + case 'u': + case 'U': { uint32_t n = 0; char e[4]; - for (int i = 0; i < 4; i++) { + for (char &i : e) { n *= 16; char c = *++it; - e[i] = c; + i = c; if (isdigit(c)) { n += c - '0'; } else if (c >= 'a' && c <= 'f') { @@ -375,8 +360,8 @@ string JsonParser::decodeString(const string& s, bool binary) if (binary) { if (n > 0xff) { throw Exception(boost::format( - "Invalid byte for binary: %1%%2%") % ch % - string(e, 4)); + "Invalid byte for binary: %1%%2%") + % ch % string(e, 4)); } else { result.push_back(n); continue; @@ -389,20 +374,22 @@ string JsonParser::decodeString(const string& s, bool binary) result.push_back((n & 0x3f) | 0x80); } else if (n < 0x10000) { result.push_back((n >> 12) | 0xe0); - result.push_back(((n >> 6)& 0x3f) | 0x80); + result.push_back(((n >> 6) & 0x3f) | 0x80); result.push_back((n & 0x3f) | 0x80); } else if (n < 110000) { result.push_back((n >> 18) | 0xf0); - result.push_back(((n >> 12)& 0x3f) | 0x80); - result.push_back(((n >> 6)& 0x3f) | 0x80); + result.push_back(((n >> 12) & 0x3f) | 0x80); + result.push_back(((n >> 6) & 0x3f) | 0x80); result.push_back((n & 0x3f) | 0x80); } else { throw Exception(boost::format( - "Invalid unicode value: %1%i%2%") % ch % - string(e, 4)); + "Invalid unicode value: %1%i%2%") + % ch % string(e, 4)); } } - continue; + continue; + default: + throw Exception("Unexpected JSON parse state"); } } else { result.push_back(ch); @@ -411,17 +398,15 @@ string JsonParser::decodeString(const string& s, bool binary) return result; } -Exception JsonParser::unexpected(unsigned char c) -{ +Exception JsonParser::unexpected(unsigned char c) { std::ostringstream oss; oss << "Unexpected character in json " << toHex(c / 16) << toHex(c % 16); return Exception(oss.str()); } -JsonParser::Token JsonParser::tryLiteral(const char exp[], size_t n, Token tk) -{ +JsonParser::Token JsonParser::tryLiteral(const char exp[], size_t n, Token tk) { char c[100]; - in_.readBytes(reinterpret_cast<uint8_t*>(c), n); + in_.readBytes(reinterpret_cast<uint8_t *>(c), n); for (size_t i = 0; i < n; ++i) { if (c[i] != exp[i]) { throw unexpected(c[i]); @@ -437,6 +422,5 @@ JsonParser::Token JsonParser::tryLiteral(const char exp[], size_t n, Token tk) return tk; } -} -} - +} // namespace json +} // namespace avro diff --git a/contrib/libs/apache/avro/impl/json/JsonIO.hh b/contrib/libs/apache/avro/impl/json/JsonIO.hh index 5ae7ae07dc5..94889e5d010 100644 --- a/contrib/libs/apache/avro/impl/json/JsonIO.hh +++ b/contrib/libs/apache/avro/impl/json/JsonIO.hh @@ -19,13 +19,13 @@ #ifndef avro_json_JsonIO_hh__ #define avro_json_JsonIO_hh__ +#include <boost/lexical_cast.hpp> +#include <boost/math/special_functions/fpclassify.hpp> +#include <boost/utility.hpp> #include <locale> +#include <sstream> #include <stack> #include <string> -#include <sstream> -#include <boost/math/special_functions/fpclassify.hpp> -#include <boost/lexical_cast.hpp> -#include <boost/utility.hpp> #include "Config.hh" #include "Stream.hh" @@ -37,31 +37,30 @@ inline char toHex(unsigned int n) { return (n < 10) ? (n + '0') : (n + 'a' - 10); } - class AVRO_DECL JsonParser : boost::noncopyable { public: - enum Token { - tkNull, - tkBool, - tkLong, - tkDouble, - tkString, - tkArrayStart, - tkArrayEnd, - tkObjectStart, - tkObjectEnd + enum class Token { + Null, + Bool, + Long, + Double, + String, + ArrayStart, + ArrayEnd, + ObjectStart, + ObjectEnd }; size_t line() const { return line_; } private: enum State { - stValue, // Expect a data type - stArray0, // Expect a data type or ']' - stArrayN, // Expect a ',' or ']' - stObject0, // Expect a string or a '}' - stObjectN, // Expect a ',' or '}' - stKey // Expect a ':' + stValue, // Expect a data type + stArray0, // Expect a data type or ']' + stArrayN, // Expect a ',' or ']' + stObject0, // Expect a string or a '}' + stObjectN, // Expect a ',' or '}' + stKey // Expect a ':' }; std::stack<State> stateStack; State curState; @@ -81,15 +80,16 @@ private: Token tryLiteral(const char exp[], size_t n, Token tk); Token tryNumber(char ch); Token tryString(); - Exception unexpected(unsigned char ch); + static Exception unexpected(unsigned char ch); char next(); - static std::string decodeString(const std::string& s, bool binary); + static std::string decodeString(const std::string &s, bool binary); public: - JsonParser() : curState(stValue), hasNext(false), peeked(false), line_(1) { } + JsonParser() : curState(stValue), hasNext(false), nextChar(0), peeked(false), + curToken(Token::Null), bv(false), lv(0), dv(0), line_(1) {} - void init(InputStream& is) { + void init(InputStream &is) { // Clear by swapping with an empty stack std::stack<State>().swap(stateStack); curState = stValue; @@ -100,7 +100,7 @@ public: } Token advance() { - if (! peeked) { + if (!peeked) { curToken = doAdvance(); } else { peeked = false; @@ -109,7 +109,7 @@ public: } Token peek() { - if (! peeked) { + if (!peeked) { curToken = doAdvance(); peeked = true; } @@ -134,7 +134,7 @@ public: return lv; } - const std::string& rawString() const { + const std::string &rawString() const { return sv; } @@ -157,7 +157,7 @@ public: /** * Return UTF-8 encoded string value. */ - static std::string toStringValue(const std::string& sv) { + static std::string toStringValue(const std::string &sv) { return decodeString(sv, false); } @@ -165,20 +165,20 @@ public: * Return byte-encoded string value. It is an error if the input * JSON string contained unicode characters more than "\u00ff'. */ - static std::string toBytesValue(const std::string& sv) { + static std::string toBytesValue(const std::string &sv) { return decodeString(sv, true); } - static const char* const tokenNames[]; + static const char *const tokenNames[]; - static const char* toString(Token tk) { - return tokenNames[tk]; + static const char *toString(Token tk) { + return tokenNames[static_cast<size_t>(tk)]; } }; class AVRO_DECL JsonNullFormatter { public: - JsonNullFormatter(StreamWriter&) { } + explicit JsonNullFormatter(StreamWriter &) {} void handleObjectStart() {} void handleObjectEnd() {} @@ -187,7 +187,7 @@ public: }; class AVRO_DECL JsonPrettyFormatter { - StreamWriter& out_; + StreamWriter &out_; size_t level_; std::vector<uint8_t> indent_; @@ -200,8 +200,9 @@ class AVRO_DECL JsonPrettyFormatter { } out_.writeBytes(indent_.data(), charsToIndent); } + public: - JsonPrettyFormatter(StreamWriter& out) : out_(out), level_(0), indent_(10, ' ') { } + explicit JsonPrettyFormatter(StreamWriter &out) : out_(out), level_(0), indent_(10, ' ') {} void handleObjectStart() { out_.write('\n'); @@ -225,7 +226,7 @@ public: } }; -template <class F> +template<class F> class AVRO_DECL JsonGenerator { StreamWriter out_; F formatter_; @@ -241,13 +242,13 @@ class AVRO_DECL JsonGenerator { std::stack<State> stateStack; State top; - void write(const char *b, const char* p) { + void write(const char *b, const char *p) { if (b != p) { - out_.writeBytes(reinterpret_cast<const uint8_t*>(b), p - b); + out_.writeBytes(reinterpret_cast<const uint8_t *>(b), p - b); } } - void escape(char c, const char* b, const char *p) { + void escape(char c, const char *b, const char *p) { write(b, p); out_.write('\\'); out_.write(c); @@ -268,10 +269,10 @@ class AVRO_DECL JsonGenerator { writeHex((c >> 8) & 0xff); writeHex(c & 0xff); } - void doEncodeString(const char* b, size_t len, bool binary) { - const char* e = b + len; + void doEncodeString(const char *b, size_t len, bool binary) { + const char *e = b + len; out_.write('"'); - for (const char* p = b; p != e; p++) { + for (const char *p = b; p != e; p++) { if ((*p & 0x80) != 0) { write(b, p); if (binary) { @@ -280,7 +281,7 @@ class AVRO_DECL JsonGenerator { throw Exception("Invalid UTF-8 sequence"); } else { int more = 1; - uint32_t value = 0; + uint32_t value; if ((*p & 0x20) != 0) { more++; if ((*p & 0x10) != 0) { @@ -307,34 +308,34 @@ class AVRO_DECL JsonGenerator { } } else { switch (*p) { - case '\\': - case '"': - case '/': - escape(*p, b, p); - break; - case '\b': - escape('b', b, p); - break; - case '\f': - escape('f', b, p); - break; - case '\n': - escape('n', b, p); - break; - case '\r': - escape('r', b, p); - break; - case '\t': - escape('t', b, p); - break; - default: - if (std::iscntrl(*p, std::locale::classic())) { - write(b, p); - escapeCtl(*p); + case '\\': + case '"': + case '/': + escape(*p, b, p); break; - } else { - continue; - } + case '\b': + escape('b', b, p); + break; + case '\f': + escape('f', b, p); + break; + case '\n': + escape('n', b, p); + break; + case '\r': + escape('r', b, p); + break; + case '\t': + escape('t', b, p); + break; + default: + if (std::iscntrl(*p, std::locale::classic())) { + write(b, p); + escapeCtl(*p); + break; + } else { + continue; + } } } b = p + 1; @@ -359,9 +360,9 @@ class AVRO_DECL JsonGenerator { } public: - JsonGenerator() : formatter_(out_), top(stStart) { } + JsonGenerator() : formatter_(out_), top(stStart) {} - void init(OutputStream& os) { + void init(OutputStream &os) { out_.reset(os); } @@ -375,27 +376,27 @@ public: void encodeNull() { sep(); - out_.writeBytes(reinterpret_cast<const uint8_t*>("null"), 4); + out_.writeBytes(reinterpret_cast<const uint8_t *>("null"), 4); sep2(); } void encodeBool(bool b) { sep(); if (b) { - out_.writeBytes(reinterpret_cast<const uint8_t*>("true"), 4); + out_.writeBytes(reinterpret_cast<const uint8_t *>("true"), 4); } else { - out_.writeBytes(reinterpret_cast<const uint8_t*>("false"), 5); + out_.writeBytes(reinterpret_cast<const uint8_t *>("false"), 5); } sep2(); } - template <typename T> + template<typename T> void encodeNumber(T t) { sep(); std::ostringstream oss; oss << boost::lexical_cast<std::string>(t); - const std::string& s = oss.str(); - out_.writeBytes(reinterpret_cast<const uint8_t*>(s.data()), s.size()); + const std::string s = oss.str(); + out_.writeBytes(reinterpret_cast<const uint8_t *>(s.data()), s.size()); sep2(); } @@ -411,13 +412,12 @@ public: } else { oss << "-Infinity"; } - const std::string& s = oss.str(); - out_.writeBytes(reinterpret_cast<const uint8_t*>(s.data()), s.size()); + const std::string s = oss.str(); + out_.writeBytes(reinterpret_cast<const uint8_t *>(s.data()), s.size()); sep2(); } - - void encodeString(const std::string& s) { + void encodeString(const std::string &s) { if (top == stMap0) { top = stKey; } else if (top == stMapN) { @@ -436,7 +436,7 @@ public: } } - void encodeBinary(const uint8_t* bytes, size_t len) { + void encodeBinary(const uint8_t *bytes, size_t len) { sep(); doEncodeString(reinterpret_cast<const char *>(bytes), len, true); sep2(); @@ -473,10 +473,9 @@ public: out_.write('}'); sep2(); } - }; -} -} +} // namespace json +} // namespace avro #endif diff --git a/contrib/libs/apache/avro/impl/parsing/JsonCodec.cc b/contrib/libs/apache/avro/impl/parsing/JsonCodec.cc index 8bca2984aee..4fd04816069 100644 --- a/contrib/libs/apache/avro/impl/parsing/JsonCodec.cc +++ b/contrib/libs/apache/avro/impl/parsing/JsonCodec.cc @@ -16,21 +16,17 @@ * limitations under the License. */ -#define __STDC_LIMIT_MACROS - -#include <string> -#include <map> #include <algorithm> -#include <ctype.h> -#include <memory> #include <boost/math/special_functions/fpclassify.hpp> +#include <map> +#include <memory> +#include <string> -#include "ValidatingCodec.hh" -#include "Symbol.hh" -#include "ValidSchema.hh" #include "Decoder.hh" #include "Encoder.hh" -#include "NodeImpl.hh" +#include "Symbol.hh" +#include "ValidSchema.hh" +#include "ValidatingCodec.hh" #include "../json/JsonIO.hh" @@ -40,50 +36,48 @@ namespace parsing { using std::make_shared; +using std::istringstream; using std::map; -using std::vector; -using std::string; -using std::reverse; using std::ostringstream; -using std::istringstream; +using std::reverse; +using std::string; +using std::vector; -using avro::json::JsonParser; using avro::json::JsonGenerator; using avro::json::JsonNullFormatter; +using avro::json::JsonParser; class JsonGrammarGenerator : public ValidatingGrammarGenerator { - ProductionPtr doGenerate(const NodePtr& n, - std::map<NodePtr, ProductionPtr> &m); + ProductionPtr doGenerate(const NodePtr &n, + std::map<NodePtr, ProductionPtr> &m) final; }; -static std::string nameOf(const NodePtr& n) -{ +static std::string nameOf(const NodePtr &n) { if (n->hasName()) { - return n->name(); + return std::string(n->name()); } std::ostringstream oss; oss << n->type(); return oss.str(); } -ProductionPtr JsonGrammarGenerator::doGenerate(const NodePtr& n, - std::map<NodePtr, ProductionPtr> &m) { +ProductionPtr JsonGrammarGenerator::doGenerate(const NodePtr &n, + std::map<NodePtr, ProductionPtr> &m) { switch (n->type()) { - case AVRO_NULL: - case AVRO_BOOL: - case AVRO_INT: - case AVRO_LONG: - case AVRO_FLOAT: - case AVRO_DOUBLE: - case AVRO_STRING: - case AVRO_BYTES: - case AVRO_FIXED: - case AVRO_ARRAY: - case AVRO_MAP: - case AVRO_SYMBOLIC: - return ValidatingGrammarGenerator::doGenerate(n, m); - case AVRO_RECORD: - { + case AVRO_NULL: + case AVRO_BOOL: + case AVRO_INT: + case AVRO_LONG: + case AVRO_FLOAT: + case AVRO_DOUBLE: + case AVRO_STRING: + case AVRO_BYTES: + case AVRO_FIXED: + case AVRO_ARRAY: + case AVRO_MAP: + case AVRO_SYMBOLIC: + return ValidatingGrammarGenerator::doGenerate(n, m); + case AVRO_RECORD: { ProductionPtr result = make_shared<Production>(); m.erase(n); @@ -92,7 +86,7 @@ ProductionPtr JsonGrammarGenerator::doGenerate(const NodePtr& n, result->reserve(2 + 2 * c); result->push_back(Symbol::recordStartSymbol()); for (size_t i = 0; i < c; ++i) { - const NodePtr& leaf = n->leafAt(i); + const NodePtr &leaf = n->leafAt(i); ProductionPtr v = doGenerate(leaf, m); result->push_back(Symbol::fieldSymbol(n->nameAt(i))); copy(v->rbegin(), v->rend(), back_inserter(*result)); @@ -103,8 +97,7 @@ ProductionPtr JsonGrammarGenerator::doGenerate(const NodePtr& n, m[n] = result; return make_shared<Production>(1, Symbol::indirect(result)); } - case AVRO_ENUM: - { + case AVRO_ENUM: { vector<string> nn; size_t c = n->names(); nn.reserve(c); @@ -117,8 +110,7 @@ ProductionPtr JsonGrammarGenerator::doGenerate(const NodePtr& n, m[n] = result; return result; } - case AVRO_UNION: - { + case AVRO_UNION: { size_t c = n->leaves(); vector<ProductionPtr> vv; @@ -128,7 +120,7 @@ ProductionPtr JsonGrammarGenerator::doGenerate(const NodePtr& n, names.reserve(c); for (size_t i = 0; i < c; ++i) { - const NodePtr& nn = n->leafAt(i); + const NodePtr &nn = n->leafAt(i); ProductionPtr v = doGenerate(nn, m); if (nn->type() != AVRO_NULL) { ProductionPtr v2 = make_shared<Production>(); @@ -145,234 +137,215 @@ ProductionPtr JsonGrammarGenerator::doGenerate(const NodePtr& n, result->push_back(Symbol::unionSymbol()); return result; } - default: - throw Exception("Unknown node type"); + default: + throw Exception("Unknown node type"); } } -static void expectToken(JsonParser& in, JsonParser::Token tk) -{ +static void expectToken(JsonParser &in, JsonParser::Token tk) { in.expectToken(tk); } class JsonDecoderHandler { - JsonParser& in_; + JsonParser &in_; + public: - JsonDecoderHandler(JsonParser& p) : in_(p) { } - size_t handle(const Symbol& s) { + explicit JsonDecoderHandler(JsonParser &p) : in_(p) {} + size_t handle(const Symbol &s) { switch (s.kind()) { - case Symbol::sRecordStart: - expectToken(in_, JsonParser::tkObjectStart); - break; - case Symbol::sRecordEnd: - expectToken(in_, JsonParser::tkObjectEnd); - break; - case Symbol::sField: - expectToken(in_, JsonParser::tkString); - if (s.extra<string>() != in_.stringValue()) { - throw Exception("Incorrect field"); - } - break; - default: - break; + case Symbol::Kind::RecordStart: + expectToken(in_, JsonParser::Token::ObjectStart); + break; + case Symbol::Kind::RecordEnd: + expectToken(in_, JsonParser::Token::ObjectEnd); + break; + case Symbol::Kind::Field: + expectToken(in_, JsonParser::Token::String); + if (s.extra<string>() != in_.stringValue()) { + throw Exception(boost::format("Incorrect field: expected \"%1%\" but got \"%2%\".") % + s.extra<string>() % in_.stringValue()); + } + break; + default: + break; } return 0; } }; -template <typename P> +template<typename P> class JsonDecoder : public Decoder { JsonParser in_; JsonDecoderHandler handler_; P parser_; - void init(InputStream& is); - void decodeNull(); - bool decodeBool(); - int32_t decodeInt(); - int64_t decodeLong(); - float decodeFloat(); - double decodeDouble(); - void decodeString(string& value); - void skipString(); - void decodeBytes(vector<uint8_t>& value); - void skipBytes(); - void decodeFixed(size_t n, vector<uint8_t>& value); - void skipFixed(size_t n); - size_t decodeEnum(); - size_t arrayStart(); - size_t arrayNext(); - size_t skipArray(); - size_t mapStart(); - size_t mapNext(); - size_t skipMap(); - size_t decodeUnionIndex(); + void init(InputStream &is) final; + void decodeNull() final; + bool decodeBool() final; + int32_t decodeInt() final; + int64_t decodeLong() final; + float decodeFloat() final; + double decodeDouble() final; + void decodeString(string &value) final; + void skipString() final; + void decodeBytes(vector<uint8_t> &value) final; + void skipBytes() final; + void decodeFixed(size_t n, vector<uint8_t> &value) final; + void skipFixed(size_t n) final; + size_t decodeEnum() final; + size_t arrayStart() final; + size_t arrayNext() final; + size_t skipArray() final; + size_t mapStart() final; + size_t mapNext() final; + size_t skipMap() final; + size_t decodeUnionIndex() final; void expect(JsonParser::Token tk); void skipComposite(); - void drain(); -public: - - JsonDecoder(const ValidSchema& s) : - handler_(in_), - parser_(JsonGrammarGenerator().generate(s), NULL, handler_) { } + void drain() final; +public: + explicit JsonDecoder(const ValidSchema &s) : handler_(in_), + parser_(JsonGrammarGenerator().generate(s), NULL, handler_) {} }; -template <typename P> -void JsonDecoder<P>::init(InputStream& is) -{ +template<typename P> +void JsonDecoder<P>::init(InputStream &is) { in_.init(is); parser_.reset(); } -template <typename P> -void JsonDecoder<P>::expect(JsonParser::Token tk) -{ +template<typename P> +void JsonDecoder<P>::expect(JsonParser::Token tk) { expectToken(in_, tk); } -template <typename P> -void JsonDecoder<P>::decodeNull() -{ - parser_.advance(Symbol::sNull); - expect(JsonParser::tkNull); +template<typename P> +void JsonDecoder<P>::decodeNull() { + parser_.advance(Symbol::Kind::Null); + expect(JsonParser::Token::Null); } -template <typename P> -bool JsonDecoder<P>::decodeBool() -{ - parser_.advance(Symbol::sBool); - expect(JsonParser::tkBool); +template<typename P> +bool JsonDecoder<P>::decodeBool() { + parser_.advance(Symbol::Kind::Bool); + expect(JsonParser::Token::Bool); bool result = in_.boolValue(); return result; } -template <typename P> -int32_t JsonDecoder<P>::decodeInt() -{ - parser_.advance(Symbol::sInt); - expect(JsonParser::tkLong); +template<typename P> +int32_t JsonDecoder<P>::decodeInt() { + parser_.advance(Symbol::Kind::Int); + expect(JsonParser::Token::Long); int64_t result = in_.longValue(); if (result < INT32_MIN || result > INT32_MAX) { throw Exception(boost::format("Value out of range for Avro int: %1%") - % result); + % result); } return static_cast<int32_t>(result); } -template <typename P> -int64_t JsonDecoder<P>::decodeLong() -{ - parser_.advance(Symbol::sLong); - expect(JsonParser::tkLong); +template<typename P> +int64_t JsonDecoder<P>::decodeLong() { + parser_.advance(Symbol::Kind::Long); + expect(JsonParser::Token::Long); int64_t result = in_.longValue(); return result; } -template <typename P> -float JsonDecoder<P>::decodeFloat() -{ - parser_.advance(Symbol::sFloat); - expect(JsonParser::tkDouble); +template<typename P> +float JsonDecoder<P>::decodeFloat() { + parser_.advance(Symbol::Kind::Float); + expect(JsonParser::Token::Double); double result = in_.doubleValue(); return static_cast<float>(result); } -template <typename P> -double JsonDecoder<P>::decodeDouble() -{ - parser_.advance(Symbol::sDouble); - expect(JsonParser::tkDouble); +template<typename P> +double JsonDecoder<P>::decodeDouble() { + parser_.advance(Symbol::Kind::Double); + expect(JsonParser::Token::Double); double result = in_.doubleValue(); return result; } -template <typename P> -void JsonDecoder<P>::decodeString(string& value) -{ - parser_.advance(Symbol::sString); - expect(JsonParser::tkString); +template<typename P> +void JsonDecoder<P>::decodeString(string &value) { + parser_.advance(Symbol::Kind::String); + expect(JsonParser::Token::String); value = in_.stringValue(); } -template <typename P> -void JsonDecoder<P>::skipString() -{ - parser_.advance(Symbol::sString); - expect(JsonParser::tkString); +template<typename P> +void JsonDecoder<P>::skipString() { + parser_.advance(Symbol::Kind::String); + expect(JsonParser::Token::String); } -static vector<uint8_t> toBytes(const string& s) -{ +static vector<uint8_t> toBytes(const string &s) { return vector<uint8_t>(s.begin(), s.end()); } -template <typename P> -void JsonDecoder<P>::decodeBytes(vector<uint8_t>& value ) -{ - parser_.advance(Symbol::sBytes); - expect(JsonParser::tkString); +template<typename P> +void JsonDecoder<P>::decodeBytes(vector<uint8_t> &value) { + parser_.advance(Symbol::Kind::Bytes); + expect(JsonParser::Token::String); value = toBytes(in_.bytesValue()); } -template <typename P> -void JsonDecoder<P>::skipBytes() -{ - parser_.advance(Symbol::sBytes); - expect(JsonParser::tkString); +template<typename P> +void JsonDecoder<P>::skipBytes() { + parser_.advance(Symbol::Kind::Bytes); + expect(JsonParser::Token::String); } -template <typename P> -void JsonDecoder<P>::decodeFixed(size_t n, vector<uint8_t>& value) -{ - parser_.advance(Symbol::sFixed); +template<typename P> +void JsonDecoder<P>::decodeFixed(size_t n, vector<uint8_t> &value) { + parser_.advance(Symbol::Kind::Fixed); parser_.assertSize(n); - expect(JsonParser::tkString); + expect(JsonParser::Token::String); value = toBytes(in_.bytesValue()); if (value.size() != n) { throw Exception("Incorrect value for fixed"); } } -template <typename P> -void JsonDecoder<P>::skipFixed(size_t n) -{ - parser_.advance(Symbol::sFixed); +template<typename P> +void JsonDecoder<P>::skipFixed(size_t n) { + parser_.advance(Symbol::Kind::Fixed); parser_.assertSize(n); - expect(JsonParser::tkString); + expect(JsonParser::Token::String); vector<uint8_t> result = toBytes(in_.bytesValue()); if (result.size() != n) { throw Exception("Incorrect value for fixed"); } } -template <typename P> -size_t JsonDecoder<P>::decodeEnum() -{ - parser_.advance(Symbol::sEnum); - expect(JsonParser::tkString); +template<typename P> +size_t JsonDecoder<P>::decodeEnum() { + parser_.advance(Symbol::Kind::Enum); + expect(JsonParser::Token::String); size_t result = parser_.indexForName(in_.stringValue()); return result; } -template <typename P> -size_t JsonDecoder<P>::arrayStart() -{ - parser_.advance(Symbol::sArrayStart); +template<typename P> +size_t JsonDecoder<P>::arrayStart() { + parser_.advance(Symbol::Kind::ArrayStart); parser_.pushRepeatCount(0); - expect(JsonParser::tkArrayStart); + expect(JsonParser::Token::ArrayStart); return arrayNext(); } -template <typename P> -size_t JsonDecoder<P>::arrayNext() -{ +template<typename P> +size_t JsonDecoder<P>::arrayNext() { parser_.processImplicitActions(); - if (in_.peek() == JsonParser::tkArrayEnd) { + if (in_.peek() == JsonParser::Token::ArrayEnd) { in_.advance(); parser_.popRepeater(); - parser_.advance(Symbol::sArrayEnd); + parser_.advance(Symbol::Kind::ArrayEnd); return 0; } parser_.nextRepeatCount(1); @@ -380,91 +353,84 @@ size_t JsonDecoder<P>::arrayNext() } template<typename P> -void JsonDecoder<P>::skipComposite() -{ +void JsonDecoder<P>::skipComposite() { size_t level = 0; - for (; ;) { + for (;;) { switch (in_.advance()) { - case JsonParser::tkArrayStart: - case JsonParser::tkObjectStart: - ++level; - continue; - case JsonParser::tkArrayEnd: - case JsonParser::tkObjectEnd: - if (level == 0) { - return; - } - --level; - continue; - default: - continue; + case JsonParser::Token::ArrayStart: + case JsonParser::Token::ObjectStart: + ++level; + continue; + case JsonParser::Token::ArrayEnd: + case JsonParser::Token::ObjectEnd: + if (level == 0) { + return; + } + --level; + continue; + default: + continue; } } } template<typename P> -void JsonDecoder<P>::drain() -{ +void JsonDecoder<P>::drain() { parser_.processImplicitActions(); in_.drain(); } -template <typename P> -size_t JsonDecoder<P>::skipArray() -{ - parser_.advance(Symbol::sArrayStart); +template<typename P> +size_t JsonDecoder<P>::skipArray() { + parser_.advance(Symbol::Kind::ArrayStart); parser_.pop(); - parser_.advance(Symbol::sArrayEnd); - expect(JsonParser::tkArrayStart); + parser_.advance(Symbol::Kind::ArrayEnd); + expect(JsonParser::Token::ArrayStart); skipComposite(); return 0; } -template <typename P> -size_t JsonDecoder<P>::mapStart() -{ - parser_.advance(Symbol::sMapStart); +template<typename P> +size_t JsonDecoder<P>::mapStart() { + parser_.advance(Symbol::Kind::MapStart); parser_.pushRepeatCount(0); - expect(JsonParser::tkObjectStart); + expect(JsonParser::Token::ObjectStart); return mapNext(); } -template <typename P> -size_t JsonDecoder<P>::mapNext() -{ +template<typename P> +size_t JsonDecoder<P>::mapNext() { parser_.processImplicitActions(); - if (in_.peek() == JsonParser::tkObjectEnd) { + if (in_.peek() == JsonParser::Token::ObjectEnd) { in_.advance(); parser_.popRepeater(); - parser_.advance(Symbol::sMapEnd); + parser_.advance(Symbol::Kind::MapEnd); return 0; } parser_.nextRepeatCount(1); return 1; } -template <typename P> -size_t JsonDecoder<P>::skipMap() -{ - parser_.advance(Symbol::sMapStart); +template<typename P> +size_t JsonDecoder<P>::skipMap() { + parser_.advance(Symbol::Kind::MapStart); parser_.pop(); - parser_.advance(Symbol::sMapEnd); - expect(JsonParser::tkObjectStart); + parser_.advance(Symbol::Kind::MapEnd); + expect(JsonParser::Token::ObjectStart); skipComposite(); return 0; } -template <typename P> -size_t JsonDecoder<P>::decodeUnionIndex() -{ - parser_.advance(Symbol::sUnion); +template<typename P> +size_t JsonDecoder<P>::decodeUnionIndex() { + parser_.advance(Symbol::Kind::Union); size_t result; - if (in_.peek() == JsonParser::tkNull) { + if (in_.peek() == JsonParser::Token::Null) { result = parser_.indexForName("null"); } else { - expect(JsonParser::tkObjectStart); - expect(JsonParser::tkString); + expect(JsonParser::Token::ObjectStart); + expect(JsonParser::Token::String); result = parser_.indexForName(in_.stringValue()); } parser_.selectBranch(result); @@ -473,113 +439,106 @@ size_t JsonDecoder<P>::decodeUnionIndex() template<typename F = JsonNullFormatter> class JsonHandler { - JsonGenerator<F>& generator_; + JsonGenerator<F> &generator_; + public: - JsonHandler(JsonGenerator<F>& g) : generator_(g) { } - size_t handle(const Symbol& s) { + explicit JsonHandler(JsonGenerator<F> &g) : generator_(g) {} + size_t handle(const Symbol &s) { switch (s.kind()) { - case Symbol::sRecordStart: - generator_.objectStart(); - break; - case Symbol::sRecordEnd: - generator_.objectEnd(); - break; - case Symbol::sField: - generator_.encodeString(s.extra<string>()); - break; - default: - break; + case Symbol::Kind::RecordStart: + generator_.objectStart(); + break; + case Symbol::Kind::RecordEnd: + generator_.objectEnd(); + break; + case Symbol::Kind::Field: + generator_.encodeString(s.extra<string>()); + break; + default: + break; } return 0; } }; -template <typename P, typename F = JsonNullFormatter> +template<typename P, typename F = JsonNullFormatter> class JsonEncoder : public Encoder { JsonGenerator<F> out_; JsonHandler<F> handler_; P parser_; - void init(OutputStream& os); - void flush(); - int64_t byteCount() const; - void encodeNull(); - void encodeBool(bool b); - void encodeInt(int32_t i); - void encodeLong(int64_t l); - void encodeFloat(float f); - void encodeDouble(double d); - void encodeString(const std::string& s); - void encodeBytes(const uint8_t *bytes, size_t len); - void encodeFixed(const uint8_t *bytes, size_t len); - void encodeEnum(size_t e); - void arrayStart(); - void arrayEnd(); - void mapStart(); - void mapEnd(); - void setItemCount(size_t count); - void startItem(); - void encodeUnionIndex(size_t e); + void init(OutputStream &os) final; + void flush() final; + int64_t byteCount() const final; + void encodeNull() final; + void encodeBool(bool b) final; + void encodeInt(int32_t i) final; + void encodeLong(int64_t l) final; + void encodeFloat(float f) final; + void encodeDouble(double d) final; + void encodeString(const std::string &s) final; + void encodeBytes(const uint8_t *bytes, size_t len) final; + void encodeFixed(const uint8_t *bytes, size_t len) final; + void encodeEnum(size_t e) final; + void arrayStart() final; + void arrayEnd() final; + void mapStart() final; + void mapEnd() final; + void setItemCount(size_t count) final; + void startItem() final; + void encodeUnionIndex(size_t e) final; + public: - JsonEncoder(const ValidSchema& schema) : - handler_(out_), - parser_(JsonGrammarGenerator().generate(schema), NULL, handler_) { } + explicit JsonEncoder(const ValidSchema &schema) : handler_(out_), + parser_(JsonGrammarGenerator().generate(schema), NULL, handler_) {} }; template<typename P, typename F> -void JsonEncoder<P, F>::init(OutputStream& os) -{ +void JsonEncoder<P, F>::init(OutputStream &os) { out_.init(os); } template<typename P, typename F> -void JsonEncoder<P, F>::flush() -{ +void JsonEncoder<P, F>::flush() { parser_.processImplicitActions(); out_.flush(); } template<typename P, typename F> -int64_t JsonEncoder<P, F>::byteCount() const -{ +int64_t JsonEncoder<P, F>::byteCount() const { return out_.byteCount(); } template<typename P, typename F> -void JsonEncoder<P, F>::encodeNull() -{ - parser_.advance(Symbol::sNull); +void JsonEncoder<P, F>::encodeNull() { + parser_.advance(Symbol::Kind::Null); out_.encodeNull(); } template<typename P, typename F> -void JsonEncoder<P, F>::encodeBool(bool b) -{ - parser_.advance(Symbol::sBool); +void JsonEncoder<P, F>::encodeBool(bool b) { + parser_.advance(Symbol::Kind::Bool); out_.encodeBool(b); } template<typename P, typename F> -void JsonEncoder<P, F>::encodeInt(int32_t i) -{ - parser_.advance(Symbol::sInt); +void JsonEncoder<P, F>::encodeInt(int32_t i) { + parser_.advance(Symbol::Kind::Int); out_.encodeNumber(i); } template<typename P, typename F> -void JsonEncoder<P, F>::encodeLong(int64_t l) -{ - parser_.advance(Symbol::sLong); +void JsonEncoder<P, F>::encodeLong(int64_t l) { + parser_.advance(Symbol::Kind::Long); out_.encodeNumber(l); } template<typename P, typename F> -void JsonEncoder<P, F>::encodeFloat(float f) -{ - parser_.advance(Symbol::sFloat); +void JsonEncoder<P, F>::encodeFloat(float f) { + parser_.advance(Symbol::Kind::Float); if (f == std::numeric_limits<float>::infinity()) { out_.encodeString("Infinity"); - } else if (f == -std::numeric_limits<float>::infinity()) { + } else if (-f == std::numeric_limits<float>::infinity()) { out_.encodeString("-Infinity"); } else if (boost::math::isnan(f)) { out_.encodeString("NaN"); @@ -589,12 +548,11 @@ void JsonEncoder<P, F>::encodeFloat(float f) } template<typename P, typename F> -void JsonEncoder<P, F>::encodeDouble(double d) -{ - parser_.advance(Symbol::sDouble); +void JsonEncoder<P, F>::encodeDouble(double d) { + parser_.advance(Symbol::Kind::Double); if (d == std::numeric_limits<double>::infinity()) { out_.encodeString("Infinity"); - } else if (d == -std::numeric_limits<double>::infinity()) { + } else if (-d == std::numeric_limits<double>::infinity()) { out_.encodeString("-Infinity"); } else if (boost::math::isnan(d)) { out_.encodeString("NaN"); @@ -604,86 +562,75 @@ void JsonEncoder<P, F>::encodeDouble(double d) } template<typename P, typename F> -void JsonEncoder<P, F>::encodeString(const std::string& s) -{ - parser_.advance(Symbol::sString); +void JsonEncoder<P, F>::encodeString(const std::string &s) { + parser_.advance(Symbol::Kind::String); out_.encodeString(s); } template<typename P, typename F> -void JsonEncoder<P, F>::encodeBytes(const uint8_t *bytes, size_t len) -{ - parser_.advance(Symbol::sBytes); +void JsonEncoder<P, F>::encodeBytes(const uint8_t *bytes, size_t len) { + parser_.advance(Symbol::Kind::Bytes); out_.encodeBinary(bytes, len); } template<typename P, typename F> -void JsonEncoder<P, F>::encodeFixed(const uint8_t *bytes, size_t len) -{ - parser_.advance(Symbol::sFixed); +void JsonEncoder<P, F>::encodeFixed(const uint8_t *bytes, size_t len) { + parser_.advance(Symbol::Kind::Fixed); parser_.assertSize(len); out_.encodeBinary(bytes, len); } template<typename P, typename F> -void JsonEncoder<P, F>::encodeEnum(size_t e) -{ - parser_.advance(Symbol::sEnum); - const string& s = parser_.nameForIndex(e); +void JsonEncoder<P, F>::encodeEnum(size_t e) { + parser_.advance(Symbol::Kind::Enum); + const string &s = parser_.nameForIndex(e); out_.encodeString(s); } template<typename P, typename F> -void JsonEncoder<P, F>::arrayStart() -{ - parser_.advance(Symbol::sArrayStart); +void JsonEncoder<P, F>::arrayStart() { + parser_.advance(Symbol::Kind::ArrayStart); parser_.pushRepeatCount(0); out_.arrayStart(); } template<typename P, typename F> -void JsonEncoder<P, F>::arrayEnd() -{ +void JsonEncoder<P, F>::arrayEnd() { parser_.popRepeater(); - parser_.advance(Symbol::sArrayEnd); + parser_.advance(Symbol::Kind::ArrayEnd); out_.arrayEnd(); } template<typename P, typename F> -void JsonEncoder<P, F>::mapStart() -{ - parser_.advance(Symbol::sMapStart); +void JsonEncoder<P, F>::mapStart() { + parser_.advance(Symbol::Kind::MapStart); parser_.pushRepeatCount(0); out_.objectStart(); } template<typename P, typename F> -void JsonEncoder<P, F>::mapEnd() -{ +void JsonEncoder<P, F>::mapEnd() { parser_.popRepeater(); - parser_.advance(Symbol::sMapEnd); + parser_.advance(Symbol::Kind::MapEnd); out_.objectEnd(); } template<typename P, typename F> -void JsonEncoder<P, F>::setItemCount(size_t count) -{ +void JsonEncoder<P, F>::setItemCount(size_t count) { parser_.nextRepeatCount(count); } template<typename P, typename F> -void JsonEncoder<P, F>::startItem() -{ +void JsonEncoder<P, F>::startItem() { parser_.processImplicitActions(); - if (parser_.top() != Symbol::sRepeater) { + if (parser_.top() != Symbol::Kind::Repeater) { throw Exception("startItem at not an item boundary"); } } template<typename P, typename F> -void JsonEncoder<P, F>::encodeUnionIndex(size_t e) -{ - parser_.advance(Symbol::sUnion); +void JsonEncoder<P, F>::encodeUnionIndex(size_t e) { + parser_.advance(Symbol::Kind::Union); const std::string name = parser_.nameForIndex(e); @@ -694,25 +641,21 @@ void JsonEncoder<P, F>::encodeUnionIndex(size_t e) parser_.selectBranch(e); } -} // namespace parsing +} // namespace parsing -DecoderPtr jsonDecoder(const ValidSchema& s) -{ +DecoderPtr jsonDecoder(const ValidSchema &s) { return std::make_shared<parsing::JsonDecoder< - parsing::SimpleParser<parsing::JsonDecoderHandler> > >(s); + parsing::SimpleParser<parsing::JsonDecoderHandler>>>(s); } -EncoderPtr jsonEncoder(const ValidSchema& schema) -{ +EncoderPtr jsonEncoder(const ValidSchema &schema) { return std::make_shared<parsing::JsonEncoder< - parsing::SimpleParser<parsing::JsonHandler<avro::json::JsonNullFormatter> >, avro::json::JsonNullFormatter> >(schema); + parsing::SimpleParser<parsing::JsonHandler<avro::json::JsonNullFormatter>>, avro::json::JsonNullFormatter>>(schema); } -EncoderPtr jsonPrettyEncoder(const ValidSchema& schema) -{ +EncoderPtr jsonPrettyEncoder(const ValidSchema &schema) { return std::make_shared<parsing::JsonEncoder< - parsing::SimpleParser<parsing::JsonHandler<avro::json::JsonPrettyFormatter> >, avro::json::JsonPrettyFormatter> >(schema); + parsing::SimpleParser<parsing::JsonHandler<avro::json::JsonPrettyFormatter>>, avro::json::JsonPrettyFormatter>>(schema); } -} // namespace avro - +} // namespace avro diff --git a/contrib/libs/apache/avro/impl/parsing/ResolvingDecoder.cc b/contrib/libs/apache/avro/impl/parsing/ResolvingDecoder.cc index f6dbacabcf1..d86f6e58293 100644 --- a/contrib/libs/apache/avro/impl/parsing/ResolvingDecoder.cc +++ b/contrib/libs/apache/avro/impl/parsing/ResolvingDecoder.cc @@ -16,24 +16,21 @@ * limitations under the License. */ -#define __STDC_LIMIT_MACROS - -#include <string> -#include <stack> -#include <map> #include <algorithm> +#include <map> #include <memory> -#include <ctype.h> +#include <string> +#include <utility> -#include "ValidatingCodec.hh" -#include "Symbol.hh" -#include "Types.hh" -#include "ValidSchema.hh" #include "Decoder.hh" #include "Encoder.hh" -#include "NodeImpl.hh" #include "Generic.hh" +#include "NodeImpl.hh" #include "Stream.hh" +#include "Symbol.hh" +#include "Types.hh" +#include "ValidSchema.hh" +#include "ValidatingCodec.hh" namespace avro { @@ -41,60 +38,60 @@ using std::make_shared; namespace parsing { +using std::make_shared; using std::shared_ptr; using std::static_pointer_cast; -using std::make_shared; -using std::unique_ptr; +using std::find_if; +using std::istringstream; +using std::make_pair; using std::map; +using std::ostringstream; using std::pair; -using std::vector; -using std::string; using std::reverse; -using std::ostringstream; -using std::istringstream; using std::stack; -using std::find_if; -using std::make_pair; +using std::string; +using std::unique_ptr; +using std::vector; typedef pair<NodePtr, NodePtr> NodePair; class ResolvingGrammarGenerator : public ValidatingGrammarGenerator { - ProductionPtr doGenerate2(const NodePtr& writer, - const NodePtr& reader, map<NodePair, ProductionPtr> &m, - map<NodePtr, ProductionPtr> &m2); - ProductionPtr resolveRecords(const NodePtr& writer, - const NodePtr& reader, map<NodePair, ProductionPtr> &m, - map<NodePtr, ProductionPtr> &m2); - ProductionPtr resolveUnion(const NodePtr& writer, - const NodePtr& reader, map<NodePair, ProductionPtr> &m, - map<NodePtr, ProductionPtr> &m2); - - static vector<pair<string, size_t> > fields(const NodePtr& n) { - vector<pair<string, size_t> > result; + ProductionPtr doGenerate2(const NodePtr &writer, + const NodePtr &reader, map<NodePair, ProductionPtr> &m, + map<NodePtr, ProductionPtr> &m2); + ProductionPtr resolveRecords(const NodePtr &writer, + const NodePtr &reader, map<NodePair, ProductionPtr> &m, + map<NodePtr, ProductionPtr> &m2); + ProductionPtr resolveUnion(const NodePtr &writer, + const NodePtr &reader, map<NodePair, ProductionPtr> &m, + map<NodePtr, ProductionPtr> &m2); + + static vector<pair<string, size_t>> fields(const NodePtr &n) { + vector<pair<string, size_t>> result; size_t c = n->names(); for (size_t i = 0; i < c; ++i) { - result.push_back(make_pair(n->nameAt(i), i)); + result.emplace_back(n->nameAt(i), i); } return result; } - static int bestBranch(const NodePtr& writer, const NodePtr& reader); + static int bestBranch(const NodePtr &writer, const NodePtr &reader); - ProductionPtr getWriterProduction(const NodePtr& n, - map<NodePtr, ProductionPtr>& m2); + ProductionPtr getWriterProduction(const NodePtr &n, + map<NodePtr, ProductionPtr> &m2); public: Symbol generate( - const ValidSchema& writer, const ValidSchema& reader); + const ValidSchema &writer, const ValidSchema &reader); }; Symbol ResolvingGrammarGenerator::generate( - const ValidSchema& writer, const ValidSchema& reader) { + const ValidSchema &writer, const ValidSchema &reader) { map<NodePtr, ProductionPtr> m2; - const NodePtr& rr = reader.root(); - const NodePtr& rw = writer.root(); + const NodePtr &rr = reader.root(); + const NodePtr &rw = writer.root(); ProductionPtr backup = ValidatingGrammarGenerator::doGenerate(rw, m2); fixup(backup, m2); @@ -104,9 +101,8 @@ Symbol ResolvingGrammarGenerator::generate( return Symbol::rootSymbol(main, backup); } -int ResolvingGrammarGenerator::bestBranch(const NodePtr& writer, - const NodePtr& reader) -{ +int ResolvingGrammarGenerator::bestBranch(const NodePtr &writer, + const NodePtr &reader) { Type t = writer->type(); const size_t c = reader->leaves(); @@ -127,30 +123,29 @@ int ResolvingGrammarGenerator::bestBranch(const NodePtr& writer, } for (size_t j = 0; j < c; ++j) { - const NodePtr& r = reader->leafAt(j); + const NodePtr &r = reader->leafAt(j); Type rt = r->type(); switch (t) { - case AVRO_INT: - if (rt == AVRO_LONG || rt == AVRO_DOUBLE || rt == AVRO_FLOAT) { - return j; - } - break; - case AVRO_LONG: - case AVRO_FLOAT: - if (rt == AVRO_DOUBLE) { - return j; - } - break; - default: - break; + case AVRO_INT: + if (rt == AVRO_LONG || rt == AVRO_DOUBLE || rt == AVRO_FLOAT) { + return j; + } + break; + case AVRO_LONG: + case AVRO_FLOAT: + if (rt == AVRO_DOUBLE) { + return j; + } + break; + default: + break; } } return -1; } -static shared_ptr<vector<uint8_t> > getAvroBinary( - const GenericDatum& defaultValue) -{ +static shared_ptr<vector<uint8_t>> getAvroBinary( + const GenericDatum &defaultValue) { EncoderPtr e = binaryEncoder(); unique_ptr<OutputStream> os = memoryOutputStream(); e->init(*os); @@ -160,20 +155,17 @@ static shared_ptr<vector<uint8_t> > getAvroBinary( } template<typename T1, typename T2> -struct equalsFirst -{ - const T1& v_; - equalsFirst(const T1& v) : v_(v) { } - bool operator()(const pair<T1, T2>& p) { +struct equalsFirst { + const T1 &v_; + explicit equalsFirst(const T1 &v) : v_(v) {} + bool operator()(const pair<T1, T2> &p) { return p.first == v_; } }; ProductionPtr ResolvingGrammarGenerator::getWriterProduction( - const NodePtr& n, map<NodePtr, ProductionPtr>& m2) -{ - const NodePtr& nn = (n->type() == AVRO_SYMBOLIC) ? - static_cast<const NodeSymbolic& >(*n).getNode() : n; + const NodePtr &n, map<NodePtr, ProductionPtr> &m2) { + const NodePtr &nn = (n->type() == AVRO_SYMBOLIC) ? static_cast<const NodeSymbolic &>(*n).getNode() : n; map<NodePtr, ProductionPtr>::const_iterator it2 = m2.find(nn); if (it2 != m2.end()) { return it2->second; @@ -185,14 +177,13 @@ ProductionPtr ResolvingGrammarGenerator::getWriterProduction( } ProductionPtr ResolvingGrammarGenerator::resolveRecords( - const NodePtr& writer, const NodePtr& reader, - map<NodePair, ProductionPtr>& m, - map<NodePtr, ProductionPtr>& m2) -{ + const NodePtr &writer, const NodePtr &reader, + map<NodePair, ProductionPtr> &m, + map<NodePtr, ProductionPtr> &m2) { ProductionPtr result = make_shared<Production>(); - vector<pair<string, size_t> > wf = fields(writer); - vector<pair<string, size_t> > rf = fields(reader); + vector<pair<string, size_t>> wf = fields(writer); + vector<pair<string, size_t>> rf = fields(reader); vector<size_t> fieldOrder; fieldOrder.reserve(reader->names()); @@ -202,14 +193,13 @@ ProductionPtr ResolvingGrammarGenerator::resolveRecords( * If no matching field is found for reader, arrange to skip the writer * field. */ - for (vector<pair<string, size_t> >::const_iterator it = wf.begin(); - it != wf.end(); ++it) { - vector<pair<string, size_t> >::iterator it2 = - find_if(rf.begin(), rf.end(), - equalsFirst<string, size_t>(it->first)); + for (vector<pair<string, size_t>>::const_iterator it = wf.begin(); + it != wf.end(); ++it) { + auto it2 = find_if(rf.begin(), rf.end(), + equalsFirst<string, size_t>(it->first)); if (it2 != rf.end()) { ProductionPtr p = doGenerate2(writer->leafAt(it->second), - reader->leafAt(it2->second), m, m2); + reader->leafAt(it2->second), m, m2); copy(p->rbegin(), p->rend(), back_inserter(*result)); fieldOrder.push_back(it2->second); rf.erase(it2); @@ -229,8 +219,8 @@ ProductionPtr ResolvingGrammarGenerator::resolveRecords( * Examine the reader fields left out, (i.e. those didn't have corresponding * writer field). */ - for (vector<pair<string, size_t> >::const_iterator it = rf.begin(); - it != rf.end(); ++it) { + for (vector<pair<string, size_t>>::const_iterator it = rf.begin(); + it != rf.end(); ++it) { NodePtr s = reader->leafAt(it->second); fieldOrder.push_back(it->second); @@ -238,13 +228,12 @@ ProductionPtr ResolvingGrammarGenerator::resolveRecords( if (s->type() == AVRO_SYMBOLIC) { s = resolveSymbol(s); } - shared_ptr<vector<uint8_t> > defaultBinary = + shared_ptr<vector<uint8_t>> defaultBinary = getAvroBinary(reader->defaultValueAt(it->second)); result->push_back(Symbol::defaultStartAction(defaultBinary)); - map<NodePair, shared_ptr<Production> >::const_iterator it2 = + map<NodePair, shared_ptr<Production>>::const_iterator it2 = m.find(NodePair(s, s)); - ProductionPtr p = (it2 == m.end()) ? - doGenerate2(s, s, m, m2) : it2->second; + ProductionPtr p = (it2 == m.end()) ? doGenerate2(s, s, m, m2) : it2->second; copy(p->rbegin(), p->rend(), back_inserter(*result)); result->push_back(Symbol::defaultEndAction()); } @@ -253,14 +242,12 @@ ProductionPtr ResolvingGrammarGenerator::resolveRecords( result->push_back(Symbol::recordAction()); return result; - } ProductionPtr ResolvingGrammarGenerator::resolveUnion( - const NodePtr& writer, const NodePtr& reader, - map<NodePair, ProductionPtr>& m, - map<NodePtr, ProductionPtr>& m2) -{ + const NodePtr &writer, const NodePtr &reader, + map<NodePair, ProductionPtr> &m, + map<NodePtr, ProductionPtr> &m2) { vector<ProductionPtr> v; size_t c = writer->leaves(); v.reserve(c); @@ -275,10 +262,9 @@ ProductionPtr ResolvingGrammarGenerator::resolveUnion( } ProductionPtr ResolvingGrammarGenerator::doGenerate2( - const NodePtr& w, const NodePtr& r, + const NodePtr &w, const NodePtr &r, map<NodePair, ProductionPtr> &m, - map<NodePtr, ProductionPtr> &m2) -{ + map<NodePtr, ProductionPtr> &m2) { const NodePtr writer = w->type() == AVRO_SYMBOLIC ? resolveSymbol(w) : w; const NodePtr reader = r->type() == AVRO_SYMBOLIC ? resolveSymbol(r) : r; Type writerType = writer->type(); @@ -286,59 +272,55 @@ ProductionPtr ResolvingGrammarGenerator::doGenerate2( if (writerType == readerType) { switch (writerType) { - case AVRO_NULL: - return make_shared<Production>(1, Symbol::nullSymbol()); - case AVRO_BOOL: - return make_shared<Production>(1, Symbol::boolSymbol()); - case AVRO_INT: - return make_shared<Production>(1, Symbol::intSymbol()); - case AVRO_LONG: - return make_shared<Production>(1, Symbol::longSymbol()); - case AVRO_FLOAT: - return make_shared<Production>(1, Symbol::floatSymbol()); - case AVRO_DOUBLE: - return make_shared<Production>(1, Symbol::doubleSymbol()); - case AVRO_STRING: - return make_shared<Production>(1, Symbol::stringSymbol()); - case AVRO_BYTES: - return make_shared<Production>(1, Symbol::bytesSymbol()); - case AVRO_FIXED: - if (writer->name() == reader->name() && - writer->fixedSize() == reader->fixedSize()) { - ProductionPtr result = make_shared<Production>(); - result->push_back(Symbol::sizeCheckSymbol(reader->fixedSize())); - result->push_back(Symbol::fixedSymbol()); - m[make_pair(writer, reader)] = result; - return result; - } - break; - case AVRO_RECORD: - if (writer->name() == reader->name()) { - const pair<NodePtr, NodePtr> key(writer, reader); - map<NodePair, ProductionPtr>::const_iterator kp = m.find(key); - if (kp != m.end()) { - return (kp->second) ? kp->second : - make_shared<Production>(1, Symbol::placeholder(key)); + case AVRO_NULL: + return make_shared<Production>(1, Symbol::nullSymbol()); + case AVRO_BOOL: + return make_shared<Production>(1, Symbol::boolSymbol()); + case AVRO_INT: + return make_shared<Production>(1, Symbol::intSymbol()); + case AVRO_LONG: + return make_shared<Production>(1, Symbol::longSymbol()); + case AVRO_FLOAT: + return make_shared<Production>(1, Symbol::floatSymbol()); + case AVRO_DOUBLE: + return make_shared<Production>(1, Symbol::doubleSymbol()); + case AVRO_STRING: + return make_shared<Production>(1, Symbol::stringSymbol()); + case AVRO_BYTES: + return make_shared<Production>(1, Symbol::bytesSymbol()); + case AVRO_FIXED: + if (writer->name() == reader->name() && writer->fixedSize() == reader->fixedSize()) { + ProductionPtr result = make_shared<Production>(); + result->push_back(Symbol::sizeCheckSymbol(reader->fixedSize())); + result->push_back(Symbol::fixedSymbol()); + m[make_pair(writer, reader)] = result; + return result; } - m[key] = ProductionPtr(); - ProductionPtr result = resolveRecords(writer, reader, m, m2); - m[key] = result; - return make_shared<Production>(1, Symbol::indirect(result)); - } - break; - - case AVRO_ENUM: - if (writer->name() == reader->name()) { - ProductionPtr result = make_shared<Production>(); - result->push_back(Symbol::enumAdjustSymbol(writer, reader)); - result->push_back(Symbol::enumSymbol()); - m[make_pair(writer, reader)] = result; - return result; - } - break; + break; + case AVRO_RECORD: + if (writer->name() == reader->name()) { + const pair<NodePtr, NodePtr> key(writer, reader); + map<NodePair, ProductionPtr>::const_iterator kp = m.find(key); + if (kp != m.end()) { + return (kp->second) ? kp->second : make_shared<Production>(1, Symbol::placeholder(key)); + } + m[key] = ProductionPtr(); + ProductionPtr result = resolveRecords(writer, reader, m, m2); + m[key] = result; + return make_shared<Production>(1, Symbol::indirect(result)); + } + break; - case AVRO_ARRAY: - { + case AVRO_ENUM: + if (writer->name() == reader->name()) { + ProductionPtr result = make_shared<Production>(); + result->push_back(Symbol::enumAdjustSymbol(writer, reader)); + result->push_back(Symbol::enumSymbol()); + m[make_pair(writer, reader)] = result; + return result; + } + break; + case AVRO_ARRAY: { ProductionPtr p = getWriterProduction(writer->leafAt(0), m2); ProductionPtr p2 = doGenerate2(writer->leafAt(0), reader->leafAt(0), m, m2); ProductionPtr result = make_shared<Production>(); @@ -347,10 +329,9 @@ ProductionPtr ResolvingGrammarGenerator::doGenerate2( result->push_back(Symbol::arrayStartSymbol()); return result; } - case AVRO_MAP: - { + case AVRO_MAP: { ProductionPtr pp = - doGenerate2(writer->leafAt(1),reader->leafAt(1), m, m2); + doGenerate2(writer->leafAt(1), reader->leafAt(1), m, m2); ProductionPtr v(new Production(*pp)); v->push_back(Symbol::stringSymbol()); @@ -365,16 +346,15 @@ ProductionPtr ResolvingGrammarGenerator::doGenerate2( result->push_back(Symbol::mapStartSymbol()); return result; } - case AVRO_UNION: - return resolveUnion(writer, reader, m, m2); - case AVRO_SYMBOLIC: - { - shared_ptr<NodeSymbolic> w = + case AVRO_UNION: + return resolveUnion(writer, reader, m, m2); + case AVRO_SYMBOLIC: { + shared_ptr<NodeSymbolic> w2 = static_pointer_cast<NodeSymbolic>(writer); - shared_ptr<NodeSymbolic> r = + shared_ptr<NodeSymbolic> r2 = static_pointer_cast<NodeSymbolic>(reader); - NodePair p(w->getNode(), r->getNode()); - map<NodePair, ProductionPtr>::iterator it = m.find(p); + NodePair p(w2->getNode(), r2->getNode()); + auto it = m.find(p); if (it != m.end() && it->second) { return it->second; } else { @@ -382,38 +362,34 @@ ProductionPtr ResolvingGrammarGenerator::doGenerate2( return make_shared<Production>(1, Symbol::placeholder(p)); } } - default: - throw Exception("Unknown node type"); + default: + throw Exception("Unknown node type"); } } else if (writerType == AVRO_UNION) { return resolveUnion(writer, reader, m, m2); } else { switch (readerType) { - case AVRO_LONG: - if (writerType == AVRO_INT) { - return make_shared<Production>(1, - Symbol::resolveSymbol(Symbol::sInt, Symbol::sLong)); - } - break; - case AVRO_FLOAT: - if (writerType == AVRO_INT || writerType == AVRO_LONG) { - return make_shared<Production>(1, - Symbol::resolveSymbol(writerType == AVRO_INT ? - Symbol::sInt : Symbol::sLong, Symbol::sFloat)); - } - break; - case AVRO_DOUBLE: - if (writerType == AVRO_INT || writerType == AVRO_LONG - || writerType == AVRO_FLOAT) { - return make_shared<Production>(1, - Symbol::resolveSymbol(writerType == AVRO_INT ? - Symbol::sInt : writerType == AVRO_LONG ? - Symbol::sLong : Symbol::sFloat, Symbol::sDouble)); - } - break; + case AVRO_LONG: + if (writerType == AVRO_INT) { + return make_shared<Production>(1, + Symbol::resolveSymbol(Symbol::Kind::Int, Symbol::Kind::Long)); + } + break; + case AVRO_FLOAT: + if (writerType == AVRO_INT || writerType == AVRO_LONG) { + return make_shared<Production>(1, + Symbol::resolveSymbol(writerType == AVRO_INT ? Symbol::Kind::Int : Symbol::Kind::Long, Symbol::Kind::Float)); + } + break; + case AVRO_DOUBLE: + if (writerType == AVRO_INT || writerType == AVRO_LONG + || writerType == AVRO_FLOAT) { + return make_shared<Production>(1, + Symbol::resolveSymbol(writerType == AVRO_INT ? Symbol::Kind::Int : writerType == AVRO_LONG ? Symbol::Kind::Long : Symbol::Kind::Float, Symbol::Kind::Double)); + } + break; - case AVRO_UNION: - { + case AVRO_UNION: { int j = bestBranch(writer, reader); if (j >= 0) { ProductionPtr p = doGenerate2(writer, reader->leafAt(j), m, m2); @@ -422,245 +398,219 @@ ProductionPtr ResolvingGrammarGenerator::doGenerate2( result->push_back(Symbol::unionSymbol()); return result; } - } - break; - case AVRO_NULL: - case AVRO_BOOL: - case AVRO_INT: - case AVRO_STRING: - case AVRO_BYTES: - case AVRO_ENUM: - case AVRO_ARRAY: - case AVRO_MAP: - case AVRO_RECORD: - break; - default: - throw Exception("Unknown node type"); + } break; + case AVRO_NULL: + case AVRO_BOOL: + case AVRO_INT: + case AVRO_STRING: + case AVRO_BYTES: + case AVRO_ENUM: + case AVRO_ARRAY: + case AVRO_MAP: + case AVRO_RECORD: + break; + default: + throw Exception("Unknown node type"); } } return make_shared<Production>(1, Symbol::error(writer, reader)); } class ResolvingDecoderHandler { - shared_ptr<vector<uint8_t> > defaultData_; + shared_ptr<vector<uint8_t>> defaultData_; unique_ptr<InputStream> inp_; DecoderPtr backup_; - DecoderPtr& base_; + DecoderPtr &base_; const DecoderPtr binDecoder; - public: - ResolvingDecoderHandler(DecoderPtr& base) : base_(base), - binDecoder(binaryDecoder()) { } - size_t handle(const Symbol& s) { + +public: + explicit ResolvingDecoderHandler(DecoderPtr &base) : base_(base), + binDecoder(binaryDecoder()) {} + size_t handle(const Symbol &s) { switch (s.kind()) { - case Symbol::sWriterUnion: - return base_->decodeUnionIndex(); - case Symbol::sDefaultStart: - defaultData_ = s.extra<shared_ptr<vector<uint8_t> > >(); - backup_ = base_; - inp_ = memoryInputStream(&(*defaultData_)[0], defaultData_->size()); - base_ = binDecoder; - base_->init(*inp_); - return 0; - case Symbol::sDefaultEnd: - base_= backup_; - backup_.reset(); - return 0; - default: - return 0; + case Symbol::Kind::WriterUnion: + return base_->decodeUnionIndex(); + case Symbol::Kind::DefaultStart: + defaultData_ = s.extra<shared_ptr<vector<uint8_t>>>(); + backup_ = base_; + inp_ = memoryInputStream(&(*defaultData_)[0], defaultData_->size()); + base_ = binDecoder; + base_->init(*inp_); + return 0; + case Symbol::Kind::DefaultEnd: + base_ = backup_; + backup_.reset(); + return 0; + default: + return 0; } } - void reset() - { - if (backup_ != NULL) - { - base_= backup_; + void reset() { + if (backup_ != nullptr) { + base_ = backup_; backup_.reset(); } } }; -template <typename Parser> -class ResolvingDecoderImpl : public ResolvingDecoder -{ +template<typename Parser> +class ResolvingDecoderImpl : public ResolvingDecoder { DecoderPtr base_; ResolvingDecoderHandler handler_; Parser parser_; - void init(InputStream& is); - void decodeNull(); - bool decodeBool(); - int32_t decodeInt(); - int64_t decodeLong(); - float decodeFloat(); - double decodeDouble(); - void decodeString(string& value); - void skipString(); - void decodeBytes(vector<uint8_t>& value); - void skipBytes(); - void decodeFixed(size_t n, vector<uint8_t>& value); - void skipFixed(size_t n); - size_t decodeEnum(); - size_t arrayStart(); - size_t arrayNext(); - size_t skipArray(); - size_t mapStart(); - size_t mapNext(); - size_t skipMap(); - size_t decodeUnionIndex(); - const vector<size_t>& fieldOrder(); - void drain() { + void init(InputStream &is) final; + void decodeNull() final; + bool decodeBool() final; + int32_t decodeInt() final; + int64_t decodeLong() final; + float decodeFloat() final; + double decodeDouble() final; + void decodeString(string &value) final; + void skipString() final; + void decodeBytes(vector<uint8_t> &value) final; + void skipBytes() final; + void decodeFixed(size_t n, vector<uint8_t> &value) final; + void skipFixed(size_t n) final; + size_t decodeEnum() final; + size_t arrayStart() final; + size_t arrayNext() final; + size_t skipArray() final; + size_t mapStart() final; + size_t mapNext() final; + size_t skipMap() final; + size_t decodeUnionIndex() final; + const vector<size_t> &fieldOrder() final; + void drain() final { parser_.processImplicitActions(); base_->drain(); } + public: - ResolvingDecoderImpl(const ValidSchema& writer, const ValidSchema& reader, - const DecoderPtr& base) : - base_(base), - handler_(base_), - parser_(ResolvingGrammarGenerator().generate(writer, reader), - &(*base_), handler_) - { + ResolvingDecoderImpl(const ValidSchema &writer, const ValidSchema &reader, + DecoderPtr base) : base_(std::move(base)), + handler_(base_), + parser_(ResolvingGrammarGenerator().generate(writer, reader), + &(*base_), handler_) { } }; -template <typename P> -void ResolvingDecoderImpl<P>::init(InputStream& is) -{ +template<typename P> +void ResolvingDecoderImpl<P>::init(InputStream &is) { handler_.reset(); base_->init(is); parser_.reset(); } -template <typename P> -void ResolvingDecoderImpl<P>::decodeNull() -{ - parser_.advance(Symbol::sNull); +template<typename P> +void ResolvingDecoderImpl<P>::decodeNull() { + parser_.advance(Symbol::Kind::Null); base_->decodeNull(); } -template <typename P> -bool ResolvingDecoderImpl<P>::decodeBool() -{ - parser_.advance(Symbol::sBool); +template<typename P> +bool ResolvingDecoderImpl<P>::decodeBool() { + parser_.advance(Symbol::Kind::Bool); return base_->decodeBool(); } -template <typename P> -int32_t ResolvingDecoderImpl<P>::decodeInt() -{ - parser_.advance(Symbol::sInt); +template<typename P> +int32_t ResolvingDecoderImpl<P>::decodeInt() { + parser_.advance(Symbol::Kind::Int); return base_->decodeInt(); } -template <typename P> -int64_t ResolvingDecoderImpl<P>::decodeLong() -{ - Symbol::Kind k = parser_.advance(Symbol::sLong); - return k == Symbol::sInt ? base_->decodeInt() : base_->decodeLong(); +template<typename P> +int64_t ResolvingDecoderImpl<P>::decodeLong() { + Symbol::Kind k = parser_.advance(Symbol::Kind::Long); + return k == Symbol::Kind::Int ? base_->decodeInt() : base_->decodeLong(); } -template <typename P> -float ResolvingDecoderImpl<P>::decodeFloat() -{ - Symbol::Kind k = parser_.advance(Symbol::sFloat); - return k == Symbol::sInt ? base_->decodeInt() : - k == Symbol::sLong ? base_->decodeLong() : - base_->decodeFloat(); +template<typename P> +float ResolvingDecoderImpl<P>::decodeFloat() { + Symbol::Kind k = parser_.advance(Symbol::Kind::Float); + return k == Symbol::Kind::Int ? base_->decodeInt() : k == Symbol::Kind::Long ? base_->decodeLong() : base_->decodeFloat(); } -template <typename P> -double ResolvingDecoderImpl<P>::decodeDouble() -{ - Symbol::Kind k = parser_.advance(Symbol::sDouble); - return k == Symbol::sInt ? base_->decodeInt() : - k == Symbol::sLong ? base_->decodeLong() : - k == Symbol::sFloat ? base_->decodeFloat() : - base_->decodeDouble(); +template<typename P> +double ResolvingDecoderImpl<P>::decodeDouble() { + Symbol::Kind k = parser_.advance(Symbol::Kind::Double); + return k == Symbol::Kind::Int ? base_->decodeInt() : k == Symbol::Kind::Long ? base_->decodeLong() : k == Symbol::Kind::Float ? base_->decodeFloat() : base_->decodeDouble(); } -template <typename P> -void ResolvingDecoderImpl<P>::decodeString(string& value) -{ - parser_.advance(Symbol::sString); +template<typename P> +void ResolvingDecoderImpl<P>::decodeString(string &value) { + parser_.advance(Symbol::Kind::String); base_->decodeString(value); } -template <typename P> -void ResolvingDecoderImpl<P>::skipString() -{ - parser_.advance(Symbol::sString); +template<typename P> +void ResolvingDecoderImpl<P>::skipString() { + parser_.advance(Symbol::Kind::String); base_->skipString(); } -template <typename P> -void ResolvingDecoderImpl<P>::decodeBytes(vector<uint8_t>& value) -{ - parser_.advance(Symbol::sBytes); +template<typename P> +void ResolvingDecoderImpl<P>::decodeBytes(vector<uint8_t> &value) { + parser_.advance(Symbol::Kind::Bytes); base_->decodeBytes(value); } -template <typename P> -void ResolvingDecoderImpl<P>::skipBytes() -{ - parser_.advance(Symbol::sBytes); +template<typename P> +void ResolvingDecoderImpl<P>::skipBytes() { + parser_.advance(Symbol::Kind::Bytes); base_->skipBytes(); } -template <typename P> -void ResolvingDecoderImpl<P>::decodeFixed(size_t n, vector<uint8_t>& value) -{ - parser_.advance(Symbol::sFixed); +template<typename P> +void ResolvingDecoderImpl<P>::decodeFixed(size_t n, vector<uint8_t> &value) { + parser_.advance(Symbol::Kind::Fixed); parser_.assertSize(n); return base_->decodeFixed(n, value); } -template <typename P> -void ResolvingDecoderImpl<P>::skipFixed(size_t n) -{ - parser_.advance(Symbol::sFixed); +template<typename P> +void ResolvingDecoderImpl<P>::skipFixed(size_t n) { + parser_.advance(Symbol::Kind::Fixed); parser_.assertSize(n); base_->skipFixed(n); } -template <typename P> -size_t ResolvingDecoderImpl<P>::decodeEnum() -{ - parser_.advance(Symbol::sEnum); +template<typename P> +size_t ResolvingDecoderImpl<P>::decodeEnum() { + parser_.advance(Symbol::Kind::Enum); size_t n = base_->decodeEnum(); return parser_.enumAdjust(n); } -template <typename P> -size_t ResolvingDecoderImpl<P>::arrayStart() -{ - parser_.advance(Symbol::sArrayStart); +template<typename P> +size_t ResolvingDecoderImpl<P>::arrayStart() { + parser_.advance(Symbol::Kind::ArrayStart); size_t result = base_->arrayStart(); parser_.pushRepeatCount(result); if (result == 0) { parser_.popRepeater(); - parser_.advance(Symbol::sArrayEnd); + parser_.advance(Symbol::Kind::ArrayEnd); } return result; } -template <typename P> -size_t ResolvingDecoderImpl<P>::arrayNext() -{ +template<typename P> +size_t ResolvingDecoderImpl<P>::arrayNext() { parser_.processImplicitActions(); size_t result = base_->arrayNext(); parser_.nextRepeatCount(result); if (result == 0) { parser_.popRepeater(); - parser_.advance(Symbol::sArrayEnd); + parser_.advance(Symbol::Kind::ArrayEnd); } return result; } -template <typename P> -size_t ResolvingDecoderImpl<P>::skipArray() -{ - parser_.advance(Symbol::sArrayStart); +template<typename P> +size_t ResolvingDecoderImpl<P>::skipArray() { + parser_.advance(Symbol::Kind::ArrayStart); size_t n = base_->skipArray(); if (n == 0) { parser_.pop(); @@ -668,40 +618,37 @@ size_t ResolvingDecoderImpl<P>::skipArray() parser_.pushRepeatCount(n); parser_.skip(*base_); } - parser_.advance(Symbol::sArrayEnd); + parser_.advance(Symbol::Kind::ArrayEnd); return 0; } -template <typename P> -size_t ResolvingDecoderImpl<P>::mapStart() -{ - parser_.advance(Symbol::sMapStart); +template<typename P> +size_t ResolvingDecoderImpl<P>::mapStart() { + parser_.advance(Symbol::Kind::MapStart); size_t result = base_->mapStart(); parser_.pushRepeatCount(result); if (result == 0) { parser_.popRepeater(); - parser_.advance(Symbol::sMapEnd); + parser_.advance(Symbol::Kind::MapEnd); } return result; } -template <typename P> -size_t ResolvingDecoderImpl<P>::mapNext() -{ +template<typename P> +size_t ResolvingDecoderImpl<P>::mapNext() { parser_.processImplicitActions(); size_t result = base_->mapNext(); parser_.nextRepeatCount(result); if (result == 0) { parser_.popRepeater(); - parser_.advance(Symbol::sMapEnd); + parser_.advance(Symbol::Kind::MapEnd); } return result; } -template <typename P> -size_t ResolvingDecoderImpl<P>::skipMap() -{ - parser_.advance(Symbol::sMapStart); +template<typename P> +size_t ResolvingDecoderImpl<P>::skipMap() { + parser_.advance(Symbol::Kind::MapStart); size_t n = base_->skipMap(); if (n == 0) { parser_.pop(); @@ -709,32 +656,28 @@ size_t ResolvingDecoderImpl<P>::skipMap() parser_.pushRepeatCount(n); parser_.skip(*base_); } - parser_.advance(Symbol::sMapEnd); + parser_.advance(Symbol::Kind::MapEnd); return 0; } -template <typename P> -size_t ResolvingDecoderImpl<P>::decodeUnionIndex() -{ - parser_.advance(Symbol::sUnion); +template<typename P> +size_t ResolvingDecoderImpl<P>::decodeUnionIndex() { + parser_.advance(Symbol::Kind::Union); return parser_.unionAdjust(); } -template <typename P> -const vector<size_t>& ResolvingDecoderImpl<P>::fieldOrder() -{ - parser_.advance(Symbol::sRecord); +template<typename P> +const vector<size_t> &ResolvingDecoderImpl<P>::fieldOrder() { + parser_.advance(Symbol::Kind::Record); return parser_.sizeList(); } -} // namespace parsing +} // namespace parsing -ResolvingDecoderPtr resolvingDecoder(const ValidSchema& writer, - const ValidSchema& reader, const DecoderPtr& base) { - return make_shared<parsing::ResolvingDecoderImpl - <parsing::SimpleParser<parsing::ResolvingDecoderHandler> > >( +ResolvingDecoderPtr resolvingDecoder(const ValidSchema &writer, + const ValidSchema &reader, const DecoderPtr &base) { + return make_shared<parsing::ResolvingDecoderImpl<parsing::SimpleParser<parsing::ResolvingDecoderHandler>>>( writer, reader, base); } -} // namespace avro - +} // namespace avro diff --git a/contrib/libs/apache/avro/impl/parsing/Symbol.cc b/contrib/libs/apache/avro/impl/parsing/Symbol.cc index 6eb83309be4..b7a35517af8 100644 --- a/contrib/libs/apache/avro/impl/parsing/Symbol.cc +++ b/contrib/libs/apache/avro/impl/parsing/Symbol.cc @@ -16,17 +16,16 @@ * limitations under the License. */ - #include "Symbol.hh" namespace avro { namespace parsing { -using std::vector; -using std::string; using std::ostringstream; +using std::string; +using std::vector; -const char* Symbol::stringValues[] = { +const char *Symbol::stringValues[] = { "TerminalLow", "Null", "Bool", @@ -66,11 +65,9 @@ const char* Symbol::stringValues[] = { "DefaultStart", "DefaultEnd", "ImplicitActionHigh", - "Error" -}; + "Error"}; -Symbol Symbol::enumAdjustSymbol(const NodePtr& writer, const NodePtr& reader) -{ +Symbol Symbol::enumAdjustSymbol(const NodePtr &writer, const NodePtr &reader) { vector<string> rs; size_t rc = reader->names(); for (size_t i = 0; i < rc; ++i) { @@ -84,28 +81,28 @@ Symbol Symbol::enumAdjustSymbol(const NodePtr& writer, const NodePtr& reader) vector<string> err; for (size_t i = 0; i < wc; ++i) { - const string& s = writer->nameAt(i); + const string &s = writer->nameAt(i); vector<string>::const_iterator it = find(rs.begin(), rs.end(), s); if (it == rs.end()) { - int pos = err.size() + 1; + auto pos = err.size() + 1; adj.push_back(-pos); err.push_back(s); } else { adj.push_back(it - rs.begin()); } } - return Symbol(sEnumAdjust, make_pair(adj, err)); + return Symbol(Kind::EnumAdjust, make_pair(adj, err)); } -Symbol Symbol::error(const NodePtr& writer, const NodePtr& reader) -{ +Symbol Symbol::error(const NodePtr &writer, const NodePtr &reader) { ostringstream oss; oss << "Cannot resolve: " << std::endl; writer->printJson(oss, 0); - oss << std::endl << "with" << std::endl; + oss << std::endl + << "with" << std::endl; reader->printJson(oss, 0); - return Symbol(sError, oss.str()); + return Symbol(Kind::Error, oss.str()); } -} // namespace parsing -} // namespace avro +} // namespace parsing +} // namespace avro diff --git a/contrib/libs/apache/avro/impl/parsing/Symbol.hh b/contrib/libs/apache/avro/impl/parsing/Symbol.hh index f4ecfe6e839..21e46a85ae4 100644 --- a/contrib/libs/apache/avro/impl/parsing/Symbol.hh +++ b/contrib/libs/apache/avro/impl/parsing/Symbol.hh @@ -19,18 +19,19 @@ #ifndef avro_parsing_Symbol_hh__ #define avro_parsing_Symbol_hh__ -#include <vector> #include <map> #include <set> -#include <stack> #include <sstream> +#include <stack> +#include <utility> +#include <vector> #include <boost/any.hpp> #include <boost/tuple/tuple.hpp> -#include "Node.hh" #include "Decoder.hh" #include "Exception.hh" +#include "Node.hh" namespace avro { namespace parsing { @@ -44,255 +45,253 @@ typedef boost::tuple<ProductionPtr, ProductionPtr> RootInfo; class Symbol { public: - enum Kind { - sTerminalLow, // extra has nothing - sNull, - sBool, - sInt, - sLong, - sFloat, - sDouble, - sString, - sBytes, - sArrayStart, - sArrayEnd, - sMapStart, - sMapEnd, - sFixed, - sEnum, - sUnion, - sTerminalHigh, - sSizeCheck, // Extra has size - sNameList, // Extra has a vector<string> - sRoot, // Root for a schema, extra is Symbol - sRepeater, // Array or Map, extra is symbol - sAlternative, // One of many (union), extra is Union - sPlaceholder, // To be fixed up later. - sIndirect, // extra is shared_ptr<Production> - sSymbolic, // extra is weal_ptr<Production> - sEnumAdjust, - sUnionAdjust, - sSkipStart, - sResolve, - - sImplicitActionLow, - sRecordStart, - sRecordEnd, - sField, // extra is string - sRecord, - sSizeList, - sWriterUnion, - sDefaultStart, // extra has default value in Avro binary encoding - sDefaultEnd, - sImplicitActionHigh, - sError + enum class Kind { + TerminalLow, // extra has nothing + Null, + Bool, + Int, + Long, + Float, + Double, + String, + Bytes, + ArrayStart, + ArrayEnd, + MapStart, + MapEnd, + Fixed, + Enum, + Union, + TerminalHigh, + SizeCheck, // Extra has size + NameList, // Extra has a vector<string> + Root, // Root for a schema, extra is Symbol + Repeater, // Array or Map, extra is symbol + Alternative, // One of many (union), extra is Union + Placeholder, // To be fixed up later. + Indirect, // extra is shared_ptr<Production> + Symbolic, // extra is weal_ptr<Production> + EnumAdjust, + UnionAdjust, + SkipStart, + Resolve, + + ImplicitActionLow, + RecordStart, + RecordEnd, + Field, // extra is string + Record, + SizeList, + WriterUnion, + DefaultStart, // extra has default value in Avro binary encoding + DefaultEnd, + ImplicitActionHigh, + Error }; private: Kind kind_; boost::any extra_; + explicit Symbol(Kind k) : kind_(k) {} + template<typename T> + Symbol(Kind k, T t) : kind_(k), extra_(t) {} - explicit Symbol(Kind k) : kind_(k) { } - template <typename T> Symbol(Kind k, T t) : kind_(k), extra_(t) { } public: - Kind kind() const { return kind_; } - template <typename T> T extra() const { + template<typename T> + T extra() const { return boost::any_cast<T>(extra_); } - template <typename T> T* extrap() { + template<typename T> + T *extrap() { return boost::any_cast<T>(&extra_); } - template <typename T> const T* extrap() const { + template<typename T> + const T *extrap() const { return boost::any_cast<T>(&extra_); } - template <typename T> void extra(const T& t) { + template<typename T> + void extra(const T &t) { extra_ = t; } bool isTerminal() const { - return kind_ > sTerminalLow && kind_ < sTerminalHigh; + return kind_ > Kind::TerminalLow && kind_ < Kind::TerminalHigh; } bool isImplicitAction() const { - return kind_ > sImplicitActionLow && kind_ < sImplicitActionHigh; + return kind_ > Kind::ImplicitActionLow && kind_ < Kind::ImplicitActionHigh; } - static const char* stringValues[]; - static const char* toString(Kind k) { - return stringValues[k]; + static const char *stringValues[]; + static const char *toString(Kind k) { + return stringValues[static_cast<size_t>(k)]; } - static Symbol rootSymbol(ProductionPtr& s) - { - return Symbol(Symbol::sRoot, RootInfo(s, std::make_shared<Production>())); + static Symbol rootSymbol(ProductionPtr &s) { + return Symbol(Kind::Root, RootInfo(s, std::make_shared<Production>())); } - static Symbol rootSymbol(const ProductionPtr& main, - const ProductionPtr& backup) - { - return Symbol(Symbol::sRoot, RootInfo(main, backup)); + static Symbol rootSymbol(const ProductionPtr &main, + const ProductionPtr &backup) { + return Symbol(Kind::Root, RootInfo(main, backup)); } static Symbol nullSymbol() { - return Symbol(sNull); + return Symbol(Kind::Null); } static Symbol boolSymbol() { - return Symbol(sBool); + return Symbol(Kind::Bool); } static Symbol intSymbol() { - return Symbol(sInt); + return Symbol(Kind::Int); } static Symbol longSymbol() { - return Symbol(sLong); + return Symbol(Kind::Long); } static Symbol floatSymbol() { - return Symbol(sFloat); + return Symbol(Kind::Float); } static Symbol doubleSymbol() { - return Symbol(sDouble); + return Symbol(Kind::Double); } static Symbol stringSymbol() { - return Symbol(sString); + return Symbol(Kind::String); } static Symbol bytesSymbol() { - return Symbol(sBytes); + return Symbol(Kind::Bytes); } static Symbol sizeCheckSymbol(size_t s) { - return Symbol(sSizeCheck, s); + return Symbol(Kind::SizeCheck, s); } static Symbol fixedSymbol() { - return Symbol(sFixed); + return Symbol(Kind::Fixed); } static Symbol enumSymbol() { - return Symbol(sEnum); + return Symbol(Kind::Enum); } static Symbol arrayStartSymbol() { - return Symbol(sArrayStart); + return Symbol(Kind::ArrayStart); } static Symbol arrayEndSymbol() { - return Symbol(sArrayEnd); + return Symbol(Kind::ArrayEnd); } static Symbol mapStartSymbol() { - return Symbol(sMapStart); + return Symbol(Kind::MapStart); } static Symbol mapEndSymbol() { - return Symbol(sMapEnd); + return Symbol(Kind::MapEnd); } - static Symbol repeater(const ProductionPtr& p, + static Symbol repeater(const ProductionPtr &p, bool isArray) { return repeater(p, p, isArray); } - static Symbol repeater(const ProductionPtr& read, - const ProductionPtr& skip, + static Symbol repeater(const ProductionPtr &read, + const ProductionPtr &skip, bool isArray) { std::stack<ssize_t> s; - return Symbol(sRepeater, RepeaterInfo(s, isArray, read, skip)); + return Symbol(Kind::Repeater, RepeaterInfo(s, isArray, read, skip)); } - static Symbol defaultStartAction(std::shared_ptr<std::vector<uint8_t> > bb) - { - return Symbol(sDefaultStart, bb); + static Symbol defaultStartAction(std::shared_ptr<std::vector<uint8_t>> bb) { + return Symbol(Kind::DefaultStart, std::move(bb)); } - static Symbol defaultEndAction() - { - return Symbol(sDefaultEnd); + static Symbol defaultEndAction() { + return Symbol(Kind::DefaultEnd); } static Symbol alternative( - const std::vector<ProductionPtr>& branches) - { - return Symbol(Symbol::sAlternative, branches); + const std::vector<ProductionPtr> &branches) { + return Symbol(Symbol::Kind::Alternative, branches); } static Symbol unionSymbol() { - return Symbol(sUnion); + return Symbol(Kind::Union); } static Symbol recordStartSymbol() { - return Symbol(sRecordStart); + return Symbol(Kind::RecordStart); } static Symbol recordEndSymbol() { - return Symbol(sRecordEnd); + return Symbol(Kind::RecordEnd); } - static Symbol fieldSymbol(const std::string& name) { - return Symbol(sField, name); + static Symbol fieldSymbol(const std::string &name) { + return Symbol(Kind::Field, name); } static Symbol writerUnionAction() { - return Symbol(sWriterUnion); + return Symbol(Kind::WriterUnion); } static Symbol nameListSymbol( - const std::vector<std::string>& v) { - return Symbol(sNameList, v); + const std::vector<std::string> &v) { + return Symbol(Kind::NameList, v); } - template <typename T> - static Symbol placeholder(const T& n) { - return Symbol(sPlaceholder, n); + template<typename T> + static Symbol placeholder(const T &n) { + return Symbol(Kind::Placeholder, n); } - static Symbol indirect(const ProductionPtr& p) { - return Symbol(sIndirect, p); + static Symbol indirect(const ProductionPtr &p) { + return Symbol(Kind::Indirect, p); } - static Symbol symbolic(const std::weak_ptr<Production>& p) { - return Symbol(sSymbolic, p); + static Symbol symbolic(const std::weak_ptr<Production> &p) { + return Symbol(Kind::Symbolic, p); } - static Symbol enumAdjustSymbol(const NodePtr& writer, - const NodePtr& reader); + static Symbol enumAdjustSymbol(const NodePtr &writer, + const NodePtr &reader); static Symbol unionAdjustSymbol(size_t branch, - const ProductionPtr& p) { - return Symbol(sUnionAdjust, std::make_pair(branch, p)); + const ProductionPtr &p) { + return Symbol(Kind::UnionAdjust, std::make_pair(branch, p)); } static Symbol sizeListAction(std::vector<size_t> order) { - return Symbol(sSizeList, order); + return Symbol(Kind::SizeList, std::move(order)); } static Symbol recordAction() { - return Symbol(sRecord); + return Symbol(Kind::Record); } - static Symbol error(const NodePtr& writer, const NodePtr& reader); + static Symbol error(const NodePtr &writer, const NodePtr &reader); static Symbol resolveSymbol(Kind w, Kind r) { - return Symbol(sResolve, std::make_pair(w, r)); + return Symbol(Kind::Resolve, std::make_pair(w, r)); } static Symbol skipStart() { - return Symbol(sSkipStart); + return Symbol(Kind::SkipStart); } - }; /** @@ -300,111 +299,95 @@ public: * corresponding values. */ template<typename T> -void fixup(const ProductionPtr& p, - const std::map<T, ProductionPtr> &m) -{ +void fixup(const ProductionPtr &p, + const std::map<T, ProductionPtr> &m) { std::set<ProductionPtr> seen; - for (Production::iterator it = p->begin(); it != p->end(); ++it) { - fixup(*it, m, seen); + for (auto &it : *p) { + fixup(it, m, seen); } } - /** * Recursively replaces all placeholders in the symbol with the values with the * corresponding values. */ template<typename T> -void fixup_internal(const ProductionPtr& p, +void fixup_internal(const ProductionPtr &p, const std::map<T, ProductionPtr> &m, - std::set<ProductionPtr>& seen) -{ + std::set<ProductionPtr> &seen) { if (seen.find(p) == seen.end()) { seen.insert(p); - for (Production::iterator it = p->begin(); it != p->end(); ++it) { - fixup(*it, m, seen); + for (auto &it : *p) { + fixup(it, m, seen); } } } template<typename T> -void fixup(Symbol& s, const std::map<T, ProductionPtr> &m, - std::set<ProductionPtr>& seen) -{ +void fixup(Symbol &s, const std::map<T, ProductionPtr> &m, + std::set<ProductionPtr> &seen) { switch (s.kind()) { - case Symbol::sIndirect: - fixup_internal(s.extra<ProductionPtr>(), m, seen); - break; - case Symbol::sAlternative: - { + case Symbol::Kind::Indirect: + fixup_internal(s.extra<ProductionPtr>(), m, seen); + break; + case Symbol::Kind::Alternative: { const std::vector<ProductionPtr> *vv = - s.extrap<std::vector<ProductionPtr> >(); - for (std::vector<ProductionPtr>::const_iterator it = vv->begin(); - it != vv->end(); ++it) { - fixup_internal(*it, m, seen); + s.extrap<std::vector<ProductionPtr>>(); + for (const auto &it : *vv) { + fixup_internal(it, m, seen); } - } - break; - case Symbol::sRepeater: - { - const RepeaterInfo& ri = *s.extrap<RepeaterInfo>(); + } break; + case Symbol::Kind::Repeater: { + const RepeaterInfo &ri = *s.extrap<RepeaterInfo>(); fixup_internal(boost::tuples::get<2>(ri), m, seen); fixup_internal(boost::tuples::get<3>(ri), m, seen); - } - break; - case Symbol::sPlaceholder: - { - typename std::map<T, std::shared_ptr<Production> >::const_iterator it = + } break; + case Symbol::Kind::Placeholder: { + typename std::map<T, std::shared_ptr<Production>>::const_iterator it = m.find(s.extra<T>()); if (it == m.end()) { throw Exception("Placeholder symbol cannot be resolved"); } s = Symbol::symbolic(std::weak_ptr<Production>(it->second)); - } - break; - case Symbol::sUnionAdjust: - fixup_internal(s.extrap<std::pair<size_t, ProductionPtr> >()->second, - m, seen); - break; - default: - break; + } break; + case Symbol::Kind::UnionAdjust: + fixup_internal(s.extrap<std::pair<size_t, ProductionPtr>>()->second, + m, seen); + break; + default: + break; } } template<typename Handler> class SimpleParser { - Decoder* decoder_; - Handler& handler_; + Decoder *decoder_; + Handler &handler_; std::stack<Symbol> parsingStack; - static void throwMismatch(Symbol::Kind actual, Symbol::Kind expected) - { + static void throwMismatch(Symbol::Kind actual, Symbol::Kind expected) { std::ostringstream oss; - oss << "Invalid operation. Schema requires: " << - Symbol::toString(expected) << ", got: " << - Symbol::toString(actual); + oss << "Invalid operation. Schema requires: " << Symbol::toString(expected) << ", got: " << Symbol::toString(actual); throw Exception(oss.str()); } - static void assertMatch(Symbol::Kind actual, Symbol::Kind expected) - { + static void assertMatch(Symbol::Kind actual, Symbol::Kind expected) { if (expected != actual) { throwMismatch(actual, expected); } - } - void append(const ProductionPtr& ss) { + void append(const ProductionPtr &ss) { for (Production::const_iterator it = ss->begin(); - it != ss->end(); ++it) { + it != ss->end(); ++it) { parsingStack.push(*it); } } size_t popSize() { - const Symbol& s = parsingStack.top(); - assertMatch(Symbol::sSizeCheck, s.kind()); - size_t result = s.extra<size_t>(); + const Symbol &s = parsingStack.top(); + assertMatch(Symbol::Kind::SizeCheck, s.kind()); + auto result = s.extra<size_t>(); parsingStack.pop(); return result; } @@ -419,10 +402,10 @@ class SimpleParser { public: Symbol::Kind advance(Symbol::Kind k) { - for (; ;) { - Symbol& s = parsingStack.top(); -// std::cout << "advance: " << Symbol::toString(s.kind()) -// << " looking for " << Symbol::toString(k) << '\n'; + for (;;) { + Symbol &s = parsingStack.top(); + // std::cout << "advance: " << Symbol::toString(s.kind()) + // << " looking for " << Symbol::toString(k) << '\n'; if (s.kind() == k) { parsingStack.pop(); return k; @@ -430,29 +413,26 @@ public: throwMismatch(k, s.kind()); } else { switch (s.kind()) { - case Symbol::sRoot: - append(boost::tuples::get<0>(*s.extrap<RootInfo>())); - continue; - case Symbol::sIndirect: - { + case Symbol::Kind::Root: + append(boost::tuples::get<0>(*s.extrap<RootInfo>())); + continue; + case Symbol::Kind::Indirect: { ProductionPtr pp = s.extra<ProductionPtr>(); parsingStack.pop(); append(pp); } - continue; - case Symbol::sSymbolic: - { + continue; + case Symbol::Kind::Symbolic: { ProductionPtr pp( - s.extra<std::weak_ptr<Production> >()); + s.extra<std::weak_ptr<Production>>()); parsingStack.pop(); append(pp); } - continue; - case Symbol::sRepeater: - { - RepeaterInfo *p = s.extrap<RepeaterInfo>(); - std::stack<ssize_t>& ns = boost::tuples::get<0>(*p); + continue; + case Symbol::Kind::Repeater: { + auto *p = s.extrap<RepeaterInfo>(); + std::stack<ssize_t> &ns = boost::tuples::get<0>(*p); if (ns.empty()) { throw Exception( "Empty item count stack in repeater advance"); @@ -464,134 +444,127 @@ public: --ns.top(); append(boost::tuples::get<2>(*p)); } - continue; - case Symbol::sError: - throw Exception(s.extra<std::string>()); - case Symbol::sResolve: - { - const std::pair<Symbol::Kind, Symbol::Kind>* p = - s.extrap<std::pair<Symbol::Kind, Symbol::Kind> >(); + continue; + case Symbol::Kind::Error: + throw Exception(s.extra<std::string>()); + case Symbol::Kind::Resolve: { + const std::pair<Symbol::Kind, Symbol::Kind> *p = + s.extrap<std::pair<Symbol::Kind, Symbol::Kind>>(); assertMatch(p->second, k); Symbol::Kind result = p->first; parsingStack.pop(); return result; } - case Symbol::sSkipStart: - parsingStack.pop(); - skip(*decoder_); - break; - default: - if (s.isImplicitAction()) { - size_t n = handler_.handle(s); - if (s.kind() == Symbol::sWriterUnion) { - parsingStack.pop(); - selectBranch(n); + case Symbol::Kind::SkipStart: + parsingStack.pop(); + skip(*decoder_); + break; + default: + if (s.isImplicitAction()) { + size_t n = handler_.handle(s); + if (s.kind() == Symbol::Kind::WriterUnion) { + parsingStack.pop(); + selectBranch(n); + } else { + parsingStack.pop(); + } } else { - parsingStack.pop(); + std::ostringstream oss; + oss << "Encountered " << Symbol::toString(s.kind()) + << " while looking for " << Symbol::toString(k); + throw Exception(oss.str()); } - } else { - std::ostringstream oss; - oss << "Encountered " << Symbol::toString(s.kind()) - << " while looking for " << Symbol::toString(k); - throw Exception(oss.str()); - } } } } } - void skip(Decoder& d) { + void skip(Decoder &d) { const size_t sz = parsingStack.size(); if (sz == 0) { throw Exception("Nothing to skip!"); } while (parsingStack.size() >= sz) { - Symbol& t = parsingStack.top(); + Symbol &t = parsingStack.top(); // std::cout << "skip: " << Symbol::toString(t.kind()) << '\n'; switch (t.kind()) { - case Symbol::sNull: - d.decodeNull(); - break; - case Symbol::sBool: - d.decodeBool(); - break; - case Symbol::sInt: - d.decodeInt(); - break; - case Symbol::sLong: - d.decodeLong(); - break; - case Symbol::sFloat: - d.decodeFloat(); - break; - case Symbol::sDouble: - d.decodeDouble(); - break; - case Symbol::sString: - d.skipString(); - break; - case Symbol::sBytes: - d.skipBytes(); - break; - case Symbol::sArrayStart: - { + case Symbol::Kind::Null: + d.decodeNull(); + break; + case Symbol::Kind::Bool: + d.decodeBool(); + break; + case Symbol::Kind::Int: + d.decodeInt(); + break; + case Symbol::Kind::Long: + d.decodeLong(); + break; + case Symbol::Kind::Float: + d.decodeFloat(); + break; + case Symbol::Kind::Double: + d.decodeDouble(); + break; + case Symbol::Kind::String: + d.skipString(); + break; + case Symbol::Kind::Bytes: + d.skipBytes(); + break; + case Symbol::Kind::ArrayStart: { parsingStack.pop(); size_t n = d.skipArray(); processImplicitActions(); - assertMatch(Symbol::sRepeater, parsingStack.top().kind()); + assertMatch(Symbol::Kind::Repeater, parsingStack.top().kind()); if (n == 0) { break; } - Symbol& t = parsingStack.top(); - RepeaterInfo *p = t.extrap<RepeaterInfo>(); + Symbol &t2 = parsingStack.top(); + auto *p = t2.extrap<RepeaterInfo>(); boost::tuples::get<0>(*p).push(n); continue; } - case Symbol::sArrayEnd: - break; - case Symbol::sMapStart: - { + case Symbol::Kind::ArrayEnd: + break; + case Symbol::Kind::MapStart: { parsingStack.pop(); size_t n = d.skipMap(); processImplicitActions(); - assertMatch(Symbol::sRepeater, parsingStack.top().kind()); + assertMatch(Symbol::Kind::Repeater, parsingStack.top().kind()); if (n == 0) { break; } - Symbol& t = parsingStack.top(); - RepeaterInfo *p = t.extrap<RepeaterInfo>(); - boost::tuples::get<0>(*p).push(n); + Symbol &t2 = parsingStack.top(); + auto *p2 = t2.extrap<RepeaterInfo>(); + boost::tuples::get<0>(*p2).push(n); continue; } - case Symbol::sMapEnd: - break; - case Symbol::sFixed: - { + case Symbol::Kind::MapEnd: + break; + case Symbol::Kind::Fixed: { parsingStack.pop(); - Symbol& t = parsingStack.top(); - d.decodeFixed(t.extra<size_t>()); - } - break; - case Symbol::sEnum: - parsingStack.pop(); - d.decodeEnum(); - break; - case Symbol::sUnion: - { + Symbol &t2 = parsingStack.top(); + d.decodeFixed(t2.extra<size_t>()); + } break; + case Symbol::Kind::Enum: + parsingStack.pop(); + d.decodeEnum(); + break; + case Symbol::Kind::Union: { parsingStack.pop(); size_t n = d.decodeUnionIndex(); selectBranch(n); continue; } - case Symbol::sRepeater: - { - RepeaterInfo *p = t.extrap<RepeaterInfo>(); - std::stack<ssize_t>& ns = boost::tuples::get<0>(*p); + case Symbol::Kind::Repeater: { + auto *p = t.extrap<RepeaterInfo>(); + std::stack<ssize_t> &ns = boost::tuples::get<0>(*p); if (ns.empty()) { throw Exception( "Empty item count stack in repeater skip"); } - ssize_t& n = ns.top(); + ssize_t &n = ns.top(); if (n == 0) { n = boost::tuples::get<1>(*p) ? d.arrayNext() : d.mapNext(); @@ -603,26 +576,23 @@ public: } else { ns.pop(); } + break; } - break; - case Symbol::sIndirect: - { + case Symbol::Kind::Indirect: { ProductionPtr pp = t.extra<ProductionPtr>(); parsingStack.pop(); append(pp); } - continue; - case Symbol::sSymbolic: - { + continue; + case Symbol::Kind::Symbolic: { ProductionPtr pp( - t.extra<std::weak_ptr<Production> >()); + t.extra<std::weak_ptr<Production>>()); parsingStack.pop(); append(pp); } - continue; - default: - { + continue; + default: { std::ostringstream oss; oss << "Don't know how to skip " << Symbol::toString(t.kind()); @@ -647,10 +617,9 @@ public: } size_t enumAdjust(size_t n) { - const Symbol& s = parsingStack.top(); - assertMatch(Symbol::sEnumAdjust, s.kind()); - const std::pair<std::vector<int>, std::vector<std::string> >* v = - s.extrap<std::pair<std::vector<int>, std::vector<std::string> > >(); + const Symbol &s = parsingStack.top(); + assertMatch(Symbol::Kind::EnumAdjust, s.kind()); + const auto *v = s.extrap<std::pair<std::vector<int>, std::vector<std::string>>>(); assertLessThan(n, v->first.size()); int result = v->first[n]; @@ -665,20 +634,20 @@ public: } size_t unionAdjust() { - const Symbol& s = parsingStack.top(); - assertMatch(Symbol::sUnionAdjust, s.kind()); + const Symbol &s = parsingStack.top(); + assertMatch(Symbol::Kind::UnionAdjust, s.kind()); std::pair<size_t, ProductionPtr> p = - s.extra<std::pair<size_t, ProductionPtr> >(); + s.extra<std::pair<size_t, ProductionPtr>>(); parsingStack.pop(); append(p.second); return p.first; } std::string nameForIndex(size_t e) { - const Symbol& s = parsingStack.top(); - assertMatch(Symbol::sNameList, s.kind()); + const Symbol &s = parsingStack.top(); + assertMatch(Symbol::Kind::NameList, s.kind()); const std::vector<std::string> names = - s.extra<std::vector<std::string> >(); + s.extra<std::vector<std::string>>(); if (e >= names.size()) { throw Exception("Not that many names"); } @@ -688,12 +657,11 @@ public: } size_t indexForName(const std::string &name) { - const Symbol& s = parsingStack.top(); - assertMatch(Symbol::sNameList, s.kind()); + const Symbol &s = parsingStack.top(); + assertMatch(Symbol::Kind::NameList, s.kind()); const std::vector<std::string> names = - s.extra<std::vector<std::string> >(); - std::vector<std::string>::const_iterator it = - std::find(names.begin(), names.end(), name); + s.extra<std::vector<std::string>>(); + auto it = std::find(names.begin(), names.end(), name); if (it == names.end()) { throw Exception("No such enum symbol"); } @@ -704,30 +672,30 @@ public: void pushRepeatCount(size_t n) { processImplicitActions(); - Symbol& s = parsingStack.top(); - assertMatch(Symbol::sRepeater, s.kind()); - RepeaterInfo *p = s.extrap<RepeaterInfo>(); + Symbol &s = parsingStack.top(); + assertMatch(Symbol::Kind::Repeater, s.kind()); + auto *p = s.extrap<RepeaterInfo>(); std::stack<ssize_t> &nn = boost::tuples::get<0>(*p); nn.push(n); } void nextRepeatCount(size_t n) { processImplicitActions(); - Symbol& s = parsingStack.top(); - assertMatch(Symbol::sRepeater, s.kind()); - RepeaterInfo *p = s.extrap<RepeaterInfo>(); + Symbol &s = parsingStack.top(); + assertMatch(Symbol::Kind::Repeater, s.kind()); + auto *p = s.extrap<RepeaterInfo>(); std::stack<ssize_t> &nn = boost::tuples::get<0>(*p); if (nn.empty() || nn.top() != 0) { - throw Exception("Wrong number of items"); + throw Exception("Wrong number of items"); } nn.top() = n; } void popRepeater() { processImplicitActions(); - Symbol& s = parsingStack.top(); - assertMatch(Symbol::sRepeater, s.kind()); - RepeaterInfo *p = s.extrap<RepeaterInfo>(); + Symbol &s = parsingStack.top(); + assertMatch(Symbol::Kind::Repeater, s.kind()); + auto *p = s.extrap<RepeaterInfo>(); std::stack<ssize_t> &ns = boost::tuples::get<0>(*p); if (ns.empty()) { throw Exception("Incorrect number of items (empty)"); @@ -740,10 +708,10 @@ public: } void selectBranch(size_t n) { - const Symbol& s = parsingStack.top(); - assertMatch(Symbol::sAlternative, s.kind()); + const Symbol &s = parsingStack.top(); + assertMatch(Symbol::Kind::Alternative, s.kind()); std::vector<ProductionPtr> v = - s.extra<std::vector<ProductionPtr> >(); + s.extra<std::vector<ProductionPtr>>(); if (n >= v.size()) { throw Exception("Not that many branches"); } @@ -751,10 +719,10 @@ public: append(v[n]); } - const std::vector<size_t>& sizeList() { - const Symbol& s = parsingStack.top(); - assertMatch(Symbol::sSizeList, s.kind()); - return *s.extrap<std::vector<size_t> >(); + const std::vector<size_t> &sizeList() { + const Symbol &s = parsingStack.top(); + assertMatch(Symbol::Kind::SizeList, s.kind()); + return *s.extrap<std::vector<size_t>>(); } Symbol::Kind top() const { @@ -766,12 +734,12 @@ public: } void processImplicitActions() { - for (; ;) { - Symbol& s = parsingStack.top(); + for (;;) { + Symbol &s = parsingStack.top(); if (s.isImplicitAction()) { handler_.handle(s); parsingStack.pop(); - } else if (s.kind() == Symbol::sSkipStart) { + } else if (s.kind() == Symbol::Kind::SkipStart) { parsingStack.pop(); skip(*decoder_); } else { @@ -780,8 +748,7 @@ public: } } - SimpleParser(const Symbol& s, Decoder* d, Handler& h) : - decoder_(d), handler_(h) { + SimpleParser(const Symbol &s, Decoder *d, Handler &h) : decoder_(d), handler_(h) { parsingStack.push(s); } @@ -790,65 +757,51 @@ public: parsingStack.pop(); } } - }; -inline std::ostream& operator<<(std::ostream& os, const Symbol s); +inline std::ostream &operator<<(std::ostream &os, const Symbol &s); -inline std::ostream& operator<<(std::ostream& os, const Production p) -{ +inline std::ostream &operator<<(std::ostream &os, const Production &p) { os << '('; - for (Production::const_iterator it = p.begin(); it != p.end(); ++it) { - os << *it << ", "; + for (const auto &it : p) { + os << it << ", "; } os << ')'; return os; } -inline std::ostream& operator<<(std::ostream& os, const Symbol s) -{ - switch (s.kind()) { - case Symbol::sRepeater: - { - const RepeaterInfo& ri = *s.extrap<RepeaterInfo>(); - os << '(' << Symbol::toString(s.kind()) - << ' ' << *boost::tuples::get<2>(ri) - << ' ' << *boost::tuples::get<3>(ri) - << ')'; - } - break; - case Symbol::sIndirect: - { - os << '(' << Symbol::toString(s.kind()) << ' ' - << *s.extra<std::shared_ptr<Production> >() << ')'; - } - break; - case Symbol::sAlternative: - { - os << '(' << Symbol::toString(s.kind()); - for (std::vector<ProductionPtr>::const_iterator it = - s.extrap<std::vector<ProductionPtr> >()->begin(); - it != s.extrap<std::vector<ProductionPtr> >()->end(); - ++it) { - os << ' ' << **it; - } - os << ')'; +inline std::ostream &operator<<(std::ostream &os, const Symbol &s) { + switch (s.kind()) { + case Symbol::Kind::Repeater: { + const RepeaterInfo &ri = *s.extrap<RepeaterInfo>(); + os << '(' << Symbol::toString(s.kind()) + << ' ' << *boost::tuples::get<2>(ri) + << ' ' << *boost::tuples::get<3>(ri) + << ')'; + } break; + case Symbol::Kind::Indirect: { + os << '(' << Symbol::toString(s.kind()) << ' ' + << *s.extra<std::shared_ptr<Production>>() << ')'; + } break; + case Symbol::Kind::Alternative: { + os << '(' << Symbol::toString(s.kind()); + for (const auto &it : *s.extrap<std::vector<ProductionPtr>>()) { + os << ' ' << *it; } - break; - case Symbol::sSymbolic: - { - os << '(' << Symbol::toString(s.kind()) - << ' ' << s.extra<std::weak_ptr<Production> >().lock() - << ')'; - } - break; + os << ')'; + } break; + case Symbol::Kind::Symbolic: { + os << '(' << Symbol::toString(s.kind()) + << ' ' << s.extra<std::weak_ptr<Production>>().lock() + << ')'; + } break; default: os << Symbol::toString(s.kind()); break; - } - return os; } -} // namespace parsing -} // namespace avro + return os; +} +} // namespace parsing +} // namespace avro #endif diff --git a/contrib/libs/apache/avro/impl/parsing/ValidatingCodec.cc b/contrib/libs/apache/avro/impl/parsing/ValidatingCodec.cc index fdf6ef898f9..cfb82225f15 100644 --- a/contrib/libs/apache/avro/impl/parsing/ValidatingCodec.cc +++ b/contrib/libs/apache/avro/impl/parsing/ValidatingCodec.cc @@ -18,16 +18,16 @@ #include "ValidatingCodec.hh" -#include <string> -#include <map> #include <algorithm> -#include <memory> #include <boost/any.hpp> +#include <map> +#include <memory> +#include <utility> -#include "ValidSchema.hh" #include "Decoder.hh" #include "Encoder.hh" #include "NodeImpl.hh" +#include "ValidSchema.hh" namespace avro { @@ -39,62 +39,58 @@ using std::shared_ptr; using std::static_pointer_cast; using std::map; -using std::vector; +using std::ostringstream; using std::pair; -using std::string; using std::reverse; -using std::ostringstream; +using std::string; +using std::vector; /** Follows the design of Avro Parser in Java. */ -ProductionPtr ValidatingGrammarGenerator::generate(const NodePtr& n) -{ +ProductionPtr ValidatingGrammarGenerator::generate(const NodePtr &n) { map<NodePtr, ProductionPtr> m; ProductionPtr result = doGenerate(n, m); fixup(result, m); return result; } -Symbol ValidatingGrammarGenerator::generate(const ValidSchema& schema) -{ +Symbol ValidatingGrammarGenerator::generate(const ValidSchema &schema) { ProductionPtr r = generate(schema.root()); return Symbol::rootSymbol(r); } -ProductionPtr ValidatingGrammarGenerator::doGenerate(const NodePtr& n, - map<NodePtr, ProductionPtr> &m) { +ProductionPtr ValidatingGrammarGenerator::doGenerate(const NodePtr &n, + map<NodePtr, ProductionPtr> &m) { switch (n->type()) { - case AVRO_NULL: - return make_shared<Production>(1, Symbol::nullSymbol()); - case AVRO_BOOL: - return make_shared<Production>(1, Symbol::boolSymbol()); - case AVRO_INT: - return make_shared<Production>(1, Symbol::intSymbol()); - case AVRO_LONG: - return make_shared<Production>(1, Symbol::longSymbol()); - case AVRO_FLOAT: - return make_shared<Production>(1, Symbol::floatSymbol()); - case AVRO_DOUBLE: - return make_shared<Production>(1, Symbol::doubleSymbol()); - case AVRO_STRING: - return make_shared<Production>(1, Symbol::stringSymbol()); - case AVRO_BYTES: - return make_shared<Production>(1, Symbol::bytesSymbol()); - case AVRO_FIXED: - { + case AVRO_NULL: + return make_shared<Production>(1, Symbol::nullSymbol()); + case AVRO_BOOL: + return make_shared<Production>(1, Symbol::boolSymbol()); + case AVRO_INT: + return make_shared<Production>(1, Symbol::intSymbol()); + case AVRO_LONG: + return make_shared<Production>(1, Symbol::longSymbol()); + case AVRO_FLOAT: + return make_shared<Production>(1, Symbol::floatSymbol()); + case AVRO_DOUBLE: + return make_shared<Production>(1, Symbol::doubleSymbol()); + case AVRO_STRING: + return make_shared<Production>(1, Symbol::stringSymbol()); + case AVRO_BYTES: + return make_shared<Production>(1, Symbol::bytesSymbol()); + case AVRO_FIXED: { ProductionPtr result = make_shared<Production>(); result->push_back(Symbol::sizeCheckSymbol(n->fixedSize())); result->push_back(Symbol::fixedSymbol()); m[n] = result; return result; } - case AVRO_RECORD: - { + case AVRO_RECORD: { ProductionPtr result = make_shared<Production>(); m.erase(n); size_t c = n->leaves(); for (size_t i = 0; i < c; ++i) { - const NodePtr& leaf = n->leafAt(i); + const NodePtr &leaf = n->leafAt(i); ProductionPtr v = doGenerate(leaf, m); copy(v->rbegin(), v->rend(), back_inserter(*result)); } @@ -103,26 +99,23 @@ ProductionPtr ValidatingGrammarGenerator::doGenerate(const NodePtr& n, m[n] = result; return make_shared<Production>(1, Symbol::indirect(result)); } - case AVRO_ENUM: - { + case AVRO_ENUM: { ProductionPtr result = make_shared<Production>(); result->push_back(Symbol::sizeCheckSymbol(n->names())); result->push_back(Symbol::enumSymbol()); m[n] = result; return result; } - case AVRO_ARRAY: - { + case AVRO_ARRAY: { ProductionPtr result = make_shared<Production>(); result->push_back(Symbol::arrayEndSymbol()); result->push_back(Symbol::repeater(doGenerate(n->leafAt(0), m), true)); result->push_back(Symbol::arrayStartSymbol()); return result; } - case AVRO_MAP: - { + case AVRO_MAP: { ProductionPtr pp = doGenerate(n->leafAt(1), m); - ProductionPtr v(new Production(*pp)); + ProductionPtr v(new Production(*pp)); v->push_back(Symbol::stringSymbol()); ProductionPtr result = make_shared<Production>(); result->push_back(Symbol::mapEndSymbol()); @@ -130,8 +123,7 @@ ProductionPtr ValidatingGrammarGenerator::doGenerate(const NodePtr& n, result->push_back(Symbol::mapStartSymbol()); return result; } - case AVRO_UNION: - { + case AVRO_UNION: { vector<ProductionPtr> vv; size_t c = n->leaves(); vv.reserve(c); @@ -143,12 +135,10 @@ ProductionPtr ValidatingGrammarGenerator::doGenerate(const NodePtr& n, result->push_back(Symbol::unionSymbol()); return result; } - case AVRO_SYMBOLIC: - { + case AVRO_SYMBOLIC: { shared_ptr<NodeSymbolic> ns = static_pointer_cast<NodeSymbolic>(n); NodePtr nn = ns->getNode(); - map<NodePtr, ProductionPtr>::iterator it = - m.find(nn); + auto it = m.find(nn); if (it != m.end() && it->second) { return it->second; } else { @@ -156,186 +146,166 @@ ProductionPtr ValidatingGrammarGenerator::doGenerate(const NodePtr& n, return make_shared<Production>(1, Symbol::placeholder(nn)); } } - default: - throw Exception("Unknown node type"); + default: + throw Exception("Unknown node type"); } } struct DummyHandler { - size_t handle(const Symbol& s) { + static size_t handle(const Symbol &s) { return 0; } }; -template <typename P> +template<typename P> class ValidatingDecoder : public Decoder { const shared_ptr<Decoder> base; DummyHandler handler_; P parser; - void init(InputStream& is); - void decodeNull(); - bool decodeBool(); - int32_t decodeInt(); - int64_t decodeLong(); - float decodeFloat(); - double decodeDouble(); - void decodeString(string& value); - void skipString(); - void decodeBytes(vector<uint8_t>& value); - void skipBytes(); - void decodeFixed(size_t n, vector<uint8_t>& value); - void skipFixed(size_t n); - size_t decodeEnum(); - size_t arrayStart(); - size_t arrayNext(); - size_t skipArray(); - size_t mapStart(); - size_t mapNext(); - size_t skipMap(); - size_t decodeUnionIndex(); - void drain() { + void init(InputStream &is) final; + void decodeNull() final; + bool decodeBool() final; + int32_t decodeInt() final; + int64_t decodeLong() final; + float decodeFloat() final; + double decodeDouble() final; + void decodeString(string &value) final; + void skipString() final; + void decodeBytes(vector<uint8_t> &value) final; + void skipBytes() final; + void decodeFixed(size_t n, vector<uint8_t> &value) final; + void skipFixed(size_t n) final; + size_t decodeEnum() final; + size_t arrayStart() final; + size_t arrayNext() final; + size_t skipArray() final; + size_t mapStart() final; + size_t mapNext() final; + size_t skipMap() final; + size_t decodeUnionIndex() final; + void drain() final { base->drain(); } public: - - ValidatingDecoder(const ValidSchema& s, const shared_ptr<Decoder> b) : - base(b), - parser(ValidatingGrammarGenerator().generate(s), NULL, handler_) { } - + ValidatingDecoder(const ValidSchema &s, const shared_ptr<Decoder> &b) : base(b), + parser(ValidatingGrammarGenerator().generate(s), NULL, handler_) {} }; -template <typename P> -void ValidatingDecoder<P>::init(InputStream& is) -{ +template<typename P> +void ValidatingDecoder<P>::init(InputStream &is) { base->init(is); } -template <typename P> -void ValidatingDecoder<P>::decodeNull() -{ - parser.advance(Symbol::sNull); +template<typename P> +void ValidatingDecoder<P>::decodeNull() { + parser.advance(Symbol::Kind::Null); base->decodeNull(); } -template <typename P> -bool ValidatingDecoder<P>::decodeBool() -{ - parser.advance(Symbol::sBool); +template<typename P> +bool ValidatingDecoder<P>::decodeBool() { + parser.advance(Symbol::Kind::Bool); return base->decodeBool(); } -template <typename P> -int32_t ValidatingDecoder<P>::decodeInt() -{ - parser.advance(Symbol::sInt); +template<typename P> +int32_t ValidatingDecoder<P>::decodeInt() { + parser.advance(Symbol::Kind::Int); return base->decodeInt(); } -template <typename P> -int64_t ValidatingDecoder<P>::decodeLong() -{ - parser.advance(Symbol::sLong); +template<typename P> +int64_t ValidatingDecoder<P>::decodeLong() { + parser.advance(Symbol::Kind::Long); return base->decodeLong(); } -template <typename P> -float ValidatingDecoder<P>::decodeFloat() -{ - parser.advance(Symbol::sFloat); +template<typename P> +float ValidatingDecoder<P>::decodeFloat() { + parser.advance(Symbol::Kind::Float); return base->decodeFloat(); } -template <typename P> -double ValidatingDecoder<P>::decodeDouble() -{ - parser.advance(Symbol::sDouble); +template<typename P> +double ValidatingDecoder<P>::decodeDouble() { + parser.advance(Symbol::Kind::Double); return base->decodeDouble(); } -template <typename P> -void ValidatingDecoder<P>::decodeString(string& value) -{ - parser.advance(Symbol::sString); +template<typename P> +void ValidatingDecoder<P>::decodeString(string &value) { + parser.advance(Symbol::Kind::String); base->decodeString(value); } -template <typename P> -void ValidatingDecoder<P>::skipString() -{ - parser.advance(Symbol::sString); +template<typename P> +void ValidatingDecoder<P>::skipString() { + parser.advance(Symbol::Kind::String); base->skipString(); } -template <typename P> -void ValidatingDecoder<P>::decodeBytes(vector<uint8_t>& value) -{ - parser.advance(Symbol::sBytes); +template<typename P> +void ValidatingDecoder<P>::decodeBytes(vector<uint8_t> &value) { + parser.advance(Symbol::Kind::Bytes); base->decodeBytes(value); } -template <typename P> -void ValidatingDecoder<P>::skipBytes() -{ - parser.advance(Symbol::sBytes); +template<typename P> +void ValidatingDecoder<P>::skipBytes() { + parser.advance(Symbol::Kind::Bytes); base->skipBytes(); } -template <typename P> -void ValidatingDecoder<P>::decodeFixed(size_t n, vector<uint8_t>& value) -{ - parser.advance(Symbol::sFixed); +template<typename P> +void ValidatingDecoder<P>::decodeFixed(size_t n, vector<uint8_t> &value) { + parser.advance(Symbol::Kind::Fixed); parser.assertSize(n); base->decodeFixed(n, value); } -template <typename P> -void ValidatingDecoder<P>::skipFixed(size_t n) -{ - parser.advance(Symbol::sFixed); +template<typename P> +void ValidatingDecoder<P>::skipFixed(size_t n) { + parser.advance(Symbol::Kind::Fixed); parser.assertSize(n); base->skipFixed(n); } -template <typename P> -size_t ValidatingDecoder<P>::decodeEnum() -{ - parser.advance(Symbol::sEnum); +template<typename P> +size_t ValidatingDecoder<P>::decodeEnum() { + parser.advance(Symbol::Kind::Enum); size_t result = base->decodeEnum(); parser.assertLessThanSize(result); return result; } -template <typename P> -size_t ValidatingDecoder<P>::arrayStart() -{ - parser.advance(Symbol::sArrayStart); +template<typename P> +size_t ValidatingDecoder<P>::arrayStart() { + parser.advance(Symbol::Kind::ArrayStart); size_t result = base->arrayStart(); parser.pushRepeatCount(result); if (result == 0) { parser.popRepeater(); - parser.advance(Symbol::sArrayEnd); + parser.advance(Symbol::Kind::ArrayEnd); } return result; } -template <typename P> -size_t ValidatingDecoder<P>::arrayNext() -{ +template<typename P> +size_t ValidatingDecoder<P>::arrayNext() { size_t result = base->arrayNext(); parser.nextRepeatCount(result); if (result == 0) { parser.popRepeater(); - parser.advance(Symbol::sArrayEnd); + parser.advance(Symbol::Kind::ArrayEnd); } return result; } -template <typename P> -size_t ValidatingDecoder<P>::skipArray() -{ - parser.advance(Symbol::sArrayStart); +template<typename P> +size_t ValidatingDecoder<P>::skipArray() { + parser.advance(Symbol::Kind::ArrayStart); size_t n = base->skipArray(); if (n == 0) { parser.pop(); @@ -343,39 +313,36 @@ size_t ValidatingDecoder<P>::skipArray() parser.pushRepeatCount(n); parser.skip(*base); } - parser.advance(Symbol::sArrayEnd); + parser.advance(Symbol::Kind::ArrayEnd); return 0; } -template <typename P> -size_t ValidatingDecoder<P>::mapStart() -{ - parser.advance(Symbol::sMapStart); +template<typename P> +size_t ValidatingDecoder<P>::mapStart() { + parser.advance(Symbol::Kind::MapStart); size_t result = base->mapStart(); parser.pushRepeatCount(result); if (result == 0) { parser.popRepeater(); - parser.advance(Symbol::sMapEnd); + parser.advance(Symbol::Kind::MapEnd); } return result; } -template <typename P> -size_t ValidatingDecoder<P>::mapNext() -{ +template<typename P> +size_t ValidatingDecoder<P>::mapNext() { size_t result = base->mapNext(); parser.nextRepeatCount(result); if (result == 0) { parser.popRepeater(); - parser.advance(Symbol::sMapEnd); + parser.advance(Symbol::Kind::MapEnd); } return result; } -template <typename P> -size_t ValidatingDecoder<P>::skipMap() -{ - parser.advance(Symbol::sMapStart); +template<typename P> +size_t ValidatingDecoder<P>::skipMap() { + parser.advance(Symbol::Kind::MapStart); size_t n = base->skipMap(); if (n == 0) { parser.pop(); @@ -383,209 +350,185 @@ size_t ValidatingDecoder<P>::skipMap() parser.pushRepeatCount(n); parser.skip(*base); } - parser.advance(Symbol::sMapEnd); + parser.advance(Symbol::Kind::MapEnd); return 0; } -template <typename P> -size_t ValidatingDecoder<P>::decodeUnionIndex() -{ - parser.advance(Symbol::sUnion); +template<typename P> +size_t ValidatingDecoder<P>::decodeUnionIndex() { + parser.advance(Symbol::Kind::Union); size_t result = base->decodeUnionIndex(); parser.selectBranch(result); return result; } -template <typename P> +template<typename P> class ValidatingEncoder : public Encoder { DummyHandler handler_; P parser_; EncoderPtr base_; - void init(OutputStream& os); - void flush(); - int64_t byteCount() const; - void encodeNull(); - void encodeBool(bool b); - void encodeInt(int32_t i); - void encodeLong(int64_t l); - void encodeFloat(float f); - void encodeDouble(double d); - void encodeString(const std::string& s); - void encodeBytes(const uint8_t *bytes, size_t len); - void encodeFixed(const uint8_t *bytes, size_t len); - void encodeEnum(size_t e); - void arrayStart(); - void arrayEnd(); - void mapStart(); - void mapEnd(); - void setItemCount(size_t count); - void startItem(); - void encodeUnionIndex(size_t e); + void init(OutputStream &os) final; + void flush() final; + int64_t byteCount() const final; + void encodeNull() final; + void encodeBool(bool b) final; + void encodeInt(int32_t i) final; + void encodeLong(int64_t l) final; + void encodeFloat(float f) final; + void encodeDouble(double d) final; + void encodeString(const std::string &s) final; + void encodeBytes(const uint8_t *bytes, size_t len) final; + void encodeFixed(const uint8_t *bytes, size_t len) final; + void encodeEnum(size_t e) final; + void arrayStart() final; + void arrayEnd() final; + void mapStart() final; + void mapEnd() final; + void setItemCount(size_t count) final; + void startItem() final; + void encodeUnionIndex(size_t e) final; + public: - ValidatingEncoder(const ValidSchema& schema, const EncoderPtr& base) : - parser_(ValidatingGrammarGenerator().generate(schema), NULL, handler_), - base_(base) { } + ValidatingEncoder(const ValidSchema &schema, EncoderPtr base) : parser_(ValidatingGrammarGenerator().generate(schema), NULL, handler_), + base_(std::move(base)) {} }; template<typename P> -void ValidatingEncoder<P>::init(OutputStream& os) -{ +void ValidatingEncoder<P>::init(OutputStream &os) { base_->init(os); } template<typename P> -void ValidatingEncoder<P>::flush() -{ +void ValidatingEncoder<P>::flush() { base_->flush(); } template<typename P> -void ValidatingEncoder<P>::encodeNull() -{ - parser_.advance(Symbol::sNull); +void ValidatingEncoder<P>::encodeNull() { + parser_.advance(Symbol::Kind::Null); base_->encodeNull(); } template<typename P> -void ValidatingEncoder<P>::encodeBool(bool b) -{ - parser_.advance(Symbol::sBool); +void ValidatingEncoder<P>::encodeBool(bool b) { + parser_.advance(Symbol::Kind::Bool); base_->encodeBool(b); } template<typename P> -void ValidatingEncoder<P>::encodeInt(int32_t i) -{ - parser_.advance(Symbol::sInt); +void ValidatingEncoder<P>::encodeInt(int32_t i) { + parser_.advance(Symbol::Kind::Int); base_->encodeInt(i); } template<typename P> -void ValidatingEncoder<P>::encodeLong(int64_t l) -{ - parser_.advance(Symbol::sLong); +void ValidatingEncoder<P>::encodeLong(int64_t l) { + parser_.advance(Symbol::Kind::Long); base_->encodeLong(l); } template<typename P> -void ValidatingEncoder<P>::encodeFloat(float f) -{ - parser_.advance(Symbol::sFloat); +void ValidatingEncoder<P>::encodeFloat(float f) { + parser_.advance(Symbol::Kind::Float); base_->encodeFloat(f); } template<typename P> -void ValidatingEncoder<P>::encodeDouble(double d) -{ - parser_.advance(Symbol::sDouble); +void ValidatingEncoder<P>::encodeDouble(double d) { + parser_.advance(Symbol::Kind::Double); base_->encodeDouble(d); } template<typename P> -void ValidatingEncoder<P>::encodeString(const std::string& s) -{ - parser_.advance(Symbol::sString); +void ValidatingEncoder<P>::encodeString(const std::string &s) { + parser_.advance(Symbol::Kind::String); base_->encodeString(s); } template<typename P> -void ValidatingEncoder<P>::encodeBytes(const uint8_t *bytes, size_t len) -{ - parser_.advance(Symbol::sBytes); +void ValidatingEncoder<P>::encodeBytes(const uint8_t *bytes, size_t len) { + parser_.advance(Symbol::Kind::Bytes); base_->encodeBytes(bytes, len); } template<typename P> -void ValidatingEncoder<P>::encodeFixed(const uint8_t *bytes, size_t len) -{ - parser_.advance(Symbol::sFixed); +void ValidatingEncoder<P>::encodeFixed(const uint8_t *bytes, size_t len) { + parser_.advance(Symbol::Kind::Fixed); parser_.assertSize(len); base_->encodeFixed(bytes, len); } template<typename P> -void ValidatingEncoder<P>::encodeEnum(size_t e) -{ - parser_.advance(Symbol::sEnum); +void ValidatingEncoder<P>::encodeEnum(size_t e) { + parser_.advance(Symbol::Kind::Enum); parser_.assertLessThanSize(e); base_->encodeEnum(e); } template<typename P> -void ValidatingEncoder<P>::arrayStart() -{ - parser_.advance(Symbol::sArrayStart); +void ValidatingEncoder<P>::arrayStart() { + parser_.advance(Symbol::Kind::ArrayStart); parser_.pushRepeatCount(0); base_->arrayStart(); } template<typename P> -void ValidatingEncoder<P>::arrayEnd() -{ +void ValidatingEncoder<P>::arrayEnd() { parser_.popRepeater(); - parser_.advance(Symbol::sArrayEnd); + parser_.advance(Symbol::Kind::ArrayEnd); base_->arrayEnd(); } template<typename P> -void ValidatingEncoder<P>::mapStart() -{ - parser_.advance(Symbol::sMapStart); +void ValidatingEncoder<P>::mapStart() { + parser_.advance(Symbol::Kind::MapStart); parser_.pushRepeatCount(0); base_->mapStart(); } template<typename P> -void ValidatingEncoder<P>::mapEnd() -{ +void ValidatingEncoder<P>::mapEnd() { parser_.popRepeater(); - parser_.advance(Symbol::sMapEnd); + parser_.advance(Symbol::Kind::MapEnd); base_->mapEnd(); } template<typename P> -void ValidatingEncoder<P>::setItemCount(size_t count) -{ +void ValidatingEncoder<P>::setItemCount(size_t count) { parser_.nextRepeatCount(count); base_->setItemCount(count); } template<typename P> -void ValidatingEncoder<P>::startItem() -{ - if (parser_.top() != Symbol::sRepeater) { +void ValidatingEncoder<P>::startItem() { + if (parser_.top() != Symbol::Kind::Repeater) { throw Exception("startItem at not an item boundary"); } base_->startItem(); } template<typename P> -void ValidatingEncoder<P>::encodeUnionIndex(size_t e) -{ - parser_.advance(Symbol::sUnion); +void ValidatingEncoder<P>::encodeUnionIndex(size_t e) { + parser_.advance(Symbol::Kind::Union); parser_.selectBranch(e); base_->encodeUnionIndex(e); } template<typename P> -int64_t ValidatingEncoder<P>::byteCount() const -{ +int64_t ValidatingEncoder<P>::byteCount() const { return base_->byteCount(); } -} // namespace parsing +} // namespace parsing -DecoderPtr validatingDecoder(const ValidSchema& s, - const DecoderPtr& base) -{ - return make_shared<parsing::ValidatingDecoder<parsing::SimpleParser<parsing::DummyHandler> > >(s, base); +DecoderPtr validatingDecoder(const ValidSchema &s, + const DecoderPtr &base) { + return make_shared<parsing::ValidatingDecoder<parsing::SimpleParser<parsing::DummyHandler>>>(s, base); } -EncoderPtr validatingEncoder(const ValidSchema& schema, const EncoderPtr& base) -{ - return make_shared<parsing::ValidatingEncoder<parsing::SimpleParser<parsing::DummyHandler> > >(schema, base); +EncoderPtr validatingEncoder(const ValidSchema &schema, const EncoderPtr &base) { + return make_shared<parsing::ValidatingEncoder<parsing::SimpleParser<parsing::DummyHandler>>>(schema, base); } -} // namespace avro - +} // namespace avro diff --git a/contrib/libs/apache/avro/impl/parsing/ValidatingCodec.hh b/contrib/libs/apache/avro/impl/parsing/ValidatingCodec.hh index 39ceda033e0..c8b859ead15 100644 --- a/contrib/libs/apache/avro/impl/parsing/ValidatingCodec.hh +++ b/contrib/libs/apache/avro/impl/parsing/ValidatingCodec.hh @@ -22,30 +22,25 @@ #include <map> #include <vector> +#include "NodeImpl.hh" #include "Symbol.hh" #include "ValidSchema.hh" -#include "NodeImpl.hh" namespace avro { namespace parsing { class ValidatingGrammarGenerator { protected: - template<typename T> - static void doFixup(Production& p, const std::map<T, ProductionPtr> &m); + virtual ProductionPtr doGenerate(const NodePtr &n, + std::map<NodePtr, ProductionPtr> &m); - template<typename T> - static void doFixup(Symbol &s, const std::map<T, ProductionPtr> &m); - virtual ProductionPtr doGenerate(const NodePtr& n, - std::map<NodePtr, ProductionPtr> &m); + ProductionPtr generate(const NodePtr &schema); - ProductionPtr generate(const NodePtr& schema); public: - Symbol generate(const ValidSchema& schema); - + Symbol generate(const ValidSchema &schema); }; -} // namespace parsing -} // namespace avro +} // namespace parsing +} // namespace avro #endif |