memgraph/storage/model/properties/properties.hpp

66 lines
1.3 KiB
C++
Raw Normal View History

2015-12-06 23:37:42 +08:00
#pragma once
#include <map>
#include "property.hpp"
2015-08-31 03:23:48 +08:00
class Properties
{
using props_t = std::map<std::string, Property::sptr>;
public:
props_t::iterator find(const std::string& key)
{
return props.find(key);
}
2015-12-06 23:37:42 +08:00
Property* at(const std::string& key)
{
2015-08-31 03:23:48 +08:00
auto it = props.find(key);
return it == props.end() ? nullptr : it->second.get();
}
2015-10-14 02:17:45 +08:00
template <class T, class... Args>
void emplace(const std::string& key, Args&&... args)
{
auto value = std::make_shared<T>(std::forward<Args>(args)...);
// try to emplace the item
auto result = props.emplace(std::make_pair(key, std::move(value)));
// return if we succedded
if(result.second)
return;
// the key already exists, replace the value it holds
result.first->second = std::move(value);
}
void put(const std::string& key, Property::sptr value)
{
props[key] = std::move(value);
}
void clear(const std::string& key)
{
props.erase(key);
}
2015-10-14 02:17:45 +08:00
template <class Handler>
2015-12-06 23:37:42 +08:00
void accept(Handler& handler) const
{
2015-10-14 02:17:45 +08:00
bool first = true;
2015-10-14 02:17:45 +08:00
for(auto& kv : props)
{
2015-10-14 02:17:45 +08:00
handler.handle(kv.first, *kv.second, first);
2015-12-06 23:37:42 +08:00
2015-10-14 02:17:45 +08:00
if(first)
first = false;
}
}
private:
props_t props;
};