You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by al...@apache.org on 2018/11/19 15:37:51 UTC

nifi-minifi-cpp git commit: MINIFICPP-679: Make minor changes for const correctness improvements

Repository: nifi-minifi-cpp
Updated Branches:
  refs/heads/master 5dffa37d5 -> 3d2372f41


MINIFICPP-679: Make minor changes for const correctness improvements

This closes #442.

Signed-off-by: Aldrin Piri <al...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/commit/3d2372f4
Tree: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/tree/3d2372f4
Diff: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/diff/3d2372f4

Branch: refs/heads/master
Commit: 3d2372f41f995ca611bc8d878367acc6ce25487e
Parents: 5dffa37
Author: Marc Parisi <ph...@apache.org>
Authored: Thu Nov 15 17:10:42 2018 -0500
Committer: Aldrin Piri <al...@apache.org>
Committed: Mon Nov 19 10:25:04 2018 -0500

----------------------------------------------------------------------
 libminifi/include/core/Connectable.h            | 33 ++++++++-------
 libminifi/include/core/Core.h                   | 42 +++++---------------
 libminifi/include/core/ProcessorNode.h          |  2 +-
 .../include/core/state/nodes/FlowInformation.h  |  8 +---
 libminifi/include/utils/Id.h                    | 31 ++++++++++-----
 libminifi/src/core/Connectable.cpp              | 19 +++++----
 libminifi/src/core/Core.cpp                     | 12 ++----
 libminifi/src/utils/Id.cpp                      | 20 ++++++----
 libminifi/test/unit/IdTests.cpp                 | 18 ++++++++-
 9 files changed, 90 insertions(+), 95 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/3d2372f4/libminifi/include/core/Connectable.h
