私は、さまざまなタイプ (プロパティ、親、子など) で構成されるモデルに反対しています。各タイプは、ac api の一連の関数に関連付けられています。例えば:
Type "Properties":
char* getName(PropertiesHandle);
char* getDescription(PropertiesHandle);
Type "Parent"
PropertiesHandle getProperties(ParentHandle);
ChildHanlde getFirstChild(ParentHandle);
Type "Child"
PropertiesHandle getProperties(ChildHandle);
ParentHanlde getParent(ChildHandle);
ChildHandle getNextChild(ChildHandle);
次のように、この c api ライブラリの一連の C++ インターフェイスを作成しました。
class IProperties
{
public:
virtual std::string getName() = 0;
virtual std::string getDescription() = 0;
};
class IParent
{
public:
virtual std::shared_ptr<IProperties> getProperties() = 0;
virtual std::shared_ptr<IChild> getFirstChild() = 0;
};
class IChild
{
public:
virtual std::shared_ptr<IProperties> getProperties() = 0;
virtual std::shared_ptr<IParent> getParent() = 0;
virtual std::shared_ptr<IChild> getNextChild() = 0;
};
次に、Properties、Parent、および Child クラスを介してこれらのインターフェイスを実装します。
したがって、Child インスタンスはコンストラクターを介して特定の ChildHandle を取得し、その getParent 関数は次のようになります。
std::shared_ptr<IParent> getParent()
{
// get the parent handle and wrap it in a Parent object
return std::shared_ptr<IParent>(new Parent(_c_api->getParent(_handle)));
}
あなたの意見では、ここで shared_ptr を返すことは合理的ですか。私は std::unique_ptr を使用できません。Google Mock では、モック化されたメソッドのパラメーターと戻り値をコピー可能にする必要があるためです。Google Mock を使用して、テストでこれらのインターフェイスをモックしています。
循環参照の可能性を提示する可能性がある将来、物事がどのように最適化されるかについても考えています。これは、キャッシングを使用して C API への複数回の呼び出しを回避する (たとえば、子がその親を複数回確立する必要がない) 場合に、親を取得する Child コンストラクターと組み合わせて使用する場合に発生する可能性があります。これには、インターフェイスと多くのコードを変更するweak_ptrsの使用が必要になります...