szaszm commented on code in PR #1589:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1589#discussion_r1259593659


##########
libminifi/include/core/PropertyDefinition.h:
##########
@@ -0,0 +1,78 @@
+/**
+ * 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 <array>
+#include <optional>
+#include <string_view>
+#include <utility>
+
+#include "core/PropertyType.h"
+#include "utils/gsl.h"
+
+namespace org::apache::nifi::minifi::core {
+
+template<size_t NumAllowedValues = 0, size_t NumAllowedTypes = 0, size_t 
NumDependentProperties = 0, size_t NumExclusiveOfProperties = 0>
+struct PropertyDefinition {
+  std::string_view name;
+  std::string_view display_name;
+  std::string_view description;
+  bool is_required = false;
+  std::string_view valid_regex;
+  std::array<std::string_view, NumAllowedValues> allowed_values;
+  std::array<std::string_view, NumAllowedTypes> allowed_types;
+  std::array<std::string_view, NumDependentProperties> dependent_properties;
+  std::array<std::pair<std::string_view, std::string_view>, 
NumExclusiveOfProperties> exclusive_of_properties;
+  std::optional<std::string_view> default_value;
+  gsl::not_null<const PropertyType*> type = 
gsl::make_not_null(&StandardPropertyTypes::VALID_TYPE);
+  bool supports_expression_language = false;
+};
+
+struct PropertyReference {
+  std::string_view name;
+  std::string_view display_name;
+  std::string_view description;
+  bool is_required = false;
+  std::string_view valid_regex;
+  gsl::span<const std::string_view> allowed_values;
+  gsl::span<const std::string_view> allowed_types;
+  gsl::span<const std::string_view> dependent_properties;
+  gsl::span<const std::pair<std::string_view, std::string_view>> 
exclusive_of_properties;
+  std::optional<std::string_view> default_value;
+  const PropertyType* type;

Review Comment:
   Can this ever be null? `PropertyDefinition` doesn't allow null.



##########
libminifi/include/core/Core.h:
##########
@@ -95,6 +98,62 @@ static inline std::string getClassName() {
 #endif
 }
 
+constexpr std::string_view removeStructOrClassPrefix(std::string_view input) {
+  constexpr std::string_view STRUCT = "struct ";  // should be static 
constexpr, but that is only allowed inside a constexpr function with std >= 
c++23
+  constexpr std::string_view CLASS = "class ";
+
+  for (auto prefix : { STRUCT, CLASS }) {
+    if (input.find(prefix) == 0) {
+      return input.substr(prefix.size());
+    }
+  }
+  return input;
+}
+
+// based on 
https://bitwizeshift.github.io/posts/2021/03/09/getting-an-unmangled-type-name-at-compile-time/
+template<typename T>
+constexpr auto typeNameArray() {
+#if defined(__clang__)
+  constexpr auto prefix   = std::string_view{"[T = "};
+  constexpr auto suffix   = std::string_view{"]"};
+  constexpr auto function = std::string_view{__PRETTY_FUNCTION__};
+#elif defined(__GNUC__)
+  constexpr auto prefix   = std::string_view{"with T = "};
+  constexpr auto suffix   = std::string_view{"]"};
+  constexpr auto function = std::string_view{__PRETTY_FUNCTION__};
+#elif defined(_MSC_VER)
+  constexpr auto prefix   = std::string_view{"typeNameArray<"};
+  constexpr auto suffix   = std::string_view{">(void)"};
+  constexpr auto function = std::string_view{__FUNCSIG__};
+#else
+# error Unsupported compiler
+#endif
+
+  static_assert(function.find(prefix) != std::string_view::npos && 
function.rfind(suffix) != std::string_view::npos);
+
+  constexpr auto start = function.find(prefix) + prefix.size();
+  constexpr auto end = function.rfind(suffix);
+  static_assert(start < end);
+
+#if defined(_MSC_VER)
+  constexpr auto name = removeStructOrClassPrefix(function.substr(start, end - 
start));
+#else
+  constexpr auto name = function.substr(start, end - start);
+#endif
+
+  return utils::string_view_to_array<name.length()>(name);
+}
+
+template<typename T>
+struct TypeNameHolder {
+  static constexpr auto value = typeNameArray<T>();
+};
+
+template<typename T>
+constexpr std::string_view className() {
+  return utils::array_to_string_view(TypeNameHolder<T>::value);
+}

Review Comment:
   The taken code is a non-trivial amount, and the changes to it are fairly 
minimal, so I think we should add this to the LICENSE and NOTICE files. 
https://github.com/bitwizeshift/bitwizeshift.github.io/blob/source/LICENSE



##########
libminifi/include/agent/agent_docs.h:
##########
@@ -61,9 +63,12 @@ struct Components {
 };
 
 namespace detail {
-template<typename Container>
-auto toVector(const Container& container) {
-  return std::vector<typename Container::value_type>(container.begin(), 
container.end());
+inline auto toVector(std::span<const core::PropertyReference> properties) {
+  return std::vector<core::Property>(properties.begin(), properties.end());
+}
+
+inline auto toVector(std::span<const core::RelationshipDefinition> 
relationships) {
+  return std::vector<core::Relationship>(relationships.begin(), 
relationships.end());

Review Comment:
   I was expecting something like this
   ```c++
   .class_properties_ = utils::span_to<std::vector>(Class::Properties)
   ```
   
   If it's not that simple, then don't bother changing it.



##########
libminifi/include/core/Core.h:
##########
@@ -95,6 +98,62 @@ static inline std::string getClassName() {
 #endif
 }
 
+constexpr std::string_view removeStructOrClassPrefix(std::string_view input) {
+  constexpr std::string_view STRUCT = "struct ";  // should be static 
constexpr, but that is only allowed inside a constexpr function with std >= 
c++23
+  constexpr std::string_view CLASS = "class ";

Review Comment:
   I'm neutral, pick what you think is best.
   
   What about this, using literals inline?
   
   ```cpp
   constexpr std::string_view removeStructOrClassPrefix(std::string_view input) 
{
     using namespace std::literals;
     for (auto prefix : { "struct "sv, "class "sv }) {
       if (input.find(prefix) == 0) {
         return input.substr(prefix.size());
       }
     }
     return input;
   }
   ```



##########
libminifi/include/core/ProcessContext.h:
##########
@@ -90,6 +91,7 @@ class ProcessContext : public 
controller::ControllerServiceLookup, public core::
     return processor_node_;
   }
 
+  // TODO(fgerlits): remove if possible

Review Comment:
   Is it possible?



##########
libminifi/include/utils/ArrayUtils.h:
##########
@@ -37,4 +39,35 @@ constexpr auto array_cat(const std::array<Type, sizes>&... 
arrays) {
   return result;
 }
 
+template<std::size_t size>
+constexpr auto string_view_to_array(std::string_view input) {
+  std::array<char, size> result;
+  std::copy_n(input.begin(), size, result.begin());
+  return result;
+}
+
+template<std::size_t size>
+constexpr std::string_view array_to_string_view(const std::array<char, size>& 
input) {
+  return {input.data(), input.size()};
+}
+
+template<typename Key, typename Value, size_t Size>
+constexpr std::array<Key, Size> getKeys(const std::array<std::pair<Key, 
Value>, Size>& mapping) {
+  std::array<Key, Size> result;
+  for (size_t idx = 0; idx < Size; ++idx) {
+    result.at(idx) = mapping.at(idx).first;
+  }
+  return result;

Review Comment:
   That's a `std::transform`



##########
libminifi/include/utils/ArrayUtils.h:
##########
@@ -37,4 +39,35 @@ constexpr auto array_cat(const std::array<Type, sizes>&... 
arrays) {
   return result;
 }
 
+template<std::size_t size>
+constexpr auto string_view_to_array(std::string_view input) {
+  std::array<char, size> result;
+  std::copy_n(input.begin(), size, result.begin());
+  return result;
+}
+
+template<std::size_t size>
+constexpr std::string_view array_to_string_view(const std::array<char, size>& 
input) {
+  return {input.data(), input.size()};
+}
+
+template<typename Key, typename Value, size_t Size>
+constexpr std::array<Key, Size> getKeys(const std::array<std::pair<Key, 
Value>, Size>& mapping) {
+  std::array<Key, Size> result;
+  for (size_t idx = 0; idx < Size; ++idx) {
+    result.at(idx) = mapping.at(idx).first;
+  }
+  return result;
+}
+
+template<typename Container, typename ComparableToKeyType>
+constexpr auto at(const Container& mapping, ComparableToKeyType key) {
+  for (const auto& kv : mapping) {
+    if (kv.first == key) {
+      return kv.second;
+    }
+  }

Review Comment:
   That's a `std::find_if`



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to