Skip to content

@shawnchin /gist:2587386
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.