I'm trying to make a resource manager using C++11 and variadic templates. The question is how to store std::tuple to collection and get it back? I've tried to store it to void* in this example (trying not to use boost::any here). Every time I'm casting back to std::tuple I'm getting that cast'ed tuple is the same as tuple created from the params (currentArgs == storedArgs). The code below I think explains everything.
#include <memory>
#include <typeindex>
#include <iostream>
#include <string>
#include <vector>
#include <map>
typedef std::multimap<std::type_index, void*> Object;
typedef std::map<Object, std::shared_ptr<void>> ObjectCollection;
Object object;
ObjectCollection objectCollection;
template<typename T, typename... Args>
T* getResource(Args&& ... args)
{
// Creating tuple from the arguments
std::tuple<Args...> currentArgs(std::forward<Args>(args)...);
// Getting object type info
std::type_index type = { typeid(T) };
// Getting all objects from the collection that are of the same type
auto range = object.equal_range(type);
for (auto it = range.first; it != range.second; ++it)
{
// it->second is a void* Since we are iterating through
// the the collection of the same type I'm trying to cast
// back. Object construct parameters should be the same
// (in this example: const std::string &fileName)
auto storedArgs = *static_cast<std::tuple<Args...>*>(it->second);
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Problem is here. currentArgs and storedArgs are always equal :/
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Return the object from the collection if current arguments and
// arguments from the collection are the same
if (currentArgs == storedArgs)
{
std::cout << "Found... returning..." << std::endl;
// found... return...
return static_cast<T*>(objectCollection[object].get());
}
}
// Object with the same arguments were not found
// Adding to collection and return
std::cout << "Adding to collection..." << std::endl;
object.emplace(type, ¤tArgs);
objectCollection[object] = std::make_shared<T>(std::forward<Args>(args)...);
return static_cast<T*>(objectCollection[object].get());
}
class Resource
{
public:
virtual ~Resource() = default;
template<typename T, typename... Args>
static T* get(Args&& ... args)
{
return getResource<T>(std::forward<Args>(args)...);
}
};
class Image
{
public:
Image(const std::string &fileName)
{
std::cout << "Loading image " << fileName.c_str() << std::endl;
}
~Image(){};
};
int main()
{
auto image1 = Resource::get<Image>("aaa.jpg");
auto image2 = Resource::get<Image>("bbb.jpg");
auto image3 = Resource::get<Image>("aaa.jpg");
getchar();
}
EDIT
Thanks everybody for the input. In case anyone cares my final Resource.h looks like this and works perfect:
#pragma once
#include <memory>
#include <map>
template<class T, class...Args>
std::map<std::tuple<Args...>, std::shared_ptr<T>>& getCache()
{
static std::map<std::tuple<Args...>, std::shared_ptr<T>> cache; // only run once
return cache;
}
template<typename T, typename... Args>
std::shared_ptr<T> getResource(Args&& ... args)
{
// std::decay_t should be used
auto& cache = getCache<T, std::decay_t<Args>...>();
// Creating tuple from the arguments
auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
// Search for object in the cache
auto it = cache.find(arguments);
if (it != cache.end())
{
// Found. Return.
return it->second;
}
// Not found. Add to cache.
auto object = std::make_shared<T>(std::forward<Args>(args)...);
cache.emplace(std::make_pair(std::move(arguments), object));
return object;
}
class Resource
{
public:
virtual ~Resource() = default;
template<typename T, typename... Args>
static std::shared_ptr<T> get(Args&& ... args)
{
return getResource<T>(std::forward<Args>(args)...);
}
};