进化版:一个C++模板工厂的编译问题的解决。针对第三方库的构造函数以及追加了的对象构造函数。牵扯到重载、特化等

原始版本在这里

一个C++模板工厂的编译问题的解决。针对第三方库的构造函数以及追加了的对象构造函数。牵扯到重载、特化等-CSDN博客

问题

1、关于类型的判断,适应性不强

比如template <typename T>IsFarElementId<>,目前只能判断FarElementId,如果推导出FarElementId&&并传给它,它就推导为false

2、如何去除重载函数

不能接收/判断所有的类型,比如接收ElementId的函数,就接收不了右值类型比如ElementId&&的函数,那么就得添加一个重载函数,用来接收ElementId&&。

相应的,还有其他组合,比如const std::wstring&等。它们内部的逻辑都是一样的,总不能给每个组合都添加对应的函数吧。

问题就变成了不管是ElementId&还是ElementId&&,想让它们都走一个函数。

第一个问题

先上最后的结果。

struct EEhFactory
{
    ///@code{.unparsed}
    ///此函数的功能:
    ///  创建EditElementHandle的指针对象
    ///  最泛化版本
    ///@endcode 
    ///@return   true:成功 false:失败
    ///@author Simon.Zou @date 2024/02/20
    template <typename ... Args>
    static std::shared_ptr<DgnPlatform::EditElementHandle>/*HCHXKERNEL::EditElementHandlePtr*/
        Create(Args&& ... args)
    {
        return std::make_shared<DgnPlatform::EditElementHandle>(std::forward<Args>(args)...);
    }

    ///@code{.unparsed}
    ///此函数的功能:
    ///  创建EditElementHandle的指针对象,并且Schedule写入XA数据:sortFlag
    ///  最泛化版本
    ///@endcode 
    ///@return   true:成功 false:失败
    ///@author Simon.Zou @date 2024/02/20
    template <typename ... Args>
    static std::shared_ptr<DgnPlatform::EditElementHandle>/*HCHXKERNEL::EditElementHandlePtr*/
        CreateWithSortFlag(
            UInt64 sortFlag, ///<IN uuid
            Args&& ... args  ///< parameters
        )
    {
        auto eeh = std::make_shared<EditElementHandle>(std::forward<Args>(args)...);
        HCHXKERNEL::SortEEh::SetFlagForSort(sortFlag, *eeh);

        return eeh;
    }

    template<typename T>
    struct _IsUInt64 : std::false_type
    {};

    template<>
    struct _IsUInt64<UInt64> : std::true_type
    {};

    template<class _Ty>
    struct IsUInt64
        : _IsUInt64<typename std::decay<_Ty>::type>
    {	// determine whether _Ty is UInt64
    };

    template<typename T>
    struct _IsElementId : std::false_type
    {};

    template<>
    struct _IsElementId<DgnPlatform::ElementId> : std::true_type
    {};

    template<class _Ty>
    struct IsElementId
        : _IsElementId<typename std::decay<_Ty>::type>
    {	// determine whether _Ty is IsElementId
    };

    template<typename T>
    struct _IsFarElementID : std::false_type
    {};

    template<>
    struct _IsFarElementID<DgnPlatform::DgnHistory::FarElementID> : std::true_type
    {};

    template<class _Ty>
    struct IsFarElementID
        : _IsFarElementID<typename std::decay<_Ty>::type>
    {	// determine whether _Ty is FarElementID
    };

    template<typename T>
    struct _IsWstring : std::false_type
    {};

    template<>
    struct _IsWstring<std::wstring> : std::true_type
    {};

    template<class _Ty>
    struct IsWstring
        : _IsWstring<typename std::decay<_Ty>::type>
    {	// determine whether _Ty is std::wstring
    };

    template<typename ParamType,
        std::enable_if<IsFarElementID<ParamType>::value>* = 0
    >
        static HCHXKERNEL::EditElementHandlePtr Create(ParamType&& farId) ///< 用FarElementId创建EditElementHandle
    {
        return MiscUtil::GetElementHandle(farId);
    }

    template <typename Param1st, typename Param2ndst, typename std::enable_if<IsElementId<Param1st>::value &&IsWstring<Param2ndst>::value, int>::type = 0>
    static HCHXKERNEL::EditElementHandlePtr Create(
        Param1st&& eid, ///<IN ElementId
        Param2ndst&& modelName ///< std::wstring model的名称
    )
    {
        DgnModelRefP modelRef = NULL;
        if (!MiscUtil::GetModelRefByName(&modelRef, modelName.c_str(), false, false))
            return NULL;

        //using eid_decayType = typename std::decay<Param1st>::type;
        //typename std::decay<Param1st>::type a;
        //TypeDisplayer<decltype(a)> aType;
        //return NULL;
        return EEhFactory::Create(eid, modelRef);
    }

