Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add C++ bindings #332

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions CMakeLists.txt
Expand Up @@ -19,6 +19,7 @@ option(NATS_BUILD_WITH_TLS "Build with TLS support" ON)
option(NATS_BUILD_TLS_FORCE_HOST_VERIFY "Forces hostname verification" ON)
option(NATS_BUILD_TLS_USE_OPENSSL_1_1_API "Build for OpenSSL 1.1+" OFF)
option(NATS_BUILD_USE_SODIUM "Build using libsodium library" OFF)
option(NATS_BUILD_CPP_BINDINGS "Build binding for C++" OFF)
option(NATS_BUILD_EXAMPLES "Build examples" ON)
option(NATS_BUILD_LIBUV_EXAMPLE "Build libuv examples" OFF)
option(NATS_BUILD_LIBEVENT_EXAMPLE "Build libevent examples" OFF)
Expand Down Expand Up @@ -51,6 +52,10 @@ if(NATS_BUILD_WITH_TLS_CLIENT_METHOD)
set(NATS_BUILD_TLS_USE_OPENSSL_1_1_API ON)
endif(NATS_BUILD_WITH_TLS_CLIENT_METHOD)

if(NATS_BUILD_CPP_BINDINGS)
find_package(Python3 REQUIRED)
endif(NATS_BUILD_CPP_BINDINGS)

set(LIBUV_DIR "/usr/local/" CACHE PATH "Libuv install directory")
set(LIBEVENT_DIR "/usr/local/" CACHE PATH "Libevent install directory")

Expand Down Expand Up @@ -259,6 +264,9 @@ add_subdirectory(examples/getstarted)
if(NATS_BUILD_STREAMING)
add_subdirectory(examples/stan)
endif()
if(NATS_BUILD_CPP_BINDINGS)
add_subdirectory(examples/cpp)
endif()
add_subdirectory(test)
add_subdirectory(test/dylib)
#----------------------------
34 changes: 34 additions & 0 deletions examples/cpp/CMakeLists.txt
@@ -0,0 +1,34 @@
if(NOT NATS_BUILD_EXAMPLES)
return()
endif()

# We need this directory to build the examples
include_directories(${PROJECT_SOURCE_DIR}/src)
include_directories(${PROJECT_SOURCE_DIR}/examples/cpp)
include_directories(${CMAKE_BINARY_DIR}/src)

# Get all the .cpp files in the examples directory
file(GLOB EXAMPLES_SOURCES RELATIVE ${PROJECT_SOURCE_DIR}/examples/cpp *.cpp)

if(NOT NATS_BUILD_STATIC_EXAMPLES)
add_definitions(-Dnats_IMPORTS)
endif()

# For each file...
foreach(examples_src ${EXAMPLES_SOURCES})

# Remove the suffix so that it becomes the executable name
string(REPLACE ".cpp" "" examplename ${examples_src})
set(exampleexe "nats-cpp-${examplename}")

# Build the executable
add_executable(${exampleexe} ${PROJECT_SOURCE_DIR}/examples/cpp/${examples_src})

# Link
if(NATS_BUILD_STATIC_EXAMPLES)
target_link_libraries(${exampleexe} nats_static ${NATS_EXTRA_LIB})
else()
target_link_libraries(${exampleexe} nats ${NATS_EXTRA_LIB})
endif()

endforeach()
65 changes: 65 additions & 0 deletions examples/cpp/subscriber.cpp
@@ -0,0 +1,65 @@
// Copyright 2015-2020 The NATS Authors
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copyright should be: "Copyright 2020 The NATS Authors" since this is a new file.

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

#include "nats.hpp"
#include <iostream>

class ErrHandler {
public:
ErrHandler(const char* id) : id_(id) {}

void handle(nats::Connection & nc, nats::Subscription & sub, natsStatus err)
{
std::cout << id_ << " error:" << nats::GetText(err) << std::endl;
}

private:
const char* id_;
};

class Handler {
public:
Handler(const char* id) : id_(id) {}

void msg(nats::Connection & nc, nats::Subscription & sub, nats::Msg && msg)
{
std::string data(msg.GetData(), msg.GetDataLength());
std::cout << id_ << " received: " << msg.GetSubject() << " - " << data << std::endl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mentioned that you don't need to call natsMsg_Destroy() here. Is your binding auto-generating calls to this when the callback returns?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, exactly. since the ownership is passed into the callback msg will be destroyed when the function gets out of scope (and msg has not been moved or released) in the destructor

}

private:
const char* id_;
};

int main(int argc, char **argv)
{
Handler hh("HandlerId");
ErrHandler eh("MyErrorHandler");

nats::Open(10);

nats::Options options;
options.SetErrorHandler<ErrHandler, &ErrHandler::handle>(&eh);

nats::Connection connection(options);
nats::Subscription sub = connection.Subscribe<Handler, &Handler::msg>("subject", &hh);

nats::Statistics stat;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if you meant to use this but did not?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's just an example that it does not leak memory, but i agree that the example could be improved. for now i only wanted to include a simple one to show the basic idea


for (;;)
nats::Sleep(1000);

nats::Close();

return 0;
}
19 changes: 19 additions & 0 deletions src/CMakeLists.txt
Expand Up @@ -50,6 +50,25 @@ if(NATS_BUILD_LIB_STATIC)
target_compile_definitions(nats_static PRIVATE -DNATS_STATIC)
endif(NATS_BUILD_LIB_STATIC)

# ---------------------------
# Create the bindings for C++
# ---------------------------
if(NATS_BUILD_CPP_BINDINGS)
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/nats.hpp
COMMAND ${Python3_EXECUTABLE}
bindings/cpp_generator.py
bindings/cpp_template.mako
nats.h
${CMAKE_CURRENT_BINARY_DIR}/nats.hpp
DEPENDS
bindings/cpp_generator.py
bindings/cpp_template.mako
nats.h
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_custom_target(nats_cpp_bindings ALL DEPENDS ${CMAKE_BINARY_DIR}/nats.hpp)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/nats.hpp DESTINATION ${NATS_INCLUDE_DIR}/nats)
endif(NATS_BUILD_CPP_BINDINGS)

# --------------------------------------
# Install the libraries and header files
# --------------------------------------
Expand Down