Skip to content

Commit

Permalink
Support for C++ enum class (#112)
Browse files Browse the repository at this point in the history
* Refs #20063. Support C++ enum class

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

* Refs #20063. Fix bitmasks

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

* Refs #20063. Accessing through Context

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

---------

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>
  • Loading branch information
richiware committed Mar 13, 2024
1 parent f7dfc11 commit bd41748
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 22 deletions.
21 changes: 19 additions & 2 deletions src/main/java/com/eprosima/idl/context/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@

public class Context
{
/*!
* Built-in custom property to specify a TemplateGroup is using explicitly modules (in C++ when opening the
* namespace "namespace module {")
*/
public static final String using_explicitly_modules_custom_property = "using_explicitly_modules";

public Context(
TemplateManager tmanager,
String file,
Expand Down Expand Up @@ -1415,9 +1421,20 @@ public boolean isGenerateTypesC()
return generate_typesc_;
}

public TemplateManager get_template_manager()
/*!
* @brief Checks a custom property was enables for a TemplateGroup.
*
* There are built-in custom properties:
* - Context.using_explicitly_modules_custom_property: specifies a TemplateGroup is using explicitly modules.
*/
public boolean is_enabled_custom_property_in_current_group(String custom_property)
{
return tmanager_;
if (null != tmanager_)
{
return tmanager_.is_enabled_custom_property_in_current_group(custom_property);
}

return false;
}

// OS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

package com.eprosima.idl.generator.manager;

import com.eprosima.idl.generator.manager.TemplateGroup;

import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
Expand Down Expand Up @@ -88,8 +86,13 @@ public void set_current_template_stgroup(TemplateSTGroup template_stgroup)
current_template_stgroup_ = template_stgroup;
}

public TemplateSTGroup get_current_template_stgroup()
public boolean is_enabled_custom_property_in_current_group(String custom_property)
{
return current_template_stgroup_;
if (null != current_template_stgroup_)
{
return current_template_stgroup_.is_enabled_custom_property(custom_property);
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.eprosima.idl.generator.manager;

import org.stringtemplate.v4.*;
import java.util.ArrayList;

public class TemplateSTGroup
{
Expand All @@ -34,24 +35,23 @@ public STGroup get_stgroup()
}

/*!
* @brief Returns if the current `Context` scope will be remove from the scoped name of types (returned by
* `getScopedname()`).
* @brief Checks a custom property was added in this template group.
*/
public boolean is_enabled_using_explicitly_modules()
public boolean is_enabled_custom_property(String custom_property)
{
return using_explicitly_modules_;
return custom_properties.contains(custom_property);
}

/*!
* @brief Enable removing the current `Context` scope from the scoped name of types (returned by `getScopedname()`).
* @brief Enables a custom property.
*/
public void enable_using_explicitly_modules()
public void enable_custom_property(String custom_property)
{
using_explicitly_modules_ = true;
custom_properties.add(custom_property);
}

private STGroup st_group_ = null;

//! If set, the scoped name of types (returned by `getScopedname()`) will remove the current `Context` scope.
private boolean using_explicitly_modules_ = false;
//! List of custom properties added by the user.
ArrayList<String> custom_properties = new ArrayList<String>();
};
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ else if(dist_type.getKind() == Kind.KIND_ENUM)

if(enum_members.size() > 0)
{
union_type.setDefaultvalue(enume.getScope() + "::" + enum_members.get(0).getName());
union_type.setDefaultvalue(enume.getScopedname() + "::" + enum_members.get(0).getName());
union_type.setJavaDefaultvalue(enume.javapackage + enume.getJavaScopedname() + "." + enum_members.get(0).getName());
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,16 @@ public String getName()
return m_name;
}

/*!
* @brief Returns the full scoped name of the type, unless the developer uses
* `TemplateSTGroup.enable_custom_proeprty("using_explicitly_modules")`, by removing from the full scoped name the
* current `Context` scope.
*/
public String getScopedname()
{
String scoped_name = getFullScopedname();

if (!ctx.get_template_manager().get_current_template_stgroup().is_enabled_using_explicitly_modules())
if (!ctx.is_enabled_custom_property_in_current_group(ctx.using_explicitly_modules_custom_property))
{
return scoped_name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public String getInitialValue()
{
if (getMembers().size() > 0)
{
return (getScope() != null ? getScope() + "::" : "") + getMembers().get(0).getName();
return getScopedname() + "::" + getMembers().get(0).getName();
}

return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public String getName()

/*!
* @brief Returns the full scoped name of the type, unless the developer uses
* `TemplateSTGroup.enable_using_explicitly_modules()`, by removing from the full scoped name the current
* `Context` scope.
* `TemplateSTGroup.enable_custom_proeprty("using_explicitly_modules")`, by removing from the full scoped name the
* current `Context` scope.
*/
public String getScopedname()
{
String scoped_name = getFullScopedname();

if (!ctx.get_template_manager().get_current_template_stgroup().is_enabled_using_explicitly_modules())
if (!ctx.is_enabled_custom_property_in_current_group(ctx.using_explicitly_modules_custom_property))
{
return scoped_name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public int addMember(

for (int count = 0; count < internal_labels.size(); ++count)
{
labels.add(enum_type.getScope() + "::" + internal_labels.get(count));
labels.add((Kind.KIND_ENUM == discriminator_.getTypecode().getKind() ?
enum_type.getScopedname() + "::" : "")+ internal_labels.get(count));
javalabels.add(javapackage + enum_type.getJavaScopedname() + "." + internal_labels.get(count));
}
}
Expand Down

0 comments on commit bd41748

Please sign in to comment.