Skip to content

Commit

Permalink
Merge pull request #56 from eclipse-zenoh/windows_compile
Browse files Browse the repository at this point in the history
fixes to compile with MSVC
  • Loading branch information
milyin authored Jul 21, 2023
2 parents d401db3 + 4c1bc1e commit 36bd8ae
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 41 deletions.
7 changes: 6 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ else()
add_custom_target(examples)
endif()

add_custom_target(examples_zenohc)
add_custom_target(examples_zenohpico)
add_dependencies(examples examples_zenohc)
add_dependencies(examples examples_zenohpico)

function(add_example file mode lib)
get_filename_component(filename ${file} NAME_WE)
set(target ${filename}_${mode})
add_executable(${target} EXCLUDE_FROM_ALL ${file})
set_target_properties(${target} PROPERTIES
OUTPUT_NAME ${filename}
RUNTIME_OUTPUT_DIRECTORY ${mode})
add_dependencies(examples ${target})
add_dependencies(examples_${mode} ${target})
add_dependencies(${target} ${lib})
target_link_libraries(${target} PUBLIC ${lib})
set_property(TARGET ${target} PROPERTY LANGUAGE CXX)
Expand Down
18 changes: 11 additions & 7 deletions examples/universal/z_ping.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ char* getopt(int argc, char** argv, char option) {
struct args_t parse_args(int argc, char** argv) {
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "-h") == 0) {
return (struct args_t){.help_requested = 1};
struct args_t args;
args.help_requested = 1;
return args;
}
}
char* arg = getopt(argc, argv, 's');
Expand All @@ -127,12 +129,14 @@ struct args_t parse_args(int argc, char** argv) {
if (arg) {
timeout_ms = atoi(arg);
}
return (struct args_t){.help_requested = 0,
.number_of_pings = number_of_pings,
.size = size,
.warmup_ms = warmup_ms,
.timeout_ms = timeout_ms,
.config_path = getopt(argc, argv, 'c')};
struct args_t args;
args.help_requested = 0;
args.number_of_pings = number_of_pings;
args.size = size;
args.warmup_ms = warmup_ms;
args.timeout_ms = timeout_ms;
args.config_path = getopt(argc, argv, 'c');
return args;
}

