Skip to content

Commit

Permalink
Merge pull request #413 from jwillemsen/jwi-bitsetdefault
Browse files Browse the repository at this point in the history
Add and test support for bitset default annotation
  • Loading branch information
jwillemsen authored Nov 25, 2024
2 parents 6b8e775 + 7fe9b8c commit 65ca4a2
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ridlbe/c++11/templates/cli/hdr/bitset.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class <%= cxxname %><% unless base.nil? %> : public <%= base.cxxname %><% end %>
{
public:
%# With C++17 we need to initialize all bitfield members explicitly
<%= cxxname %> () = default;
<%= cxxname %> ();
~<%= cxxname %> () noexcept = default;
<%= cxxname %> (const <%= cxxname %>&) = default;
<%= cxxname %> (<%= cxxname %>&&) = default;
Expand Down
16 changes: 16 additions & 0 deletions ridlbe/c++11/templates/cli/inl/bitset_inl.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@
%# Constructor inlines
%#
%# Initializer CTOR
%# Required with C++17, with C++20 we can use {} for bitfields
%_ms = bitfields.dup.find_all {|bitf| !bitf.cxxname.empty? }
inline <%= scoped_cxxtype %>::<%= cxxname %> ()
% _pfx = nil
% unless base.nil?
: <%= base.cxxname %> ()
% end
% str = ""
% bitfields.find_all {|bitf| !bitf.cxxname.empty? }.each do |_m|
% str = "#{str}#{_pfx}#{_m.default_value}"
% _pfx = ', ' unless _pfx
% end
<% if base.nil? %>:<% else %>,<% end %> _taox11_<%= cxxname.downcase %> { <%= str %> }
{
}

%_ms = bitfields.dup.find_all {|bitf| !bitf.cxxname.empty? }
inline <%= scoped_cxxtype %>::<%= cxxname %> (
% unless base.nil?
Expand Down
15 changes: 15 additions & 0 deletions ridlbe/c++11/visitors/bitset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ class BitFieldVisitor < NodeVisitorBase
def bits
node.bits
end
def default_value
unless node.annotations[:default].first.nil?
"#{node.annotations[:default].first.fields[:value]}"
else
# Check whether it is a typedef, if so, we need to see if there is an annotation applied to the typedef (or its typedef)
res_idl_type = _idltype
while res_idl_type.is_a?(IDL::Type::ScopedName)
unless res_idl_type.node.annotations[:default].first.nil?
return "#{res_idl_type.node.annotations[:default].first.fields[:value]}"
end
res_idl_type = res_idl_type.node.idltype
end
_resolved_idltype.default
end
end
end
end
end
27 changes: 27 additions & 0 deletions tests/idl4/default/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ int main (int /*argc*/, char* /*argv*/[])
int retval {};
bar mybar;
foo myfoo;
MyBitset1 mybitset1 {};
MyBitset2 mybitset2 {};

TAOX11_TEST_INFO << "mybar: " << mybar << std::endl;
TAOX11_TEST_INFO << "myfoo: " << myfoo << std::endl;
Expand Down Expand Up @@ -79,6 +81,31 @@ int main (int /*argc*/, char* /*argv*/[])
TAOX11_TEST_ERROR << "myfoo.ushort_5() not 5 but: " << myfoo.ushort_5() << std::endl;
++retval;
}
if (mybitset1.a() != 0)
{
TAOX11_TEST_ERROR << "mybitset1.a() not 0 but: " << mybitset1.a() << std::endl;
++retval;
}
if (mybitset1.d() != 5)
{
TAOX11_TEST_ERROR << "mybitset1.d() not 5 but: " << mybitset1.d() << std::endl;
++retval;
}
if (mybitset2.g() != 3)
{
TAOX11_TEST_ERROR << "mybitset2.g() not 3 but: " << mybitset2.g() << std::endl;
++retval;
}
if (mybitset1.x() != true)
{
TAOX11_TEST_ERROR << "mybitset1.x() not true but: " << mybitset1.x() << std::endl;
++retval;
}
if (mybitset2.h() != false)
{
TAOX11_TEST_ERROR << "mybitset2.h() not false but: " << mybitset2.h() << std::endl;
++retval;
}

TestUnion_Octet tuo;
TAOX11_TEST_INFO << "tuo: " << tuo << std::endl;
Expand Down
14 changes: 14 additions & 0 deletions tests/idl4/default/test.idl
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,17 @@ struct Shape {
ColorNoDefaultLiteral color_no_default_literal;
ColorNoDefaultLiteralArray color_no_default_literal_array;
};

bitset MyBitset1 {
@default(true) bitfield<1> x;
bitfield<3> a;
@default(5) bitfield<12, short> d;
bitfield<33> tt;
};

bitset MyBitset2 : MyBitset1 {
bitfield<3> c;
bitfield<2>;
@default(3) bitfield<12, short> g;
bitfield<1> h;
};

0 comments on commit 65ca4a2

Please sign in to comment.