Storing functors in a stl map and calling them - c++

I am trying to store functors in a stl map and then call it one by one , but now sure how to call it. This is what I have tried so far.
#include <iostream>
#include <map>
#include <string>
class BaseFunctor {
public:
BaseFunctor() {
}
~BaseFunctor() {
}
};
template <typename T>
class MyFunctor : public BaseFunctor {
public:
T operator()(T x) {
return x * 2;
}
};
int main ( int argc, char**argv ) {
std::map<std::string, BaseFunctor*> m_functorMap;
m_functorMap.insert(std::make_pair("int", new MyFunctor<int>()));
m_functorMap.insert(std::make_pair("double", new MyFunctor<double>()));
m_functorMap.insert(std::make_pair("float", new MyFunctor<float>()));
m_functorMap.insert(std::make_pair("long", new MyFunctor<long>()));
for ( std::map<std::string, BaseFunctor*>::iterator itr = m_functorMap.begin(); itr != m_functorMap.end(); ++itr ) {
std::cout << *(itr->second)() << std::endl;
}
return 0;
}
I cannot use boost

You have a map full of BaseFunctor*, but BaseFunctor is not callable since it has no operator(). You cannot call without casting to a pointer of a derived type, preferably with dynamic_cast. Overall it doesn't look like a good design. You are attempting to use run-time polymorphism where it can't.

Related

Insert an object pointer into a map of maps through emplace() does not work

I'm trying to insert a pointer object to a map through emplace() but it does not work.
I've created a simple representation of the problem below. I'm trying to insert to newFooList pointer object type Foo*.
I can't seem to find a way to create a type for FooMap* in std::map<int, FooMap*> m_fooMapList. Should it be done with the new on the second field of the map?
#include <iostream>
#include <utility>
#include <stdint.h>
#include <cstdlib>
#include <map>
class Foo
{
private:
int m_foobar;
public:
Foo(int value)
{
m_foobar = value;
}
void setfoobar(int value);
int getfoobar();
};
class FooMap
{
private:
std::map<int, Foo*> m_newFoo;
public:
FooMap() = default;
};
class FooMapList
{
private:
std::map<int, FooMap*> m_fooMapList;
public:
FooMapList() = default;
void insertFoo(Foo* newFooObj);
};
int Foo::getfoobar(void)
{
return(m_foobar);
}
void FooMapList::insertFoo(Foo* newFooObj)
{
if(m_fooMapList.empty())
{
std::cout << "m_fooMapList is empty" << std::endl ;
}
//m_fooMapList.emplace( newFooObj->getfoobar(), newFooObj );
// Need to find a way to insert newFooObj to m_fooMapList
m_fooMapList.second = new FooMap;
}
int main() {
FooMapList newFooList;
for (auto i=1; i<=5; i++)
{
Foo *newFoo = new Foo(i);
newFoo->getfoobar();
newFooList.insertFoo(newFoo);
}
return 0;
}
On g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-28)
$ g++ -std=c++11 -Wall map_of_map.cpp
map_of_map.cpp: In member function ‘void FooMapList::insertFoo(Foo*)’:
map_of_map.cpp:51:18: error: ‘class std::map<int, FooMap*>’ has no member named ‘second’
m_fooMapList.second = new FooMap;
m_fooMapList is defined as
std::map<int, FooMap*> m_fooMapList;
So to insert into it, you need an int and a pointer to FooMap:
m_fooMapList.emplace(newFooObj->getfoobar(), new FooMap);
Having said that, you should use C++ value semantics and rely less on raw pointers:
std::map<int, FooMap> m_fooMapList; // no pointers
m_fooMapList.emplace(newFooObj->getfoobar(), {}); // construct objects in-place
That is, instances of FooMap can reside directly in the map itself.
That way you get better performance and avoid memory leaks.
It's also worth looking into smart pointers (e.g. unique_ptr) if you really want to work with pointers.
I am not sure if you need a map structure where the values are pointers to another map. The FooMapList class could be simple
std::map<int, FooMap> m_fooMapList;
On the other hand, the entire play with the row pointer will bring you nothing but a pain on the neck.
In case the use of
std::map<int, FooMap*> m_fooMapList; and std::map<int, Foo*> are neccesarry, I would go for smartpointers.
Following is an example code with replacing row pointers with std::unique_ptr and shows how to insert map of Foo s to map in-place. See live here
#include <iostream>
#include <utility>
#include <map>
#include <memory>
class Foo
{
private:
int m_foobar;
public:
Foo(int value): m_foobar(value) {}
void setfoobar(int value) noexcept { m_foobar = value; }
int getfoobar() const noexcept { return m_foobar; }
};
class FooMap
{
private:
std::map<int, std::unique_ptr<Foo>> m_newFoo;
// ^^^^^^^^^^^^^^^^^^^^
public:
FooMap() = default;
#if 0 // optional
// copy disabled
FooMap(const FooMap&) = delete;
FooMap& operator=(const FooMap&) = delete;
// move enabled
FooMap(FooMap&&) = default;
FooMap& operator=(FooMap&&) = default;
#endif
// provide a helper function to insert new Foo to the map of Foo s
void insertFoo(std::unique_ptr<Foo> newFooObj)
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
{
std::cout << "inserting to FooMap..." << std::endl;
m_newFoo.emplace(newFooObj->getfoobar(), std::move(newFooObj)); // construct in place
}
};
class FooMapList
{
private:
std::map<int, std::unique_ptr<FooMap>> m_fooMapList;
// ^^^^^^^^^^^^^^^^^^^^^^^
public:
FooMapList() = default;
void insertFooMap(std::unique_ptr<Foo> newFooObj)
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
{
if (m_fooMapList.empty())
{
std::cout << "m_fooMapList is empty" << std::endl;
}
// create FooMap and insert Foo to it.
FooMap fooMap;
const auto key = newFooObj->getfoobar();
fooMap.insertFoo(std::move(newFooObj));
// finally insert the FooMap to m_fooMapList
std::cout << "inserting to fooMapList..." << std::endl;
m_fooMapList.emplace(key, std::make_unique<FooMap>(std::move(fooMap))); // construct in place
}
};
int main()
{
FooMapList newFooList;
for (auto i = 1; i <= 5; i++)
{
auto newFoo = std::make_unique<Foo>(i);
std::cout << newFoo->getfoobar() << std::endl;
newFooList.insertFooMap(std::move(newFoo));
}
return 0;
}
Output:
1
m_fooMapList is empty
inserting to FooMap...
inserting to fooMapList...
2
inserting to FooMap...
inserting to fooMapList...
3
inserting to FooMap...
inserting to fooMapList...
4
inserting to FooMap...
inserting to fooMapList...
5
inserting to FooMap...
inserting to fooMapList...
You can throw away your do-nothing map classes, stop using pointers, and just
#include <iostream>
#include <utility>
#include <stdint.h>
#include <cstdlib>
#include <map>
class Foo
{
private:
int m_foobar;
public:
Foo(int value) : m_foobar(value) { }
void setfoobar(int value) { m_foobar = value; }
int getfoobar() const { return m_foobar; }
// or more simply
// int foobar;
};
using FooMap = std::map<int, Foo>;
using FooMapMap = std::map<int, FooMap>;
int main() {
FooMapMap foos;
for (auto i=1; i<=5; i++)
{
foos[i][i] = Foo(i);
}
return 0;
}
Do note that the inner map is totally pointless at this stage, as they only ever have one entry
Except if you have a really good reason to do so, avoid to obfuscate things a la Java like this and try to leverage the STL anyway you can. To that end you can use type aliases
using FooMap = std::map<int, Foo*>; // Maybe use a smart pointer instead here?
using FooMapList = std::map<int, FooMap>; // Maybe List is not an appropriate name for a map
Now, you have a Foo element you just created and want to insert it in your map list, to do so you need a way to select in which map in the list you want to insert it. I will assume you'll insert in the first map in the list:
auto FooMap::emplace(int key, Foo* value)
{
return m_newFoo.emplace(key, value);
}
void FooMapList::insertFoo(Foo* newFooObj)
{
// If the map for `getfoobar` does not exist yet, operator[] will create it
auto& mapPtr = m_fooMapList[newFooObj->getfoobar()];
if (nullptr == mapPtr)
mapPtr = new FooMap();
mapPtr->emplace(
newFooObj->getfoobar(),
newFooObj
);
}
Note that I didn't handle memory cleanup. I suggest you try to use smart pointers when applicable (std::unique_ptr and std::shared_ptr)
I have considered valid points from each of the answers to remove pointers and remove useless double level map representations. But the real world abstraction is a much complex problem which involves thousands of objects on the fly which needs to be created and destroyed dynamically. Using pointers seemed a valid approach, but JeJo' approach seemed much better.
I tried to re-use his attempt but with object pointers and the below seems to work. With the below insert functions
In the FooMap class the function would be
void FooMap::insertFoo(Foo* newFooObj)
{
m_newFoo.emplace(newFooObj->getfoobar(), newFooObj);
}
const std::map<int, Foo*> FooMap::getList()
{
return m_newFoo;
}
and in FooMapList it would be
void FooMapList::insertFooList(Foo* newFooObj)
{
std::map <int, FooMap*>::iterator iter;
FooMap *localFooMap = NULL;
iter = m_fooMapList.find( newFooObj->getfoobar() );
if( iter == m_fooMapList.end() )
{
localFooMap = new FooMap;
localFooMap->insertFoo(newFooObj);
m_fooMapList.emplace(newFooObj->getfoobar(), localFooMap );
}
else
{
localFooMap = iter->second;
localFooMap->insertFoo(newFooObj);
m_fooMapList.emplace(newFooObj->getfoobar(), localFooMap );
}
}
const std::map<int, FooMap*> FooMapList::getList()
{
return m_fooMapList;
}
I would appreciate feedback for this approach too. I will be adding the calls to destructor to clean up the created objects

std::find return a class that I can't acesses functions

I come from C/C# language and now I'm trying to learn about C++ and his standards functions.
Now, I'm creating a class called IMonsterDead. I will have a std::vector<IMonsterDead*> with N monsters.
Example:
class IMonsterDead {
public:
IMonsterDead(int Id)
{
this->_Id = Id;
}
virtual void OnDead() = 0;
int Id() const {
return _Id;
}
private:
int _Id;
};
One class which implements that class:
class MonsterTest : public IMonsterDead {
public:
MonsterTest(int generId)
: IMonsterDead(generId)
{
}
virtual void OnDead()
{
std::cout << "MonsterTesd died" << std::endl;
}
};
Ok, if I access directly everything works fine. But I'm trying to use std::find.
Full program test:
int main()
{
std::vector<IMonsterDead*> monsters;
for (int i = 0; i < 1000; i++)
{
monsters.emplace_back(new MonsterTest(1000 + i));
}
int id = 1033;
std::vector<IMonsterDead*>::iterator result = std::find(monsters.begin(), monsters.end(), [id]( IMonsterDead const* l) {
return l->Id() == id;
});
if (result == monsters.end())
std::cout << "Not found" << std::endl;
else
{
// Here I want to access OnDead function from result
}
return 0;
}
So I need to access OnDead function from result but I can't. Intellisense doesn't show anything for me. The result exists.
How can I access that function? Have another better way to do that?
You need to use std::find_if() instead of std::find(). std::find() is for finding an element with a specific value, so you have to pass it the actual value to find, not a user_defined predicate. std::find_if() is for finding an element based on a predicate.
Either way, if a match is found, dereferencing the returned iterator will give you a IMonsterDead* pointer (more accurately, it will give you a IMonsterDead*& reference-to-pointer). You need to then dereference that pointer in order to access any members, like OnDead().
You are also leaking memory. You are not delete'ing the objects you new. And when dealing with polymorphic types that get deleted via a pointer to a base class, the base class needs a virtual destructor to ensure all derived destructors get called properly.
With that said, you are clearly using C++11 or later (by the fact that you are using vector::emplace_back()), so you should use C++11 features to help you manage your code better:
You should use std::unique_ptr to wrap your monster objects so you don't need to delete them manually.
You should always use the override keyword when overriding a virtual method, to ensure you override it properly. The compiler can catch more syntax errors when using override than without it.
You should use auto whenever you declare a variable that the compiler can deduce its type for you. Especially useful when dealing with templated code.
Try something more like this:
#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>
class IMonsterDead {
public:
IMonsterDead(int Id)
: m_Id(Id)
{
}
virtual ~IMonsterDead() {}
virtual void OnDead() = 0;
int Id() const {
return m_Id;
}
private:
int m_Id;
};
class MonsterTest : public IMonsterDead {
public:
MonsterTest(int generId)
: IMonsterDead(generId)
{
}
void OnDead() override
{
std::cout << "MonsterTest died" << std::endl;
}
};
int main()
{
std::vector<std::unique_ptr<IMonsterDead>> monsters;
for (int i = 0; i < 1000; i++)
{
// using emplace_back() with a raw pointer risks leaking memory
// if the emplacement fails, so push a fully-constructed
// std::unique_ptr instead, to maintain ownership at all times...
monsters.push_back(std::unique_ptr<IMonsterDead>(new MonsterTest(1000 + i)));
// or:
// std::unique_ptr<IMonsterDead> monster(new MonsterTest(1000 + i));
// monsters.push_back(std::move(monster));
// or, if you are using C++14 or later:
// monsters.push_back(std::make_unique<MonsterTest>(1000 + i));
}
int id = 1033;
auto result = std::find_if(monsters.begin(), monsters.end(),
[id](decltype(monsters)::value_type &l) // or: (decltype(*monsters.begin()) l)
{
return (l->Id() == id);
}
// or, if you are using C++14 or later:
// [id](auto &l) { return (l->Id() == id); }
);
if (result == monsters.end())
std::cout << "Not found" << std::endl;
else
{
auto &monster = *result; // monster is 'std::unique_ptr<IMonsterDead>&'
monster->OnDead();
}
return 0;
}
Iterators are an interesting abstraction, in this case to be reduced to pointers.
Either you receive the pointer to the element or you get an invalid end.
You can use it as a pointer: (*result)->func();
You can also use it to create a new variable:
IMonsterDead &m = **result;
m.func();
This should give the same assembly, both possible.

Create a MultiClass Queue C++

Is possible create a queue for differents types of Objects, but with same interface?
As example, I have an interface called SensorItem, and 4 kinds of Class, SensorItemA
,SensorItemB,
SensorItemC
,SensorItemD
`
queue <SensorItem> cola;
void encolar(SensorItem* dato)
{
cola.push (*dato);
}
SensorItem* sacar()
{
SensorItem* d=cola.front();
cola.pop();
return d;
}
Thats my class Queue(Cola)
and here i try to use it
void main()
{
Cola c=new Cola();
TemperatureItem t=new TemperatureItem(3.25);
c.encolar(t);
ImuItem i=new ImuItem(3,4,8);
}
its something wrong in my syntax? or just is not possible to do it?
Polymorphism in C++ only works with references and pointers. Objects in C++ are objects, not references. If you create a SensorItem it will always be a SensorItem, not a TemperatureItem or a ImuItem. If you have a std::queue<SensorItem>, it's elements will always be SensorItems, never TemperatureItems or ImuItems.
If you want to make a queue of objects derived from SensorItem, you need to use a queue of pointers to SensorItems:
#include <iostream>
#include <queue>
#include <memory>
struct SensorItem
{
virtual void doAThing() = 0;
virtual ~SensorItem() {}
};
struct TemperatureItem : SensorItem
{
void doAThing() { std::cout << "TemperatureItem\n"; }
};
struct ImuItem : SensorItem
{
void doAThing() { std::cout << "ImuItem\n"; }
};
class Cola
{
private:
std::queue<std::unique_ptr<SensorItem>> cola;
public:
void encolar(std::unique_ptr<SensorItem> dato)
{
cola.push(std::move(dato));
}
std::unique_ptr<SensorItem> sacar()
{
std::unique_ptr<SensorItem> d = std::move(cola.front());
cola.pop();
return d;
}
};
int main()
{
Cola c;
c.encolar(std::make_unique<TemperatureItem>());
c.encolar(std::make_unique<ImuItem>());
std::unique_ptr<SensorItem> item = c.sacar();
item->doAThing();
item = c.sacar();
item->doAThing();
}
Live on Coliru
Here I've used std::unique_ptr to avoid having to do manual memory management. You could use raw SensorItem*s, but I would advise against it.

C++ array of object constructors

I have several classes named Child1, Child2 ... etc, inherited from object Parent. I need to create an object by its name, for example if I have string "Child1", I need to create object Child1 and so on.
I thought about something like:
struct registry_entry {
const char* name;
IREGISTRY* (*initializer)();
};
struct registry_entry registry_list[] =
{
{"xml", &REGISTRY_XML::REGISTRY_XML},
}
But I can't get address of object constructor.I believe that this problem must be already solved, and the solution is pretty simple, but I can't find it.
Constructors and destructors are special functions and cannot be accessed through function pointers.
You need to create a static member function like
struct REGISTRY_XML {
static IREGISTRY* create();
};
thus you can refer to it as
struct registry_entry registry_list[] =
{
{"xml", &REGISTRY_XML::create},
}
Your code looks very C-like and not very C++-like, but if you can actually use the full power of C++11, I would go with a combination of std::function, lambdas, std::unique_ptr and std::map.
Lambdas can wrap constructors without you having to write separate wrapper functions, std::function allows you to store the lambdas in a map, std::unique_ptr eliminates many memory-management bugs, and std::map performs the actual mapping from strings to initialiser functions.
Here is a complete example:
#include <functional>
#include <string>
#include <memory>
#include <map>
#include <iostream>
struct IREGISTRY {
virtual ~IREGISTRY() {}
virtual void print() = 0;
};
struct REGISTRY_XML : IREGISTRY { void print() override { std::cout << "XML\n"; } };
struct REGISTRY_INI : IREGISTRY { void print() override { std::cout << "INI\n"; } };
struct REGISTRY_JSON : IREGISTRY { void print() override { std::cout << "JSON\n"; } };
int main()
{
std::map<std::string, std::function<std::unique_ptr<IREGISTRY>()>> const registry_list = {
{ "xml", []() { return std::make_unique<REGISTRY_XML>(); } },
{ "ini", []() { return std::make_unique<REGISTRY_INI>(); } },
{ "json", []() { return std::make_unique<REGISTRY_JSON>(); } },
};
auto const initializer_iter = registry_list.find("xml");
if (initializer_iter != registry_list.end())
{
auto const initializer = initializer_iter->second;
auto const registry_ptr = initializer();
registry_ptr->print();
}
}

How to get rid of weak_ptrs in a container

I have a class that stores weak_ptrs in a container and later does something if the weak_ptr is not expired:
class Example
{
public:
void fill(std::shared_ptr<int> thing)
{
member.push_back(thing);
}
void dosomething() const
{
for (const auto& i : member)
if (!i.expired())
;// do something. the weak_ptr will not be locked
}
private:
std::vector<std::weak_ptr<int>> member;
};
If Example is an object that lives forever and fill is used regularily, the vector allocates memory for elements continously, but they are never removed after they expired.
Is there any automatic C++ way to get rid of the expired weak_ptrs in the container or is there a better way to store a variable number of them?
My naive way would be to iterate over the container each time fill is called and remove all the expired weak_ptrs. In scenarios where Example has many elements in the container and fill is frequently called this seems to be very inefficient.
Since you clarified that you are actually using a std::map and not a std::vector, it might be easiest to remove the expired elements on-the-fly in doSomething(). Switch back from a range-based for loop to a normal iterator based design:
void dosomething() const
{
auto i = member.begin();
while( i != member.end() ) {
if( i->expired() ) { i = member.erase( i ); continue; }
;// do something. the weak_ptr will not be locked
++i;
}
}
Does the shared_ptr<int> have to be a shared_ptr<int>?
How about a shared_ptr<IntWrapper>?
#include <iostream>
#include <forward_list>
using namespace std;
class IntWrapper {
public:
int i;
static forward_list<IntWrapper*>& all() {
static forward_list<IntWrapper*> intWrappers;
return intWrappers;
}
IntWrapper(int i) : i(i) {
all().push_front(this);
}
~IntWrapper() {
all().remove(this);
}
};
void DoSomething() {
for(auto iw : IntWrapper::all()) {
cout << iw->i << endl;
}
}
int main(int argc, char *argv[]) {
shared_ptr<IntWrapper> a = make_shared<IntWrapper>(1);
shared_ptr<IntWrapper> b = make_shared<IntWrapper>(2);
shared_ptr<IntWrapper> c = make_shared<IntWrapper>(3);
DoSomething();
return 0;
}
I would rather use a custom deleter for the shared_ptr. But this implies here to change the interface of the Example class. The advantage using custom deleter is that there is no need to check for expired objects in the collection. The collection is directly maintained by the custom deleter.
Quick implementation :
#include <memory>
#include <iostream>
#include <set>
template <typename Container>
// requires Container to be an associative container type with key type
// a raw pointer type
class Deleter {
Container* c;
public:
Deleter(Container& c) : c(&c) {}
using key_type = typename Container::key_type;
void operator()(key_type ptr) {
c->erase(ptr);
delete ptr;
}
};
class Example {
public:
// cannot change the custom deleter of an existing shared_ptr
// so i changed the interface here to take a unique_ptr instead
std::shared_ptr<int> fill(std::unique_ptr<int> thing) {
std::shared_ptr<int> managed_thing(thing.release(), Deleter<containter_type>(member));
member.insert(managed_thing.get());
return managed_thing;
}
void dosomething() const {
// we don't need to check for expired pointers
for (const auto & i : member)
std::cout << *i << ", ";
std::cout << std::endl;
}
using containter_type = std::set<int*>;
private:
containter_type member;
};
int main()
{
Example example;
auto one = example.fill(std::unique_ptr<int>(new int(1)));
auto two = example.fill(std::unique_ptr<int>(new int(2)));
auto three = example.fill(std::unique_ptr<int>(new int(3)));
example.dosomething();
three.reset();
example.dosomething();
}