Reputation: 13135
I'm wrapping a C api which has the following struct:
typedef struct Value
{
union
{
int i;
const char* s;
bool b;
struct StructureMember_T* firstStructureMember
double d;
} value;
} Value_T;
I use a setter function to set values in this struct from C++.
class ValueSetter : public IValueSetter
{
public:
explicit ValueSetter(Value_T* value) : _value(value)
{
}
void operator()(int8_t v) const
{
_value->value.i = v;
}
...
void operator()(std::string& s) const
{
_value->value.s = s.c_str();
}
void operator()(Structure& s) const
{
_value->value.firstStructureMember = s.getFirstMember();
}
private:
Value_T* _value;
};
To deal with StructureMember_T*, I added the following class which I'm not happy with. For example, its interface is dependant on StructureMember_T from the c api. Any suggestion regarding another approach? I will be adding more functionality to the Structure class at a later date.
class Structure
{
public:
explicit Structure(Value_T* firstMember)
: _firstMember(firstMember)
{
}
StructureMember_T* getFirstMember()
{
return _firstMember;
}
void setFirstMember(StructureMember_T* firstMember)
{
_firstMember = firstMember;
}
private:
StructureMember_T* _firstMember;
};
Upvotes: 0
Views: 53
Reputation: 1699
Given the goal you have, I don't really see a problem with having a setter and a getter in the C++ API.
If you really wanted to hide that, I would suggest to simply make Value a friend of Structure and have the get/set methods as private.
Upvotes: 1