Skip to content

Commit

Permalink
Merge pull request #311 from jwillemsen/jwi-bitsetclass
Browse files Browse the repository at this point in the history
Generate a class for a bitset
  • Loading branch information
jwillemsen authored Aug 14, 2023
2 parents 4583344 + 15b71de commit 747c0ae
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 5 deletions.
29 changes: 24 additions & 5 deletions ridlbe/c++11/templates/cli/hdr/bitset.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@

// generated from <%= ridl_template_path %>
/// @copydoc <%= doc_scoped_name %>
/// THIS MAPPING IS NOT COMPLETE
struct <%= cxxname %>
/// @todo THIS MAPPING IS NOT COMPLETE
class <%= cxxname %><% unless base.nil? %> : public <%= base.cxxname %><% end %>
{
%bitfields.each do |_bitfield|
/// @copydoc #{doc_scoped_name}::#{_bitfield.name}
<%= _bitfield.cxx_member_type %> <%= _bitfield.cxxname %> : <%= _bitfield.bits %>;
public:
<%= cxxname %> () = default;
~<%= cxxname %> () noexcept = default;
<%= cxxname %> (const <%= cxxname %>&) = default;
<%= cxxname %> (<%= cxxname %>&&) = default;
<%= cxxname %>& operator= (const <%= cxxname %>&) = default;
<%= cxxname %>& operator= (<%= cxxname %>&&) = default;

%bitfields.each do |_m|
% unless _m.cxxname.empty?
inline void <%= _m.cxxname %> (<%= _m.cxx_in_type %> _x11_<%= _m.cxxname %>) { this->_taox11_<%= cxxname.downcase %>.<%= _m.cxxname %> = _x11_<%= _m.cxxname %>; }
inline <%= _m.cxx_in_type %> <%= _m.cxxname %> () const { return this->_taox11_<%= cxxname.downcase %>.<%= _m.cxxname %>; }
% end
%end

private:
struct
{
%bitfields.each do |bitfield|
/// @copydoc #{bitfield.doc_scoped_name}::#{bitfield.name}
<%= bitfield.cxx_member_type %> <%= bitfield.cxxname %> : <%= bitfield.bits %>;
%end
} _taox11_<%= cxxname.downcase %>;
}; // <%= cxxname %>

% visit_template('typecode') if generate_typecode_support?
47 changes: 47 additions & 0 deletions ridlbe/c++11/templates/cli/hdr/bitset_idl_traits.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

%# TODO handle inheritance
// generated from <%= ridl_template_path %>
template<>
struct traits <<%= scoped_cxxtype %>>
: IDL::common_byval_traits<<%= scoped_cxxtype %>>
{
template <typename OStrm_, typename Formatter = formatter<value_type, OStrm_>>
static inline OStrm_& write_on(OStrm_& os_, in_type val_, Formatter fmt_ = Formatter ())
{
return fmt_ (os_, val_);
}

template <typename Formatter = std::false_type>
static inline __Writer<Formatter> write (in_type val) { return {val}; }
};

template <typename OStrm_>
struct formatter<<%= scoped_cxxtype %>, OStrm_>
{
inline OStrm_& operator ()(OStrm_& os_, <%= scoped_cxx_in_type %> val_)
{
%_sep = nil
os_ << "<%= formatted_cxxname %>{"
%bitfields.each do |_m|
% unless _m.cxxname.empty?
<< "<%= _sep %><%= _m.cxxname %>=" << IDL::traits<<%= _m.scoped_cxxtype %>>::write(val_.<%= _m.cxxname %> ())
% _sep = ',' unless _sep
% end
%end
<< '}';
return os_;
}
};

template <typename OStrm_, typename Fmt>
inline OStrm_& operator <<(OStrm_& os, IDL::traits<<%= scoped_cxxtype %>>::__Writer<Fmt> w)
{
using writer_t = IDL::traits<<%= scoped_cxxtype %>>::__Writer<Fmt>;
using formatter_t = typename std::conditional<
std::is_same<
typename writer_t::formatter_t,
std::false_type>::value,
formatter<<%= scoped_cxxtype %>, OStrm_>,
typename writer_t::formatter_t>::type;
return IDL::traits<<%= scoped_cxxtype %>>::write_on (os, w.val_, formatter_t ());
}
5 changes: 5 additions & 0 deletions ridlbe/c++11/visitors/bitset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ def bitfields
}
end

# Return the base of this bitset, nil in case of no base bitset
def base
node.base
end

# template mapping

map_template :bitset, :bitset
Expand Down
4 changes: 4 additions & 0 deletions ridlbe/c++11/writers/stubheader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@ def visit_bitmask(node)
visitor(BitmaskVisitor).visit_idl_traits(node)
end

def visit_bitset(node)
visitor(BitsetVisitor).visit_idl_traits(node)
end

def declare_struct(node)
visitor(StructVisitor).visit_idl_traits(node)
end
Expand Down
16 changes: 16 additions & 0 deletions tests/idl4/illegal_idl/bitset_inherit_struct.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @file bitset_inherit_struct.idl
* @author Johnny Willemsen
*
* ridlc shall reject use of a bitset which inherits from a struct
*
* @copyright Copyright (c) Remedy IT Expertise BV
*/

struct Base {
short Foo;
};

bitset MyBitset : Base {
bitfield<6> x;
};
20 changes: 20 additions & 0 deletions tests/idl4/illegal_idl/bitset_multiple_inheritance.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @file bitset_multiple_inheritance.idl
* @author Johnny Willemsen
*
* ridlc shall reject use of a bitset which inherits from multiple bases
*
* @copyright Copyright (c) Remedy IT Expertise BV
*/

bitset Base1 {
bitfield<1> y;
};

bitset Base2 {
bitfield<1> z;
};

bitset MyBitset : Base1, Base2 {
bitfield<6> x;
};

0 comments on commit 747c0ae

Please sign in to comment.