I have such group -> element to add/remove, with code snippt:
// production code:
Group::add(Element* e) {...};
Group::remove(Element* e) {...};
ElementDerived::ElementDerived(Group* group){ group->add(this);}
ElementDerived::~ElementDerived(Group* group){ group->remove(this);}
whenever i want to test the class ElementDerived, i have to do the following in the setup/teardown. However, how can I set the argument expectation explicitly without _?
// test code:
struct Test_ElementDerived : public ::testing::Test {
void SetUp() override {
ElementDerived* p = nullptr;
EXPECT_CALL(group, add(_)) // how to set expection on the pointer argument?
.WillOnce(Invoke([&p](auto base_ptr) {
p = static_cast<ElementDerived*>(base_ptr);
}));
sut = std::make_unique<ElementDerived>(&group);
}
void TearDown() override {
EXPECT_CALL(group, remove(sut.get()));
}
MockGroup group{};
std::unique_ptr<ElementDerived> sut;
};
You can do this:
Element* p = nullptr;
EXPECT_CALL(group, add(_)).WillOnce(SaveArg<0>(&p));
sut = std::make_unique<ElementDerived>(&group);
ASSERT_EQ(p, static_cast<Element*>(sut.get()));
This does not differ much from what you just did. To replace _ with some matcher - you would have to know the pointer - the only way to achieve that is to use placement new:
struct DestructCaller {
template <typename T> void operator()(T* ptr)
{
if (ptr) ptr->~T();
}
};
std::aligned_storage_t<sizeof(ElementDerived), alignof(ElementDerived)> sut_storage;
std::unique_ptr<ElementDerived, DestructCaller> sut;
void SetUp() override
{
EXPECT_CALL(group, add((ElementDerived*)&sut_storage));
sut.reset(new (&sut_storage) ElementDerived(&group));
}
Or similar concept with unrestricted union:
union
{
ElementDerived sut;
};
void SetUp() override
{
EXPECT_CALL(group, add(&sut));
new (&sut) ElementDerived(&group);
}
void TearDown() override
{
EXPECT_CALL(group, remove(&sut));
sut.~ElementDerived();
}
In my personal opinion - a way with SaveArg and ASSERT_EQ is more readable.
Related
The following works but feels ugly when I do the (*this)(5) part.
struct MyStruct
{
void operator()(int a)
{
// Do something with "a"
}
void myFunc()
{
(*this)(5);
}
};
I will need to overload the () operator and use it from within other class methods.
You have a few options:
(*this)(5)
this->operator()(5)
or just operator()(5)
Create a method that you call from within the operator(), e.g.:
void do_work(int a) { /* ... */ }
void operator()(int a) { do_work(a); }
void myFunc() { do_work(5); }
Whichever you choose is just a matter of personal taste.
Just for fun, here are some more (ridiculous) options:
std::invoke (mentioned in comments):
std::invoke(&MyStruct::operator(), this, 5);
mem_fn:
auto call_op = std::mem_fn(&MyStruct::operator());
call_op(this, 5);
lambda:
auto call_op = [this] (int a) { operator()(a); };
call_op(5);
bind_front
auto call_op = std::bind_front(&MyStruct::operator(), this);
call_op(5);
Is there a way to match the address of a given parameter in an EXPECT_CALL?
I have code like the following:
EXPECT_CALL(mock1, GetTheData()).WillOnce(Return(theData));
EXPECT_CALL(mock2, SetTheData(_)); // How to check the parameter is the same object as the one returned by GetTheData
// the following was tried but does not work
EXPECT_CALL(mock2, SetTheData(_)).WillOnce([&theData](auto param){ EXPECT_EQ(&theData, param) })
But because the SetTheData function takes its argument by value, the address is different. So I would need to find a way to get the object, before it was passed to the SetTheData function.
I tried some stuff with matches, but that did not seem to work either.
Is this possible at all? If so, how? And if not, why not?
EDIT:
As requested here is a more complete example to give more context.
struct TheData
{
// some stl containers
std::unordered_map<int, std::array<std::byte, 16>> mapToArrays;
std::unordered_map<int, long long> mapToInts;
}
class IDataFetcher
{
public:
virtual TheData GetTheData() = 0;
}
class IDataReceiver
{
public:
virtual void SetTheData(TheData theData) = 0;
}
class DataFetcherMock : public IDataFetcher
{
public:
MOCK_METHOD(TheData, GetTheData, (), (override));
}
class DataReceiverMock
{
public:
MOCK_METHOD(void, SetTheData, (TheData), (override));
}
class Sut
{
public:
Sut(std::unique_ptr<IDataFetcher> fetcher, std::unique_ptr<IDataReceiver> receiver)
void DoTheThing()
{
mReceiver->SetTheData(mFetcher->GetTheData());
}
private:
std::unique_ptr<IDataFetcher> mFetcher;
std::unique_ptr<IDataReceiver> mReceiver;
}
TEST(TestFoo, TestGroupFoo)
{
auto fetcherMock = std::make_unique<DataFetcherMock>();
auto receiverMock = std::make_unique<DataReceiverMock>();
EXPECT_CALL(*fetcherMock, GetTheData()).WillOnce(Return(theData));
EXPECT_CALL(*receiverMock, SetTheData(_)); // Here I want to check the objects are the same
Sut sut(std::move(fetcherMock), std::move(receiverMock));
sut.DoTheThing();
}
I have several classes that each of them has an ID and the Id is passed to the class as a template parameter:
typedef class1<1> baseClass;
typedef class2<2> baseClass;
typedef class<100> baseClass;
Now I need a map so if I can associate 1 with Class1 and 2 with Class2 and so on.
How can I create such vector? I am working on a header only library, so it should be a header only definition.
I am looking something that do the same thing that this code would do (if someone can compile it!):
std::map<int,Type> getMap()
{
std::map<int,Type> output;
output.add(1,class1);
output.add(2,class2);
output.add(100,class100);
}
The idea is that when I get as input 1, I create a class1 and when I receive 2, I create class2.
Any suggestion is very appreciated.
using this data, then I can write a function like this:
void consume(class1 c)
{
// do something interesting with c
}
void consume(class2 c)
{
// do something interesting with c
}
void consume(class3 c)
{
// do something interesting with c
}
void consume(int id,void * buffer)
{
auto map=getMap();
auto data= new map[id](buffer); // assuming that this line create a class based on map, so the map provide the type that it should be created and then this line create that class and pass buffer to it.
consume(data);
}
As a sketch:
class BaseClass { virtual ~BaseClass() = default; };
template<std::size_t I>
class SubClass : public BaseClass {};
namespace detail {
template<std::size_t I>
std::unique_ptr<BaseClass> makeSubClass() { return { new SubClass<I> }; }
template<std::size_t... Is>
std::vector<std::unique_ptr<BaseClass>(*)> makeFactory(std::index_sequence<Is...>)
{ return { makeSubclass<Is>... }; }
}
std::vector<std::unique_ptr<BaseClass>(*)> factory = detail::makeFactory(std::make_index_sequence<100>{});
We populate the vector by expanding a parameter pack, so we don't have to write out all 100 instantiations by hand. This gives you Subclass<0> at factory[0], Subclass<1> at factory[1], etc. up to Subclass<99> at factory[99].
If I understand correctly you want a map to create different types according to a given number.
If that is so, then the code should look something like this:
class Base
{
};
template <int number>
class Type : public Base
{
public:
Type()
{
std::cout << "type is " << number << std::endl;
}
};
using Type1 = Type<1>;
using Type2 = Type<2>;
using Type3 = Type<3>;
using CreateFunction = std::function<Base*()>;
std::map<int, CreateFunction> creators;
int main()
{
creators[1] = []() -> Base* { return new Type1(); };
creators[2] = []() -> Base* { return new Type2(); };
creators[3] = []() -> Base* { return new Type3(); };
std::vector<Base*> vector;
vector.push_back(creators[1]());
vector.push_back(creators[2]());
vector.push_back(creators[3]());
}
output:
type is 1
type is 2
type is 3
If you need only to create object, it would be enough to implement template creator function like:
template<int ID>
Base<ID> Create()
{
return Base<ID>();
}
And then use it:
auto obj1 = Create<1>();
auto obj2 = Create<2>();
// etc
Working example: https://ideone.com/urh7h6
Due to C++ being a statically-typed language, you may choose to either have arbitrary types that do a fixed set of things or have a fixed set of types do arbitrary things, but not both.
Such limitations is embodied by std::function and std::variant. std::function can have arbitrary types call operator() with a fixed signature, and std::variant can have arbitrary functions visit the fixed set of types.
Since you already said the types may be arbitrary, you may only have a fixed set of things you can do with such a type (e.g. consume). The simplest way is to delegate the hard work to std::function
struct Type
{
template<typename T>
Type(T&& t)
: f{[t = std::forward<T>(t)]() mutable { consume(t); }} {}
std::function<void()> f;
};
void consume(Type& t)
{
t.f();
}
What you are looking for is either the Stategy pattern:
#include <iostream>
#include <memory>
#include <string>
#include <vector>
class A {
public:
A() {}
virtual void doIt() {};
};
class Aa : public A {
public:
Aa() {}
virtual void doIt() {
std::cout << "do it the Aa way" << std::endl;
}
};
class Ab : public A {
public:
Ab() {}
virtual void doIt() {
std::cout << "do it the Ab way" << std::endl;
}
};
class Concrete {
public:
Concrete(std::string const& type) {
if (type == ("Aa")) {
_a.reset(new Aa());
} else if (type == "Ab") {
_a.reset(new Ab());
}
}
void doIt () const {
_a->doIt();
}
private:
std::unique_ptr<A> _a;
};
int main() {
std::vector<Concrete> vc;
vc.push_back(Concrete("Aa"));
vc.push_back(Concrete("Ab"));
for (auto const& i : vc) {
i.doIt();
}
return 0;
}
Will output:
do it the Aa way
do it the Ab way
I'm changing some code to deal with a change in the signature of some functions which are being called by it. The original code looks something like (this is only illustrative):
int main()
{
TestClassA instance;
instance.MethodA();
instance.MethodB();
// do other stuff with instance
}
class TestClassA
{
void MethodA ()
{
UList* aList = NULL;
getListOfStuff(aList);
// do some stuff
fList = aList;
}
void MethodB ()
{
//do something with fList
}
UList* fList;
}
struct Ulist
{
int value;
std::vector<Ulist> components;
}
getListOfStuff(UList* aList)
{
aList = new UList();
PopulateOriginal(aList);
}
The signature of getListOfStuff has now changed (the change is beyond my control) to:
getListOfStuff(UList& aList)
{
aList=PopulateNewVersion();
}
Seems I wasn't clear about what I was asking, so to clarify...what I am asking is how to call the new version of the method getListOfStuff and safely store the returned data from it (the aList parameter) in a raw pointer field fList, which can then be accessed from MethodB later on.
Following on from #GuillaumeGris answer below, would the following work? (The bit I'm unsure about is whether the assignment within the new getListOfStuff affects whether fList is still pointing to valid data after MethodA has exited.)
void MethodA ()
{
UList* aList = new UList();
getListOfStuff(*aList);
// do some stuff
fList = aList;
}
You don't need an explicit heap allocation (new).
class TestClassA
{
void MethodA ()
{
// Clear components
fList.components.clear();
getListOfStuff(fList);
}
UList fList;
}
If fList is optional, I would recommend the use of a std::unique_ptr
class TestClassA
{
void MethodA ()
{
if (!fList)
fList = std::make_unique<Ulist>();
else
{
// Clear components
fList->components.clear();
}
getListOfStuff(*fList);
}
std::unique_ptr<UList> fList;
}
Sorry for the long title but I did want to be specific.
I expected the following code to work but it doesn't and I can't figure out why :/
#include <cstdio>
#include <cassert>
class UniquePointer
{
public:
void Dispose()
{
delete this;
}
friend void SafeDispose(UniquePointer*& p)
{
if (p != NULL)
{
p->Dispose();
p = NULL;
}
}
protected:
UniquePointer() { }
UniquePointer(const UniquePointer&) { }
virtual ~UniquePointer() { }
};
class Building : public UniquePointer
{
public:
Building()
: mType(0)
{}
void SetBuildingType(int type) { mType = type; }
int GetBuildingType() const { return mType; }
protected:
virtual ~Building() { }
int mType;
};
void Foo()
{
Building* b = new Building();
b->SetBuildingType(5);
int a = b->GetBuildingType();
SafeDispose(b); // error C2664: 'SafeDispose' : cannot convert parameter 1 from 'Building *' to 'UniquePointer *&'
b->Dispose();
}
int main(int argc, char* argv[])
{
Foo();
return 0;
}
Imagine it were legal. Then you could write code like this:
class Animal : public UniquePointer
{
};
void Transmogrify(UniquePointer*& p)
{
p = new Animal();
}
void Foo()
{
Building* b = nullptr;
Transmogrify(b);
b->SetBuildingType(0); // crash
}
Observe that you have violated the type system (you put an Animal where a Building should be) without requiring a cast or raising a compiler error.
I do not think that it is possible to make it work the way you have it designed. Instead, try the following:
template <typename T>
void SafeDispose(T * & p)
{
if (p != NULL)
{
p->Dispose();
p = NULL;
}
}
class UniquePointer
{
public:
void Dispose()
{
delete this;
}
protected:
UniquePointer() { }
UniquePointer(const UniquePointer&) { }
virtual ~UniquePointer() { }
};
It is not allowed because if it were you could do the following:
friend void SafeDispose(UniquePointer*& p)
{
p = new UniquePointer();
}
Building* building;
SafeDispose(building)
//building points to a UniquePointer not a Building.
I guess the work around would be a template function.
To answer the title of your question, you cannot bind a non-const reference to base to a derived class instance because you could then set that reference to a pointer to a base instance that isn't a derived. Consider this function:
void Renew(UniquePointer *& p) {
delete p;
p = new UniquePointer();
}
if you could pass it a pointer to Building you would be able to set it incorrectly to point to a UniquePointer instance.
As it has already been suggested the solution is to change your reference to a plain pointer. Not only this solves your problem, but it is also a better implementation of SafeDispose(); as you wrote it this function gave the false idea that you would always set to 0 all your UniquePointer instances. But what would happen if somebody wrote (assuming UniquePointer constructor was public for simplicity):
UniquePointer *p1 = new UniquePointer();
UniquePointer *p2 = p1;
SafeDispose(p1);
They would expect all of their UniquePointers to be properly taken care of, when p2 is actually invalid.
I guess your SafeDispose should probably look more like :
friend void SafeDispose(UniquePointer** p) ...
In order to invoke it using
SafeDispose(&(UniquePointer*)b);
Then it should work this way.
But your next statement
b->Dispose();
will break cause b should now be NULL, cause it has been disposed and set to NULL by your SafeDispose method.