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

More bitmask support code #305

Merged
merged 1 commit into from
Aug 7, 2023
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
4 changes: 2 additions & 2 deletions ridlbe/c++11/config/cxx_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ def is_pod?
end
end

class BitMask
class Bitmask
include IdlType_Mixin
def cxx_type(scope = nil)
(scope && (scope == node || scope == node.enclosure)) ? node.cxxname : ('::' + node.scoped_cxxname)
Expand All @@ -860,7 +860,7 @@ def is_pod?
end
end

class BitSet
class Bitset
include IdlType_Mixin
def cxx_type(scope = nil)
(scope && (scope == node || scope == node.enclosure)) ? node.cxxname : ('::' + node.scoped_cxxname)
Expand Down
13 changes: 10 additions & 3 deletions ridlbe/c++11/templates/cli/hdr/bitmask.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@

// generated from <%= ridl_template_path %>
/// @copydoc <%= doc_scoped_name %>
enum <%= cxxname %>Bit : <%= bitbound.cxx_type %>
enum class <%= cxxname %> : <%= bitbound.cxx_type %>
{
<%= bitvalues.collect {|e| "/// @copydoc #{doc_scoped_name}::#{e.name}\n #{e.cxxname} = #{e.value}" }.join(",\n ") %>
};// <%= cxxname %>Bit
using <%= cxxname %> = <%= bitbound.cxx_type %>;
};// <%= cxxname %>

inline <%= cxx_return_type %> operator~ (<%= cxx_in_type %> _taox11_t) { return static_cast<<%= cxxname %>>(~static_cast<<%= bitbound.cxx_type %>>(_taox11_t)); }
inline <%= cxx_return_type %> operator| (<%= cxx_in_type %> _taox11_t, <%= cxx_in_type %> _taox11_y) { return static_cast<<%= cxxname %>>(static_cast<<%= bitbound.cxx_type %>>(_taox11_t) | static_cast<<%= bitbound.cxx_type %>>(_taox11_y)); }
inline <%= cxx_return_type %> operator^ (<%= cxx_in_type %> _taox11_t, <%= cxx_in_type %> _taox11_y) { return static_cast<<%= cxxname %>>(static_cast<<%= bitbound.cxx_type %>>(_taox11_t) ^ static_cast<<%= bitbound.cxx_type %>>(_taox11_y)); }
inline <%= cxx_return_type %> operator& (<%= cxx_in_type %> _taox11_t, <%= cxx_in_type %> _taox11_y) { return static_cast<<%= cxxname %>>(static_cast<<%= bitbound.cxx_type %>>(_taox11_t) & static_cast<<%= bitbound.cxx_type %>>(_taox11_y)); }
inline <%= cxx_return_type %> operator |= (<%= cxx_inout_type %> _taox11_lhs, <%= cxx_in_type %> _taox11_rhs) { _taox11_lhs = _taox11_lhs | _taox11_rhs; return _taox11_lhs; }
inline <%= cxx_return_type %> operator &= (<%= cxx_inout_type %> _taox11_lhs, <%= cxx_in_type %> _taox11_rhs) { _taox11_lhs = _taox11_lhs & _taox11_rhs; return _taox11_lhs; }
inline <%= cxx_return_type %> operator ^= (<%= cxx_inout_type %> _taox11_lhs, <%= cxx_in_type %> _taox11_rhs) { _taox11_lhs = _taox11_lhs ^ _taox11_rhs; return _taox11_lhs; }

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

// generated from <%= ridl_template_path %>
template<>
struct traits <<%= scoped_cxxtype %>>
: IDL::common_byval_traits<<%= scoped_cxxtype %>>
{
/// Underlying type of the bitmask
using underlying_type = <%= bitbound.cxx_type %>;

/// bit_bound
using bit_bound = std::integral_constant<uint32_t, <%= bitbound_bits %>>;

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_)
{
% # TODO
switch (val_)
{
% bitvalues.each do |e|

// case <%= scoped_cxxtype %>::<%=e.cxxname %>: os_ << "<%= formatted_cxxname %>::<%=e.cxxname %>"; break;
%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 ());
}
2 changes: 1 addition & 1 deletion ridlbe/c++11/visitorbase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ def is_stdlib_type?
true
when IDL::Type::Object, IDL::Type::Any, IDL::Type::Valuetype
true
when IDL::Type::Enum
when IDL::Type::Enum, IDL::Type::BitMask
false
else
self._idltype.is_pod?
Expand Down
4 changes: 2 additions & 2 deletions ridlbe/c++11/visitors/bitmask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

