Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions libminifi/include/utils/CControllerService.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "minifi-cpp/core/ControllerServiceMetadata.h"
#include "minifi-cpp/core/Property.h"
#include "minifi-cpp/core/controller/ControllerServiceApi.h"
#include "utils/minifi-api-utils.h"

namespace org::apache::nifi::minifi {
struct ClassDescription;
Expand All @@ -47,9 +48,9 @@ class CControllerService final : public core::controller::ControllerServiceApi,
metadata_(std::move(metadata)) {
minifi_controller_service_metadata c_metadata;
auto uuid_str = metadata_.uuid.to_string();
c_metadata.uuid = minifi_string_view{.data = uuid_str.data(), .length = uuid_str.length()};
c_metadata.name = minifi_string_view{.data = metadata_.name.data(), .length = metadata_.name.length()};
c_metadata.logger = reinterpret_cast<minifi_logger*>(&metadata_.logger);
c_metadata.uuid = minifiStringView(uuid_str.view());
c_metadata.name = minifiStringView(metadata_.name);
c_metadata.logger = toC(&metadata_.logger);
impl_ = class_description_.callbacks.create(c_metadata);
}
CControllerService(CControllerServiceClassDescription class_description, core::ControllerServiceMetadata metadata, gsl::owner<void*> impl)
Expand All @@ -75,7 +76,7 @@ class CControllerService final : public core::controller::ControllerServiceApi,
void onEnable(core::controller::ControllerServiceContext& controller_service_context,
const std::shared_ptr<Configure>&,
const std::vector<std::shared_ptr<core::controller::ControllerServiceHandle>>&) override {
const auto enable_status = class_description_.callbacks.enable(impl_, reinterpret_cast<minifi_controller_service_context*>(&controller_service_context));
const auto enable_status = class_description_.callbacks.enable(impl_, toC(&controller_service_context));
if (enable_status != MINIFI_STATUS_SUCCESS) {
throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Could not enable controller service");
}
Expand Down
11 changes: 6 additions & 5 deletions libminifi/include/utils/CProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "minifi-cpp/core/ProcessorApi.h"
#include "minifi-cpp/core/ProcessorDescriptor.h"
#include "minifi-cpp/core/ProcessorMetadata.h"
#include "utils/minifi-api-utils.h"

namespace org::apache::nifi::minifi::utils {

Expand Down Expand Up @@ -67,9 +68,9 @@ class CProcessor : public minifi::core::ProcessorApi {
metrics_extension_(std::make_shared<CProcessorMetricsWrapper>(*this)) {
minifi_processor_metadata c_metadata;
const auto uuid_str = metadata.uuid.to_string();
c_metadata.uuid = minifi_string_view{.data = uuid_str.data(), .length = uuid_str.length()};
c_metadata.name = minifi_string_view{.data = metadata_.name.data(), .length = metadata_.name.length()};
c_metadata.logger = reinterpret_cast<minifi_logger*>(&metadata_.logger);
c_metadata.uuid = minifiStringView(uuid_str.view());
c_metadata.name = minifiStringView(metadata_.name);
c_metadata.logger = toC(&metadata_.logger);
impl_ = class_description_.callbacks.create(c_metadata);
}
CProcessor(CProcessorClassDescription class_description, minifi::core::ProcessorMetadata metadata, gsl::owner<void*> impl)
Expand Down Expand Up @@ -116,7 +117,7 @@ class CProcessor : public minifi::core::ProcessorApi {

void onTrigger(minifi::core::ProcessContext& process_context, minifi::core::ProcessSession& process_session) override {
std::optional<std::string> error;
auto status = class_description_.callbacks.trigger(impl_, reinterpret_cast<minifi_process_context*>(&process_context), reinterpret_cast<minifi_process_session*>(&process_session));
auto status = class_description_.callbacks.trigger(impl_, toC(&process_context), toC(&process_session));
if (status == MINIFI_STATUS_PROCESSOR_YIELD) {
process_context.yield();
return;
Expand All @@ -128,7 +129,7 @@ class CProcessor : public minifi::core::ProcessorApi {

void onSchedule(minifi::core::ProcessContext& process_context, minifi::core::ProcessSessionFactory& /*process_session_factory*/) override {
std::optional<std::string> error;
auto status = class_description_.callbacks.schedule(impl_, reinterpret_cast<minifi_process_context*>(&process_context));
auto status = class_description_.callbacks.schedule(impl_, toC(&process_context));
if (status != MINIFI_STATUS_SUCCESS) {
throw minifi::Exception(minifi::ExceptionType::PROCESS_SCHEDULE_EXCEPTION, "Error while scheduling processor");
}
Expand Down
96 changes: 96 additions & 0 deletions libminifi/include/utils/minifi-api-utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* 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 <memory>
Comment thread
martinzink marked this conversation as resolved.

#include "core/extension/Extension.h"
#include "minifi-api.h"
#include "minifi-cpp/core/FlowFile.h"
#include "minifi-cpp/core/ProcessContext.h"
#include "minifi-cpp/core/ProcessSession.h"
#include "minifi-cpp/core/controller/ControllerServiceContext.h"
#include "minifi-cpp/core/logging/Logger.h"
#include "minifi-cpp/io/InputStream.h"
#include "minifi-cpp/io/OutputStream.h"

namespace org::apache::nifi::minifi::utils {

inline minifi_string_view minifiStringView(const std::string_view s) {
return minifi_string_view{.data = s.data(), .length = s.size()};
}

inline std::string toString(minifi_string_view sv) {
return {sv.data, sv.length};
}

inline std::string_view toStringView(minifi_string_view sv) {
return {sv.data, sv.length};
}

// Bidirectional type map between the C API's opaque handle types and their
// underlying C++ implementation types. A missing specialization at a call site
// is intentionally a compile error - any new opaque handle must be registered
// here before it can be passed through the generic cast helpers below.
template<typename C>
struct CppFor;
template<typename Cpp>
struct CFor;

#define MINIFI_API_MAP(CType, CppType) \
template<> \
struct CppFor<CType> { \
using type = CppType; \
}; \
template<> \
struct CFor<CppType> { \
using type = CType; \
}

MINIFI_API_MAP(minifi_process_context, minifi::core::ProcessContext);
MINIFI_API_MAP(minifi_process_session, minifi::core::ProcessSession);
MINIFI_API_MAP(minifi_controller_service_context, minifi::core::controller::ControllerServiceContext);
MINIFI_API_MAP(minifi_input_stream, minifi::io::InputStream);
MINIFI_API_MAP(minifi_output_stream, minifi::io::OutputStream);
MINIFI_API_MAP(minifi_extension, minifi::core::extension::Extension);
MINIFI_API_MAP(minifi_extension_context, minifi::core::extension::Extension::Context);
MINIFI_API_MAP(minifi_logger, std::shared_ptr<minifi::core::logging::Logger>);
MINIFI_API_MAP(minifi_flow_file, std::shared_ptr<minifi::core::FlowFile>);

#undef MINIFI_API_MAP

// Generic direct-pointer casts. Only usable for types registered above -
// otherwise the CppFor/CFor lookup fails to compile.
template<typename C>
auto* toCpp(C* c_ptr) noexcept {
return reinterpret_cast<typename CppFor<C>::type*>(c_ptr);
}

template<typename Cpp>
auto* toC(Cpp* cpp_ptr) noexcept {
return reinterpret_cast<typename CFor<Cpp>::type*>(cpp_ptr);
}

inline minifi::core::FlowFile* toRawFlowFile(minifi_flow_file* flow_file) {
if (auto cpp_flow_file = toCpp(flow_file)) {
return cpp_flow_file->get();
}
return nullptr;
}

} // namespace org::apache::nifi::minifi::utils
Loading
Loading