From e147d8fa326b66caaf3e0090b24fec7476730606 Mon Sep 17 00:00:00 2001 From: GhostofCookie Date: Wed, 23 Apr 2025 12:55:12 -0400 Subject: [PATCH] Adding simple module example. --- CMakeLists.txt | 9 ++++ examples/CMakeLists.txt | 1 + examples/simple_module/CMakeLists.txt | 22 +++++++++ examples/simple_module/include/Example.hpp | 54 ++++++++++++++++++++++ examples/simple_module/src/register.cpp | 29 ++++++++++++ 5 files changed, 115 insertions(+) create mode 100644 examples/CMakeLists.txt create mode 100644 examples/simple_module/CMakeLists.txt create mode 100644 examples/simple_module/include/Example.hpp create mode 100644 examples/simple_module/src/register.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 836580f..457af49 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -196,3 +196,12 @@ if(flow-ui_INSTALL) write_basic_package_version_file(${PROJECT_NAME}ConfigVersion.cmake COMPATIBILITY SameMajorVersion) endif() + +# ----------------------------------------------------------------------------- +# Examples +# ----------------------------------------------------------------------------- + +option(BUILD_EXAMPLES OFF) +if (BUILD_EXAMPLES) + add_subdirectory(examples) +endif() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..058ca69 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(simple_module) diff --git a/examples/simple_module/CMakeLists.txt b/examples/simple_module/CMakeLists.txt new file mode 100644 index 0000000..27e981c --- /dev/null +++ b/examples/simple_module/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.10) + +project(simple_module VERSION 1.0.0 LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +if(APPLE) + enable_language(OBJC) +endif() + +add_library(${PROJECT_NAME} SHARED src/register.cpp) +target_compile_definitions(${PROJECT_NAME} PRIVATE FLOW_SIMPLE_EXPORT) + +if(MSVC) + set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE) + target_compile_options(${PROJECT_NAME} PRIVATE /W4) +endif() + +add_dependencies(${PROJECT_NAME} flow-core) +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) +target_link_libraries(${PROJECT_NAME} PUBLIC flow-core) diff --git a/examples/simple_module/include/Example.hpp b/examples/simple_module/include/Example.hpp new file mode 100644 index 0000000..4a31cfd --- /dev/null +++ b/examples/simple_module/include/Example.hpp @@ -0,0 +1,54 @@ +// Copyright (c) 2024,Cisco Systems, Inc. +// All rights reserved. + +#pragma once + +#include +#include + +#include + +namespace flow::simple +{ +class ExampleNode : public Node +{ + public: + explicit ExampleNode(const std::string& uuid_str, const std::string& name, std::shared_ptr env) + : Node(uuid_str, flow::TypeName_v, name, std::move(env)) + { + AddInput("name", "Input name"); + AddOutput("greeting", "A greeting to the specified user"); + } + + virtual ~ExampleNode() = default; + + protected: + void Compute() override + { + if (auto name_data = GetInputData("name")) + { + const std::string& name = name_data->Get(); + SetOutputData("greeting", flow::MakeNodeData("Hello " + name + "!")); + } + } + + json SaveInputs() const override + { + if (auto name_data = GetInputData("name")) + { + const std::string& name = name_data->Get(); + return {{"name", name}}; + } + + return {}; + } + + void RestoreInputs(const json& j) override + { + if (j.contains("name")) + { + SetInputData("name", flow::MakeNodeData(j["name"].get_ref())); + } + } +}; +} // namespace flow::simple diff --git a/examples/simple_module/src/register.cpp b/examples/simple_module/src/register.cpp new file mode 100644 index 0000000..7f0ec18 --- /dev/null +++ b/examples/simple_module/src/register.cpp @@ -0,0 +1,29 @@ +#include "Example.hpp" + +#include + +#ifdef FLOW_WINDOWS +#ifdef FLOW_SIMPLE_EXPORT +#define FLOW_SIMPLE_API __declspec(dllexport) FLOW_CORE_CALL +#else +#define FLOW_SIMPLE_API __declspec(dllimport) FLOW_CORE_CALL +#endif +#else +#define FLOW_SIMPLE_API +#endif + +extern "C" +{ + namespace flow::simple + { + void FLOW_SIMPLE_API RegisterModule(std::shared_ptr factory) + { + factory->RegisterNodeClass("example", "Example"); + } + + void FLOW_SIMPLE_API UnregisterModule(std::shared_ptr factory) + { + factory->UnregisterNodeClass("example"); + } + } // namespace flow::simple +}