I am using a 3rd party library that is for the most part consists of POD structures. Even more, those structures are shared pointed by a special library pointer, build with a special library factory, so they come and go around in the form of Ptr<APod>
. Since PODs are by definition all public, this has led them to be modified all around the code base, scattering the code everywhere. I am trying to find a better approach. I would like to wrap them in order to make them become real classes, so that every operation regarding those data are forced to be inside the wrapper class:
我正在使用第三方库,其中大部分由POD结构组成。更重要的是,这些结构由一个特殊的库指针共享,由一个特殊的库工厂构建,因此它们以Ptr
class Wrapper
{
public:
Wrapper( /* params to correcly build APod */ );
/** required when it needs to be sent back to the library **/
const Ptr<APod>& unwrap() const;
private:
Ptr<APod> m_data;
};
But even if this is fine for most of the library PODs, it is a little bit tricky when PODs have fields that consist of other POD, or when there is a collection of PODs (the library also have its own Vector of Ptr to PODs). Another tricky thing is that since the library accepts back only const Ptr<APod>&
, which means constant pointer to non-constant object, my unwrap()
method is still allowing programmers to have too much freedom on these PODs. Can my approach be improved? Or is it entirely wrong, and should be replaced with another approach?
但即使这对于大多数库POD来说都没问题,当POD具有由其他POD组成的字段时,或者当存在POD集合时(该库也具有其自己的Ptr到POD的向量),这有点棘手。 。另一个棘手的问题是,由于库只接受const Ptr
1 个解决方案
#1
0
If some of the PODs can be included in other PODs or in array/containers, it means that you should never try to build the POD yourself, but just assume that you build your wrapper from a pointer to POD or in the case of your specific library a Ptr<APod>
.
如果某些POD可以包含在其他POD或数组/容器中,这意味着您不应该自己尝试构建POD,而只是假设您从指向POD的指针构建包装器,或者在您的特定情况下库a Ptr
Without knowing the semantics of Ptr
(unique/shared/weak) I cannot say whether you should then keep a copy of the original Ptr<APod>
or a reference to it.
在不知道Ptr(唯一/共享/弱)的语义的情况下,我不能说你是否应该保留原始Ptr
#1
0
If some of the PODs can be included in other PODs or in array/containers, it means that you should never try to build the POD yourself, but just assume that you build your wrapper from a pointer to POD or in the case of your specific library a Ptr<APod>
.
如果某些POD可以包含在其他POD或数组/容器中,这意味着您不应该自己尝试构建POD,而只是假设您从指向POD的指针构建包装器,或者在您的特定情况下库a Ptr
Without knowing the semantics of Ptr
(unique/shared/weak) I cannot say whether you should then keep a copy of the original Ptr<APod>
or a reference to it.
在不知道Ptr(唯一/共享/弱)的语义的情况下,我不能说你是否应该保留原始Ptr