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

2019-10-24 18:32:00 +08:00
 amiwrong123

在编写 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 该怎么用

2410 次点击
所在节点    程序员
6 条回复
amiwrong123
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
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
2019-10-24 23:44:11 +08:00
试试
```
Return<EnCommonFuncResult> returnOk{EnCommonFuncResult::EN_COMMON_RESULT_OK}
```
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
2019-10-25 10:55:58 +08:00
@xiaoloudoufu
谢谢,我也是刚看见有这个,等会我试试
amiwrong123
2019-10-25 12:13:01 +08:00
@xiaoloudoufu
果然 ,用了这个就好了,怪自己没好好文档了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/612654

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX