You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2020/08/07 10:06:43 UTC

[GitHub] [nifi-minifi-cpp] adamdebreceni opened a new pull request #864: MINIFICPP-1319 - Stream refactor

adamdebreceni opened a new pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864


   Thank you for submitting a contribution to Apache NiFi - MiNiFi C++.
   
   In order to streamline the review of the contribution we ask you
   to ensure the following steps have been taken:
   
   ### For all changes:
   - [ ] Is there a JIRA ticket associated with this PR? Is it referenced
        in the commit message?
   
   - [ ] Does your PR title start with MINIFICPP-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
   
   - [ ] Has your PR been rebased against the latest commit within the target branch (typically main)?
   
   - [ ] Is your initial contribution a single, squashed commit?
   
   ### For code changes:
   - [ ] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)?
   - [ ] If applicable, have you updated the LICENSE file?
   - [ ] If applicable, have you updated the NOTICE file?
   
   ### For documentation related changes:
   - [ ] Have you ensured that format looks appropriate for the output in which it is rendered?
   
   ### Note:
   Please ensure that once the PR is submitted, you check travis-ci for build issues and submit an update to your PR as soon as possible.
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] szaszm commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
szaszm commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r469923156



##########
File path: extensions/coap/tests/CoapC2VerifyHeartbeat.cpp
##########
@@ -132,18 +132,18 @@ class VerifyCoAPServer : public CoapIntegrationBase {
 
     {
       // should result in valid operation
-      minifi::io::BaseStream stream;
+      minifi::io::BufferStream stream;
       uint16_t version = 0, size = 1;
       uint8_t operation = 1;
       stream.write(version);
       stream.write(size);
       stream.write(&operation, 1);
-      stream.writeUTF("id");
-      stream.writeUTF("operand");
+      stream.write("id");
+      stream.write("operand");
 
-      uint8_t *data = new uint8_t[stream.getSize()];
-      memcpy(data, stream.getBuffer(), stream.getSize());
-      minifi::coap::CoapResponse response(205, std::unique_ptr<uint8_t>(data), stream.getSize());
+      uint8_t *data = new uint8_t[stream.size()];

Review comment:
       Our `utils::make_unique` doesn't support arrays yet.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] szaszm commented on pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
szaszm commented on pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#issuecomment-698282021


   I'm waiting for more reviews before merging.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] szaszm commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
szaszm commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r469864618



##########
File path: libminifi/include/io/Stream.h
##########
@@ -0,0 +1,52 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+/**
+ * All streams serialize/deserialize in big-endian
+ */
+class Stream {
+ public:
+  virtual void close() {}
+
+  virtual void seek(uint64_t offset) {
+    throw std::runtime_error("Seek is not supported");
+  }
+
+  virtual int initialize() {
+    return 1;
+  }
+
+  virtual const uint8_t* getBuffer() const {
+    throw std::runtime_error("Not a buffered stream");
+  }
+  virtual ~Stream() = default;
+};

Review comment:
       I don't see the reason for the existence of this base class.
   - close: could be the destructor
   - seek: if it's not implemented, it probably shouldn't be part of the interface
   - initialize: same as seek, if it's not needed, it shouldn't be there
   - getBuffer: same
   - virtual dtor: if this class is gone, it can move to current derived classes
   

##########
File path: libminifi/include/io/ZlibStream.h
##########
@@ -62,12 +61,12 @@ class ZlibBaseStream : public BaseStream {
   ZlibStreamState state_{ZlibStreamState::UNINITIALIZED};
   z_stream strm_{};
   std::vector<uint8_t> outputBuffer_;
+  OutputStream* output_;

Review comment:
       I think inner streams should be `gsl::not_null`




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] szaszm commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
szaszm commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r470076811



##########
File path: libminifi/include/io/OutputStream.h
##########
@@ -0,0 +1,94 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <stdexcept>
+#include <vector>
+#include <string>
+#include "Stream.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+/**
+ * Serializable instances provide base functionality to
+ * write certain objects/primitives to a data stream.
+ *
+ */
+class OutputStream : public virtual Stream {
+ public:
+  /**
+   * write valueto stream
+   * @param value non encoded value
+   * @param len length of value
+   * @return resulting write size
+   **/
+  virtual int write(const uint8_t *value, int len) = 0;
+
+  int write(const std::vector<uint8_t>& buffer, int len);
+
+  /**
+   * write bool to stream
+   * @param value non encoded value
+   * @return resulting write size
+   **/
+  int write(bool value);
+
+  /**
+   * write string to stream
+   * @param str string to write
+   * @return resulting write size
+   **/
+  int write(const std::string& str, bool widen = false);
+
+  /**
+   * write string to stream
+   * @param str string to write
+   * @return resulting write size
+   **/
+  int write(const char* str, bool widen = false);
+
+  /**
+  * writes sizeof(Integral) bytes to the stream
+  * @param value to write
+  * @return resulting write size
+  **/
+  template<typename Integral, typename = std::enable_if<std::is_unsigned<Integral>::value && !std::is_same<Integral, bool>::value>>
+  int write(Integral value) {
+    uint8_t buffer[sizeof(Integral)]{};
+
+    for (std::size_t byteIdx = 0; byteIdx < sizeof(Integral); ++byteIdx) {
+      buffer[byteIdx] = static_cast<uint8_t>(value >> (8*(sizeof(Integral) - 1) - 8*byteIdx));

Review comment:
       This should be a `gsl::narrow_cast` instead of `static_cast`. It's just an alias for `static_cast` to be used to annotate narrowing conversions without checking.

##########
File path: libminifi/test/unit/FileStreamTests.cpp
##########
@@ -115,21 +115,21 @@ TEST_CASE("TestFileBadArgumentNoChange2", "[TestLoader]") {
 
   minifi::io::FileStream stream(path, 0, true);
   std::vector<uint8_t> readBuffer;
-  REQUIRE(stream.readData(readBuffer, stream.getSize()) == stream.getSize());
+  REQUIRE(stream.read(readBuffer, stream.size()) == stream.size());
 
   uint8_t* data = readBuffer.data();
 
   REQUIRE(std::string(reinterpret_cast<char*>(data), readBuffer.size()) == "tempFile");
 
   stream.seek(4);
 
-  stream.write(nullptr, 0);
+  stream.write((const uint8_t*)nullptr, 0);

Review comment:
       Please use a named cast or constructor call to avoid linter issues.

##########
File path: libminifi/test/unit/FileStreamTests.cpp
##########
@@ -153,21 +153,21 @@ TEST_CASE("TestFileBadArgumentNoChange3", "[TestLoader]") {
 
   minifi::io::FileStream stream(path, 0, true);
   std::vector<uint8_t> readBuffer;
-  REQUIRE(stream.readData(readBuffer, stream.getSize()) == stream.getSize());
+  REQUIRE(stream.read(readBuffer, stream.size()) == stream.size());
 
   uint8_t* data = readBuffer.data();
 
   REQUIRE(std::string(reinterpret_cast<char*>(data), readBuffer.size()) == "tempFile");
 
   stream.seek(4);
 
-  stream.write(nullptr, 0);
+  stream.write((const uint8_t*)nullptr, 0);

Review comment:
       Please use a named cast or constructor call to avoid linter issues.

##########
File path: libminifi/include/io/CRCStream.h
##########
@@ -34,307 +35,111 @@
 #endif
 #include "BaseStream.h"
 #include "Exception.h"
-#include "Serializable.h"
 
 namespace org {
 namespace apache {
 namespace nifi {
 namespace minifi {
 namespace io {
+namespace internal {
 
-template<typename T>
-class CRCStream : public BaseStream {
+template<typename StreamType>
+class CRCStreamBase : public virtual Stream {
  public:
-  /**
-   * Raw pointer because the caller guarantees that
-   * it will exceed our lifetime.
-   */
-  explicit CRCStream(T *child_stream);
-  CRCStream(T *child_stream, uint64_t initial_crc);
-
-  CRCStream(CRCStream<T>&&) noexcept;
-
-  ~CRCStream() override = default;
-
-  T *getstream() const {
+  StreamType *getstream() const {
     return child_stream_;
   }
 
-  void disableEncoding() {
-    disable_encoding_ = true;
-  }
-
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(std::vector<uint8_t> &buf, int buflen) override;
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(uint8_t *buf, int buflen) override;
-
-  /**
-   * Write value to the stream using std::vector
-   * @param buf incoming buffer
-   * @param buflen buffer to write
-   */
-  virtual int writeData(std::vector<uint8_t> &buf, int buflen);
-
-  /**
-   * writes value to stream
-   * @param value value to write
-   * @param size size of value
-   */
-  int writeData(uint8_t *value, int size) override;
-
-  using BaseStream::write;
-
-  /**
-   * write 4 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint32_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-  /**
-   * write 2 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint16_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * write 8 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint64_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
+  void close() override { child_stream_->close(); }
 
-  /**
-   * Reads a system word
-   * @param value value to write
-   */
-  int read(uint64_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a uint32_t
-   * @param value value to write
-   */
-  int read(uint32_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a system short
-   * @param value value to write
-   */
-  int read(uint16_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  const size_t getSize() const override { return child_stream_->getSize(); }
-
-  void closeStream() override { child_stream_->closeStream(); }
-
-  short initialize() override { // NOLINT
+  int initialize() override {
     child_stream_->initialize();
     reset();
     return 0;
   }
 
-  void updateCRC(uint8_t *buffer, uint32_t length);
+  void updateCRC(uint8_t *buffer, uint32_t length) {
+    crc_ = crc32(crc_, buffer, length);
+  }
 
   uint64_t getCRC() {
-    return crc_;
+    return gsl::narrow<uint64_t>(crc_);
   }
 
-  void reset();
-
- protected:
-  /**
-   * Creates a vector and returns the vector using the provided
-   * type name.
-   * @param t incoming object
-   * @returns vector.
-   */
-  template<typename K>
-  std::vector<uint8_t> readBuffer(const K& t) {
-    std::vector<uint8_t> buf;
-    readBuffer(buf, t);
-    return buf;
+  void reset() {
+    crc_ = crc32(0L, Z_NULL, 0);
   }
 
-  /**
-   * Populates the vector using the provided type name.
-   * @param buf output buffer
-   * @param t incoming object
-   * @returns number of bytes read.
-   */
-  template<typename K>
-  int readBuffer(std::vector<uint8_t>& buf, const K& t) {
-    buf.resize(sizeof t);
-    return readData(reinterpret_cast<uint8_t*>(buf.data()), sizeof(t));
+ protected:
+  explicit CRCStreamBase(gsl::not_null<StreamType*> child_stream) : child_stream_(child_stream) {}
+  //  this implementation is here to make MSVC happy, declaration would be enough
+  //  as it will never get called (and should not be called)
+  CRCStreamBase() : child_stream_(gsl::make_not_null<StreamType*>(nullptr)) {
+    assert(false);
   }
 
-  uLong crc_;
-  T *child_stream_;
-  bool disable_encoding_;
+  uLong crc_ = 0;
+  gsl::not_null<StreamType*> child_stream_;
 };
 
-template<typename T>
-CRCStream<T>::CRCStream(T *child_stream)
-    : child_stream_(child_stream),
-      disable_encoding_(false) {
-  crc_ = crc32(0L, Z_NULL, 0);
-}
-
-template<typename T>
-CRCStream<T>::CRCStream(T *child_stream, uint64_t initial_crc)
-    : crc_(gsl::narrow<uLong>(initial_crc)),
-      child_stream_(child_stream),
-      disable_encoding_(false) {
-}
-
-template<typename T>
-CRCStream<T>::CRCStream(CRCStream<T> &&move) noexcept
-    : crc_(std::move(move.crc_)),
-      child_stream_(std::move(move.child_stream_)),
-      disable_encoding_(false) {
-}
-
-template<typename T>
-int CRCStream<T>::readData(std::vector<uint8_t> &buf, int buflen) {
-  if (buflen < 0) {
-    throw minifi::Exception{ExceptionType::GENERAL_EXCEPTION, "negative buflen"};
-  }
-
-  if (buf.size() < static_cast<size_t>(buflen))
-    buf.resize(buflen);
-  return readData(buf.data(), buflen);
-}
-
-template<typename T>
-int CRCStream<T>::readData(uint8_t *buf, int buflen) {
-  int ret = child_stream_->read(buf, buflen);
-  if (ret > 0) {
-    crc_ = crc32(crc_, buf, ret);
-  }
-  return ret;
-}
-
-template<typename T>
-int CRCStream<T>::writeData(std::vector<uint8_t> &buf, int buflen) {
-  if (buflen < 0) {
-    throw minifi::Exception{ExceptionType::GENERAL_EXCEPTION, "negative buflen"};
+template<typename StreamType>
+class InputCRCStream : public virtual CRCStreamBase<StreamType>, public InputStream {
+ public:
+  using InputStream::read;
+  using CRCStreamBase<StreamType>::child_stream_;
+  using CRCStreamBase<StreamType>::crc_;

Review comment:
       Why `public`?

##########
File path: libminifi/test/unit/SiteToSiteHelper.h
##########
@@ -19,132 +19,46 @@
 #define LIBMINIFI_TEST_UNIT_SITE2SITE_HELPER_H_
 
 #include <queue>
-#include "io/BaseStream.h"
+#include "io/BufferStream.h"
 #include "io/EndianCheck.h"
 #include "core/Core.h"
 /**
  * Test repository
  */
 class SiteToSiteResponder : public minifi::io::BaseStream {
  private:
-  std::queue<std::string> server_responses_;
+  minifi::io::BufferStream server_responses_;
   std::queue<std::string> client_responses_;
  public:
   SiteToSiteResponder() = default;
   // initialize
-  virtual short initialize() {
+  int initialize() override {
     return 1;
   }
 
-  void push_response(std::string resp) {
-    server_responses_.push(resp);
+  void push_response(const std::string& resp) {
+    server_responses_.write((const uint8_t*)resp.data(), resp.length());

Review comment:
       Avoid C-style casts. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-casts-named https://google.github.io/styleguide/cppguide.html#Casting

##########
File path: libminifi/src/io/InputStream.cpp
##########
@@ -0,0 +1,83 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <cstdio>
+#include <iostream>
+#include <vector>
+#include <string>
+#include <algorithm>
+#include "io/InputStream.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+int InputStream::read(std::vector<uint8_t>& buffer, int len) {
+  if (buffer.size() < len) {
+    buffer.resize(len);
+  }
+  int ret = read(buffer.data(), len);
+  buffer.resize((std::max)(ret, 0));
+  return ret;
+}
+
+int InputStream::read(bool &value) {
+  uint8_t buf = 0;
+
+  if (read(&buf, 1) != 1) {
+    return -1;
+  }
+  value = buf;
+  return 1;
+}
+
+int InputStream::read(std::string &str, bool widen) {

Review comment:
       I know it's not new but I don't understand the purpose of `!widen`. If there is none, can we get rid of it?

##########
File path: libminifi/include/io/CRCStream.h
##########
@@ -34,307 +35,111 @@
 #endif
 #include "BaseStream.h"
 #include "Exception.h"
-#include "Serializable.h"
 
 namespace org {
 namespace apache {
 namespace nifi {
 namespace minifi {
 namespace io {
+namespace internal {
 
-template<typename T>
-class CRCStream : public BaseStream {
+template<typename StreamType>
+class CRCStreamBase : public virtual Stream {
  public:
-  /**
-   * Raw pointer because the caller guarantees that
-   * it will exceed our lifetime.
-   */
-  explicit CRCStream(T *child_stream);
-  CRCStream(T *child_stream, uint64_t initial_crc);
-
-  CRCStream(CRCStream<T>&&) noexcept;
-
-  ~CRCStream() override = default;
-
-  T *getstream() const {
+  StreamType *getstream() const {
     return child_stream_;
   }
 
-  void disableEncoding() {
-    disable_encoding_ = true;
-  }
-
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(std::vector<uint8_t> &buf, int buflen) override;
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(uint8_t *buf, int buflen) override;
-
-  /**
-   * Write value to the stream using std::vector
-   * @param buf incoming buffer
-   * @param buflen buffer to write
-   */
-  virtual int writeData(std::vector<uint8_t> &buf, int buflen);
-
-  /**
-   * writes value to stream
-   * @param value value to write
-   * @param size size of value
-   */
-  int writeData(uint8_t *value, int size) override;
-
-  using BaseStream::write;
-
-  /**
-   * write 4 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint32_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-  /**
-   * write 2 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint16_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * write 8 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint64_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
+  void close() override { child_stream_->close(); }
 
-  /**
-   * Reads a system word
-   * @param value value to write
-   */
-  int read(uint64_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a uint32_t
-   * @param value value to write
-   */
-  int read(uint32_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a system short
-   * @param value value to write
-   */
-  int read(uint16_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  const size_t getSize() const override { return child_stream_->getSize(); }
-
-  void closeStream() override { child_stream_->closeStream(); }
-
-  short initialize() override { // NOLINT
+  int initialize() override {
     child_stream_->initialize();
     reset();
     return 0;
   }
 
-  void updateCRC(uint8_t *buffer, uint32_t length);
+  void updateCRC(uint8_t *buffer, uint32_t length) {
+    crc_ = crc32(crc_, buffer, length);
+  }
 
   uint64_t getCRC() {
-    return crc_;
+    return gsl::narrow<uint64_t>(crc_);
   }
 
-  void reset();
-
- protected:
-  /**
-   * Creates a vector and returns the vector using the provided
-   * type name.
-   * @param t incoming object
-   * @returns vector.
-   */
-  template<typename K>
-  std::vector<uint8_t> readBuffer(const K& t) {
-    std::vector<uint8_t> buf;
-    readBuffer(buf, t);
-    return buf;
+  void reset() {
+    crc_ = crc32(0L, Z_NULL, 0);
   }
 
-  /**
-   * Populates the vector using the provided type name.
-   * @param buf output buffer
-   * @param t incoming object
-   * @returns number of bytes read.
-   */
-  template<typename K>
-  int readBuffer(std::vector<uint8_t>& buf, const K& t) {
-    buf.resize(sizeof t);
-    return readData(reinterpret_cast<uint8_t*>(buf.data()), sizeof(t));
+ protected:
+  explicit CRCStreamBase(gsl::not_null<StreamType*> child_stream) : child_stream_(child_stream) {}
+  //  this implementation is here to make MSVC happy, declaration would be enough
+  //  as it will never get called (and should not be called)
+  CRCStreamBase() : child_stream_(gsl::make_not_null<StreamType*>(nullptr)) {
+    assert(false);

Review comment:
       Why was MSVC unhappy?

##########
File path: libminifi/src/io/BufferStream.cpp
##########
@@ -0,0 +1,52 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <cstdint>
+#include <algorithm>
+#include "io/BufferStream.h"
+#include "utils/StreamUtils.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+int BufferStream::write(const uint8_t *value, int size) {
+  utils::internal::ensureNonNegativeWrite(size);

Review comment:
       Just an idea:
   ```suggestion
     gsl_Expects(size > 0);
   ```
   or create a `PositiveInt` type with an invariant that its value is greater than zero, checked in the constructor, without implicitly underflowing like unsigned int or `size_t`.

##########
File path: libminifi/include/sitetosite/Peer.h
##########
@@ -345,7 +310,7 @@ class SiteToSitePeer : public org::apache::nifi::minifi::io::BaseStream {
    * Move assignment operator.
    */
   SiteToSitePeer& operator=(SiteToSitePeer&& other) {
-    stream_ = std::unique_ptr<org::apache::nifi::minifi::io::DataStream>(other.stream_.release());
+    stream_ = std::unique_ptr<org::apache::nifi::minifi::io::BaseStream>(other.stream_.release());

Review comment:
       ```suggestion
       stream_ = utils::exchange(other.stream_, nullptr);
   ```

##########
File path: libminifi/src/sitetosite/SiteToSiteClient.cpp
##########
@@ -499,8 +499,7 @@ int16_t SiteToSiteClient::send(std::string transactionID, DataPacket *packet, co
       return -1;
     }
 
-    ret = transaction->getStream().writeData(reinterpret_cast<uint8_t *>(const_cast<char*>(packet->payload_.c_str())),
-                                             gsl::narrow<int>(len));
+    ret = transaction->getStream().write(reinterpret_cast<uint8_t *>(const_cast<char*>(packet->payload_.c_str())), len);

Review comment:
       This conversion is still narrowing so I don't see the reason for removing `gsl::narrow`.
   ```suggestion
       ret = transaction->getStream().write(reinterpret_cast<uint8_t *>(const_cast<char*>(packet->payload_.c_str())), gsl::narrow<int>(len));
   ```

##########
File path: libminifi/test/unit/Site2SiteTests.cpp
##########
@@ -94,7 +94,7 @@ TEST_CASE("TestSiteToSiteVerifySend", "[S2S3]") {
   sunny_path_bootstrap(collector);
 
   std::unique_ptr<minifi::sitetosite::SiteToSitePeer> peer = std::unique_ptr<minifi::sitetosite::SiteToSitePeer>(
-      new minifi::sitetosite::SiteToSitePeer(std::unique_ptr<minifi::io::DataStream>(new org::apache::nifi::minifi::io::BaseStream(collector)), "fake_host", 65433, ""));
+      new minifi::sitetosite::SiteToSitePeer(std::unique_ptr<minifi::io::BaseStream>(collector), "fake_host", 65433, ""));

Review comment:
       This looks like a fixed memory leak in the test. :+1: 

##########
File path: libminifi/include/io/CRCStream.h
##########
@@ -34,307 +35,111 @@
 #endif
 #include "BaseStream.h"
 #include "Exception.h"
-#include "Serializable.h"
 
 namespace org {
 namespace apache {
 namespace nifi {
 namespace minifi {
 namespace io {
+namespace internal {
 
-template<typename T>
-class CRCStream : public BaseStream {
+template<typename StreamType>
+class CRCStreamBase : public virtual Stream {
  public:
-  /**
-   * Raw pointer because the caller guarantees that
-   * it will exceed our lifetime.
-   */
-  explicit CRCStream(T *child_stream);
-  CRCStream(T *child_stream, uint64_t initial_crc);
-
-  CRCStream(CRCStream<T>&&) noexcept;
-
-  ~CRCStream() override = default;
-
-  T *getstream() const {
+  StreamType *getstream() const {
     return child_stream_;
   }
 
-  void disableEncoding() {
-    disable_encoding_ = true;
-  }
-
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(std::vector<uint8_t> &buf, int buflen) override;
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(uint8_t *buf, int buflen) override;
-
-  /**
-   * Write value to the stream using std::vector
-   * @param buf incoming buffer
-   * @param buflen buffer to write
-   */
-  virtual int writeData(std::vector<uint8_t> &buf, int buflen);
-
-  /**
-   * writes value to stream
-   * @param value value to write
-   * @param size size of value
-   */
-  int writeData(uint8_t *value, int size) override;
-
-  using BaseStream::write;
-
-  /**
-   * write 4 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint32_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-  /**
-   * write 2 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint16_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * write 8 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint64_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
+  void close() override { child_stream_->close(); }
 
-  /**
-   * Reads a system word
-   * @param value value to write
-   */
-  int read(uint64_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a uint32_t
-   * @param value value to write
-   */
-  int read(uint32_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a system short
-   * @param value value to write
-   */
-  int read(uint16_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  const size_t getSize() const override { return child_stream_->getSize(); }
-
-  void closeStream() override { child_stream_->closeStream(); }
-
-  short initialize() override { // NOLINT
+  int initialize() override {
     child_stream_->initialize();
     reset();
     return 0;
   }
 
-  void updateCRC(uint8_t *buffer, uint32_t length);
+  void updateCRC(uint8_t *buffer, uint32_t length) {
+    crc_ = crc32(crc_, buffer, length);
+  }
 
   uint64_t getCRC() {
-    return crc_;
+    return gsl::narrow<uint64_t>(crc_);
   }
 
-  void reset();
-
- protected:
-  /**
-   * Creates a vector and returns the vector using the provided
-   * type name.
-   * @param t incoming object
-   * @returns vector.
-   */
-  template<typename K>
-  std::vector<uint8_t> readBuffer(const K& t) {
-    std::vector<uint8_t> buf;
-    readBuffer(buf, t);
-    return buf;
+  void reset() {
+    crc_ = crc32(0L, Z_NULL, 0);
   }
 
-  /**
-   * Populates the vector using the provided type name.
-   * @param buf output buffer
-   * @param t incoming object
-   * @returns number of bytes read.
-   */
-  template<typename K>
-  int readBuffer(std::vector<uint8_t>& buf, const K& t) {
-    buf.resize(sizeof t);
-    return readData(reinterpret_cast<uint8_t*>(buf.data()), sizeof(t));
+ protected:
+  explicit CRCStreamBase(gsl::not_null<StreamType*> child_stream) : child_stream_(child_stream) {}
+  //  this implementation is here to make MSVC happy, declaration would be enough
+  //  as it will never get called (and should not be called)
+  CRCStreamBase() : child_stream_(gsl::make_not_null<StreamType*>(nullptr)) {
+    assert(false);
   }
 
-  uLong crc_;
-  T *child_stream_;
-  bool disable_encoding_;
+  uLong crc_ = 0;
+  gsl::not_null<StreamType*> child_stream_;
 };
 
-template<typename T>
-CRCStream<T>::CRCStream(T *child_stream)
-    : child_stream_(child_stream),
-      disable_encoding_(false) {
-  crc_ = crc32(0L, Z_NULL, 0);
-}
-
-template<typename T>
-CRCStream<T>::CRCStream(T *child_stream, uint64_t initial_crc)
-    : crc_(gsl::narrow<uLong>(initial_crc)),
-      child_stream_(child_stream),
-      disable_encoding_(false) {
-}
-
-template<typename T>
-CRCStream<T>::CRCStream(CRCStream<T> &&move) noexcept
-    : crc_(std::move(move.crc_)),
-      child_stream_(std::move(move.child_stream_)),
-      disable_encoding_(false) {
-}
-
-template<typename T>
-int CRCStream<T>::readData(std::vector<uint8_t> &buf, int buflen) {
-  if (buflen < 0) {
-    throw minifi::Exception{ExceptionType::GENERAL_EXCEPTION, "negative buflen"};
-  }
-
-  if (buf.size() < static_cast<size_t>(buflen))
-    buf.resize(buflen);
-  return readData(buf.data(), buflen);
-}
-
-template<typename T>
-int CRCStream<T>::readData(uint8_t *buf, int buflen) {
-  int ret = child_stream_->read(buf, buflen);
-  if (ret > 0) {
-    crc_ = crc32(crc_, buf, ret);
-  }
-  return ret;
-}
-
-template<typename T>
-int CRCStream<T>::writeData(std::vector<uint8_t> &buf, int buflen) {
-  if (buflen < 0) {
-    throw minifi::Exception{ExceptionType::GENERAL_EXCEPTION, "negative buflen"};
+template<typename StreamType>
+class InputCRCStream : public virtual CRCStreamBase<StreamType>, public InputStream {
+ public:
+  using InputStream::read;
+  using CRCStreamBase<StreamType>::child_stream_;
+  using CRCStreamBase<StreamType>::crc_;
+
+  int read(uint8_t *buf, int buflen) override {
+    int ret = child_stream_->read(buf, buflen);
+    if (ret > 0) {
+      crc_ = crc32(crc_, buf, ret);
+    }
+    return ret;
   }
 
-  if (buf.size() < static_cast<size_t>(buflen))
-    buf.resize(buflen);
-  return writeData(buf.data(), buflen);
-}
+  size_t size() const override { return child_stream_->size(); }
+};
 
-template<typename T>
-int CRCStream<T>::writeData(uint8_t *value, int size) {
-  int ret = child_stream_->write(value, size);
-  if (ret > 0) {
-    crc_ = crc32(crc_, value, ret);
+template<typename StreamType>
+class OutputCRCStream : public virtual CRCStreamBase<StreamType>, public OutputStream {
+ public:
+  using OutputStream::write;
+  using CRCStreamBase<StreamType>::child_stream_;
+  using CRCStreamBase<StreamType>::crc_;
+
+  int write(const uint8_t *value, int size) override {
+    int ret = child_stream_->write(value, size);
+    if (ret > 0) {
+      crc_ = crc32(crc_, value, ret);
+    }
+    return ret;
   }
-  return ret;
-}
-template<typename T>
-void CRCStream<T>::reset() {
-  crc_ = crc32(0L, Z_NULL, 0);
-}
-template<typename T>
-void CRCStream<T>::updateCRC(uint8_t *buffer, uint32_t length) {
-  crc_ = crc32(crc_, buffer, length);
-}
-
-template<typename T>
-int CRCStream<T>::write(uint64_t base_value, bool is_little_endian) {
-  if (disable_encoding_)
-    is_little_endian = false;
-  const uint64_t value = is_little_endian == 1 ? byteSwap(base_value) : base_value;
-  uint8_t bytes[sizeof value];
-  std::copy(static_cast<const char*>(static_cast<const void*>(&value)), static_cast<const char*>(static_cast<const void*>(&value)) + sizeof value, bytes);
-  return writeData(bytes, sizeof value);
-}
+};
 
-template<typename T>
-int CRCStream<T>::write(uint32_t base_value, bool is_little_endian) {
-  if (disable_encoding_)
-    is_little_endian = false;
-  const uint32_t value = is_little_endian ? byteSwap(base_value) : base_value;
-  uint8_t bytes[sizeof value];
-  std::copy(static_cast<const char*>(static_cast<const void*>(&value)), static_cast<const char*>(static_cast<const void*>(&value)) + sizeof value, bytes);
-  return writeData(bytes, sizeof value);
-}
+struct empty_class {};
 
-template<typename T>
-int CRCStream<T>::write(uint16_t base_value, bool is_little_endian) {
-  if (disable_encoding_)
-    is_little_endian = false;
-  const uint16_t value = is_little_endian == 1 ? byteSwap(base_value) : base_value;
-  uint8_t bytes[sizeof value];
-  std::copy(static_cast<const char*>(static_cast<const void*>(&value)), static_cast<const char*>(static_cast<const void*>(&value)) + sizeof value, bytes);
-  return writeData(bytes, sizeof value);
-}
+}  // namespace internal
 
-template<typename T>
-int CRCStream<T>::read(uint64_t &value, bool is_little_endian) {
-  if (disable_encoding_)
-    is_little_endian = false;
-  std::vector<uint8_t> buf;
-  auto ret = readBuffer(buf, value);
-  if (ret <= 0) return ret;
+template<typename StreamType>
+class CRCStream : public std::conditional<std::is_base_of<InputStream, StreamType>::value, internal::InputCRCStream<StreamType>, internal::empty_class>::type
+                  , public std::conditional<std::is_base_of<OutputStream, StreamType>::value, internal::OutputCRCStream<StreamType>, internal::empty_class>::type {
+  using internal::CRCStreamBase<StreamType>::child_stream_;
+  using internal::CRCStreamBase<StreamType>::crc_;
 
-  if (is_little_endian) {
-    value = ((uint64_t) buf[0] << 56) | ((uint64_t) (buf[1] & 255) << 48) | ((uint64_t) (buf[2] & 255) << 40) | ((uint64_t) (buf[3] & 255) << 32) | ((uint64_t) (buf[4] & 255) << 24)
-        | ((uint64_t) (buf[5] & 255) << 16) | ((uint64_t) (buf[6] & 255) << 8) | ((uint64_t) (buf[7] & 255) << 0);
-  } else {
-    value = ((uint64_t) buf[0] << 0) | ((uint64_t) (buf[1] & 255) << 8) | ((uint64_t) (buf[2] & 255) << 16) | ((uint64_t) (buf[3] & 255) << 24) | ((uint64_t) (buf[4] & 255) << 32)
-        | ((uint64_t) (buf[5] & 255) << 40) | ((uint64_t) (buf[6] & 255) << 48) | ((uint64_t) (buf[7] & 255) << 56);
+ public:
+  explicit CRCStream(gsl::not_null<StreamType*> child_stream) : internal::CRCStreamBase<StreamType>(child_stream) {
+    crc_ = crc32(0L, Z_NULL, 0);
   }
-  return sizeof(value);
-}
-
-template<typename T>
-int CRCStream<T>::read(uint32_t &value, bool is_little_endian) {
-  if (disable_encoding_)
-    is_little_endian = false;
-  std::vector<uint8_t> buf;
-  auto ret = readBuffer(buf, value);
-  if (ret <= 0) return ret;
 
-  if (is_little_endian) {
-    value = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
-  } else {
-    value = buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
+  CRCStream(gsl::not_null<StreamType*> child_stream, uint64_t initial_crc) : internal::CRCStreamBase<StreamType>(child_stream) {
+    crc_ = gsl::narrow<uLong>(initial_crc);
   }
 
-  return sizeof(value);
-}
-
-template<typename T>
-int CRCStream<T>::read(uint16_t &value, bool is_little_endian) {
-  if (disable_encoding_)
-    is_little_endian = false;
-  std::vector<uint8_t> buf;
-  auto ret = readBuffer(buf, value);
-  if (ret <= 0) return ret;
-
-  if (is_little_endian) {
-    value = (buf[0] << 8) | buf[1];
-  } else {
-    value = buf[0] | buf[1] << 8;
+  CRCStream(CRCStream &&stream) noexcept : internal::CRCStreamBase<StreamType>(std::move(stream.child_stream_)){
+    crc_ = std::move(stream.crc_);

Review comment:
       Shouldn't we reset `stream`? This would also mean that `child_stream_` must be nullable.
   ```suggestion
     CRCStream(CRCStream &&stream) noexcept : internal::CRCStreamBase<StreamType>(utils::exchange(stream.child_stream_, nullptr)){
       crc_ = utils::exchange(stream.crc_, 0);
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r486372290



##########
File path: libminifi/test/unit/FileStreamTests.cpp
##########
@@ -153,21 +153,21 @@ TEST_CASE("TestFileBadArgumentNoChange3", "[TestLoader]") {
 
   minifi::io::FileStream stream(path, 0, true);
   std::vector<uint8_t> readBuffer;
-  REQUIRE(stream.readData(readBuffer, stream.getSize()) == stream.getSize());
+  REQUIRE(stream.read(readBuffer, stream.size()) == stream.size());
 
   uint8_t* data = readBuffer.data();
 
   REQUIRE(std::string(reinterpret_cast<char*>(data), readBuffer.size()) == "tempFile");
 
   stream.seek(4);
 
-  stream.write(nullptr, 0);
+  stream.write((const uint8_t*)nullptr, 0);

Review comment:
       done, I don't remember why the cast was needed at all

##########
File path: libminifi/test/unit/FileStreamTests.cpp
##########
@@ -115,21 +115,21 @@ TEST_CASE("TestFileBadArgumentNoChange2", "[TestLoader]") {
 
   minifi::io::FileStream stream(path, 0, true);
   std::vector<uint8_t> readBuffer;
-  REQUIRE(stream.readData(readBuffer, stream.getSize()) == stream.getSize());
+  REQUIRE(stream.read(readBuffer, stream.size()) == stream.size());
 
   uint8_t* data = readBuffer.data();
 
   REQUIRE(std::string(reinterpret_cast<char*>(data), readBuffer.size()) == "tempFile");
 
   stream.seek(4);
 
-  stream.write(nullptr, 0);
+  stream.write((const uint8_t*)nullptr, 0);

Review comment:
       done




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r486382255



##########
File path: libminifi/src/io/InputStream.cpp
##########
@@ -0,0 +1,83 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <cstdio>
+#include <iostream>
+#include <vector>
+#include <string>
+#include <algorithm>
+#include "io/InputStream.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+int InputStream::read(std::vector<uint8_t>& buffer, int len) {
+  if (buffer.size() < len) {
+    buffer.resize(len);
+  }
+  int ret = read(buffer.data(), len);
+  buffer.resize((std::max)(ret, 0));
+  return ret;
+}
+
+int InputStream::read(bool &value) {
+  uint8_t buf = 0;
+
+  if (read(&buf, 1) != 1) {
+    return -1;
+  }
+  value = buf;
+  return 1;
+}
+
+int InputStream::read(std::string &str, bool widen) {

Review comment:
       it seems like there is actual need for `widen` as some parts call it with `true` while others with `false`, further investigation is necessary but I wouldn't change this in this PR




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r486275648



##########
File path: libminifi/include/io/CRCStream.h
##########
@@ -34,307 +35,111 @@
 #endif
 #include "BaseStream.h"
 #include "Exception.h"
-#include "Serializable.h"
 
 namespace org {
 namespace apache {
 namespace nifi {
 namespace minifi {
 namespace io {
+namespace internal {
 
-template<typename T>
-class CRCStream : public BaseStream {
+template<typename StreamType>
+class CRCStreamBase : public virtual Stream {
  public:
-  /**
-   * Raw pointer because the caller guarantees that
-   * it will exceed our lifetime.
-   */
-  explicit CRCStream(T *child_stream);
-  CRCStream(T *child_stream, uint64_t initial_crc);
-
-  CRCStream(CRCStream<T>&&) noexcept;
-
-  ~CRCStream() override = default;
-
-  T *getstream() const {
+  StreamType *getstream() const {
     return child_stream_;
   }
 
-  void disableEncoding() {
-    disable_encoding_ = true;
-  }
-
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(std::vector<uint8_t> &buf, int buflen) override;
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(uint8_t *buf, int buflen) override;
-
-  /**
-   * Write value to the stream using std::vector
-   * @param buf incoming buffer
-   * @param buflen buffer to write
-   */
-  virtual int writeData(std::vector<uint8_t> &buf, int buflen);
-
-  /**
-   * writes value to stream
-   * @param value value to write
-   * @param size size of value
-   */
-  int writeData(uint8_t *value, int size) override;
-
-  using BaseStream::write;
-
-  /**
-   * write 4 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint32_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-  /**
-   * write 2 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint16_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * write 8 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint64_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
+  void close() override { child_stream_->close(); }
 
-  /**
-   * Reads a system word
-   * @param value value to write
-   */
-  int read(uint64_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a uint32_t
-   * @param value value to write
-   */
-  int read(uint32_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a system short
-   * @param value value to write
-   */
-  int read(uint16_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  const size_t getSize() const override { return child_stream_->getSize(); }
-
-  void closeStream() override { child_stream_->closeStream(); }
-
-  short initialize() override { // NOLINT
+  int initialize() override {
     child_stream_->initialize();
     reset();
     return 0;
   }
 
-  void updateCRC(uint8_t *buffer, uint32_t length);
+  void updateCRC(uint8_t *buffer, uint32_t length) {
+    crc_ = crc32(crc_, buffer, length);
+  }
 
   uint64_t getCRC() {
-    return crc_;
+    return gsl::narrow<uint64_t>(crc_);
   }
 
-  void reset();
-
- protected:
-  /**
-   * Creates a vector and returns the vector using the provided
-   * type name.
-   * @param t incoming object
-   * @returns vector.
-   */
-  template<typename K>
-  std::vector<uint8_t> readBuffer(const K& t) {
-    std::vector<uint8_t> buf;
-    readBuffer(buf, t);
-    return buf;
+  void reset() {
+    crc_ = crc32(0L, Z_NULL, 0);
   }
 
-  /**
-   * Populates the vector using the provided type name.
-   * @param buf output buffer
-   * @param t incoming object
-   * @returns number of bytes read.
-   */
-  template<typename K>
-  int readBuffer(std::vector<uint8_t>& buf, const K& t) {
-    buf.resize(sizeof t);
-    return readData(reinterpret_cast<uint8_t*>(buf.data()), sizeof(t));
+ protected:
+  explicit CRCStreamBase(gsl::not_null<StreamType*> child_stream) : child_stream_(child_stream) {}
+  //  this implementation is here to make MSVC happy, declaration would be enough
+  //  as it will never get called (and should not be called)
+  CRCStreamBase() : child_stream_(gsl::make_not_null<StreamType*>(nullptr)) {
+    assert(false);

Review comment:
       I am not sure what the standard-compliant behavior is here, but gcc and clang both accept the following, whereas msvc complained for the undefined reference `A::A()`
   
   ```
   struct A {
     A();  // is a declaration enough?
     A(int x) : x(x) {}
   
    private:
     int x;
   };
   
   struct B : virtual A {};
   
   struct C : virtual A {};
   
   struct D : B, C {
     // here B::B(), C::C() are called, which would not be possible without A::A()
     D(int x): A(x) {} 
   };
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r470035889



##########
File path: libminifi/include/io/Stream.h
##########
@@ -0,0 +1,52 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+/**
+ * All streams serialize/deserialize in big-endian
+ */
+class Stream {
+ public:
+  virtual void close() {}
+
+  virtual void seek(uint64_t offset) {
+    throw std::runtime_error("Seek is not supported");
+  }
+
+  virtual int initialize() {
+    return 1;
+  }
+
+  virtual const uint8_t* getBuffer() const {
+    throw std::runtime_error("Not a buffered stream");
+  }
+  virtual ~Stream() = default;
+};

Review comment:
       I'm not entirely sure where we should put them, for example `FileOutputCallback::process` takes a `BaseStream` and then calls `getBuffer` (which only makes sense for a `BufferStream`), if we would like to change the method, that it can only take a `BufferStream`, then we are up for a (not necessary infeasible) ride, I moved these methods back and forth between this Stream class and the BaseStream, `ZlibCompressStream` is only an `OutputStream` but it does implement a non-trivial `close` method, that's why they settled in `Stream`, also in the future `content_repo->write(claim)` should only return an `OutputStream`, not a `BaseStream`, and as it stands we do call `seek` on the result
   
   I think we should investigate further with your proposed "granularization" in a separate PR 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r486275648



##########
File path: libminifi/include/io/CRCStream.h
##########
@@ -34,307 +35,111 @@
 #endif
 #include "BaseStream.h"
 #include "Exception.h"
-#include "Serializable.h"
 
 namespace org {
 namespace apache {
 namespace nifi {
 namespace minifi {
 namespace io {
+namespace internal {
 
-template<typename T>
-class CRCStream : public BaseStream {
+template<typename StreamType>
+class CRCStreamBase : public virtual Stream {
  public:
-  /**
-   * Raw pointer because the caller guarantees that
-   * it will exceed our lifetime.
-   */
-  explicit CRCStream(T *child_stream);
-  CRCStream(T *child_stream, uint64_t initial_crc);
-
-  CRCStream(CRCStream<T>&&) noexcept;
-
-  ~CRCStream() override = default;
-
-  T *getstream() const {
+  StreamType *getstream() const {
     return child_stream_;
   }
 
-  void disableEncoding() {
-    disable_encoding_ = true;
-  }
-
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(std::vector<uint8_t> &buf, int buflen) override;
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(uint8_t *buf, int buflen) override;
-
-  /**
-   * Write value to the stream using std::vector
-   * @param buf incoming buffer
-   * @param buflen buffer to write
-   */
-  virtual int writeData(std::vector<uint8_t> &buf, int buflen);
-
-  /**
-   * writes value to stream
-   * @param value value to write
-   * @param size size of value
-   */
-  int writeData(uint8_t *value, int size) override;
-
-  using BaseStream::write;
-
-  /**
-   * write 4 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint32_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-  /**
-   * write 2 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint16_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * write 8 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint64_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
+  void close() override { child_stream_->close(); }
 
-  /**
-   * Reads a system word
-   * @param value value to write
-   */
-  int read(uint64_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a uint32_t
-   * @param value value to write
-   */
-  int read(uint32_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a system short
-   * @param value value to write
-   */
-  int read(uint16_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  const size_t getSize() const override { return child_stream_->getSize(); }
-
-  void closeStream() override { child_stream_->closeStream(); }
-
-  short initialize() override { // NOLINT
+  int initialize() override {
     child_stream_->initialize();
     reset();
     return 0;
   }
 
-  void updateCRC(uint8_t *buffer, uint32_t length);
+  void updateCRC(uint8_t *buffer, uint32_t length) {
+    crc_ = crc32(crc_, buffer, length);
+  }
 
   uint64_t getCRC() {
-    return crc_;
+    return gsl::narrow<uint64_t>(crc_);
   }
 
-  void reset();
-
- protected:
-  /**
-   * Creates a vector and returns the vector using the provided
-   * type name.
-   * @param t incoming object
-   * @returns vector.
-   */
-  template<typename K>
-  std::vector<uint8_t> readBuffer(const K& t) {
-    std::vector<uint8_t> buf;
-    readBuffer(buf, t);
-    return buf;
+  void reset() {
+    crc_ = crc32(0L, Z_NULL, 0);
   }
 
-  /**
-   * Populates the vector using the provided type name.
-   * @param buf output buffer
-   * @param t incoming object
-   * @returns number of bytes read.
-   */
-  template<typename K>
-  int readBuffer(std::vector<uint8_t>& buf, const K& t) {
-    buf.resize(sizeof t);
-    return readData(reinterpret_cast<uint8_t*>(buf.data()), sizeof(t));
+ protected:
+  explicit CRCStreamBase(gsl::not_null<StreamType*> child_stream) : child_stream_(child_stream) {}
+  //  this implementation is here to make MSVC happy, declaration would be enough
+  //  as it will never get called (and should not be called)
+  CRCStreamBase() : child_stream_(gsl::make_not_null<StreamType*>(nullptr)) {
+    assert(false);

Review comment:
       I am not sure what the standard-compliant behavior is here, but gcc and clang both accept the following, whereas msvc complained for the undefined reference `A::A()`
   
   ```
   struct A {
     A();  // is a declaration enough?
     A(int x) : x(x) {}
   
    private:
     int x;
   };
   
   struct B : virtual A {};
   
   struct C : virtual A {};
   
   struct D : B, C {
     D(int x): A(x) {} 
   };
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r486275648



##########
File path: libminifi/include/io/CRCStream.h
##########
@@ -34,307 +35,111 @@
 #endif
 #include "BaseStream.h"
 #include "Exception.h"
-#include "Serializable.h"
 
 namespace org {
 namespace apache {
 namespace nifi {
 namespace minifi {
 namespace io {
+namespace internal {
 
-template<typename T>
-class CRCStream : public BaseStream {
+template<typename StreamType>
+class CRCStreamBase : public virtual Stream {
  public:
-  /**
-   * Raw pointer because the caller guarantees that
-   * it will exceed our lifetime.
-   */
-  explicit CRCStream(T *child_stream);
-  CRCStream(T *child_stream, uint64_t initial_crc);
-
-  CRCStream(CRCStream<T>&&) noexcept;
-
-  ~CRCStream() override = default;
-
-  T *getstream() const {
+  StreamType *getstream() const {
     return child_stream_;
   }
 
-  void disableEncoding() {
-    disable_encoding_ = true;
-  }
-
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(std::vector<uint8_t> &buf, int buflen) override;
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(uint8_t *buf, int buflen) override;
-
-  /**
-   * Write value to the stream using std::vector
-   * @param buf incoming buffer
-   * @param buflen buffer to write
-   */
-  virtual int writeData(std::vector<uint8_t> &buf, int buflen);
-
-  /**
-   * writes value to stream
-   * @param value value to write
-   * @param size size of value
-   */
-  int writeData(uint8_t *value, int size) override;
-
-  using BaseStream::write;
-
-  /**
-   * write 4 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint32_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-  /**
-   * write 2 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint16_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * write 8 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint64_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
+  void close() override { child_stream_->close(); }
 
-  /**
-   * Reads a system word
-   * @param value value to write
-   */
-  int read(uint64_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a uint32_t
-   * @param value value to write
-   */
-  int read(uint32_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a system short
-   * @param value value to write
-   */
-  int read(uint16_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  const size_t getSize() const override { return child_stream_->getSize(); }
-
-  void closeStream() override { child_stream_->closeStream(); }
-
-  short initialize() override { // NOLINT
+  int initialize() override {
     child_stream_->initialize();
     reset();
     return 0;
   }
 
-  void updateCRC(uint8_t *buffer, uint32_t length);
+  void updateCRC(uint8_t *buffer, uint32_t length) {
+    crc_ = crc32(crc_, buffer, length);
+  }
 
   uint64_t getCRC() {
-    return crc_;
+    return gsl::narrow<uint64_t>(crc_);
   }
 
-  void reset();
-
- protected:
-  /**
-   * Creates a vector and returns the vector using the provided
-   * type name.
-   * @param t incoming object
-   * @returns vector.
-   */
-  template<typename K>
-  std::vector<uint8_t> readBuffer(const K& t) {
-    std::vector<uint8_t> buf;
-    readBuffer(buf, t);
-    return buf;
+  void reset() {
+    crc_ = crc32(0L, Z_NULL, 0);
   }
 
-  /**
-   * Populates the vector using the provided type name.
-   * @param buf output buffer
-   * @param t incoming object
-   * @returns number of bytes read.
-   */
-  template<typename K>
-  int readBuffer(std::vector<uint8_t>& buf, const K& t) {
-    buf.resize(sizeof t);
-    return readData(reinterpret_cast<uint8_t*>(buf.data()), sizeof(t));
+ protected:
+  explicit CRCStreamBase(gsl::not_null<StreamType*> child_stream) : child_stream_(child_stream) {}
+  //  this implementation is here to make MSVC happy, declaration would be enough
+  //  as it will never get called (and should not be called)
+  CRCStreamBase() : child_stream_(gsl::make_not_null<StreamType*>(nullptr)) {
+    assert(false);

Review comment:
       I am not sure what is the standard-compliant behavior here, but gcc and clang both accept the following, but msvc complained for the undefined reference `A::A()`
   
   ```
   struct A {
     A();  // is a declaration enough?
     A(int x) : x(x) {}
   
    private:
     int x;
   };
   
   struct B : virtual A {};
   
   struct C : virtual A {};
   
   struct D : B, C {
     D(int x): A(x) {} 
   };
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r469901701



##########
File path: libminifi/test/unit/FileStreamTests.cpp
##########
@@ -235,12 +235,12 @@ TEST_CASE("TestFileExceedSize", "[TestLoader]") {
   std::vector<uint8_t> verifybuffer;
 
   for (int i = 0; i < 10; i++)
-    REQUIRE(stream.readData(verifybuffer, 8192) == 8192);
-  REQUIRE(stream.readData(verifybuffer, 8192) == 0);
+    REQUIRE(stream.read(verifybuffer, 8192) == 8192);

Review comment:
       done




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] szaszm removed a comment on pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
szaszm removed a comment on pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#issuecomment-698282021


   I'm waiting for more reviews before merging.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r486275648



##########
File path: libminifi/include/io/CRCStream.h
##########
@@ -34,307 +35,111 @@
 #endif
 #include "BaseStream.h"
 #include "Exception.h"
-#include "Serializable.h"
 
 namespace org {
 namespace apache {
 namespace nifi {
 namespace minifi {
 namespace io {
+namespace internal {
 
-template<typename T>
-class CRCStream : public BaseStream {
+template<typename StreamType>
+class CRCStreamBase : public virtual Stream {
  public:
-  /**
-   * Raw pointer because the caller guarantees that
-   * it will exceed our lifetime.
-   */
-  explicit CRCStream(T *child_stream);
-  CRCStream(T *child_stream, uint64_t initial_crc);
-
-  CRCStream(CRCStream<T>&&) noexcept;
-
-  ~CRCStream() override = default;
-
-  T *getstream() const {
+  StreamType *getstream() const {
     return child_stream_;
   }
 
-  void disableEncoding() {
-    disable_encoding_ = true;
-  }
-
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(std::vector<uint8_t> &buf, int buflen) override;
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(uint8_t *buf, int buflen) override;
-
-  /**
-   * Write value to the stream using std::vector
-   * @param buf incoming buffer
-   * @param buflen buffer to write
-   */
-  virtual int writeData(std::vector<uint8_t> &buf, int buflen);
-
-  /**
-   * writes value to stream
-   * @param value value to write
-   * @param size size of value
-   */
-  int writeData(uint8_t *value, int size) override;
-
-  using BaseStream::write;
-
-  /**
-   * write 4 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint32_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-  /**
-   * write 2 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint16_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * write 8 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint64_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
+  void close() override { child_stream_->close(); }
 
-  /**
-   * Reads a system word
-   * @param value value to write
-   */
-  int read(uint64_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a uint32_t
-   * @param value value to write
-   */
-  int read(uint32_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a system short
-   * @param value value to write
-   */
-  int read(uint16_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  const size_t getSize() const override { return child_stream_->getSize(); }
-
-  void closeStream() override { child_stream_->closeStream(); }
-
-  short initialize() override { // NOLINT
+  int initialize() override {
     child_stream_->initialize();
     reset();
     return 0;
   }
 
-  void updateCRC(uint8_t *buffer, uint32_t length);
+  void updateCRC(uint8_t *buffer, uint32_t length) {
+    crc_ = crc32(crc_, buffer, length);
+  }
 
   uint64_t getCRC() {
-    return crc_;
+    return gsl::narrow<uint64_t>(crc_);
   }
 
-  void reset();
-
- protected:
-  /**
-   * Creates a vector and returns the vector using the provided
-   * type name.
-   * @param t incoming object
-   * @returns vector.
-   */
-  template<typename K>
-  std::vector<uint8_t> readBuffer(const K& t) {
-    std::vector<uint8_t> buf;
-    readBuffer(buf, t);
-    return buf;
+  void reset() {
+    crc_ = crc32(0L, Z_NULL, 0);
   }
 
-  /**
-   * Populates the vector using the provided type name.
-   * @param buf output buffer
-   * @param t incoming object
-   * @returns number of bytes read.
-   */
-  template<typename K>
-  int readBuffer(std::vector<uint8_t>& buf, const K& t) {
-    buf.resize(sizeof t);
-    return readData(reinterpret_cast<uint8_t*>(buf.data()), sizeof(t));
+ protected:
+  explicit CRCStreamBase(gsl::not_null<StreamType*> child_stream) : child_stream_(child_stream) {}
+  //  this implementation is here to make MSVC happy, declaration would be enough
+  //  as it will never get called (and should not be called)
+  CRCStreamBase() : child_stream_(gsl::make_not_null<StreamType*>(nullptr)) {
+    assert(false);

Review comment:
       I am not sure what the standard-compliant behavior is here, but gcc and clang both accept the following, but msvc complained for the undefined reference `A::A()`
   
   ```
   struct A {
     A();  // is a declaration enough?
     A(int x) : x(x) {}
   
    private:
     int x;
   };
   
   struct B : virtual A {};
   
   struct C : virtual A {};
   
   struct D : B, C {
     D(int x): A(x) {} 
   };
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r470474756



##########
File path: libminifi/include/io/ZlibStream.h
##########
@@ -62,12 +61,12 @@ class ZlibBaseStream : public BaseStream {
   ZlibStreamState state_{ZlibStreamState::UNINITIALIZED};
   z_stream strm_{};
   std::vector<uint8_t> outputBuffer_;
+  OutputStream* output_;

Review comment:
       did it for both the ZlibStream and CRCStream




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r486372528



##########
File path: libminifi/test/unit/SiteToSiteHelper.h
##########
@@ -19,132 +19,46 @@
 #define LIBMINIFI_TEST_UNIT_SITE2SITE_HELPER_H_
 
 #include <queue>
-#include "io/BaseStream.h"
+#include "io/BufferStream.h"
 #include "io/EndianCheck.h"
 #include "core/Core.h"
 /**
  * Test repository
  */
 class SiteToSiteResponder : public minifi::io::BaseStream {
  private:
-  std::queue<std::string> server_responses_;
+  minifi::io::BufferStream server_responses_;
   std::queue<std::string> client_responses_;
  public:
   SiteToSiteResponder() = default;
   // initialize
-  virtual short initialize() {
+  int initialize() override {
     return 1;
   }
 
-  void push_response(std::string resp) {
-    server_responses_.push(resp);
+  void push_response(const std::string& resp) {
+    server_responses_.write((const uint8_t*)resp.data(), resp.length());

Review comment:
       done




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r469903259



##########
File path: libminifi/src/io/BufferStream.cpp
##########
@@ -0,0 +1,56 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <vector>
+#include <cstdint>
+#include <string>
+#include <algorithm>

Review comment:
       done

##########
File path: libminifi/include/io/OutputStream.h
##########
@@ -0,0 +1,107 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <stdexcept>
+#include <vector>
+#include <string>
+#include "Stream.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+/**
+ * Serializable instances provide base functionality to
+ * write certain objects/primitives to a data stream.
+ *
+ */
+class OutputStream : public virtual Stream {
+ public:
+  /**
+   * write valueto stream
+   * @param value non encoded value
+   * @param len length of value
+   * @return resulting write size
+   **/
+  virtual int write(const uint8_t *value, int len) {
+    throw std::runtime_error("Stream is not writable");

Review comment:
       done




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] arpadboda closed pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
arpadboda closed pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] arpadboda commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
arpadboda commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r469787490



##########
File path: libminifi/include/io/OutputStream.h
##########
@@ -0,0 +1,107 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <stdexcept>
+#include <vector>
+#include <string>
+#include "Stream.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+/**
+ * Serializable instances provide base functionality to
+ * write certain objects/primitives to a data stream.
+ *
+ */
+class OutputStream : public virtual Stream {
+ public:
+  /**
+   * write valueto stream
+   * @param value non encoded value
+   * @param len length of value
+   * @return resulting write size
+   **/
+  virtual int write(const uint8_t *value, int len) {
+    throw std::runtime_error("Stream is not writable");

Review comment:
       Wonder if something like "not yet implemented" would be a bit more talkativee. As a human being I would expect an  output stream to be writeable. :)

##########
File path: libminifi/src/io/BufferStream.cpp
##########
@@ -0,0 +1,56 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <vector>
+#include <cstdint>
+#include <string>
+#include <algorithm>

Review comment:
       I suspect that we can remove some of these. 

##########
File path: libminifi/test/unit/FileStreamTests.cpp
##########
@@ -235,12 +235,12 @@ TEST_CASE("TestFileExceedSize", "[TestLoader]") {
   std::vector<uint8_t> verifybuffer;
 
   for (int i = 0; i < 10; i++)
-    REQUIRE(stream.readData(verifybuffer, 8192) == 8192);
-  REQUIRE(stream.readData(verifybuffer, 8192) == 0);
+    REQUIRE(stream.read(verifybuffer, 8192) == 8192);

Review comment:
       I know that this is not new code, but the first require being in the loop is quite misleading here, could you add brackets?

##########
File path: libminifi/src/io/OutputStream.cpp
##########
@@ -0,0 +1,118 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <cstdio>
+#include <cstring>
+#include <iostream>
+#include <vector>
+#include <string>
+#include <algorithm>
+#include "io/OutputStream.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+int OutputStream::write(const std::vector<uint8_t>& buffer, int len) {
+  if (buffer.size() < len) {
+    return -1;
+  }
+  return write(buffer.data(), len);
+}
+
+int OutputStream::write(uint8_t value) {
+  return write(&value, 1);
+}
+
+int OutputStream::write(bool value) {
+  uint8_t temp = value;
+  return write(&temp, 1);
+}
+
+int OutputStream::write(const std::string& str, bool widen) {
+  return write_str(str.c_str(), str.length(), widen);
+}
+
+int OutputStream::write(const char* str, bool widen) {
+  return write_str(str, std::strlen(str), widen);
+}
+
+int OutputStream::write_str(const char* str, uint32_t len, bool widen) {
+  int ret = 0;
+  if (!widen) {
+    uint16_t shortLen = len;
+    if (len != shortLen) {
+      return -1;
+    }
+    ret = write(shortLen);
+  } else {
+    ret = write(len);
+  }
+
+  if (ret <= 0) {
+    return ret;
+  }
+
+  if (len == 0) {
+    return ret;
+  }
+
+  return ret + write(reinterpret_cast<const uint8_t *>(str), len);
+}
+
+int OutputStream::write(uint16_t value) {
+  uint8_t buffer[2]{};
+
+  buffer[0] = value >> 8;
+  buffer[1] = value;
+
+  return write(buffer, 2);
+}
+
+int OutputStream::write(uint32_t value) {
+  uint8_t buffer[4]{};
+
+  buffer[0] = value >> 24;
+  buffer[1] = value >> 16;
+  buffer[2] = value >> 8;
+  buffer[3] = value;
+
+  return write(buffer, 4);
+}
+
+int OutputStream::write(uint64_t value) {
+  uint8_t buffer[8]{};
+
+  buffer[0] = value >> 56;

Review comment:
       Doesn't this generate warnings?
   value being shifted is still an uint64_t

##########
File path: extensions/coap/tests/CoapC2VerifyHeartbeat.cpp
##########
@@ -132,18 +132,18 @@ class VerifyCoAPServer : public CoapIntegrationBase {
 
     {
       // should result in valid operation
-      minifi::io::BaseStream stream;
+      minifi::io::BufferStream stream;
       uint16_t version = 0, size = 1;
       uint8_t operation = 1;
       stream.write(version);
       stream.write(size);
       stream.write(&operation, 1);
-      stream.writeUTF("id");
-      stream.writeUTF("operand");
+      stream.write("id");
+      stream.write("operand");
 
-      uint8_t *data = new uint8_t[stream.getSize()];
-      memcpy(data, stream.getBuffer(), stream.getSize());
-      minifi::coap::CoapResponse response(205, std::unique_ptr<uint8_t>(data), stream.getSize());
+      uint8_t *data = new uint8_t[stream.size()];

Review comment:
       As we touch it: what about make_unique and move?

##########
File path: libminifi/src/io/InputStream.cpp
##########
@@ -0,0 +1,141 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <cstdio>
+#include <iostream>
+#include <vector>
+#include <string>
+#include <algorithm>
+#include "io/InputStream.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+int InputStream::read(std::vector<uint8_t>& buffer, int len) {
+  if (buffer.size() < len) {
+    buffer.resize(len);
+  }
+  int ret = read(buffer.data(), len);
+  buffer.resize((std::max)(ret, 0));
+  return ret;
+}
+
+
+int InputStream::read(uint8_t &value) {
+  uint8_t buf = 0;
+
+  if (read(&buf, 1) != 1) {
+    return -1;
+  }
+  value = buf;
+  return 1;
+}
+
+int InputStream::read(bool &value) {
+  uint8_t buf = 0;
+
+  if (read(&buf, 1) != 1) {
+    return -1;
+  }
+  value = buf;
+  return 1;
+}
+
+int InputStream::read(std::string &str, bool widen) {
+  uint32_t len = 0;
+  int ret = 0;
+  if (!widen) {
+    uint16_t shortLength = 0;
+    ret = read(shortLength);
+    len = shortLength;
+  } else {
+    ret = read(len);
+  }
+
+  if (ret <= 0) {
+    return ret;
+  }
+
+  if (len == 0) {
+    str = "";
+    return ret;
+  }
+
+  std::vector<uint8_t> buffer(len);
+  if (read(buffer.data(), len) != len) {
+    return -1;
+  }
+
+  str = std::string(reinterpret_cast<const char*>(buffer.data()), len);
+  return ret + len;
+}
+
+int InputStream::read(uint64_t &value) {
+  uint8_t buf[8]{};
+  if (read(buf, 8) != 8) {
+    return -1;
+  }
+
+  value = 0;
+  value |= (uint64_t) buf[0] << 56;

Review comment:
       Maybe I need a bit more coffee, but it feels that we can simply sum this, don't need |=
   
   for (int i = 0; i < 8 ; ++i) {
     value += (uint64_t) buf[i] << (56 - 8*i);
   }




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r469902504



##########
File path: libminifi/src/io/OutputStream.cpp
##########
@@ -0,0 +1,118 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <cstdio>
+#include <cstring>
+#include <iostream>
+#include <vector>
+#include <string>
+#include <algorithm>
+#include "io/OutputStream.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+int OutputStream::write(const std::vector<uint8_t>& buffer, int len) {
+  if (buffer.size() < len) {
+    return -1;
+  }
+  return write(buffer.data(), len);
+}
+
+int OutputStream::write(uint8_t value) {
+  return write(&value, 1);
+}
+
+int OutputStream::write(bool value) {
+  uint8_t temp = value;
+  return write(&temp, 1);
+}
+
+int OutputStream::write(const std::string& str, bool widen) {
+  return write_str(str.c_str(), str.length(), widen);
+}
+
+int OutputStream::write(const char* str, bool widen) {
+  return write_str(str, std::strlen(str), widen);
+}
+
+int OutputStream::write_str(const char* str, uint32_t len, bool widen) {
+  int ret = 0;
+  if (!widen) {
+    uint16_t shortLen = len;
+    if (len != shortLen) {
+      return -1;
+    }
+    ret = write(shortLen);
+  } else {
+    ret = write(len);
+  }
+
+  if (ret <= 0) {
+    return ret;
+  }
+
+  if (len == 0) {
+    return ret;
+  }
+
+  return ret + write(reinterpret_cast<const uint8_t *>(str), len);
+}
+
+int OutputStream::write(uint16_t value) {
+  uint8_t buffer[2]{};
+
+  buffer[0] = value >> 8;
+  buffer[1] = value;
+
+  return write(buffer, 2);
+}
+
+int OutputStream::write(uint32_t value) {
+  uint8_t buffer[4]{};
+
+  buffer[0] = value >> 24;
+  buffer[1] = value >> 16;
+  buffer[2] = value >> 8;
+  buffer[3] = value;
+
+  return write(buffer, 4);
+}
+
+int OutputStream::write(uint64_t value) {
+  uint8_t buffer[8]{};
+
+  buffer[0] = value >> 56;

Review comment:
       didn't find the warnings, but added explicit cast




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r469966242



##########
File path: libminifi/include/io/OutputStream.h
##########
@@ -0,0 +1,107 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <stdexcept>
+#include <vector>
+#include <string>
+#include "Stream.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+/**
+ * Serializable instances provide base functionality to
+ * write certain objects/primitives to a data stream.
+ *
+ */
+class OutputStream : public virtual Stream {
+ public:
+  /**
+   * write valueto stream
+   * @param value non encoded value
+   * @param len length of value
+   * @return resulting write size
+   **/
+  virtual int write(const uint8_t *value, int len) {
+    throw std::runtime_error("Stream is not writable");

Review comment:
       made it pure virtual (InputStream::read as well)




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] szaszm removed a comment on pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
szaszm removed a comment on pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#issuecomment-698282021


   I'm waiting for more reviews before merging.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r485646889



##########
File path: libminifi/src/sitetosite/SiteToSiteClient.cpp
##########
@@ -499,8 +499,7 @@ int16_t SiteToSiteClient::send(std::string transactionID, DataPacket *packet, co
       return -1;
     }
 
-    ret = transaction->getStream().writeData(reinterpret_cast<uint8_t *>(const_cast<char*>(packet->payload_.c_str())),
-                                             gsl::narrow<int>(len));
+    ret = transaction->getStream().write(reinterpret_cast<uint8_t *>(const_cast<char*>(packet->payload_.c_str())), len);

Review comment:
       this was possibly caused by an unsuccessful rebase




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r486371426



##########
File path: libminifi/include/sitetosite/Peer.h
##########
@@ -345,7 +310,7 @@ class SiteToSitePeer : public org::apache::nifi::minifi::io::BaseStream {
    * Move assignment operator.
    */
   SiteToSitePeer& operator=(SiteToSitePeer&& other) {
-    stream_ = std::unique_ptr<org::apache::nifi::minifi::io::DataStream>(other.stream_.release());
+    stream_ = std::unique_ptr<org::apache::nifi::minifi::io::BaseStream>(other.stream_.release());

Review comment:
       done

##########
File path: libminifi/include/io/CRCStream.h
##########
@@ -34,307 +35,111 @@
 #endif
 #include "BaseStream.h"
 #include "Exception.h"
-#include "Serializable.h"
 
 namespace org {
 namespace apache {
 namespace nifi {
 namespace minifi {
 namespace io {
+namespace internal {
 
-template<typename T>
-class CRCStream : public BaseStream {
+template<typename StreamType>
+class CRCStreamBase : public virtual Stream {
  public:
-  /**
-   * Raw pointer because the caller guarantees that
-   * it will exceed our lifetime.
-   */
-  explicit CRCStream(T *child_stream);
-  CRCStream(T *child_stream, uint64_t initial_crc);
-
-  CRCStream(CRCStream<T>&&) noexcept;
-
-  ~CRCStream() override = default;
-
-  T *getstream() const {
+  StreamType *getstream() const {
     return child_stream_;
   }
 
-  void disableEncoding() {
-    disable_encoding_ = true;
-  }
-
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(std::vector<uint8_t> &buf, int buflen) override;
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(uint8_t *buf, int buflen) override;
-
-  /**
-   * Write value to the stream using std::vector
-   * @param buf incoming buffer
-   * @param buflen buffer to write
-   */
-  virtual int writeData(std::vector<uint8_t> &buf, int buflen);
-
-  /**
-   * writes value to stream
-   * @param value value to write
-   * @param size size of value
-   */
-  int writeData(uint8_t *value, int size) override;
-
-  using BaseStream::write;
-
-  /**
-   * write 4 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint32_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-  /**
-   * write 2 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint16_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * write 8 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint64_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
+  void close() override { child_stream_->close(); }
 
-  /**
-   * Reads a system word
-   * @param value value to write
-   */
-  int read(uint64_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a uint32_t
-   * @param value value to write
-   */
-  int read(uint32_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a system short
-   * @param value value to write
-   */
-  int read(uint16_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  const size_t getSize() const override { return child_stream_->getSize(); }
-
-  void closeStream() override { child_stream_->closeStream(); }
-
-  short initialize() override { // NOLINT
+  int initialize() override {
     child_stream_->initialize();
     reset();
     return 0;
   }
 
-  void updateCRC(uint8_t *buffer, uint32_t length);
+  void updateCRC(uint8_t *buffer, uint32_t length) {
+    crc_ = crc32(crc_, buffer, length);
+  }
 
   uint64_t getCRC() {
-    return crc_;
+    return gsl::narrow<uint64_t>(crc_);
   }
 
-  void reset();
-
- protected:
-  /**
-   * Creates a vector and returns the vector using the provided
-   * type name.
-   * @param t incoming object
-   * @returns vector.
-   */
-  template<typename K>
-  std::vector<uint8_t> readBuffer(const K& t) {
-    std::vector<uint8_t> buf;
-    readBuffer(buf, t);
-    return buf;
+  void reset() {
+    crc_ = crc32(0L, Z_NULL, 0);
   }
 
-  /**
-   * Populates the vector using the provided type name.
-   * @param buf output buffer
-   * @param t incoming object
-   * @returns number of bytes read.
-   */
-  template<typename K>
-  int readBuffer(std::vector<uint8_t>& buf, const K& t) {
-    buf.resize(sizeof t);
-    return readData(reinterpret_cast<uint8_t*>(buf.data()), sizeof(t));
+ protected:
+  explicit CRCStreamBase(gsl::not_null<StreamType*> child_stream) : child_stream_(child_stream) {}
+  //  this implementation is here to make MSVC happy, declaration would be enough
+  //  as it will never get called (and should not be called)
+  CRCStreamBase() : child_stream_(gsl::make_not_null<StreamType*>(nullptr)) {
+    assert(false);
   }
 
-  uLong crc_;
-  T *child_stream_;
-  bool disable_encoding_;
+  uLong crc_ = 0;
+  gsl::not_null<StreamType*> child_stream_;
 };
 
-template<typename T>
-CRCStream<T>::CRCStream(T *child_stream)
-    : child_stream_(child_stream),
-      disable_encoding_(false) {
-  crc_ = crc32(0L, Z_NULL, 0);
-}
-
-template<typename T>
-CRCStream<T>::CRCStream(T *child_stream, uint64_t initial_crc)
-    : crc_(gsl::narrow<uLong>(initial_crc)),
-      child_stream_(child_stream),
-      disable_encoding_(false) {
-}
-
-template<typename T>
-CRCStream<T>::CRCStream(CRCStream<T> &&move) noexcept
-    : crc_(std::move(move.crc_)),
-      child_stream_(std::move(move.child_stream_)),
-      disable_encoding_(false) {
-}
-
-template<typename T>
-int CRCStream<T>::readData(std::vector<uint8_t> &buf, int buflen) {
-  if (buflen < 0) {
-    throw minifi::Exception{ExceptionType::GENERAL_EXCEPTION, "negative buflen"};
-  }
-
-  if (buf.size() < static_cast<size_t>(buflen))
-    buf.resize(buflen);
-  return readData(buf.data(), buflen);
-}
-
-template<typename T>
-int CRCStream<T>::readData(uint8_t *buf, int buflen) {
-  int ret = child_stream_->read(buf, buflen);
-  if (ret > 0) {
-    crc_ = crc32(crc_, buf, ret);
-  }
-  return ret;
-}
-
-template<typename T>
-int CRCStream<T>::writeData(std::vector<uint8_t> &buf, int buflen) {
-  if (buflen < 0) {
-    throw minifi::Exception{ExceptionType::GENERAL_EXCEPTION, "negative buflen"};
+template<typename StreamType>
+class InputCRCStream : public virtual CRCStreamBase<StreamType>, public InputStream {
+ public:
+  using InputStream::read;
+  using CRCStreamBase<StreamType>::child_stream_;
+  using CRCStreamBase<StreamType>::crc_;
+
+  int read(uint8_t *buf, int buflen) override {
+    int ret = child_stream_->read(buf, buflen);
+    if (ret > 0) {
+      crc_ = crc32(crc_, buf, ret);
+    }
+    return ret;
   }
 
-  if (buf.size() < static_cast<size_t>(buflen))
-    buf.resize(buflen);
-  return writeData(buf.data(), buflen);
-}
+  size_t size() const override { return child_stream_->size(); }
+};
 
-template<typename T>
-int CRCStream<T>::writeData(uint8_t *value, int size) {
-  int ret = child_stream_->write(value, size);
-  if (ret > 0) {
-    crc_ = crc32(crc_, value, ret);
+template<typename StreamType>
+class OutputCRCStream : public virtual CRCStreamBase<StreamType>, public OutputStream {
+ public:
+  using OutputStream::write;
+  using CRCStreamBase<StreamType>::child_stream_;
+  using CRCStreamBase<StreamType>::crc_;
+
+  int write(const uint8_t *value, int size) override {
+    int ret = child_stream_->write(value, size);
+    if (ret > 0) {
+      crc_ = crc32(crc_, value, ret);
+    }
+    return ret;
   }
-  return ret;
-}
-template<typename T>
-void CRCStream<T>::reset() {
-  crc_ = crc32(0L, Z_NULL, 0);
-}
-template<typename T>
-void CRCStream<T>::updateCRC(uint8_t *buffer, uint32_t length) {
-  crc_ = crc32(crc_, buffer, length);
-}
-
-template<typename T>
-int CRCStream<T>::write(uint64_t base_value, bool is_little_endian) {
-  if (disable_encoding_)
-    is_little_endian = false;
-  const uint64_t value = is_little_endian == 1 ? byteSwap(base_value) : base_value;
-  uint8_t bytes[sizeof value];
-  std::copy(static_cast<const char*>(static_cast<const void*>(&value)), static_cast<const char*>(static_cast<const void*>(&value)) + sizeof value, bytes);
-  return writeData(bytes, sizeof value);
-}
+};
 
-template<typename T>
-int CRCStream<T>::write(uint32_t base_value, bool is_little_endian) {
-  if (disable_encoding_)
-    is_little_endian = false;
-  const uint32_t value = is_little_endian ? byteSwap(base_value) : base_value;
-  uint8_t bytes[sizeof value];
-  std::copy(static_cast<const char*>(static_cast<const void*>(&value)), static_cast<const char*>(static_cast<const void*>(&value)) + sizeof value, bytes);
-  return writeData(bytes, sizeof value);
-}
+struct empty_class {};
 
-template<typename T>
-int CRCStream<T>::write(uint16_t base_value, bool is_little_endian) {
-  if (disable_encoding_)
-    is_little_endian = false;
-  const uint16_t value = is_little_endian == 1 ? byteSwap(base_value) : base_value;
-  uint8_t bytes[sizeof value];
-  std::copy(static_cast<const char*>(static_cast<const void*>(&value)), static_cast<const char*>(static_cast<const void*>(&value)) + sizeof value, bytes);
-  return writeData(bytes, sizeof value);
-}
+}  // namespace internal
 
-template<typename T>
-int CRCStream<T>::read(uint64_t &value, bool is_little_endian) {
-  if (disable_encoding_)
-    is_little_endian = false;
-  std::vector<uint8_t> buf;
-  auto ret = readBuffer(buf, value);
-  if (ret <= 0) return ret;
+template<typename StreamType>
+class CRCStream : public std::conditional<std::is_base_of<InputStream, StreamType>::value, internal::InputCRCStream<StreamType>, internal::empty_class>::type
+                  , public std::conditional<std::is_base_of<OutputStream, StreamType>::value, internal::OutputCRCStream<StreamType>, internal::empty_class>::type {
+  using internal::CRCStreamBase<StreamType>::child_stream_;
+  using internal::CRCStreamBase<StreamType>::crc_;
 
-  if (is_little_endian) {
-    value = ((uint64_t) buf[0] << 56) | ((uint64_t) (buf[1] & 255) << 48) | ((uint64_t) (buf[2] & 255) << 40) | ((uint64_t) (buf[3] & 255) << 32) | ((uint64_t) (buf[4] & 255) << 24)
-        | ((uint64_t) (buf[5] & 255) << 16) | ((uint64_t) (buf[6] & 255) << 8) | ((uint64_t) (buf[7] & 255) << 0);
-  } else {
-    value = ((uint64_t) buf[0] << 0) | ((uint64_t) (buf[1] & 255) << 8) | ((uint64_t) (buf[2] & 255) << 16) | ((uint64_t) (buf[3] & 255) << 24) | ((uint64_t) (buf[4] & 255) << 32)
-        | ((uint64_t) (buf[5] & 255) << 40) | ((uint64_t) (buf[6] & 255) << 48) | ((uint64_t) (buf[7] & 255) << 56);
+ public:
+  explicit CRCStream(gsl::not_null<StreamType*> child_stream) : internal::CRCStreamBase<StreamType>(child_stream) {
+    crc_ = crc32(0L, Z_NULL, 0);
   }
-  return sizeof(value);
-}
-
-template<typename T>
-int CRCStream<T>::read(uint32_t &value, bool is_little_endian) {
-  if (disable_encoding_)
-    is_little_endian = false;
-  std::vector<uint8_t> buf;
-  auto ret = readBuffer(buf, value);
-  if (ret <= 0) return ret;
 
-  if (is_little_endian) {
-    value = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
-  } else {
-    value = buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
+  CRCStream(gsl::not_null<StreamType*> child_stream, uint64_t initial_crc) : internal::CRCStreamBase<StreamType>(child_stream) {
+    crc_ = gsl::narrow<uLong>(initial_crc);
   }
 
-  return sizeof(value);
-}
-
-template<typename T>
-int CRCStream<T>::read(uint16_t &value, bool is_little_endian) {
-  if (disable_encoding_)
-    is_little_endian = false;
-  std::vector<uint8_t> buf;
-  auto ret = readBuffer(buf, value);
-  if (ret <= 0) return ret;
-
-  if (is_little_endian) {
-    value = (buf[0] << 8) | buf[1];
-  } else {
-    value = buf[0] | buf[1] << 8;
+  CRCStream(CRCStream &&stream) noexcept : internal::CRCStreamBase<StreamType>(std::move(stream.child_stream_)){
+    crc_ = std::move(stream.crc_);

Review comment:
       done




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r469902822



##########
File path: libminifi/src/io/InputStream.cpp
##########
@@ -0,0 +1,141 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <cstdio>
+#include <iostream>
+#include <vector>
+#include <string>
+#include <algorithm>
+#include "io/InputStream.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+int InputStream::read(std::vector<uint8_t>& buffer, int len) {
+  if (buffer.size() < len) {
+    buffer.resize(len);
+  }
+  int ret = read(buffer.data(), len);
+  buffer.resize((std::max)(ret, 0));
+  return ret;
+}
+
+
+int InputStream::read(uint8_t &value) {
+  uint8_t buf = 0;
+
+  if (read(&buf, 1) != 1) {
+    return -1;
+  }
+  value = buf;
+  return 1;
+}
+
+int InputStream::read(bool &value) {
+  uint8_t buf = 0;
+
+  if (read(&buf, 1) != 1) {
+    return -1;
+  }
+  value = buf;
+  return 1;
+}
+
+int InputStream::read(std::string &str, bool widen) {
+  uint32_t len = 0;
+  int ret = 0;
+  if (!widen) {
+    uint16_t shortLength = 0;
+    ret = read(shortLength);
+    len = shortLength;
+  } else {
+    ret = read(len);
+  }
+
+  if (ret <= 0) {
+    return ret;
+  }
+
+  if (len == 0) {
+    str = "";
+    return ret;
+  }
+
+  std::vector<uint8_t> buffer(len);
+  if (read(buffer.data(), len) != len) {
+    return -1;
+  }
+
+  str = std::string(reinterpret_cast<const char*>(buffer.data()), len);
+  return ret + len;
+}
+
+int InputStream::read(uint64_t &value) {
+  uint8_t buf[8]{};
+  if (read(buf, 8) != 8) {
+    return -1;
+  }
+
+  value = 0;
+  value |= (uint64_t) buf[0] << 56;

Review comment:
       done, (and put back the previous template for reading/writing unsigned integrals)




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r470474756



##########
File path: libminifi/include/io/ZlibStream.h
##########
@@ -62,12 +61,12 @@ class ZlibBaseStream : public BaseStream {
   ZlibStreamState state_{ZlibStreamState::UNINITIALIZED};
   z_stream strm_{};
   std::vector<uint8_t> outputBuffer_;
+  OutputStream* output_;

Review comment:
       did it both for the ZlibStream and CRCStream




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r486373948



##########
File path: libminifi/include/io/CRCStream.h
##########
@@ -34,307 +35,111 @@
 #endif
 #include "BaseStream.h"
 #include "Exception.h"
-#include "Serializable.h"
 
 namespace org {
 namespace apache {
 namespace nifi {
 namespace minifi {
 namespace io {
+namespace internal {
 
-template<typename T>
-class CRCStream : public BaseStream {
+template<typename StreamType>
+class CRCStreamBase : public virtual Stream {
  public:
-  /**
-   * Raw pointer because the caller guarantees that
-   * it will exceed our lifetime.
-   */
-  explicit CRCStream(T *child_stream);
-  CRCStream(T *child_stream, uint64_t initial_crc);
-
-  CRCStream(CRCStream<T>&&) noexcept;
-
-  ~CRCStream() override = default;
-
-  T *getstream() const {
+  StreamType *getstream() const {
     return child_stream_;
   }
 
-  void disableEncoding() {
-    disable_encoding_ = true;
-  }
-
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(std::vector<uint8_t> &buf, int buflen) override;
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(uint8_t *buf, int buflen) override;
-
-  /**
-   * Write value to the stream using std::vector
-   * @param buf incoming buffer
-   * @param buflen buffer to write
-   */
-  virtual int writeData(std::vector<uint8_t> &buf, int buflen);
-
-  /**
-   * writes value to stream
-   * @param value value to write
-   * @param size size of value
-   */
-  int writeData(uint8_t *value, int size) override;
-
-  using BaseStream::write;
-
-  /**
-   * write 4 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint32_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-  /**
-   * write 2 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint16_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * write 8 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint64_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
+  void close() override { child_stream_->close(); }
 
-  /**
-   * Reads a system word
-   * @param value value to write
-   */
-  int read(uint64_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a uint32_t
-   * @param value value to write
-   */
-  int read(uint32_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a system short
-   * @param value value to write
-   */
-  int read(uint16_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  const size_t getSize() const override { return child_stream_->getSize(); }
-
-  void closeStream() override { child_stream_->closeStream(); }
-
-  short initialize() override { // NOLINT
+  int initialize() override {
     child_stream_->initialize();
     reset();
     return 0;
   }
 
-  void updateCRC(uint8_t *buffer, uint32_t length);
+  void updateCRC(uint8_t *buffer, uint32_t length) {
+    crc_ = crc32(crc_, buffer, length);
+  }
 
   uint64_t getCRC() {
-    return crc_;
+    return gsl::narrow<uint64_t>(crc_);
   }
 
-  void reset();
-
- protected:
-  /**
-   * Creates a vector and returns the vector using the provided
-   * type name.
-   * @param t incoming object
-   * @returns vector.
-   */
-  template<typename K>
-  std::vector<uint8_t> readBuffer(const K& t) {
-    std::vector<uint8_t> buf;
-    readBuffer(buf, t);
-    return buf;
+  void reset() {
+    crc_ = crc32(0L, Z_NULL, 0);
   }
 
-  /**
-   * Populates the vector using the provided type name.
-   * @param buf output buffer
-   * @param t incoming object
-   * @returns number of bytes read.
-   */
-  template<typename K>
-  int readBuffer(std::vector<uint8_t>& buf, const K& t) {
-    buf.resize(sizeof t);
-    return readData(reinterpret_cast<uint8_t*>(buf.data()), sizeof(t));
+ protected:
+  explicit CRCStreamBase(gsl::not_null<StreamType*> child_stream) : child_stream_(child_stream) {}
+  //  this implementation is here to make MSVC happy, declaration would be enough
+  //  as it will never get called (and should not be called)
+  CRCStreamBase() : child_stream_(gsl::make_not_null<StreamType*>(nullptr)) {
+    assert(false);

Review comment:
       in light of recent changes (i.e. making the field nullable) this whole questionable stuff is unnecessary, and has been removed




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] szaszm commented on pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
szaszm commented on pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#issuecomment-698282021


   I'm waiting for more reviews before merging.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r469928037



##########
File path: extensions/coap/tests/CoapC2VerifyHeartbeat.cpp
##########
@@ -132,18 +132,18 @@ class VerifyCoAPServer : public CoapIntegrationBase {
 
     {
       // should result in valid operation
-      minifi::io::BaseStream stream;
+      minifi::io::BufferStream stream;
       uint16_t version = 0, size = 1;
       uint8_t operation = 1;
       stream.write(version);
       stream.write(size);
       stream.write(&operation, 1);
-      stream.writeUTF("id");
-      stream.writeUTF("operand");
+      stream.write("id");
+      stream.write("operand");
 
-      uint8_t *data = new uint8_t[stream.getSize()];
-      memcpy(data, stream.getBuffer(), stream.getSize());
-      minifi::coap::CoapResponse response(205, std::unique_ptr<uint8_t>(data), stream.getSize());
+      uint8_t *data = new uint8_t[stream.size()];

Review comment:
       yep it does not, but `unique_ptr` and move works, just taking an array as `std::unique_ptr<uint8_t>` is... not great, so I will look into it




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r486371593



##########
File path: libminifi/include/io/CRCStream.h
##########
@@ -34,307 +35,111 @@
 #endif
 #include "BaseStream.h"
 #include "Exception.h"
-#include "Serializable.h"
 
 namespace org {
 namespace apache {
 namespace nifi {
 namespace minifi {
 namespace io {
+namespace internal {
 
-template<typename T>
-class CRCStream : public BaseStream {
+template<typename StreamType>
+class CRCStreamBase : public virtual Stream {
  public:
-  /**
-   * Raw pointer because the caller guarantees that
-   * it will exceed our lifetime.
-   */
-  explicit CRCStream(T *child_stream);
-  CRCStream(T *child_stream, uint64_t initial_crc);
-
-  CRCStream(CRCStream<T>&&) noexcept;
-
-  ~CRCStream() override = default;
-
-  T *getstream() const {
+  StreamType *getstream() const {
     return child_stream_;
   }
 
-  void disableEncoding() {
-    disable_encoding_ = true;
-  }
-
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(std::vector<uint8_t> &buf, int buflen) override;
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(uint8_t *buf, int buflen) override;
-
-  /**
-   * Write value to the stream using std::vector
-   * @param buf incoming buffer
-   * @param buflen buffer to write
-   */
-  virtual int writeData(std::vector<uint8_t> &buf, int buflen);
-
-  /**
-   * writes value to stream
-   * @param value value to write
-   * @param size size of value
-   */
-  int writeData(uint8_t *value, int size) override;
-
-  using BaseStream::write;
-
-  /**
-   * write 4 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint32_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-  /**
-   * write 2 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint16_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * write 8 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint64_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
+  void close() override { child_stream_->close(); }
 
-  /**
-   * Reads a system word
-   * @param value value to write
-   */
-  int read(uint64_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a uint32_t
-   * @param value value to write
-   */
-  int read(uint32_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a system short
-   * @param value value to write
-   */
-  int read(uint16_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  const size_t getSize() const override { return child_stream_->getSize(); }
-
-  void closeStream() override { child_stream_->closeStream(); }
-
-  short initialize() override { // NOLINT
+  int initialize() override {
     child_stream_->initialize();
     reset();
     return 0;
   }
 
-  void updateCRC(uint8_t *buffer, uint32_t length);
+  void updateCRC(uint8_t *buffer, uint32_t length) {
+    crc_ = crc32(crc_, buffer, length);
+  }
 
   uint64_t getCRC() {
-    return crc_;
+    return gsl::narrow<uint64_t>(crc_);
   }
 
-  void reset();
-
- protected:
-  /**
-   * Creates a vector and returns the vector using the provided
-   * type name.
-   * @param t incoming object
-   * @returns vector.
-   */
-  template<typename K>
-  std::vector<uint8_t> readBuffer(const K& t) {
-    std::vector<uint8_t> buf;
-    readBuffer(buf, t);
-    return buf;
+  void reset() {
+    crc_ = crc32(0L, Z_NULL, 0);
   }
 
-  /**
-   * Populates the vector using the provided type name.
-   * @param buf output buffer
-   * @param t incoming object
-   * @returns number of bytes read.
-   */
-  template<typename K>
-  int readBuffer(std::vector<uint8_t>& buf, const K& t) {
-    buf.resize(sizeof t);
-    return readData(reinterpret_cast<uint8_t*>(buf.data()), sizeof(t));
+ protected:
+  explicit CRCStreamBase(gsl::not_null<StreamType*> child_stream) : child_stream_(child_stream) {}
+  //  this implementation is here to make MSVC happy, declaration would be enough
+  //  as it will never get called (and should not be called)
+  CRCStreamBase() : child_stream_(gsl::make_not_null<StreamType*>(nullptr)) {
+    assert(false);
   }
 
-  uLong crc_;
-  T *child_stream_;
-  bool disable_encoding_;
+  uLong crc_ = 0;
+  gsl::not_null<StreamType*> child_stream_;
 };
 
-template<typename T>
-CRCStream<T>::CRCStream(T *child_stream)
-    : child_stream_(child_stream),
-      disable_encoding_(false) {
-  crc_ = crc32(0L, Z_NULL, 0);
-}
-
-template<typename T>
-CRCStream<T>::CRCStream(T *child_stream, uint64_t initial_crc)
-    : crc_(gsl::narrow<uLong>(initial_crc)),
-      child_stream_(child_stream),
-      disable_encoding_(false) {
-}
-
-template<typename T>
-CRCStream<T>::CRCStream(CRCStream<T> &&move) noexcept
-    : crc_(std::move(move.crc_)),
-      child_stream_(std::move(move.child_stream_)),
-      disable_encoding_(false) {
-}
-
-template<typename T>
-int CRCStream<T>::readData(std::vector<uint8_t> &buf, int buflen) {
-  if (buflen < 0) {
-    throw minifi::Exception{ExceptionType::GENERAL_EXCEPTION, "negative buflen"};
-  }
-
-  if (buf.size() < static_cast<size_t>(buflen))
-    buf.resize(buflen);
-  return readData(buf.data(), buflen);
-}
-
-template<typename T>
-int CRCStream<T>::readData(uint8_t *buf, int buflen) {
-  int ret = child_stream_->read(buf, buflen);
-  if (ret > 0) {
-    crc_ = crc32(crc_, buf, ret);
-  }
-  return ret;
-}
-
-template<typename T>
-int CRCStream<T>::writeData(std::vector<uint8_t> &buf, int buflen) {
-  if (buflen < 0) {
-    throw minifi::Exception{ExceptionType::GENERAL_EXCEPTION, "negative buflen"};
+template<typename StreamType>
+class InputCRCStream : public virtual CRCStreamBase<StreamType>, public InputStream {
+ public:
+  using InputStream::read;
+  using CRCStreamBase<StreamType>::child_stream_;
+  using CRCStreamBase<StreamType>::crc_;
+
+  int read(uint8_t *buf, int buflen) override {
+    int ret = child_stream_->read(buf, buflen);
+    if (ret > 0) {
+      crc_ = crc32(crc_, buf, ret);
+    }
+    return ret;
   }
 
-  if (buf.size() < static_cast<size_t>(buflen))
-    buf.resize(buflen);
-  return writeData(buf.data(), buflen);
-}
+  size_t size() const override { return child_stream_->size(); }
+};
 
-template<typename T>
-int CRCStream<T>::writeData(uint8_t *value, int size) {
-  int ret = child_stream_->write(value, size);
-  if (ret > 0) {
-    crc_ = crc32(crc_, value, ret);
+template<typename StreamType>
+class OutputCRCStream : public virtual CRCStreamBase<StreamType>, public OutputStream {
+ public:
+  using OutputStream::write;
+  using CRCStreamBase<StreamType>::child_stream_;
+  using CRCStreamBase<StreamType>::crc_;
+
+  int write(const uint8_t *value, int size) override {
+    int ret = child_stream_->write(value, size);
+    if (ret > 0) {
+      crc_ = crc32(crc_, value, ret);
+    }
+    return ret;
   }
-  return ret;
-}
-template<typename T>
-void CRCStream<T>::reset() {
-  crc_ = crc32(0L, Z_NULL, 0);
-}
-template<typename T>
-void CRCStream<T>::updateCRC(uint8_t *buffer, uint32_t length) {
-  crc_ = crc32(crc_, buffer, length);
-}
-
-template<typename T>
-int CRCStream<T>::write(uint64_t base_value, bool is_little_endian) {
-  if (disable_encoding_)
-    is_little_endian = false;
-  const uint64_t value = is_little_endian == 1 ? byteSwap(base_value) : base_value;
-  uint8_t bytes[sizeof value];
-  std::copy(static_cast<const char*>(static_cast<const void*>(&value)), static_cast<const char*>(static_cast<const void*>(&value)) + sizeof value, bytes);
-  return writeData(bytes, sizeof value);
-}
+};
 
-template<typename T>
-int CRCStream<T>::write(uint32_t base_value, bool is_little_endian) {
-  if (disable_encoding_)
-    is_little_endian = false;
-  const uint32_t value = is_little_endian ? byteSwap(base_value) : base_value;
-  uint8_t bytes[sizeof value];
-  std::copy(static_cast<const char*>(static_cast<const void*>(&value)), static_cast<const char*>(static_cast<const void*>(&value)) + sizeof value, bytes);
-  return writeData(bytes, sizeof value);
-}
+struct empty_class {};
 
-template<typename T>
-int CRCStream<T>::write(uint16_t base_value, bool is_little_endian) {
-  if (disable_encoding_)
-    is_little_endian = false;
-  const uint16_t value = is_little_endian == 1 ? byteSwap(base_value) : base_value;
-  uint8_t bytes[sizeof value];
-  std::copy(static_cast<const char*>(static_cast<const void*>(&value)), static_cast<const char*>(static_cast<const void*>(&value)) + sizeof value, bytes);
-  return writeData(bytes, sizeof value);
-}
+}  // namespace internal
 
-template<typename T>
-int CRCStream<T>::read(uint64_t &value, bool is_little_endian) {
-  if (disable_encoding_)
-    is_little_endian = false;
-  std::vector<uint8_t> buf;
-  auto ret = readBuffer(buf, value);
-  if (ret <= 0) return ret;
+template<typename StreamType>
+class CRCStream : public std::conditional<std::is_base_of<InputStream, StreamType>::value, internal::InputCRCStream<StreamType>, internal::empty_class>::type
+                  , public std::conditional<std::is_base_of<OutputStream, StreamType>::value, internal::OutputCRCStream<StreamType>, internal::empty_class>::type {
+  using internal::CRCStreamBase<StreamType>::child_stream_;
+  using internal::CRCStreamBase<StreamType>::crc_;
 
-  if (is_little_endian) {
-    value = ((uint64_t) buf[0] << 56) | ((uint64_t) (buf[1] & 255) << 48) | ((uint64_t) (buf[2] & 255) << 40) | ((uint64_t) (buf[3] & 255) << 32) | ((uint64_t) (buf[4] & 255) << 24)
-        | ((uint64_t) (buf[5] & 255) << 16) | ((uint64_t) (buf[6] & 255) << 8) | ((uint64_t) (buf[7] & 255) << 0);
-  } else {
-    value = ((uint64_t) buf[0] << 0) | ((uint64_t) (buf[1] & 255) << 8) | ((uint64_t) (buf[2] & 255) << 16) | ((uint64_t) (buf[3] & 255) << 24) | ((uint64_t) (buf[4] & 255) << 32)
-        | ((uint64_t) (buf[5] & 255) << 40) | ((uint64_t) (buf[6] & 255) << 48) | ((uint64_t) (buf[7] & 255) << 56);
+ public:
+  explicit CRCStream(gsl::not_null<StreamType*> child_stream) : internal::CRCStreamBase<StreamType>(child_stream) {
+    crc_ = crc32(0L, Z_NULL, 0);
   }
-  return sizeof(value);
-}
-
-template<typename T>
-int CRCStream<T>::read(uint32_t &value, bool is_little_endian) {
-  if (disable_encoding_)
-    is_little_endian = false;
-  std::vector<uint8_t> buf;
-  auto ret = readBuffer(buf, value);
-  if (ret <= 0) return ret;
 
-  if (is_little_endian) {
-    value = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
-  } else {
-    value = buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
+  CRCStream(gsl::not_null<StreamType*> child_stream, uint64_t initial_crc) : internal::CRCStreamBase<StreamType>(child_stream) {
+    crc_ = gsl::narrow<uLong>(initial_crc);
   }
 
-  return sizeof(value);
-}
-
-template<typename T>
-int CRCStream<T>::read(uint16_t &value, bool is_little_endian) {
-  if (disable_encoding_)
-    is_little_endian = false;
-  std::vector<uint8_t> buf;
-  auto ret = readBuffer(buf, value);
-  if (ret <= 0) return ret;
-
-  if (is_little_endian) {
-    value = (buf[0] << 8) | buf[1];
-  } else {
-    value = buf[0] | buf[1] << 8;
+  CRCStream(CRCStream &&stream) noexcept : internal::CRCStreamBase<StreamType>(std::move(stream.child_stream_)){
+    crc_ = std::move(stream.crc_);

Review comment:
       done (we now enforce non-nullability in the constructor only)

##########
File path: libminifi/include/io/CRCStream.h
##########
@@ -34,307 +35,111 @@
 #endif
 #include "BaseStream.h"
 #include "Exception.h"
-#include "Serializable.h"
 
 namespace org {
 namespace apache {
 namespace nifi {
 namespace minifi {
 namespace io {
+namespace internal {
 
-template<typename T>
-class CRCStream : public BaseStream {
+template<typename StreamType>
+class CRCStreamBase : public virtual Stream {
  public:
-  /**
-   * Raw pointer because the caller guarantees that
-   * it will exceed our lifetime.
-   */
-  explicit CRCStream(T *child_stream);
-  CRCStream(T *child_stream, uint64_t initial_crc);
-
-  CRCStream(CRCStream<T>&&) noexcept;
-
-  ~CRCStream() override = default;
-
-  T *getstream() const {
+  StreamType *getstream() const {
     return child_stream_;
   }
 
-  void disableEncoding() {
-    disable_encoding_ = true;
-  }
-
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(std::vector<uint8_t> &buf, int buflen) override;
-  /**
-   * Reads data and places it into buf
-   * @param buf buffer in which we extract data
-   * @param buflen
-   */
-  int readData(uint8_t *buf, int buflen) override;
-
-  /**
-   * Write value to the stream using std::vector
-   * @param buf incoming buffer
-   * @param buflen buffer to write
-   */
-  virtual int writeData(std::vector<uint8_t> &buf, int buflen);
-
-  /**
-   * writes value to stream
-   * @param value value to write
-   * @param size size of value
-   */
-  int writeData(uint8_t *value, int size) override;
-
-  using BaseStream::write;
-
-  /**
-   * write 4 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint32_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-  /**
-   * write 2 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint16_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * write 8 bytes to stream
-   * @param base_value non encoded value
-   * @param stream output stream
-   * @param is_little_endian endianness determination
-   * @return resulting write size
-   **/
-  int write(uint64_t base_value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
+  void close() override { child_stream_->close(); }
 
-  /**
-   * Reads a system word
-   * @param value value to write
-   */
-  int read(uint64_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a uint32_t
-   * @param value value to write
-   */
-  int read(uint32_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  /**
-   * Reads a system short
-   * @param value value to write
-   */
-  int read(uint16_t &value, bool is_little_endian = EndiannessCheck::IS_LITTLE) override;
-
-  const size_t getSize() const override { return child_stream_->getSize(); }
-
-  void closeStream() override { child_stream_->closeStream(); }
-
-  short initialize() override { // NOLINT
+  int initialize() override {
     child_stream_->initialize();
     reset();
     return 0;
   }
 
-  void updateCRC(uint8_t *buffer, uint32_t length);
+  void updateCRC(uint8_t *buffer, uint32_t length) {
+    crc_ = crc32(crc_, buffer, length);
+  }
 
   uint64_t getCRC() {
-    return crc_;
+    return gsl::narrow<uint64_t>(crc_);
   }
 
-  void reset();
-
- protected:
-  /**
-   * Creates a vector and returns the vector using the provided
-   * type name.
-   * @param t incoming object
-   * @returns vector.
-   */
-  template<typename K>
-  std::vector<uint8_t> readBuffer(const K& t) {
-    std::vector<uint8_t> buf;
-    readBuffer(buf, t);
-    return buf;
+  void reset() {
+    crc_ = crc32(0L, Z_NULL, 0);
   }
 
-  /**
-   * Populates the vector using the provided type name.
-   * @param buf output buffer
-   * @param t incoming object
-   * @returns number of bytes read.
-   */
-  template<typename K>
-  int readBuffer(std::vector<uint8_t>& buf, const K& t) {
-    buf.resize(sizeof t);
-    return readData(reinterpret_cast<uint8_t*>(buf.data()), sizeof(t));
+ protected:
+  explicit CRCStreamBase(gsl::not_null<StreamType*> child_stream) : child_stream_(child_stream) {}
+  //  this implementation is here to make MSVC happy, declaration would be enough
+  //  as it will never get called (and should not be called)
+  CRCStreamBase() : child_stream_(gsl::make_not_null<StreamType*>(nullptr)) {
+    assert(false);
   }
 
-  uLong crc_;
-  T *child_stream_;
-  bool disable_encoding_;
+  uLong crc_ = 0;
+  gsl::not_null<StreamType*> child_stream_;
 };
 
-template<typename T>
-CRCStream<T>::CRCStream(T *child_stream)
-    : child_stream_(child_stream),
-      disable_encoding_(false) {
-  crc_ = crc32(0L, Z_NULL, 0);
-}
-
-template<typename T>
-CRCStream<T>::CRCStream(T *child_stream, uint64_t initial_crc)
-    : crc_(gsl::narrow<uLong>(initial_crc)),
-      child_stream_(child_stream),
-      disable_encoding_(false) {
-}
-
-template<typename T>
-CRCStream<T>::CRCStream(CRCStream<T> &&move) noexcept
-    : crc_(std::move(move.crc_)),
-      child_stream_(std::move(move.child_stream_)),
-      disable_encoding_(false) {
-}
-
-template<typename T>
-int CRCStream<T>::readData(std::vector<uint8_t> &buf, int buflen) {
-  if (buflen < 0) {
-    throw minifi::Exception{ExceptionType::GENERAL_EXCEPTION, "negative buflen"};
-  }
-
-  if (buf.size() < static_cast<size_t>(buflen))
-    buf.resize(buflen);
-  return readData(buf.data(), buflen);
-}
-
-template<typename T>
-int CRCStream<T>::readData(uint8_t *buf, int buflen) {
-  int ret = child_stream_->read(buf, buflen);
-  if (ret > 0) {
-    crc_ = crc32(crc_, buf, ret);
-  }
-  return ret;
-}
-
-template<typename T>
-int CRCStream<T>::writeData(std::vector<uint8_t> &buf, int buflen) {
-  if (buflen < 0) {
-    throw minifi::Exception{ExceptionType::GENERAL_EXCEPTION, "negative buflen"};
+template<typename StreamType>
+class InputCRCStream : public virtual CRCStreamBase<StreamType>, public InputStream {
+ public:
+  using InputStream::read;
+  using CRCStreamBase<StreamType>::child_stream_;
+  using CRCStreamBase<StreamType>::crc_;

Review comment:
       done




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r486277619



##########
File path: libminifi/src/io/BufferStream.cpp
##########
@@ -0,0 +1,52 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <cstdint>
+#include <algorithm>
+#include "io/BufferStream.h"
+#include "utils/StreamUtils.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+int BufferStream::write(const uint8_t *value, int size) {
+  utils::internal::ensureNonNegativeWrite(size);

Review comment:
       switched to `gsl_Expects` everywhere




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] szaszm commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
szaszm commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r469916009



##########
File path: libminifi/include/io/OutputStream.h
##########
@@ -0,0 +1,107 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <stdexcept>
+#include <vector>
+#include <string>
+#include "Stream.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+/**
+ * Serializable instances provide base functionality to
+ * write certain objects/primitives to a data stream.
+ *
+ */
+class OutputStream : public virtual Stream {
+ public:
+  /**
+   * write valueto stream
+   * @param value non encoded value
+   * @param len length of value
+   * @return resulting write size
+   **/
+  virtual int write(const uint8_t *value, int len) {
+    throw std::runtime_error("Stream is not writable");

Review comment:
       I'd prefer a pure virtual function so that an OutputStream must be writable.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] adamdebreceni commented on a change in pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
adamdebreceni commented on a change in pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864#discussion_r486277381



##########
File path: libminifi/include/io/OutputStream.h
##########
@@ -0,0 +1,94 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <stdexcept>
+#include <vector>
+#include <string>
+#include "Stream.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace io {
+
+/**
+ * Serializable instances provide base functionality to
+ * write certain objects/primitives to a data stream.
+ *
+ */
+class OutputStream : public virtual Stream {
+ public:
+  /**
+   * write valueto stream
+   * @param value non encoded value
+   * @param len length of value
+   * @return resulting write size
+   **/
+  virtual int write(const uint8_t *value, int len) = 0;
+
+  int write(const std::vector<uint8_t>& buffer, int len);
+
+  /**
+   * write bool to stream
+   * @param value non encoded value
+   * @return resulting write size
+   **/
+  int write(bool value);
+
+  /**
+   * write string to stream
+   * @param str string to write
+   * @return resulting write size
+   **/
+  int write(const std::string& str, bool widen = false);
+
+  /**
+   * write string to stream
+   * @param str string to write
+   * @return resulting write size
+   **/
+  int write(const char* str, bool widen = false);
+
+  /**
+  * writes sizeof(Integral) bytes to the stream
+  * @param value to write
+  * @return resulting write size
+  **/
+  template<typename Integral, typename = std::enable_if<std::is_unsigned<Integral>::value && !std::is_same<Integral, bool>::value>>
+  int write(Integral value) {
+    uint8_t buffer[sizeof(Integral)]{};
+
+    for (std::size_t byteIdx = 0; byteIdx < sizeof(Integral); ++byteIdx) {
+      buffer[byteIdx] = static_cast<uint8_t>(value >> (8*(sizeof(Integral) - 1) - 8*byteIdx));

Review comment:
       done




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [nifi-minifi-cpp] arpadboda closed pull request #864: MINIFICPP-1319 - Stream refactor

Posted by GitBox <gi...@apache.org>.
arpadboda closed pull request #864:
URL: https://github.com/apache/nifi-minifi-cpp/pull/864


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org