Skip to content

proxy-types.h: add static_assert to detect int/enum size mismatch #120

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

Merged
merged 2 commits into from
Jan 13, 2025
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
3 changes: 3 additions & 0 deletions include/mp/proxy-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,9 @@ LocalType BuildPrimitive(InvokeContext& invoke_context,
TypeList<LocalType>,
typename std::enable_if<std::is_enum<Value>::value>::type* enable = nullptr)
{
using E = std::make_unsigned_t<std::underlying_type_t<Value>>;
using T = std::make_unsigned_t<LocalType>;
static_assert(std::numeric_limits<T>::max() >= std::numeric_limits<E>::max(), "mismatched integral/enum types");
return static_cast<LocalType>(value);
}

Expand Down
1 change: 1 addition & 0 deletions test/mp/test/foo.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface FooInterface $Proxy.wrap("mp::test::FooImplementation") {
passEmpty @12 (arg :FooEmpty) -> (result :FooEmpty);
passMessage @13 (arg :FooMessage) -> (result :FooMessage);
passMutable @14 (arg :FooMutable) -> (arg :FooMutable);
passEnum @15 (arg :Int32) -> (result :Int32);
}

interface FooCallback $Proxy.wrap("mp::test::FooCallback") {
Expand Down
3 changes: 3 additions & 0 deletions test/mp/test/foo.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct FooStruct
std::vector<bool> vbool;
};

enum class FooEnum : int { ONE = 1, TWO = 2, };

struct FooCustom
{
std::string v1;
Expand Down Expand Up @@ -72,6 +74,7 @@ class FooImplementation
FooEmpty passEmpty(FooEmpty foo) { return foo; }
FooMessage passMessage(FooMessage foo) { foo.message += " call"; return foo; }
void passMutable(FooMutable& foo) { foo.message += " call"; }
FooEnum passEnum(FooEnum foo) { return foo; }
std::shared_ptr<FooCallback> m_callback;
};

Expand Down