module IDL
module Cxx11
class BitMaskVisitor < NodeVisitorBase
class BitmaskVisitor < NodeVisitorBase
def bitvalues
@bitvalues ||= node.bitvalues.collect { |en|
(ev = visitor(BitValueVisitor)).visit(en)
Expand All @@ -28,7 +28,7 @@ def bitbound_bits

map_template :bitmask, :bitmask
map_template :typecode, :typecode
map_template :tao_typecode, :enum_typecode
map_template :tao_typecode, :bitmask_typecode
end

class BitValueVisitor < NodeVisitorBase
Expand Down
4 changes: 2 additions & 2 deletions ridlbe/c++11/visitors/bitset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

module IDL
module Cxx11
class BitSetVisitor < NodeVisitorBase
class BitsetVisitor < NodeVisitorBase
def bitfields
@bitfields ||= node.bitfields.collect { |en|
(ev = visitor(BitFieldVisitor)).visit(en)
Expand All @@ -21,7 +21,7 @@ def bitfields

map_template :bitset, :bitset
map_template :typecode, :typecode
map_template :tao_typecode, :enum_typecode
map_template :tao_typecode, :bitset_typecode
end

class BitFieldVisitor < NodeVisitorBase
Expand Down
8 changes: 6 additions & 2 deletions ridlbe/c++11/writers/stubheader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,11 @@ def visit_enum(node)
end

def visit_bitmask(node)
visitor(BitMaskVisitor).visit_bitmask(node)
visitor(BitmaskVisitor).visit_bitmask(node)
end

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

def visit_typedef(node)
Expand Down Expand Up @@ -514,6 +514,10 @@ def visit_enum(node)
visitor(EnumVisitor).visit_idl_traits(node)
end

def visit_bitmask(node)
visitor(BitmaskVisitor).visit_idl_traits(node)
end

def declare_struct(node)
visitor(StructVisitor).visit_idl_traits(node)
end
Expand Down
25 changes: 19 additions & 6 deletions tests/idl4/bitmask/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,32 @@ int main (int /*argc*/, char* /*argv*/[])
{
// Just compilation test

MyBitMask my_bitmask;
MyBitMaskBit my_bitmaskbit;
X11_UNUSED_ARG(my_bitmask);
X11_UNUSED_ARG(my_bitmaskbit);
MyBitMask my_bitmask = MyBitMask::flag0;

if (!std::is_same<MyBitMaskBound8, uint8_t>::value)
if (static_cast<bool>(my_bitmask & MyBitMask::flag0))
{
TAOX11_TEST_INFO << "Flag my_bitmask correctly set" << std::endl;
}
else
{
TAOX11_TEST_ERROR << "Flag my_bitmask NOT set" << std::endl;
}
if (!std::is_same<std::underlying_type_t<MyBitMaskBound8>, uint8_t>::value)
{
TAOX11_TEST_ERROR << "Type of MyBitMaskBound8 is not uint8_t" << std::endl;
}
if (!std::is_same<MyBitMaskBound16, uint16_t>::value)
if (!std::is_same<std::underlying_type_t<MyBitMaskBound16>, uint16_t>::value)
{
TAOX11_TEST_ERROR << "Type of MyBitMaskBound16 is not uint16_t" << std::endl;
}
if (!std::is_same<std::underlying_type_t<MyBitMaskBound32>, uint32_t>::value)
{
TAOX11_TEST_ERROR << "Type of MyBitMaskBound32 is not uint32_t" << std::endl;
}
if (!std::is_same<std::underlying_type_t<MyBitMaskBound64>, uint64_t>::value)
{
TAOX11_TEST_ERROR << "Type of MyBitMaskBound64 is not uint64_t" << std::endl;
}

return 0;
}
14 changes: 13 additions & 1 deletion tests/idl4/bitmask/test.idl
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ bitmask MyBitMaskBound16 {
flag16_3
};

// https://softwareengineering.stackexchange.com/questions/194412/using-scoped-enums-for-bit-flags-in-c
@bit_bound(32)
bitmask MyBitMaskBound32 {
flag32_1,
flag32_2,
flag32_3
};

@bit_bound(64)
bitmask MyBitMaskBound64 {
flag64_1,
flag64_2,
flag64_3
};
Loading