Skip to content

Commit

Permalink
fixes to compile with MSVC
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Jul 16, 2023
1 parent d401db3 commit dbcd667
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 29 deletions.
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
20 changes: 10 additions & 10 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 @@ -172,10 +172,10 @@ struct _StrArrayView : Copyable<Z_STR_ARRAY_T> {
using Copyable<Z_STR_ARRAY_T>::Copyable;
_StrArrayView() : Copyable<Z_STR_ARRAY_T>({.len = 0, .val = 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
18 changes: 8 additions & 10 deletions include/zenohcxx/base.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,14 @@ class ClosureConstPtrParam : public Owned<ZC_CLOSURE_TYPE> {
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 dbcd667

Please sign in to comment.