-
Notifications
You must be signed in to change notification settings - Fork 137
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2015-2020 The NATS Authors | ||
// 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, exactly. since the ownership is passed into the callback |
||
} | ||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if you meant to use this but did not? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
There was a problem hiding this comment.
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.