How to mock a range-based operator using gmock? - c++11

I have a class that inherits from std::map
class LeMap : public std::map<int,int>
{
...
};
I have another class that is supposed to loop over its elments:
class LeMapUser {
LeMap mMap;
public:
void printElements()
{
for( auto& element : mMap ) { whatever(element); }
}
private:
void whatever(int element){ ... }
};
Is it possible to mock LeMap in order to test whatever using gmock ?
Briefly: what operator should I overload to execute the loop?

AFAIK a range-based for loop needs the mMap object to have a begin() member which returns an iterator. The iterator needs to have a post-increment operator to walk along the range. Additionally, mMap must have an end() member, returning a value comparable to the iterator (but not necessarily being an iterator itself).

Related

C++: convert not from obj1 to obj2, but from obj1* to obj2?

A constructor for MyClass takes a pointer to another such object.
The C++ MyClass is functionally the same as a C "class" based on a typedef'd struct called MyType_T. (The C++ class is basically a wrapper to the old C code.) I'd like to be able to pass in a MyClass* anywhere I could pass in a MyType_T* before.
I'd like to write an automatic conversion of any MyClass* to MyType_T*, but I guess what's throwing me is that my type converter is written to take a MyClass not a MyClass*. Even though I'm sure that's the problem, I can't think of what syntax would solve it. I've thought about making a friend implementation of the cast, but I can't put it before the definition of class MyClass because it won't know the offset of thing. And I can't put after the definition of class MyClass because the MyClass constructor wants to use that conversion.
typedef struct MyStruct {
int iFoo;
struct MyType* ptypeParent;
} MyType_T;
void MyTypeCreator( MyType_T* ptypeSelf, int iFoo_in, MyType_T* ptypeParent );
class MyClass {
public:
MyClass( int iFoo, MyClass* pclassParent ) {
MyTypeCreator( &thing, iFoo, pclassParent ); <--------------- PROBLEM
MyTypeCreator( &thing, iFoo, &pclassParent->thing ); <------- WORKS
};
operator MyType_T*() { return &thing; } <---------------- INCORRECT: attempts to convert MyClass, not MyClass*, to MyType_T*.
MyType_T thing;
};
QUESTION 1: how to write a convertor from MyClass* instead of MyClass?
QUESTION 2: how can such a convertor check for NULL input? (If thing isn't offset of 0, but say 8, then converting from a NULL pclass without a check would give a value of 0x00000008, not NULL...)

How can I change the values of objects in tuple?

In my class, I have a tuple object which I want to access the objects of in that as reference.
Actually, I want to know how can I write a get function to access the objects in my tuple?
I wrote a function which return the lvalue of my object in the tuple. I want to know how can I change it to return rvalue?
I have a template named GetIndex, which returns the index of type object in my tuple. In my tuple I definitely sure there are unique types object.
//return just lvalue
template <typename T>
T get_module()
{
return std::get<Private::GetIndex<T, Args...>::value>(types);
}
//compilation error
template <typename T>
T& get_module()
{
return &std::get<Private::GetIndex<T, Args...>::value>(types);
}
'''
You can just put & after T. It will be enough.
template <typename T>
T &get_module()
{
return std::get<Private::GetIndex<T, Args...>::value>(types);
}

adding a shared_ptr object on a weak_ptr container

My class is based on boost::asio tutorial.
This class has a private ctor, is derived from enable_shared_from_this.
its static member function return a shared_ptr from the object created.
I want to store those pointers on a list of weak_ptr, so the list don't need to worry about its life time, either prolong it.
The caller tcp_serve instantiate tcp_connection with create method:
tcp_server:
tcp_connection::pointer new_connection =
tcp_connection::create(acceptor_.get_io_service());
tcp_connection:
PUBLIC:
typedef boost::shared_ptr<tcp_connection> pointer;
static pointer create(boost::asio::io_service& io_service)
{
return pointer(new tcp_connection(io_service));
}
PRIVATE:
tcp_connection(boost::asio::io_service& io_service)
: _socket(io_service), _timer(io_service)
{
}
I am trying to create a list on the tcp_server, I tried many different kind of types, but I can't rightly added the object to the list:
std::list<std::weak_ptr<tcp_connection>> connections;
connections.push_back(new_connection);

Wrapping std::map and provide pointer to element

I am using a library which offers a function foo(Widget*).
My Widgets are stored in
struct WidgetManager {
std::map<int, Widget> dict;
??? getWidget(int id);
}
Originally I stored (raw) Widget pointers in the std::map just because it was convenient to pass them to foo.
If I want to store the actual Widgets in the map, what should the return type of getWidget be so that I can pass a pointer of the Widget to foo?
I am compelled to make it of type iterator, but I don't like that I have to access itr->second to get the Widget(pointer).
You can use & just before you pass your widget to the foo(Widget*) function to get a pointer to it.
struct WidgetManager {
std::map<int, Widget> dict;
Widget& getWidget(int id);
}
usage
WidgetManager wm;
//...
Widget& w = wm.getWidget(id);
foo(&w);
//...

Compilation failure when using a base class reference as a predicate

class baseFunctor{
virtual ~baseFunctor() {}
virtual bool operator()(const A& lhs, const A& rhs) = 0;
};
class derivedFunctor : public baseFunctor{
bool operator()(const A& lhs, const A& rhs) override { /*implementation*/ }
};
Inside another unrelated method, I have :
baseFunctor* functor = new derivedFunctor();
std::vector<A> vectorA;
My intention is to use this functor as a compare function like this:
std::make_heap(vectorA.begin(),vectorA.end(),*functor);
However, I get the following error:
C2893 Failed to specialize function template 'void
std::make_heap(_RanIt,_RanIt,_Pr)'
What is the proper way to use my pointer to functor in that situation?
Function objects are passed by value in standard algorithms. This means that the derivedFunctor object will be passed by value as a baseFunctor. Since baseFunctor is an abstract class that code cannot compile. (If it was not an abstract class the code would compile, but probably misbehave because of the object slicing problem.)
In order to make this work, you can use something like std::reference_wrapper:
std::make_heap(vectorA.begin(),vectorA.end(),std::ref(*functor));
This works because the reference wrapper object avoids copying the functor and keeps a reference instead; and because it is directly callable and simply forwards arguments to the object reference.

Resources