    ///
    template<typename ParamType, std::enable_if<IsFarElementID<ParamType>::value>* = 0>
    static HCHXKERNEL::EditElementHandlePtr CreateWithSortFlag(UInt64 sortFlag, ParamType&& farId) ///< 用FarElementId创建EditElementHandle
    {
        HCHXKERNEL::EditElementHandlePtr eeh = MiscUtil::GetElementHandle(farId);
        if (eeh == NULL)
            return NULL;

        HCHXKERNEL::SortEEh::SetFlagForSort(sortFlag, *eeh);

        return eeh;
    }

    template <typename Param1st, typename Param2ndst, typename std::enable_if<IsElementId<Param1st>::value && IsWstring<Param2ndst>::value, int>::type = 0>
    static HCHXKERNEL::EditElementHandlePtr CreateWithSortFlag(
        UInt64 sortFlag, ///<IN uuid
        Param1st&& eid,///<IN ElementId
        Param2ndst&& modelName ///< std::wstring model的名称
    )
    {
        DgnModelRefP modelRef = NULL;
        if (!MiscUtil::GetModelRefByName(&modelRef, modelName.c_str(), false, false))
            return NULL;

        //using eid_decayType = typename std::decay<Param1st>::type;
        //typename std::decay<Param1st>::type a;
        //TypeDisplayer<decltype(a)> aType;
        //return NULL;
        auto eeh = EEhFactory::Create(eid, modelRef);
        HCHXKERNEL::SortEEh::SetFlagForSort(sortFlag, *eeh);

        return eeh;
    }
};


测试:
TEST(other, 2用EEHFactory的Create函数创建对象)
{
    if (EEhFactory::IsWstring<std::wstring&&>::value)
    {
        int i = 0;
        std::ignore = i;
    }

    if (EEhFactory::IsWstring<const std::wstring&>::value)
    {
        int i = 0;
        std::ignore = i;
    }

    if (EEhFactory::IsFarElementID<DgnPlatform::DgnHistory::FarElementID>::value)
    {
        int i = 0;
        std::ignore = i;
    }


    if (EEhFactory::IsFarElementID<DgnPlatform::DgnHistory::FarElementID&&>::value)
    {
        int i = 0;
        std::ignore = i;
    }

    if (EEhFactory::IsFarElementID<int>::value)
    {
        int i = 0;
        std::ignore = i;
    }


    if (1)
    {
        {
            DgnPlatform::ElementId eid0{ 0 };//, eid1{ 0 };
            std::wstring str{ L"" };
            auto editElementHandlePtr2 = EEhFactory::Create(eid0, str);
            auto editElementHandlePtr3 = EEhFactory::Create(eid0, std::wstring{ L"" }); //右值
        }

        {
            DgnPlatform::DgnHistory::FarElementID farId{ 0,0 };
            auto editElementHandlePtr0 = EEhFactory::Create(farId);
            auto editElementHandlePtr1 = EEhFactory::Create(DgnPlatform::DgnHistory::FarElementID{ 0,0 }); //右值
        }

        {
            ElementRefP elRef{ NULL };
            DgnModelRefP modelRef{ NULL };
            auto editElementHandlePtr1 = EEhFactory::Create(elRef, modelRef);
        }

        {
            DgnPlatform::ElementId eid0{ 0 };//, eid1{ 0 };
            DgnModelRefP modelRef = NULL;
            EEhFactory::Create(eid0, modelRef);
        }
    }

    if (1)
    {
        {
            UInt64 id = 1;
            DgnPlatform::ElementId eid0{ 0 };//, eid1{ 0 };
            std::wstring str{ L"" };
            auto editElementHandlePtr2 = EEhFactory::CreateWithSortFlag(id, eid0, str);
            auto editElementHandlePtr3 = EEhFactory::CreateWithSortFlag(id, eid0, std::wstring{ L"" });
        }

        {
            DgnPlatform::DgnHistory::FarElementID farId{ 0,0 };
            auto editElementHandlePtr0 = EEhFactory::CreateWithSortFlag(UInt64(1), farId);
            auto editElementHandlePtr1 = EEhFactory::CreateWithSortFlag(UInt64(1), DgnPlatform::DgnHistory::FarElementID{ 0,0 });
        }

        {
            ElementRefP elRef{ NULL };
            DgnModelRefP modelRef{ NULL };
            auto editElementHandlePtr1 = EEhFactory::CreateWithSortFlag(UInt64(1), elRef, modelRef);
        }

        {
            DgnPlatform::ElementId eid0{ 0 };//, eid1{ 0 };
            DgnModelRefP modelRef = NULL;
            EEhFactory::CreateWithSortFlag(UInt64(1), eid0, modelRef);
        }
    }

}

灵感来源

灵感来源,判断是否为浮点


template<typename T>
struct TypePrinter;

template<class T, class Enabled = void >
class A123;

template<class T>
class A123<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
public:
    A123() { std::cout << "partial specialization\r\n"; }
}; // specialization for floating point types


