Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
typedef std::map<std::string, boost::any> VectorMap;
typedef std::pair<std::string, boost::any> VectorMapValue;
class VectorStore {
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");
VectorMap map_;
// Usage Example:
// vs.RegisterVec<double>("radius_of_influence");
// vs.RegisterVec<int>("steps");
// std::vector<int> &steps = vs.GetVector<int>("steps")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment