c++ - Caching mechanism with any type -
i trying devise caching mechanism project. basically, need map string value, however, need value open , determined @ runtime.
what need advice how implement this.
my design this:
- use
boost::anyhold value - use
std::unordered_map<std::string, boost::any>container
i'd member give original type, want cache as transparent possible. users should use this:
double &p = cache.get("p"); std::string &q = cache.get("q"); hugeclass &r = cache.get("r"); i thinking of having instead of boost::any value, private struct this:
struct internal { boost::any value; template <typename t> t& get() { return boost::any_cast<t&>(value); } // possible? } however, don't know if possible, or how this.
so, how can transparent caching class returns references held value?
thanks!
you away this:
double &p = cache.get<double>("p"); std::string &q = cache.get<std::string>("q"); hugeclass &r = cache.get<hugeclass>("r"); otherwise, don't think asking possible.
update: try design tho:
double p; cache.get("p", p); with get this:
template <typename t> bool get(const std::string &key, t &output) const { // lookup... // return false if lookup failed output = boost::any_cast<t&>(...); return true; }
Comments
Post a Comment