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

2020-09-09 Thread GitBox


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 
+#include 
+#include 
+#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& 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::value && !std::is_same::value>>
+  int write(Integral value) {
+uint8_t buffer[sizeof(Integral)]{};
+
+for (std::size_t byteIdx = 0; byteIdx < sizeof(Integral); ++byteIdx) {
+  buffer[byteIdx] = static_cast(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 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(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 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(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
-class CRCStream : public BaseStream {
+template
+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&&) noexcept;
-
-  ~CRCStream() override = default;
-
-  T *getstream() const {
+  StreamType *getstream() const {
 return child_stream_;
   }
 
-  void disableEncoding() {
-disable_encoding_ = true;
-  }
-
-  /**
-   * Reads data and places it 

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

2020-08-13 Thread GitBox


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(, 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(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:
us...@infra.apache.org




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

2020-08-13 Thread GitBox


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 
+#include 
+#include 
+#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:
us...@infra.apache.org




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

2020-08-13 Thread GitBox


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 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:
us...@infra.apache.org