public
Created

  • Download Gist
gistfile1.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
typedef std::map<std::string, boost::any> VectorMap;
typedef std::pair<std::string, boost::any> VectorMapValue;
 
class VectorStore {
public:
template <typename T>
void RegisterVec(std::string vec_name) {
std::pair<VectorMap::iterator, bool> ret;
ret = map_.insert(VectorMapValue(vec_name, std::vector<T>()));
if (!ret.second) {
throw std::invalid_argument("vec already registered");
}
}
template <typename T>
std::vector<T>& GetVector(std::string vec_name) {
VectorMap::iterator it = map_.find(vec_name);
if (it == map_.end()) {
throw std::invalid_argument("unknown vec");
}
 
try {
return boost::any_cast<std::vector<T>&>(it->second);
} catch(boost::bad_any_cast E) {
throw std::invalid_argument("invalid type");
}
}
private:
VectorMap map_;
};
 
 
// Usage Example:
//
// vs.RegisterVec<double>("radius_of_influence");
// vs.RegisterVec<int>("steps");
//
// std::vector<int> &steps = vs.GetVector<int>("steps")

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.