如何使用元组修复多个关联关系

How to fix multiple association relashionships using a tuple?

本文关键字:关联 关系 何使用 元组      更新时间:2023-10-16

我做了一个简单的系统来处理关联(我做了一对一关联和一对多关联,但这里我将重点关注一对一的情况)。它是工作,因为我希望,如果一个对象只有一种类型的关联,但我需要处理几种类型的关联(即。A类型的对象a0与B类型的对象b0和C类型的对象c0相关联)。我试图通过将关联类封装在元组中来做到这一点(如果有更好/更简单的方法来做到这一点,请告诉我),但我现在有一个类型问题。下面是我当前的代码(有些模板现在没有使用,比如ind1,但我以后可能需要它):

template <typename A1, typename A2, size_t ind1, size_t ind2>
class Association
{
public:
    virtual ~Association()
    {
        if (!this->empty())
        {
            this->clear_associations();
        }
    }
    void associate(A2* ref)
    {
        if (!this->empty() && _ref == ref)
        {
            return;
        }
        if (_ref)
        {
            std::get<ind2>(*_ref).reset_association();
        }
        _ref = ref;
        std::get<ind2>(*ref).associate(static_cast<A1*>(this));
    };
    void associate(A2& ref)
    {
        this->associate(&ref);
    };
    bool empty() const
    {
        if (!_ref)
            return true;
        else
            return false;
    }
    void remove_association(A2* ref)
    {
        if (_ref == ref)
        {
            this->reset_association();
            std::get<ind2>(ref)->remove_association(static_cast<A1*>(this));
        }
    }
    void remove_association(A2& ref)
    {
        this->remove_association(&ref);
    }
    void reset_association()
    {
        _ref = 0;
    }
    void clear_associations()
    {
        if (_ref)
        {
            std::get<ind2>(_ref)->remove_association(static_cast<A1*>(this));
        }
        this->reset_association();
    }
    A2* get_association() const
    {
        return _ref;
    }
private:
    A2* _ref=0;
};
template <typename... T>
class Relations : public std::tuple<T...>
{
public:
    Relations();
    virtual ~Relations();
};
class J;
class K;
class I : public Relations<Association<I, J, 0, 0>, Association<I, K, 1, 0>>
{
public:
    std::string type="I";
};
class J : public Relations<Association<J, I, 0, 0>>
{
public:
    std::string type="J";
};
class K : public Relations<Association<K, I, 0, 1>>
{
public:
    std::string type="K";
};
int main()
{
    I i;
    J j;
    K k;
    std::get<0>(i).associate(j);
    return 0;
}

这里的问题是,当我尝试做std::get(*ref).associate(static_cast(this));A1是类型I,而this是类型Association,由于元组的关系不能直接强制转换。请问做这件事的好方法是什么?

您可以为Relation创建自己的get:

namespace std
{
    template <std::size_t I, typename... Ts>
    auto get(Relations<Ts...>& r)
    -> typename std::tuple_element<I, std::tuple<Ts...>>::type&
    {
        return static_cast<typename std::tuple_element<I, std::tuple<Ts...>>::type&>(r);
    }
}

现场演示。