int main()
{
    A123<double> a;
}

注意,stl中std::is_floating_point的实现,学习了。

通过继承的方式,把decay过的最“干净”的类型传给_Is_floating_point进行判断。
是个不错的主意。要不然得学习别人的经验呢。


// STRUCT TEMPLATE _Is_floating_point
template<class _Ty>
	struct _Is_floating_point
		: false_type
	{	// determine whether _Ty is floating point
	};

template<>
	struct _Is_floating_point<float>
		: true_type
	{	// determine whether _Ty is floating point
	};

template<>
	struct _Is_floating_point<double>
		: true_type
	{	// determine whether _Ty is floating point
	};

template<>
	struct _Is_floating_point<long double>
		: true_type
	{	// determine whether _Ty is floating point
	};

	// STRUCT TEMPLATE is_floating_point
template<class _Ty>
	struct is_floating_point
		: _Is_floating_point<remove_cv_t<_Ty>>::type
	{	// determine whether _Ty is floating point
	};

template<class _Ty>
	_INLINE_VAR constexpr bool is_floating_point_v = is_floating_point<_Ty>::value;

简化一下,看其主体实现

	// STRUCT TEMPLATE _Is_floating_point
template<class _Ty>
	struct _Is_floating_point
		: false_type
	{	// determine whether _Ty is floating point
	};

template<>
	struct _Is_floating_point<float>
		: true_type
	{	// determine whether _Ty is floating point
	};

...

	// STRUCT TEMPLATE is_floating_point
template<class _Ty>
	struct is_floating_point
		: _Is_floating_point<remove_cv_t<_Ty>>::type
	{	// determine whether _Ty is floating point
	};

template<class _Ty>
	_INLINE_VAR constexpr bool is_floating_point_v = is_floating_point<_Ty>::value;

那么,我们自己的版本就好办了

编译,没问题。


    template<typename T>
    struct _IsFarElementID : std::false_type
    {};

    template<>
    struct _IsFarElementID<DgnPlatform::DgnHistory::FarElementID> : std::true_type
    {};

    template<class _Ty>
    struct IsFarElementID
        : _IsFarElementID<typename std::decay<_Ty>::type>
    {	// determine whether _Ty is FarElementID
    };


测试
    if (EEhFactory::IsFarElementID<DgnPlatform::DgnHistory::FarElementID>::value)
    {
        int i = 0;
        std::ignore = i;
    }


    if (EEhFactory::IsFarElementID<DgnPlatform::DgnHistory::FarElementID&&>::value)
    {
        int i = 0;
        std::ignore = i;
    }

    if (EEhFactory::IsFarElementID<int>::value)
    {
        int i = 0;
        std::ignore = i;
    }

第二个问题

现在能判断出来,不管是ElementId还是ElmentId&&,亦或是ElementId&,都是ElementId了。

但我希望用一个函数来接收这些参数。不想为他们添加多余的函数进行重载比如ElementId&是一个函数,ElementId&&用另一个函数,虽然内部可能都是转发到一个函数身上。

万能引用和完美转发-CSDN博客

在引入万能引用之前,我们写接收引用的函数需要如下代码:

void func(int& a) {
    std::cout << "left" << '\n';
}
 
void func(int&& a) {
    std::cout << "right" << '\n';
}

我们需要写两个函数,以便可以使用func(a)和func(1)。而万能引用的存在使得仅需写一个函数即可同时支持这两种写法。这也是为什么需要万能引用。

上面的函数进行以下写法即为万能引用:

template<typename T>
void func1(T&& a)
{
    
}

为了验证这个函数是否能够接收左值和右值我们可以测试一下: 

#include <iostream>
 
void func(int& a) {
    std::cout << "left" << '\n';
}
 
void func(int&& a) {
    std::cout << "right" << '\n';
}
 
template<typename T>
void func1(T&& a)
{
    func(std::forward<T>(a));
}
 
int main()
{
    int a = 3;
    func1(3);
    func1(a);
 
    return 0;
}

结果如下:

a4ae9806760b47f4b9e8597b6752df72.png

那么,思路就来了,挑一个接收FarElementId的吧

唯一的变化就是确保形参里是&&的形式:ParamType&& farId
它在模板里既可以接收左值,又可以接收右值

template<typename ParamType,
        std::enable_if<IsFarElementID<ParamType>::value>* = 0
    >
        static HCHXKERNEL::EditElementHandlePtr Create(ParamType&& farId) ///< 用FarElementId创建EditElementHandle
    {
        return MiscUtil::GetElementHandle(farId);
    }

测试代码:
{
DgnPlatform::DgnHistory::FarElementID farId{ 0,0 };
auto editElementHandlePtr0 = EEhFactory::Create(farId);
auto editElementHandlePtr1 = EEhFactory::Create(DgnPlatform::DgnHistory::FarElementID{ 0,0 }); //右值
}

编译通过。这个函数

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值