Skip to content

Instantly share code, notes, and snippets.

@mithro
Last active June 8, 2018 16:22
Show Gist options
  • Save mithro/0e7f71b458013d858a1f61727e873dcd to your computer and use it in GitHub Desktop.
Save mithro/0e7f71b458013d858a1f61727e873dcd to your computer and use it in GitHub Desktop.
/* FIXME: Use a smarter data structure. */
class t_metadata_as {
std::string value_;
public:
t_metadata_as(std::string v) : value_(v) {}
t_metadata_as(const t_metadata_as &o) : value_(o.value_) {}
int as_int() const;
double as_double() const;
std::string as_string() const { return value_; }
std::pair<int, int> as_int_pair() const;
std::vector<int> as_int_vector() const;
};
struct t_metadata_dict : std::unordered_map<std::string, std::vector<t_metadata_as> > {
inline bool has(std::string key) {
return (this->count(key) >= 1);
}
inline std::vector<t_metadata_as>* get(std::string key) {
if (this->count(key) < 1) {
return nullptr;
}
return &((*this)[key]);
}
void add(std::string key, std::string value) {
this->emplace(key, std::vector<t_metadata_as>());
(*this)[key].push_back(t_metadata_as(value));
}
};
template<typename T>
struct t_metadata : std::unordered_map<T, t_metadata_dict> {
inline bool has(T key) {
return (this->count(key) >= 1);
}
inline t_metadata_dict* get(T key) {
if (this->count(key) < 1) {
return nullptr;
}
return &((*this)[key]);
}
void create(T key) {
this->emplace(key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment