V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
amiwrong123
V2EX  ›  程序员

[请教] android::hardware::Return 无法使用拷贝构造函数,怎么办?

  •  
  •   amiwrong123 · 2019-10-24 18:32:00 +08:00 · 2405 次点击
    这是一个创建于 1882 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在编写 c++的单体测试,但是在用 mock 的时候出了点问题。

    //在 mock 类里面,函数已经被 mock 了
    MOCK_METHOD1(unRegisterListener, Return<EnCommonFuncResult>(EnCommonListenerID));
    
    //unRegisterListener 的函数签名是
    virtual Return<EnMeterFuncResult> unRegisterListener(EnMeterListenerID serviceId)//这个 Return 是 android::hardware::Return
    
    //现在想进行插桩,EnCommonFuncResult 就是一个 enum class EnCommonFuncResult : uint8_t
    Return<EnCommonFuncResult> returnOk= EnCommonFuncResult::EN_COMMON_RESULT_OK;//这个 Return 是 android::hardware::Return
    EXPECT_CALL(mockICommon, unRegisterListener(_)).WillOnce(::testing::Return(returnOk));//这儿出错
    
    //以下为报错信息
    error: call to implicitly-deleted copy constructor of 'android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>'
    
    system/libhidl/base/include/hidl/Status.h:200:5: note: copy constructor is implicitly deleted because 'Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>' has a user-declared move constructor
        Return(Return &&other) = default;
    

    抱歉 c++有点菜,实在没搞懂这个 android::hardware::Return 该怎么用

    第 1 条附言  ·  2019-10-24 21:20:15 +08:00
    ```
    改成用这个,也报错:
    EXPECT_CALL(mockICommon, unRegisterListener(_)).WillOnce(::testing::Return(EnCommonFuncResult::EN_COMMON_RESULT_OK));

    以下是报错信息:
    error: call to implicitly-deleted copy constructor of 'testing::internal::ReturnAction<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>::Impl<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult, android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult> (iauto::hardware::ucom::common::V1_0::EnCommonListenerID)>::Result' (aka 'android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>')
    virtual Result Perform(const ArgumentTuple&) { return value_; }
    ^~~~~~
    external/googletest/googlemock/include/gmock/gmock-actions.h:575:14: note: in instantiation of member function 'testing::internal::ReturnAction<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>::Impl<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult, android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult> (iauto::hardware::ucom::common::V1_0::EnCommonListenerID)>::Perform' requested here
    explicit Impl(const linked_ptr<R>& value)
    ^
    external/googletest/googlemock/include/gmock/gmock-actions.h:557:26: note: in instantiation of member function 'testing::internal::ReturnAction<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>::Impl<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult, android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult> (iauto::hardware::ucom::common::V1_0::EnCommonListenerID)>::Impl' requested here
    return Action<F>(new Impl<R, F>(value_));
    ^


    ```
    amiwrong123
        1
    amiwrong123  
    OP
       2019-10-24 21:40:08 +08:00
    ```cpp
    template<typename T> class Return : public details::return_status {
    private:
    T mVal {};
    public:
    Return(T v) : details::return_status(), mVal{v} {}
    Return(Status s) : details::return_status(s) {}

    // move-able.
    // precondition: "this" has checked status
    // postcondition: other is safe to destroy after moving to *this.
    Return(Return &&other) = default;
    Return &operator=(Return &&) = default;

    ~Return() = default;

    operator T() const {
    assertOk();
    return mVal;
    }

    T withDefault(T t) {
    return isOk() ? mVal : t;
    }
    };
    ```
    这是 android::hardware::Return 的源码,因为有这句 Return(Return &&other) = default;,我就不能调用拷贝构造函数了,因为被隐式删除了。

    求大佬解答了
    amiwrong123
        2
    amiwrong123  
    OP
       2019-10-24 22:19:20 +08:00
    error: call to implicitly-deleted copy constructor of 'testing::internal::ReturnAction<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>::Impl<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult, android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult> (iauto::hardware::ucom::common::V1_0::EnCommonListenerID)>::Result' (aka 'android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>')
    virtual Result Perform(const ArgumentTuple&) { return value_; }
    ^~~~~~
    external/googletest/googlemock/include/gmock/gmock-actions.h:575:14: note: in instantiation of member function 'testing::internal::ReturnAction<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>::Impl<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult, android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult> (iauto::hardware::ucom::common::V1_0::EnCommonListenerID)>::Perform' requested here
    explicit Impl(const linked_ptr<R>& value)
    ^
    external/googletest/googlemock/include/gmock/gmock-actions.h:557:26: note: in instantiation of member function 'testing::internal::ReturnAction<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>::Impl<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult, android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult> (iauto::hardware::ucom::common::V1_0::EnCommonListenerID)>::Impl' requested here
    return Action<F>(new Impl<R, F>(value_));
    ^
    TestSender.cpp:54:66: note: in instantiation of function template specialization 'testing::internal::ReturnAction<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>::operator Action<android::hardware::Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult> (iauto::hardware::ucom::common::V1_0::EnCommonListenerID)>' requested here
    EXPECT_CALL(mockICommon, unRegisterListener(_)).WillOnce(::testing::Return(EnCommonFuncResult::EN_COMMON_RESULT_OK));
    ^
    system/libhidl/base/include/hidl/Status.h:200:5: note: copy constructor is implicitly deleted because 'Return<iauto::hardware::ucom::common::V1_0::EnCommonFuncResult>' has a user-declared move constructor
    Return(Return &&other) = default;

    给一下附言 1 的完整错误信息吧。

    可能是因为 gmock 的::testing::Return 的内部逻辑吧,导致我这个地方总是会调用到 android::hardware::ReturnReturn<EnComdCommonFuncResult>。gmock 的源码我也去看了,但是太难懂了。快哭了
    billyzs
        3
    billyzs  
       2019-10-24 23:44:11 +08:00
    试试
    ```
    Return<EnCommonFuncResult> returnOk{EnCommonFuncResult::EN_COMMON_RESULT_OK}
    ```
    xiaoloudoufu
        4
    xiaoloudoufu  
       2019-10-25 10:07:52 +08:00
    你可以看一下文档:https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md ,对于 move-only 的类型应该要使用 ByMove()
    amiwrong123
        5
    amiwrong123  
    OP
       2019-10-25 10:55:58 +08:00 via Android
    @xiaoloudoufu
    谢谢,我也是刚看见有这个,等会我试试
    amiwrong123
        6
    amiwrong123  
    OP
       2019-10-25 12:13:01 +08:00
    @xiaoloudoufu
    果然 ,用了这个就好了,怪自己没好好文档了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1002 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 21:20 · PVG 05:20 · LAX 13:20 · JFK 16:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.