----------------------------------------------------------------------
diff --git a/libminifi/include/core/Connectable.h b/libminifi/include/core/Connectable.h
index b1a5613..c43a742 100644
--- a/libminifi/include/core/Connectable.h
+++ b/libminifi/include/core/Connectable.h
@@ -40,17 +40,16 @@ namespace core {
 class Connectable : public CoreComponent {
  public:
 
+  explicit Connectable(const std::string &name);
 
-  explicit Connectable(std::string name);
-
-  explicit Connectable(std::string name, utils::Identifier &uuid);
+  explicit Connectable(const std::string &name, const utils::Identifier &uuid);
 
   explicit Connectable(const Connectable &&other);
 
-  bool setSupportedRelationships(std::set<Relationship> relationships);
+  bool setSupportedRelationships(const std::set<Relationship> &relationships);
 
   // Whether the relationship is supported
-  bool isSupportedRelationship(Relationship relationship);
+  bool isSupportedRelationship(const Relationship &relationship) const;
 
   std::vector<Relationship> getSupportedRelationships() const;
 
@@ -59,13 +58,13 @@ class Connectable : public CoreComponent {
    * @param relationships
    * @return result of set operation.
    */
-  bool setAutoTerminatedRelationships(std::set<Relationship> relationships);
+  bool setAutoTerminatedRelationships(const std::set<Relationship> &relationships);
 
   // Check whether the relationship is auto terminated
-  bool isAutoTerminated(Relationship relationship);
+  bool isAutoTerminated(const Relationship &relationship) const;
 
   // Get Processor penalization period in MilliSecond
-  uint64_t getPenalizationPeriodMsec(void) {
+  uint64_t getPenalizationPeriodMsec(void) const {
     return (_penalizationPeriodMsec);
   }
 
@@ -73,14 +72,14 @@ class Connectable : public CoreComponent {
    * Get outgoing connection based on relationship
    * @return set of outgoing connections.
    */
-  std::set<std::shared_ptr<Connectable>> getOutGoingConnections(std::string relationship);
+  std::set<std::shared_ptr<Connectable>> getOutGoingConnections(const std::string &relationship) const;
 
   void put(std::shared_ptr<Connectable> flow) {
 
   }
 
   /**
-   * Get next incoming connection
+   * Gets and sets next incoming connection
    * @return next incoming connection
    */
   std::shared_ptr<Connectable> getNextIncomingConnection();
@@ -88,11 +87,11 @@ class Connectable : public CoreComponent {
   /**
    * @return true if incoming connections > 0
    */
-  bool hasIncomingConnections() {
+  bool hasIncomingConnections() const {
     return (_incomingConnections.size() > 0);
   }
 
-  uint8_t getMaxConcurrentTasks() {
+  uint8_t getMaxConcurrentTasks() const {
     return max_concurrent_tasks_;
   }
 
@@ -141,7 +140,7 @@ class Connectable : public CoreComponent {
   /**
    * Sets the flow version for this connectable.
    */
-  void setFlowIdentifier(const std::shared_ptr<state::FlowIdentifier> &version){
+  void setFlowIdentifier(const std::shared_ptr<state::FlowIdentifier> &version) {
     connectable_version_ = version;
   }
 
@@ -149,7 +148,7 @@ class Connectable : public CoreComponent {
    * Returns theflow version
    * @returns flow version. can be null if a flow version is not tracked.
    */
-  virtual std::shared_ptr<state::FlowIdentifier> getFlowIdentifier(){
+  virtual std::shared_ptr<state::FlowIdentifier> getFlowIdentifier() const{
     return connectable_version_;
   }
 
@@ -170,10 +169,10 @@ class Connectable : public CoreComponent {
   // Incoming connections
   std::set<std::shared_ptr<Connectable>> _incomingConnections;
   // Outgoing connections map based on Relationship name
-  std::map<std::string, std::set<std::shared_ptr<Connectable>>>out_going_connections_;
+  std::map<std::string, std::set<std::shared_ptr<Connectable>>> out_going_connections_;
 
   // Mutex for protection
-  std::mutex relationship_mutex_;
+  mutable std::mutex relationship_mutex_;
 
   ///// work conditionals and locking mechanisms
 
@@ -188,7 +187,7 @@ class Connectable : public CoreComponent {
   // version under which this connectable was created.
   std::shared_ptr<state::FlowIdentifier> connectable_version_;
 
-private:
+ private:
   std::shared_ptr<logging::Logger> logger_;
 };
 

http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/3d2372f4/libminifi/include/core/Core.h
----------------------------------------------------------------------
diff --git a/libminifi/include/core/Core.h b/libminifi/include/core/Core.h
index c8d7b9a..aaf5bc7 100644
--- a/libminifi/include/core/Core.h
+++ b/libminifi/include/core/Core.h
@@ -24,7 +24,6 @@
 #include <string>
 #include <uuid/uuid.h>
 
-
 #ifdef WIN32
 #pragma comment(lib, "shlwapi.lib")
 #endif
@@ -142,7 +141,7 @@ class CoreComponent {
    * Constructor that sets the name and uuid.
    */
 
-  explicit CoreComponent(const std::string name, utils::Identifier uuid)
+  explicit CoreComponent(const std::string &name, utils::Identifier uuid)
       : name_(name) {
     if (uuid == nullptr) {
       // Generate the global UUID for the flow record
@@ -150,44 +149,22 @@ class CoreComponent {
     } else {
       uuid_ = uuid;
     }
-
     uuidStr_ = uuid_.to_string();
   }
 
-  explicit CoreComponent(const std::string name)
+  explicit CoreComponent(const std::string &name)
       : name_(name) {
-
     // Generate the global UUID for the flow record
     id_generator_->generate(uuid_);
-
     uuidStr_ = uuid_.to_string();
   }
-  /*
-   #ifdef WIN32
-   #else
-
-   explicit CoreComponent(const std::string name, Identifier uuid = nullptr)
-   : name_(name) {
-   if (nullptr == uuid)
-   // Generate the global UUID for the flow record
-   id_generator_->generate(uuid_);
-   else
-   uuid_copy(uuid_, uuid);
-
-   char uuidStr[37] = { 0 };
-   uuid_unparse_lower(uuid_, uuidStr);
-   uuidStr_ = uuidStr;
-   }
-   #endif
-   */
+
+  explicit CoreComponent(const CoreComponent &other) = default;
   /**
    * Move Constructor.
    */
-  explicit CoreComponent(const CoreComponent &&other)
-      : name_(std::move(other.name_)) {
-    uuid_ = other.uuid_;
-    //uuid_copy(uuid_, other.uuid_);
-  }
+
+  explicit CoreComponent(CoreComponent &&other) = default;
 
   virtual ~CoreComponent() {
 
@@ -200,7 +177,7 @@ class CoreComponent {
    * Set name.
    * @param name
    */
-  void setName(const std::string name);
+  void setName(const std::string &name);
 
   /**
    * Set UUID in this instance
@@ -208,15 +185,14 @@ class CoreComponent {
    */
   void setUUID(utils::Identifier &uuid);
 
-  void setUUIDStr(const std::string uuidStr);
+  void setUUIDStr(const std::string &uuidStr);
 
   /**
    * Returns the UUID through the provided object.
    * @param uuid uuid struct to which we will copy the memory
    * @return success of request
    */
-  //bool getUUID(m_uuid uuid);
-  bool getUUID(utils::Identifier &uuid);
+  bool getUUID(utils::Identifier &uuid) const;
 
   //unsigned const char *getUUID();
   /**

http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/3d2372f4/libminifi/include/core/ProcessorNode.h
----------------------------------------------------------------------
diff --git a/libminifi/include/core/ProcessorNode.h b/libminifi/include/core/ProcessorNode.h
index f96899b..22acea5 100644
--- a/libminifi/include/core/ProcessorNode.h
+++ b/libminifi/include/core/ProcessorNode.h
@@ -102,7 +102,7 @@ class ProcessorNode : public ConfigurableComponent, public Connectable {
    * Returns theflow version
    * @returns flow version. can be null if a flow version is not tracked.
    */
-  virtual std::shared_ptr<state::FlowIdentifier> getFlowIdentifier() {
+  virtual std::shared_ptr<state::FlowIdentifier> getFlowIdentifier() const{
     if (processor_ != nullptr) {
       return processor_->getFlowIdentifier();
     } else {

http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/3d2372f4/libminifi/include/core/state/nodes/FlowInformation.h
----------------------------------------------------------------------
diff --git a/libminifi/include/core/state/nodes/FlowInformation.h b/libminifi/include/core/state/nodes/FlowInformation.h
index 4560ed3..b7c2768 100644
--- a/libminifi/include/core/state/nodes/FlowInformation.h
+++ b/libminifi/include/core/state/nodes/FlowInformation.h
@@ -64,7 +64,7 @@ class FlowVersion : public DeviceInformation {
     return "FlowVersion";
   }
 
-  virtual std::shared_ptr<state::FlowIdentifier> getFlowIdentifier() {
+  virtual std::shared_ptr<state::FlowIdentifier> getFlowIdentifier() const {
     std::lock_guard<std::mutex> lock(guard);
     return identifier;
   }
@@ -120,12 +120,8 @@ class FlowVersion : public DeviceInformation {
   }
  protected:
 
-  std::mutex guard;
+  mutable std::mutex guard;
 
-  /*std::string registry_url_;
-   std::string bucket_id_;
-   std::string flow_id_;
-   */
   std::shared_ptr<FlowIdentifier> identifier;
 };
 

http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/3d2372f4/libminifi/include/utils/Id.h
----------------------------------------------------------------------
diff --git a/libminifi/include/utils/Id.h b/libminifi/include/utils/Id.h
index 46537be..9c1ee75 100644
--- a/libminifi/include/utils/Id.h
+++ b/libminifi/include/utils/Id.h
@@ -50,6 +50,11 @@ class IdentifierBase {
     copyInto(other.id_);
   }
 
+  IdentifierBase(IdentifierBase &&other)
+      : converted_(std::move(other.converted_)) {
+    copyInto(other.id_);
+  }
+
   IdentifierBase() {
   }
 
@@ -73,7 +78,7 @@ class IdentifierBase {
 
  protected:
 
-  void copyInto(const IdentifierBase &other){
+  void copyInto(const IdentifierBase &other) {
     memcpy(id_, other.id_, sizeof(T));
   }
 
@@ -95,23 +100,29 @@ class Identifier : public IdentifierBase<UUID_FIELD, std::string> {
   Identifier(UUID_FIELD u);
   Identifier();
   Identifier(const Identifier &other);
-  Identifier(const IdentifierBase &other);
+  Identifier(Identifier &&other);
 
+  /**
+   * I believe these exist to make windows builds happy -- need more testing
+   * to ensure this doesn't cause any issues.
+   */
+  Identifier(const IdentifierBase &other);
   Identifier &operator=(const IdentifierBase &other);
+
   Identifier &operator=(const Identifier &other);
   Identifier &operator=(UUID_FIELD o);
 
   Identifier &operator=(std::string id);
-  bool operator==(std::nullptr_t nullp);
+  bool operator==(const std::nullptr_t nullp) const;
 
-  bool operator!=(std::nullptr_t nullp);
+  bool operator!=(std::nullptr_t nullp) const;
 
-  bool operator!=(const Identifier &other);
-  bool operator==(const Identifier &other);
+  bool operator!=(const Identifier &other) const;
+  bool operator==(const Identifier &other) const;
 
-  std::string to_string();
+  std::string to_string() const;
 
-  unsigned char *toArray();
+  const unsigned char * const toArray() const;
 
  protected:
 
@@ -130,8 +141,8 @@ class IdGenerator {
     return generator;
   }
  protected:
-  uint64_t getDeviceSegmentFromString(const std::string & str, int numBits);
-  uint64_t getRandomDeviceSegment(int numBits);
+  uint64_t getDeviceSegmentFromString(const std::string & str, int numBits) const;
+  uint64_t getRandomDeviceSegment(int numBits) const;
  private:
   IdGenerator();
   int implementation_;

http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/3d2372f4/libminifi/src/core/Connectable.cpp
----------------------------------------------------------------------
diff --git a/libminifi/src/core/Connectable.cpp b/libminifi/src/core/Connectable.cpp
index a5fb1b2..3f94697 100644
--- a/libminifi/src/core/Connectable.cpp
+++ b/libminifi/src/core/Connectable.cpp
@@ -30,21 +30,20 @@ namespace nifi {
 namespace minifi {
 namespace core {
 
-Connectable::Connectable(std::string name, utils::Identifier &uuid)
+Connectable::Connectable(const std::string &name, const utils::Identifier &uuid)
     : CoreComponent(name, uuid),
       max_concurrent_tasks_(1),
       connectable_version_(nullptr),
       logger_(logging::LoggerFactory<Connectable>::getLogger()) {
 }
 
-Connectable::Connectable(std::string name)
+Connectable::Connectable(const std::string &name)
     : CoreComponent(name),
       max_concurrent_tasks_(1),
       connectable_version_(nullptr),
       logger_(logging::LoggerFactory<Connectable>::getLogger()) {
 }
 
-
 Connectable::Connectable(const Connectable &&other)
     : CoreComponent(std::move(other)),
       max_concurrent_tasks_(std::move(other.max_concurrent_tasks_)),
@@ -57,7 +56,7 @@ Connectable::Connectable(const Connectable &&other)
 Connectable::~Connectable() {
 }
 
-bool Connectable::setSupportedRelationships(std::set<core::Relationship> relationships) {
+bool Connectable::setSupportedRelationships(const std::set<core::Relationship> &relationships) {
   if (isRunning()) {
     logger_->log_warn("Can not set processor supported relationship while the process %s is running", name_);
     return false;
@@ -82,7 +81,7 @@ std::vector<Relationship> Connectable::getSupportedRelationships() const {
 }
 
 // Whether the relationship is supported
-bool Connectable::isSupportedRelationship(core::Relationship relationship) {
+bool Connectable::isSupportedRelationship(const core::Relationship &relationship) const {
   // if we are running we do not need a lock since the function to change relationships_ ( setSupportedRelationships)
   // cannot be executed while we are running
   const bool isConnectableRunning = isRunning();
@@ -97,7 +96,7 @@ bool Connectable::isSupportedRelationship(core::Relationship relationship) {
   }
 }
 
-bool Connectable::setAutoTerminatedRelationships(std::set<Relationship> relationships) {
+bool Connectable::setAutoTerminatedRelationships(const std::set<Relationship> &relationships) {
   if (isRunning()) {
     logger_->log_warn("Can not set processor auto terminated relationship while the process %s is running", name_);
     return false;
@@ -114,7 +113,7 @@ bool Connectable::setAutoTerminatedRelationships(std::set<Relationship> relation
 }
 
 // Check whether the relationship is auto terminated
-bool Connectable::isAutoTerminated(core::Relationship relationship) {
+bool Connectable::isAutoTerminated(const core::Relationship &relationship) const {
   // if we are running we do not need a lock since the function to change relationships_ ( setSupportedRelationships)
   // cannot be executed while we are running
   const bool isConnectableRunning = isRunning();
@@ -153,12 +152,12 @@ void Connectable::notifyWork() {
   }
 }
 
-std::set<std::shared_ptr<Connectable>> Connectable::getOutGoingConnections(std::string relationship) {
+std::set<std::shared_ptr<Connectable>> Connectable::getOutGoingConnections(const std::string &relationship) const {
   std::set<std::shared_ptr<Connectable>> empty;
 
-  auto &&it = out_going_connections_.find(relationship);
+  const auto &&it = out_going_connections_.find(relationship);
   if (it != out_going_connections_.end()) {
-    return out_going_connections_[relationship];
+    return it->second;
   } else {
     return empty;
   }

http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/3d2372f4/libminifi/src/core/Core.cpp
----------------------------------------------------------------------
diff --git a/libminifi/src/core/Core.cpp b/libminifi/src/core/Core.cpp
index e5c8d6c..1e95194 100644
--- a/libminifi/src/core/Core.cpp
+++ b/libminifi/src/core/Core.cpp
@@ -34,12 +34,12 @@ void CoreComponent::setUUID(utils::Identifier &uuid) {
   uuidStr_ = uuid_.to_string();
 }
 
-void CoreComponent::setUUIDStr(const std::string uuidStr) {
+void CoreComponent::setUUIDStr(const std::string &uuidStr) {
   uuid_ = uuidStr;
   uuidStr_ = uuidStr;
 }
 // Get UUID
-bool CoreComponent::getUUID(utils::Identifier &uuid) {
+bool CoreComponent::getUUID(utils::Identifier &uuid) const {
   if (uuid_ == nullptr) {
     return false;
   }
@@ -47,14 +47,8 @@ bool CoreComponent::getUUID(utils::Identifier &uuid) {
   return true;
 }
 
-// Get UUID
-/*
- unsigned const char *CoreComponent::getUUID() {
- return uuid_.getIdentifier();
- }*/
-
 // Set Processor Name
-void CoreComponent::setName(const std::string name) {
+void CoreComponent::setName(const std::string &name) {
   name_ = name;
 }
 // Get Process Name

http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/3d2372f4/libminifi/src/utils/Id.cpp
----------------------------------------------------------------------
diff --git a/libminifi/src/utils/Id.cpp b/libminifi/src/utils/Id.cpp
index 8994771..5abe714 100644
--- a/libminifi/src/utils/Id.cpp
+++ b/libminifi/src/utils/Id.cpp
@@ -51,6 +51,10 @@ Identifier::Identifier(const Identifier &other) {
   }
 }
 
+Identifier::Identifier(Identifier &&other)
+    : IdentifierBase(std::move(other)) {
+}
+
 Identifier::Identifier(const IdentifierBase &other) {
   if (!other.convert().empty()) {
     copyInto(other);
@@ -86,27 +90,27 @@ Identifier &Identifier::operator=(std::string id) {
   return *this;
 }
 
-bool Identifier::operator==(std::nullptr_t nullp) {
+bool Identifier::operator==(const std::nullptr_t nullp) const {
   return converted_.empty();
 }
 
-bool Identifier::operator!=(std::nullptr_t nullp) {
+bool Identifier::operator!=(const std::nullptr_t nullp) const {
   return !converted_.empty();
 }
 
-bool Identifier::operator!=(const Identifier &other) {
+bool Identifier::operator!=(const Identifier &other) const {
   return converted_ != other.converted_;
 }
 
-bool Identifier::operator==(const Identifier &other) {
+bool Identifier::operator==(const Identifier &other) const {
   return converted_ == other.converted_;
 }
 
-std::string Identifier::to_string() {
+std::string Identifier::to_string() const {
   return convert();
 }
 
-unsigned char *Identifier::toArray() {
+const unsigned char * const Identifier::toArray() const {
   return id_;
 }
 
@@ -129,7 +133,7 @@ IdGenerator::IdGenerator()
       incrementor_(0) {
 }
 
-uint64_t IdGenerator::getDeviceSegmentFromString(const std::string& str, int numBits) {
+uint64_t IdGenerator::getDeviceSegmentFromString(const std::string& str, int numBits) const {
   uint64_t deviceSegment = 0;
   for (size_t i = 0; i < str.length(); i++) {
     unsigned char c = toupper(str[i]);
@@ -149,7 +153,7 @@ uint64_t IdGenerator::getDeviceSegmentFromString(const std::string& str, int num
   return deviceSegment;
 }
 
-uint64_t IdGenerator::getRandomDeviceSegment(int numBits) {
+uint64_t IdGenerator::getRandomDeviceSegment(int numBits) const {
   uint64_t deviceSegment = 0;
   UUID_FIELD random_uuid;
   for (int word = 0; word < 2; word++) {

http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/3d2372f4/libminifi/test/unit/IdTests.cpp
----------------------------------------------------------------------
diff --git a/libminifi/test/unit/IdTests.cpp b/libminifi/test/unit/IdTests.cpp
index 90ddcd2..a70bc59 100644
--- a/libminifi/test/unit/IdTests.cpp
+++ b/libminifi/test/unit/IdTests.cpp
@@ -47,6 +47,23 @@ TEST_CASE("Test time", "[id]") {
   LogTestController::getInstance().reset();
 }
 
+TEST_CASE("Test Generate Move", "[id]") {
+  TestController test_controller;
+
+  LogTestController::getInstance().setDebug<utils::IdGenerator>();
+  std::shared_ptr<minifi::Properties> id_props = std::make_shared<minifi::Properties>();
+  id_props->set("uid.implementation", "TiMe");
+
+  std::shared_ptr<utils::IdGenerator> generator = utils::IdGenerator::getIdGenerator();
+  generator->initialize(id_props);
+
+  auto generated = generator->generate();
+  auto str = generated.to_string();
+  utils::Identifier moved = std::move(generated);
+  auto str2 = moved.to_string();
+  REQUIRE(str == str2);
+}
+
 TEST_CASE("Test random", "[id]") {
   TestController test_controller;
 
@@ -174,7 +191,6 @@ TEST_CASE("Test Hex Device Segment 18 bits", "[id]") {
   REQUIRE(128 == (uid[2] & 192));
   REQUIRE(1 == uid[15]);
 
-
   utils::Identifier uuid2;
   generator->generate(uuid2);
   REQUIRE(uuid.to_string() != uuid2.to_string());