Commit 19a89aeb by Maarten L. Hekkelman

- start row_initializer

parent 677c61c3
......@@ -36,7 +36,6 @@
// TODO: implement all of:
// https://en.cppreference.com/w/cpp/named_req/Container
// https://en.cppreference.com/w/cpp/named_req/SequenceContainer
// https://en.cppreference.com/w/cpp/named_req/AssociativeContainer ?
// and more?
......@@ -333,12 +332,12 @@ class category
// --------------------------------------------------------------------
// void insert(const_iterator pos, const row &row)
// void insert(const_iterator pos, const row_initializer &row)
// {
// insert_impl(pos, row);
// }
// void insert(const_iterator pos, row &&row)
// void insert(const_iterator pos, row_initializer &&row)
// {
// insert_impl(pos, std::move(row));
// }
......@@ -347,9 +346,9 @@ class category
size_t erase(condition &&cond);
size_t erase(condition &&cond, std::function<void(row_handle)> &&visit);
iterator emplace(std::initializer_list<item> items)
iterator emplace(row_initializer &&ri)
{
return this->emplace(items.begin(), items.end());
return this->emplace(ri.m_items.begin(), ri.m_items.end());
}
template <typename ItemIter>
......@@ -586,7 +585,6 @@ class category
// proxy methods for every insertion
iterator insert_impl(const_iterator pos, row *n);
iterator erase_impl(const_iterator pos);
std::string m_name;
......
......@@ -248,7 +248,7 @@ namespace detail
bool test(row_handle r) const override
{
auto &c = r.cat();
auto &c = r.get_category();
bool result = false;
for (auto &f : get_category_fields(c))
......@@ -286,7 +286,7 @@ namespace detail
bool test(row_handle r) const override
{
auto &c = r.cat();
auto &c = r.get_category();
bool result = false;
for (auto &f : get_category_fields(c))
......
......@@ -130,7 +130,7 @@ class row
};
// --------------------------------------------------------------------
/// \brief row_handle is the way to access data in rows
/// \brief row_handle is the way to access data stored in rows
class row_handle
{
......@@ -153,7 +153,7 @@ class row_handle
{
}
const category &cat() const
const category &get_category() const
{
return *m_category;
}
......@@ -202,55 +202,8 @@ class row_handle
void assign(const std::vector<item> &values)
{
// std::map<std::string, std::tuple<size_t, std::string, std::string>> changed;
for (auto &value : values)
{
assign(value, true);
// auto columnIx = cat->add_column(value.name());
// auto &col = cat->m_columns[columnIx];
// std::string tag = col.mValidator ? col.mValidator->mTag : std::to_string(columnIx);
// changed[tag] = std::make_tuple(columnIx, operator[](columnIx).c_str(), value.value());
// assign(columnIx, value.value(), true);
}
// // see if we need to update any child categories that depend on these values
// // auto iv = col.mValidator;
// if (mCascade)
// {
// for (auto &&[childCat, linked] : cat->mChildLinks)
// {
// Condition cond;
// std::string childTag;
// std::vector<Item> newValues;
// for (size_t ix = 0; ix < linked->mParentKeys.size(); ++ix)
// {
// std::string pk = linked->mParentKeys[ix];
// std::string ck = linked->mChildKeys[ix];
// if (changed.count(pk) > 0)
// {
// childTag = ck;
// cond = std::move(cond) && (Key(ck) == std::get<1>(changed[pk]));
// newValues.emplace_back(ck, std::get<2>(changed[pk]));
// }
// else
// {
// const char *value = (*this)[pk].c_str();
// cond = std::move(cond) && (Key(ck) == value);
// }
// }
// auto rows = childCat->find(std::move(cond));
// for (auto &cr : rows)
// cr.assign(newValues);
// }
// }
}
void assign(std::string_view name, std::string_view value, bool updateLinked, bool validate = true)
......@@ -282,4 +235,30 @@ class row_handle
row *m_row = nullptr;
};
// --------------------------------------------------------------------
class row_initializer
{
public:
friend class category;
row_initializer() = default;
row_initializer(const row_initializer &) = default;
row_initializer(row_initializer &&) = default;
row_initializer &operator=(const row_initializer &) = default;
row_initializer &operator=(row_initializer &&) = default;
row_initializer(std::initializer_list<item> items)
: m_items(items) {}
template <typename ItemIter, std::enable_if_t<std::is_same_v<typename ItemIter::value_type, item>, int> = 0>
row_initializer(ItemIter b, ItemIter e)
: m_items(b, e)
{
}
private:
std::vector<item> m_items;
};
} // namespace cif::v2
\ No newline at end of file
......@@ -662,6 +662,23 @@ mies Mies
cat1.erase(cif::v2::key("id") == "noot");
BOOST_CHECK(cat1.size() == 2);
// should fail with duplicate key:
BOOST_CHECK_THROW(cat1.emplace({
{"id", "aap"},
{"c", "2e-aap"}
}), std::exception);
cat1.erase(cif::v2::key("id") == "aap");
BOOST_CHECK(cat1.size() == 1);
cat1.emplace({
{"id", "aap"},
{"c", "2e-aap"}
});
BOOST_CHECK(cat1.size() == 2);
}
// --------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment