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

Fix helper macros for multiple signatures #96

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ struct facade_prototype {

#define ___PRO_DIRECT_FUNC_IMPL(__EXPR) \
noexcept(noexcept(__EXPR)) requires(requires { __EXPR; }) { return __EXPR; }
#define ___PRO_DEF_DISPATCH_IMPL(__NAME, __EXPR, __DEFEXPR, __OVERLOADS) \
#define ___PRO_DEF_DISPATCH_IMPL(__NAME, __EXPR, __DEFEXPR, ...) \
struct __NAME { \
private: \
using __Name = __NAME; \
Expand All @@ -931,7 +931,7 @@ struct facade_prototype {
}; \
\
public: \
using overload_types = __OVERLOADS; \
using overload_types = ::std::tuple<__VA_ARGS__>; \
template <class __T> \
using invoker = ::std::conditional_t<::std::is_void_v<__T>, __FV, __FT>; \
template <class __P> \
Expand All @@ -946,11 +946,11 @@ struct facade_prototype {
#define PRO_DEF_MEMBER_DISPATCH_WITH_DEFAULT(__NAME, __FUNC, __DEFFUNC, ...) \
___PRO_DEF_DISPATCH_IMPL(__NAME, \
__self.__FUNC(::std::forward<__Args>(__args)...), \
__DEFFUNC(::std::forward<__Args>(__args)...), ::std::tuple<__VA_ARGS__>)
__DEFFUNC(::std::forward<__Args>(__args)...), __VA_ARGS__)
#define PRO_DEF_FREE_DISPATCH_WITH_DEFAULT(__NAME, __FUNC, __DEFFUNC, ...) \
___PRO_DEF_DISPATCH_IMPL(__NAME, \
__FUNC(__self, ::std::forward<__Args>(__args)...), \
__DEFFUNC(::std::forward<__Args>(__args)...), ::std::tuple<__VA_ARGS__>)
__DEFFUNC(::std::forward<__Args>(__args)...), __VA_ARGS__)
#define PRO_DEF_MEMBER_DISPATCH(__NAME, ...) \
PRO_DEF_MEMBER_DISPATCH_WITH_DEFAULT( \
__NAME, __NAME, ::pro::details::invalid_call, __VA_ARGS__)
Expand Down
33 changes: 33 additions & 0 deletions tests/proxy_integration_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ PRO_DEF_MEMBER_DISPATCH(Draw, void(std::ostream&));
PRO_DEF_MEMBER_DISPATCH(Area, double() noexcept);
PRO_DEF_FACADE(Drawable, PRO_MAKE_DISPATCH_PACK(Draw, Area));

PRO_DEF_MEMBER_DISPATCH(Log, void(const char*), void(const char*, const std::exception&));
PRO_DEF_FACADE(Logger, Log);

} // namespace spec

class Rectangle {
Expand Down Expand Up @@ -106,6 +109,22 @@ pro::proxy<spec::Drawable> MakeDrawableFromCommand(const std::string& s) {
throw std::runtime_error{"Invalid command"};
}

class StreamLogger {
public:
explicit StreamLogger(std::ostream& out) : out_(&out) {}
StreamLogger(const StreamLogger&) = default;

void Log(const char* s) {
*out_ << "[INFO] " << s << "\n";
}
void Log(const char* s, const std::exception& e) {
*out_ << "[ERROR] " << s << " (exception info: " << e.what() << ")\n";
}

private:
std::ostream* out_;
};

} // namespace

TEST(ProxyIntegrationTests, TestDrawable) {
Expand All @@ -127,3 +146,17 @@ TEST(ProxyIntegrationTests, TestDrawable) {
ASSERT_STREQ(e.what(), "Invalid command");
}
}

TEST(ProxyIntegrationTests, TestLogger) {
std::ostringstream out;
auto logger = pro::make_proxy<spec::Logger, StreamLogger>(out);
logger.Log("hello");
try {
throw std::runtime_error{"runtime error!"};
} catch (const std::exception& e) {
logger.Log("world", e);
}
auto content = std::move(out).str();
ASSERT_EQ(content, "[INFO] hello\n\
[ERROR] world (exception info: runtime error!)\n");
}
Loading