int main(int argc, char** argv) {
Expand Down
2 changes: 2 additions & 0 deletions examples/universal/z_pub.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#include <windows.h>
#undef min
#undef max
#define sleep(x) Sleep(x * 1000)
#else
#include <unistd.h>
Expand Down
4 changes: 2 additions & 2 deletions examples/zenohc/z_get_channel_non_blocking.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#include <iostream>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#include <windows.h>
#define sleep(x) Sleep(x * 1000)
#else
#include <unistd.h>
#define Sleep(x) usleep(x * 1000)
#endif

#include "zenohc.hxx"
Expand Down Expand Up @@ -61,7 +61,7 @@ int _main(int argc, char **argv) {
for (bool call_success = recv(reply); !call_success || reply.check(); call_success = recv(reply)) {
if (!call_success) {
std::cout << ".";
usleep(100);
Sleep(1);
continue;
}
auto sample = std::get<Sample>(reply.get());
Expand Down
4 changes: 2 additions & 2 deletions include/zenoh.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>

// ZENOHCXX_ZENOHPICO and ZENOHCXX_ZENOHC are mutually exclusive when using this header
#if defined(ZENOHCXX_ZENOHPICO) and defined(ZENOHCXX_ZENOHC)
#if defined(ZENOHCXX_ZENOHPICO) && defined(ZENOHCXX_ZENOHC)
#error("Only one of ZENOHCXX_ZENOHPICO and ZENOHCXX_ZENOHC should be defined. \
Explictly include zenohpico.hxx and zenohc.hxx to use both libraies in the same program\
under namespaces zenohpico and zenohc respectively.")
#endif
#if not defined(ZENOHCXX_ZENOHPICO) and not defined(ZENOHCXX_ZENOHC)
#if !defined(ZENOHCXX_ZENOHPICO) && !defined(ZENOHCXX_ZENOHC)
#error("Either ZENOHCXX_ZENOHPICO or ZENOHCXX_ZENOHC should be defined")
#endif
Expand Down
22 changes: 11 additions & 11 deletions include/zenohcxx/api.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
// as this file is included multiple times into different namespaces

// Validate that __ZENOHCXX_ZENOHPICO and __ZENOHCXX_ZENOHC are mutually exclusive
#if defined(__ZENOHCXX_ZENOHPICO) and defined(__ZENOHCXX_ZENOHC)
#if defined(__ZENOHCXX_ZENOHPICO) && defined(__ZENOHCXX_ZENOHC)
#error("Internal include configuration error: both __ZENOHCXX_ZENOHC and __ZENOHCXX_ZENOHPICO are defined")
#endif
#if not defined(__ZENOHCXX_ZENOHPICO) and not defined(__ZENOHCXX_ZENOHC)
#if !defined(__ZENOHCXX_ZENOHPICO) && !defined(__ZENOHCXX_ZENOHC)
#error("Internal include configuration error: either __ZENOHCXX_ZENOHC or __ZENOHCXX_ZENOHPICO should be defined")
#endif

Expand Down Expand Up @@ -170,12 +170,12 @@ template <typename Z_STR_ARRAY_T>
struct _StrArrayView : Copyable<Z_STR_ARRAY_T> {
typedef decltype(Z_STR_ARRAY_T::val) VALTYPE;
using Copyable<Z_STR_ARRAY_T>::Copyable;
_StrArrayView() : Copyable<Z_STR_ARRAY_T>({.len = 0, .val = nullptr}) {}
_StrArrayView() : Copyable<Z_STR_ARRAY_T>({0, nullptr}) {}
_StrArrayView(const std::vector<const char*>& v)
: Copyable<Z_STR_ARRAY_T>({.len = v.size(), .val = const_cast<VALTYPE>(&v[0])}) {}
_StrArrayView(const char** v, size_t len) : Copyable<Z_STR_ARRAY_T>({.len = len, .val = const_cast<VALTYPE>(v)}) {}
: Copyable<Z_STR_ARRAY_T>({v.size(), const_cast<VALTYPE>(&v[0])}) {}
_StrArrayView(const char** v, size_t len) : Copyable<Z_STR_ARRAY_T>({len, const_cast<VALTYPE>(v)}) {}
_StrArrayView(const char* const* v, size_t len)
: Copyable<Z_STR_ARRAY_T>({.len = len, .val = const_cast<VALTYPE>(v)}) {}
: Copyable<Z_STR_ARRAY_T>({len, const_cast<VALTYPE>(v)}) {}
const char* operator[](size_t pos) const { return Copyable<Z_STR_ARRAY_T>::val[pos]; }
size_t get_len() const { return Copyable<Z_STR_ARRAY_T>::len; }
};
Expand All @@ -191,14 +191,14 @@ class BytesView : public Copyable<::z_bytes_t> {
using Copyable::Copyable;
BytesView(nullptr_t) : Copyable(init(nullptr, 0)) {}
BytesView(const void* s, size_t _len) : Copyable(init(reinterpret_cast<const uint8_t*>(s), _len)) {}
BytesView(const char* s) : Copyable({.len = s ? strlen(s) : 0, .start = reinterpret_cast<const uint8_t*>(s)}) {}
BytesView(const char* s) : Copyable({s ? strlen(s) : 0, reinterpret_cast<const uint8_t*>(s)}) {}
template <typename T>
BytesView(const std::vector<T>& v)
: Copyable({.len = v.size() * sizeof(T), .start = reinterpret_cast<const uint8_t*>(&v[0])}) {}
: Copyable({v.size() * sizeof(T), reinterpret_cast<const uint8_t*>(&v[0])}) {}
BytesView(const std::string_view& s)
: Copyable({.len = s.length(), .start = reinterpret_cast<const uint8_t*>(s.data())}) {}
: Copyable({s.length(), reinterpret_cast<const uint8_t*>(s.data())}) {}
BytesView(const std::string& s)
: Copyable({.len = s.length(), .start = reinterpret_cast<const uint8_t*>(s.data())}) {}
: Copyable({s.length(), reinterpret_cast<const uint8_t*>(s.data())}) {}
std::string_view as_string_view() const { return std::string_view(reinterpret_cast<const char*>(start), len); }
bool operator==(const BytesView& v) const { return as_string_view() == v.as_string_view(); }
bool operator!=(const BytesView& v) const { return !operator==(v); }
Expand Down Expand Up @@ -407,7 +407,7 @@ struct Sample : public Copyable<::z_sample_t> {
struct Value : public Copyable<::z_value_t> {
using Copyable::Copyable;
Value(const z::BytesView& payload, const z::Encoding& encoding)
: Copyable({.payload = payload, .encoding = encoding}) {}
: Copyable({payload, encoding}) {}
Value(const z::BytesView& payload) : Value(payload, z::Encoding()) {}
Value(const char* payload) : Value(payload, z::Encoding()) {}

Expand Down
24 changes: 11 additions & 13 deletions include/zenohcxx/base.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -142,26 +142,25 @@ class ClosureConstPtrParam : public Owned<ZC_CLOSURE_TYPE> {
private:
template <typename T>
ZC_CLOSURE_TYPE wrap_ref(T& obj) {
return {.context = &obj,
.call = [](const ZC_PARAM* pvalue, void* ctx) -> ZC_RETVAL {
return {&obj,
[](const ZC_PARAM* pvalue, void* ctx) -> ZC_RETVAL {
return static_cast<T*>(ctx)->operator()(static_cast<const ZCPP_PARAM*>(pvalue));
},
.drop = [](void* ctx) { static_cast<T*>(ctx)->operator()(nullptr); }};
[](void* ctx) { static_cast<T*>(ctx)->operator()(nullptr); }};
}

template <typename T>
ZC_CLOSURE_TYPE wrap_func(T& func) {
typedef std::function<T> CONTEXT_TYPE;
return {
.context = new CONTEXT_TYPE(func),
.call = [](const ZC_PARAM* pvalue, void* ctx) -> ZC_RETVAL {
new CONTEXT_TYPE(func),
[](const ZC_PARAM* pvalue, void* ctx) -> ZC_RETVAL {
return static_cast<CONTEXT_TYPE*>(ctx)->operator()(static_cast<const ZCPP_PARAM*>(pvalue));
},
.drop =
[](void* ctx) {
[](void* ctx) {
static_cast<CONTEXT_TYPE*>(ctx)->operator()(nullptr);
delete static_cast<CONTEXT_TYPE*>(ctx);
},
}
};
}

Expand All @@ -170,15 +169,14 @@ class ClosureConstPtrParam : public Owned<ZC_CLOSURE_TYPE> {
typedef std::remove_reference_t<T> CONTEXT_TYPE;

return {
.context = new CONTEXT_TYPE(std::forward<CONTEXT_TYPE>(obj)),
.call = [](const ZC_PARAM* pvalue, void* ctx) -> ZC_RETVAL {
new CONTEXT_TYPE(std::forward<CONTEXT_TYPE>(obj)),
[](const ZC_PARAM* pvalue, void* ctx) -> ZC_RETVAL {
return static_cast<CONTEXT_TYPE*>(ctx)->operator()(static_cast<const ZCPP_PARAM*>(pvalue));
},
.drop =
[](void* ctx) {
[](void* ctx) {
static_cast<CONTEXT_TYPE*>(ctx)->operator()(nullptr);
delete static_cast<CONTEXT_TYPE*>(ctx);
},
}
};
}
};
Expand Down
8 changes: 3 additions & 5 deletions include/zenohcxx/impl.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ inline void init_logger() { ::zc_init_logger(); }
#endif

inline ::z_bytes_t z::BytesView::init(const uint8_t* start, size_t len) {
::z_bytes_t ret = {.len = len,
.start = start
::z_bytes_t ret = {len, start
#ifdef __ZENOHCXX_ZENOHPICO
,
._is_alloc = false
,false
#endif
};
return ret;
Expand Down Expand Up @@ -106,7 +104,7 @@ inline bool keyexpr_canonize(std::string& s) {
}

inline bool keyexpr_is_canon(const std::string_view& s, ErrNo& error) {
error = ::z_keyexpr_is_canon(s.begin(), s.length());
error = ::z_keyexpr_is_canon(s.data() , s.length());
return error == 0;
}

Expand Down

0 comments on commit 36bd8ae

Please sign in to comment.