Is there a way to use existing c++ interfaces wrapped in differing namespaces in Stateflow without redefining them to C-Style enums?
1 次查看(过去 30 天)
显示 更早的评论
Problem Description:
Given a large project with interfaces defined in the form:
namespace ns1
{
class MyClass
{
public:
enum myEnum : int8
{
FOO = 0,
BAR = 1,
FOOBAR = 2
};
myEnum m_myEnum;
}
}
namespace ns2
{
class MyOtherClass
{
enum myOtherEnum : int8
{
FOO = 0,
BAR = 1,
FOOBAR = 2
};
myOtherEnum m_myOtherEnum;
}
}
Within C++, the value of the first enum could be accessed using ns1::MyClass::myEnum. In order to write on variables using these interfaces from a Stateflow generated C-File, I only came up with one solution so far: to have the Code Generator redefine both interfaces in C-Style, based on the values entered within Stateflow:
typedef enum
{
NS2_MYCLASS_MY_ENUM_FOO = 0,
NS2_MYCLASS_MY_ENUM_BAR = 1,
NS2_MYCLASS_MY_ENUM_FOOBAR = 2,
} MY_ENUM
typedef enum
{
NS2_MYOTHERCLASS_MY_OTHER_ENUM_FOO = 0,
NS2_MYOTHERCLASS_MY_OTHER_ENUM_BAR = 1,
NS2_MYOTHERCLASS_MY_OTHER_ENUM_FOOBAR = 2,
} MY_OTHER_ENUM
To get these values back on variables, defined in the form of the original enums, there have to be static casts implemented like e.g.
instanceOfMyClass.m_myEnum = static_cast<ns1::MyClass::myEnum>(MY_ENUM)
instanceOfMyOtherClass.m_myOtherEnum = static_cast<ns2::MyOtherClass::myOtherEnum>(MY_OTHER_ENUM)
The disadvantages are:
- redefinition is extra work, prone to errors and a lot of work to keep in sync
- static casting is dangerous, especially, when e.g. the order of the values within the enumeration changes
- the principle of single source of truth is violated which leads to these disadvantages
Question:
- Is there a way to directly use the original, C++-Style enums without recreating them and having all the stated disadvantages?
- I'm thinking about a solution where I can enter the names and paths of the original interfaces directly within StateFlow and generating C++ code that is therefore already integrated - without any wrapping and extra, error prone work.
0 个评论
回答(1 个)
Jafar Gholamzadeh
2024-6-6
there is another question and answer in this link , and it seems there is no way to directly using c++ constructs in charts
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!