SetArgPointee when type of pointer argument is void *

5,976 views
Skip to first unread message

Sébastien Le Duc

unread,
Sep 11, 2013, 9:43:57 AM9/11/13
to googl...@googlegroups.com
Hi,
I get an error when trying to use SetArgPointee and the corresponding argument type is "void *"
Is there a way to achieve that?

thanks
regards,
Sebastien

Vlad Losev

unread,
Oct 1, 2013, 7:17:13 PM10/1/13
to Google C++ Mocking Framework

Using SetArgPointee<>(param) is essentially equivalent to writing

*arg = param;

C++ prohibits such assignments when arg is void*. You need to define a custom action via ACTION_P and do the required type casting there:

ACTION_P(AssignMyType, param) { *static_cast<MyType*>(arg0) = param; }

EXPECT_CALL(...).WillOnce(AssignMyType(my_type_instance));

You can find slightly more info at https://code.google.com/p/googlemock/wiki/CheatSheet#Defining_Actions and much more in the comments for the code defining ACTION_P.

HTH,
Vlad


--
 
---
You received this message because you are subscribed to the Google Groups "Google C++ Mocking Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googlemock+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/googlemock/9bb61037-5e77-4395-8b69-d154ca9e620a%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Sébastien Le Duc

unread,
Oct 4, 2013, 10:23:42 AM10/4/13
to googl...@googlegroups.com
Thanks for the answer

Is there any reason why we cant change the SetArgPointee definition so that it casts the pointer to a pointer toward the type of the param?

I tried and it works for me. I dont see what it can break.

My modified code:
template <size_t N, typename A, bool kIsProto>
class SetArgumentPointeeAction {
 public:
  // Constructs an action that sets the variable pointed to by the
  // N-th function argument to 'value'.
  explicit SetArgumentPointeeAction(const A& value) : value_(value) {}

  template <typename Result, typename ArgumentTuple>
  void Perform(const ArgumentTuple& args) const {
    CompileAssertTypesEqual<void, Result>();
    *reinterpret_cast<A*>(::std::tr1::get<N>(args)) = value_;
Reply all
Reply to author
Forward
0 new messages