Commit d9db2fe2 by Maarten L. Hekkelman

insert

parent 19a89aeb
......@@ -25,7 +25,7 @@
cmake_minimum_required(VERSION 3.16)
# set the project name
project(cifpp VERSION 4.2.1 LANGUAGES CXX)
project(cifpp VERSION 5.0.0 LANGUAGES CXX)
list(PREPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
......
......@@ -12,8 +12,6 @@ include(CheckCXXSourceRuns)
cmake_push_check_state()
# set(CMAKE_CXX_STANDARD 17)
check_include_file_cxx("atomic" _CXX_ATOMIC_HAVE_HEADER)
mark_as_advanced(_CXX_ATOMIC_HAVE_HEADER)
......
......@@ -12,8 +12,6 @@ include(CheckCXXSourceCompiles)
cmake_push_check_state()
# set(CMAKE_CXX_STANDARD 17)
check_include_file_cxx("filesystem" _CXX_FILESYSTEM_HAVE_HEADER)
mark_as_advanced(_CXX_FILESYSTEM_HAVE_HEADER)
......
......@@ -410,6 +410,14 @@ class category
return result;
}
std::string_view get_column_name(uint16_t ix) const
{
if (ix >= m_columns.size())
throw std::out_of_range("column index is out of range");
return m_columns[ix].m_name;
}
uint16_t add_column(std::string_view column_name)
{
using namespace std::literals;
......
......@@ -111,6 +111,7 @@ class row
private:
friend class item_handle;
friend class category_index;
friend class row_initializer;
template <typename, typename...>
friend class iterator_impl;
......@@ -138,6 +139,7 @@ class row_handle
friend class item_handle;
friend class category;
friend class category_index;
friend class row_initializer;
row_handle() = default;
......@@ -218,6 +220,7 @@ class row_handle
private:
uint16_t get_column_ix(std::string_view name) const;
std::string_view get_column_name(uint16_t ix) const;
uint16_t add_column(std::string_view name);
......@@ -257,6 +260,8 @@ class row_initializer
{
}
row_initializer(row_handle rh);
private:
std::vector<item> m_items;
};
......
......@@ -1310,82 +1310,86 @@ category::iterator category::insert_impl(const_iterator pos, row *n)
if (n == nullptr)
throw std::runtime_error("Invalid pointer passed to insert");
// First, make sure all mandatory fields are supplied
if (m_cat_validator != nullptr)
try
{
for (uint16_t ix = 0; ix < static_cast<uint16_t>(m_columns.size()); ++ix)
// First, make sure all mandatory fields are supplied
if (m_cat_validator != nullptr)
{
const auto &[column, iv] = m_columns[ix];
for (uint16_t ix = 0; ix < static_cast<uint16_t>(m_columns.size()); ++ix)
{
const auto &[column, iv] = m_columns[ix];
if (iv == nullptr)
continue;
if (iv == nullptr)
continue;
bool seen = false;
bool seen = false;
for (auto i = n->m_head; i != nullptr; i = i->m_next)
{
if (i->m_column_ix == ix)
for (auto i = n->m_head; i != nullptr; i = i->m_next)
{
iv->operator()(i->text());
if (i->m_column_ix == ix)
{
iv->operator()(i->text());
seen = true;
break;
seen = true;
break;
}
}
if (not seen and iv->m_mandatory)
throw std::runtime_error("missing mandatory field " + column + " for category " + m_name);
}
if (not seen and iv->m_mandatory)
throw std::runtime_error("missing mandatory field " + column + " for category " + m_name);
// if (m_index != nullptr)
// {
// std::unique_ptr<ItemRow> nr(new ItemRow{nullptr, this, nullptr});
// Row r(nr.get());
// auto keys = keyFields();
// for (auto v = b; v != e; ++v)
// {
// if (keys.count(v->name()))
// r.assign(v->name(), v->value(), true);
// }
// auto test = m_index->find(nr.get());
// if (test != nullptr)
// {
// if (VERBOSE > 1)
// std::cerr << "Not inserting new record in " << mName << " (duplicate Key)" << std::endl;
// result = test;
// isNew = false;
// }
// }
}
// if (m_index != nullptr)
// {
// std::unique_ptr<ItemRow> nr(new ItemRow{nullptr, this, nullptr});
// Row r(nr.get());
// auto keys = keyFields();
// for (auto v = b; v != e; ++v)
// {
// if (keys.count(v->name()))
// r.assign(v->name(), v->value(), true);
// }
// auto test = m_index->find(nr.get());
// if (test != nullptr)
// {
// if (VERBOSE > 1)
// std::cerr << "Not inserting new record in " << mName << " (duplicate Key)" << std::endl;
// result = test;
// isNew = false;
// }
// }
}
if (m_index != nullptr)
m_index->insert(n);
if (m_index != nullptr)
m_index->insert(n);
// insert at end, most often this is the case
if (pos.m_current == nullptr)
{
if (m_head == nullptr)
m_tail = m_head = n;
// insert at end, most often this is the case
if (pos.m_current == nullptr)
{
if (m_head == nullptr)
m_tail = m_head = n;
else
m_tail = m_tail->m_next = n;
}
else
m_tail = m_tail->m_next = n;
{
assert(m_head != nullptr);
if (pos.m_current == m_head)
m_head = n->m_next = m_head;
else
n = n->m_next = m_head->m_next;
}
return iterator(*this, n);
}
else
catch(const std::exception& e)
{
assert(m_head != nullptr);
if (pos.m_current == m_head)
m_head = n->m_next = m_head;
else
n = n->m_next = m_head->m_next;
delete_row(n);
throw;
}
return iterator(*this, n);
}
category::iterator category::erase_impl(const_iterator pos)
......
......@@ -39,9 +39,25 @@ uint16_t row_handle::get_column_ix(std::string_view name) const
return m_category->get_column_ix(name);
}
std::string_view row_handle::get_column_name(uint16_t ix) const
{
return m_category->get_column_name(ix);
}
uint16_t row_handle::add_column(std::string_view name)
{
return m_category->add_column(name);
}
// --------------------------------------------------------------------
row_initializer::row_initializer(row_handle rh)
{
row *r = rh;
auto &cat = *rh.m_category;
for (auto i = r->m_head; i != nullptr; i = i->m_next)
m_items.emplace_back(cat.get_column_name(i->m_column_ix), i->text());
}
} // namespace cif::v2
\ No newline at end of file
......@@ -130,6 +130,32 @@ BOOST_AUTO_TEST_CASE(cc_2)
}
}
BOOST_AUTO_TEST_CASE(item_1)
{
using namespace cif::v2;
item i1("1", "1");
item i2("2", 2.0);
item i3("3", '3');
item ci1(i1);
item ci2(i2);
item ci3(i3);
BOOST_CHECK_EQUAL(i1.value(), ci1.value());
BOOST_CHECK_EQUAL(i2.value(), ci2.value());
BOOST_CHECK_EQUAL(i3.value(), ci3.value());
item mi1(std::move(i1));
item mi2(std::move(i2));
item mi3(std::move(i3));
BOOST_CHECK_EQUAL(i1.value(), mi1.value());
BOOST_CHECK_EQUAL(i2.value(), mi2.value());
BOOST_CHECK_EQUAL(i3.value(), mi3.value());
}
// --------------------------------------------------------------------
......@@ -243,6 +269,27 @@ BOOST_AUTO_TEST_CASE(c_2)
BOOST_CHECK_EQUAL(c.size(), 3);
}
BOOST_AUTO_TEST_CASE(c_3)
{
std::tuple<int,const char*> D[] = {
{1, "aap"},
{2, "noot"},
{3, "mies"}
};
cif::v2::category c("foo");
for (const auto &[id, s] : D)
c.emplace({ {"id", id}, { "s", s} });
cif::v2::category c2("bar");
for (auto r : c)
c2.emplace(r);
// BOOST_CHECK(c == c2);
}
BOOST_AUTO_TEST_CASE(ci_1)
{
cif::v2::category c("foo");
......
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