Skip to content

Commit

Permalink
Simplify const generation by using C++17 static inline constants
Browse files Browse the repository at this point in the history
    * ridlbe/c++11/writers/stubheader.rb:
    * ridlbe/c++11/writers/stubsource.rb:
    * tests/const/const.idl:
  • Loading branch information
jwillemsen committed Apr 12, 2024
1 parent 3aa39a8 commit cf66485
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 29 deletions.
15 changes: 5 additions & 10 deletions ridlbe/c++11/writers/stubheader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def visit_const(node)
if node.enclosure.is_a?(IDL::AST::Module)
printi("const #{node.idltype.cxx_type(node.enclosure)} ")
else
printi("static inline const #{node.idltype.cxx_type(node.enclosure)} {#{expression_to_s(node.expression, node.enclosure)}}")
printi("static inline const #{node.idltype.cxx_type(node.enclosure)} ")
end
else
if node.enclosure.is_a?(IDL::AST::Module)
Expand All @@ -279,25 +279,20 @@ def visit_const(node)
else
case node.idltype.resolved_type
when IDL::Type::Fixed
printi("static const #{node.idltype.cxx_type(node.enclosure)} ")
printi("static inline const #{node.idltype.cxx_type(node.enclosure)} ")
else
printi("static constexpr #{node.idltype.cxx_type(node.enclosure)} ")
end
end
end
if node.enclosure.is_a?(IDL::AST::Module) || ![Type::String, Type::WString].include?(node.expression.idltype.class)
if node.enclosure.is_a?(IDL::AST::Module)
if node.expression.is_a?(IDL::Expression::Value)
case node.idltype.resolved_type
when IDL::Type::Fixed
println("#{node.cxxname};")
else
println("#{node.cxxname} {#{node.idltype.resolved_type.value_to_s(node.value)}};")
end
println("#{node.cxxname} {#{node.idltype.resolved_type.value_to_s(node.value)}};")
else
println("#{node.cxxname} = #{expression_to_s(node.expression, node.enclosure)};")
end
else
println(node.cxxname + ';')
println("#{node.cxxname} {#{expression_to_s(node.expression, node.enclosure)}};")
end
end

Expand Down
18 changes: 0 additions & 18 deletions ridlbe/c++11/writers/stubsource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,6 @@ def post_visit(parser)
visitor(PostVisitor).visit
end

def visit_const(node)
unless node.enclosure.is_a?(IDL::AST::Module)
case node.idltype.resolved_type
when IDL::Type::Fixed
println
printiln('// generated from StubSourceWriter#visit_const')
printi("const #{node.idltype.cxx_type} ")
println(node.enclosure.cxxname + '::' + node.cxxname + ' {"' + expression_to_s(node.expression, node.enclosure) + '"};')
end
if [Type::String, Type::WString].include?(node.expression.idltype.class)
println
printiln('// generated from StubSourceWriter#visit_const')
printi("const #{node.idltype.cxx_type} ")
println(node.enclosure.cxxname + '::' + node.cxxname + ' {' + expression_to_s(node.expression, node.enclosure) + '};')
end
end
end

def enter_module(node)
super
println
Expand Down
16 changes: 15 additions & 1 deletion tests/const/const.idl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
* @copyright Copyright (c) Remedy IT Expertise BV
*/

/// Put the interfaces in a module, to avoid global namespace pollution
typedef fixed<10,3> fixed_type;
typedef fixed<20,3> large_type;
typedef fixed<10,2> second_fixed_type;
typedef fixed<7,6> pi_type;
const pi_type pi_double = 3.142857;

module Test
{
const char ch_val = 'a';
Expand Down Expand Up @@ -47,6 +52,9 @@ module Test
const long long_e12 = +long_e11;
const long long_e13 = -long_e12;
const long long_e14 = ~long_e13;

typedef fixed<7,6> pi_type;
const pi_type pi = 3.142857;
};

interface A
Expand All @@ -55,5 +63,11 @@ interface A

typedef string<5> bounded_string;
const bounded_string bstr = "12345";

const large_type large = 3.142857;
const pi_type pi_fixed = 3.142857;

typedef fixed<5,5> f_type;
const f_type five = 12345;
};

0 comments on commit cf66485

Please sign in to comment.