Commit 184c4918 by Maarten L. Hekkelman

changed find1 a bit more

reverted to returning empty results in case nothing is found
parent f944b3ce
......@@ -35,7 +35,6 @@ include(CheckIncludeFiles)
include(CheckLibraryExists)
include(CMakePackageConfigHelpers)
include(Dart)
include(GenerateExportHeader)
set(CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD 20)
......@@ -275,9 +274,6 @@ if(UNIX)
target_compile_definitions(cifpp PUBLIC CACHE_DIR="${CIFPP_CACHE_DIR}")
endif()
# generate_export_header(cifpp
# EXPORT_FILE_NAME cif++/Cif++Export.hpp)
set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR})
set(LIBRARY_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR})
set(SHARE_INSTALL_DIR ${CMAKE_INSTALL_DATADIR}/libcifpp)
......
......@@ -262,13 +262,7 @@ class category
{
auto h = find<T>(pos, std::forward<condition>(cond), column);
if (h.empty())
throw std::runtime_error("No hits found");
if (h.size() != 1)
throw std::runtime_error("Hit not unique");
return std::get<0>(*h.begin());
return h.size() == 1 ? std::get<0>(*h.begin()) : T{};
}
template <typename... Ts, typename... Cs, typename U = std::enable_if_t<sizeof...(Ts) != 1>>
......@@ -285,13 +279,7 @@ class category
static_assert(sizeof...(Ts) == sizeof...(Cs), "The number of column titles should be equal to the number of types to return");
auto h = find<Ts...>(pos, std::forward<condition>(cond), std::forward<Cs>(columns)...);
if (h.empty())
throw std::runtime_error("No hits found");
if (h.size() != 1)
throw std::runtime_error("Hit not unique");
return *h.begin();
return h.size() == 1 ? *h.begin() : std::tuple<Ts...>{};
}
bool exists(condition &&cond) const
......
......@@ -292,7 +292,11 @@ struct item_handle
{
}
static const item_handle s_null_item;
private:
item_handle();
uint16_t m_column;
row_handle &m_row_handle;
......@@ -421,6 +425,13 @@ struct item_handle::item_value_as<T, std::enable_if_t<std::is_same_v<T, bool>>>
template <size_t N>
struct item_handle::item_value_as<char[N]>
{
static std::string convert(const item_handle &ref)
{
if (ref.empty())
return {};
return { ref.text().data(), ref.text().size() };
}
static int compare(const item_handle &ref, const char (&value)[N], bool icase)
{
return icase ? cif::icompare(ref.text(), value) : ref.text().compare(value);
......@@ -430,6 +441,13 @@ struct item_handle::item_value_as<char[N]>
template <typename T>
struct item_handle::item_value_as<T, std::enable_if_t<std::is_same_v<T, const char *>>>
{
static std::string convert(const item_handle &ref)
{
if (ref.empty())
return {};
return { ref.text().data(), ref.text().size() };
}
static int compare(const item_handle &ref, const char *value, bool icase)
{
return icase ? cif::icompare(ref.text(), value) : ref.text().compare(value);
......@@ -439,6 +457,13 @@ struct item_handle::item_value_as<T, std::enable_if_t<std::is_same_v<T, const ch
template <typename T>
struct item_handle::item_value_as<T, std::enable_if_t<std::is_same_v<T, std::string_view>>>
{
static std::string convert(const item_handle &ref)
{
if (ref.empty())
return {};
return { ref.text().data(), ref.text().size() };
}
static int compare(const item_handle &ref, const std::string_view &value, bool icase)
{
return icase ? cif::icompare(ref.text(), value) : ref.text().compare(value);
......
......@@ -172,22 +172,22 @@ class row_handle
item_handle operator[](uint32_t column_ix)
{
return item_handle(column_ix, *this);
return empty() ? item_handle::s_null_item : item_handle(column_ix, *this);
}
const item_handle operator[](uint32_t column_ix) const
{
return item_handle(column_ix, const_cast<row_handle &>(*this));
return empty() ? item_handle::s_null_item : item_handle(column_ix, const_cast<row_handle &>(*this));
}
item_handle operator[](std::string_view column_name)
{
return item_handle(add_column(column_name), *this);
return empty() ? item_handle::s_null_item : item_handle(add_column(column_name), *this);
}
const item_handle operator[](std::string_view column_name) const
{
return item_handle(get_column_ix(column_name), const_cast<row_handle &>(*this));
return empty() ? item_handle::s_null_item : item_handle(get_column_ix(column_name), const_cast<row_handle &>(*this));
}
template <typename... C>
......@@ -202,7 +202,7 @@ class row_handle
return detail::get_row_result<Ts...>(*this, { get_column_ix(columns)... });
}
template<typename T>
template <typename T>
T get(const char *column)
{
return operator[](get_column_ix(column)).template as<T>();
......
......@@ -24,14 +24,25 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cassert>
#include <cif++/row.hpp>
namespace cif
{
const item_handle item_handle::s_null_item;
row_handle s_null_row_handle;
item_handle::item_handle()
: m_column(std::numeric_limits<uint16_t>::max())
, m_row_handle(s_null_row_handle)
{
}
std::string_view item_handle::text() const
{
if (m_row_handle.m_row != nullptr)
if (not m_row_handle.empty())
{
for (auto iv = m_row_handle.m_row->m_head; iv != nullptr; iv = iv->m_next)
{
......@@ -45,6 +56,7 @@ std::string_view item_handle::text() const
void item_handle::assign_value(const item &v)
{
assert(not m_row_handle.empty());
m_row_handle.assign(m_column, v.value(), true);
}
......
......@@ -31,21 +31,25 @@ namespace cif
void row_handle::assign(size_t column, std::string_view value, bool updateLinked, bool validate)
{
assert(m_category);
m_category->update_value(m_row, column, value, updateLinked, validate);
}
uint16_t row_handle::get_column_ix(std::string_view name) const
{
assert(m_category);
return m_category->get_column_ix(name);
}
std::string_view row_handle::get_column_name(uint16_t ix) const
{
assert(m_category);
return m_category->get_column_name(ix);
}
uint16_t row_handle::add_column(std::string_view name)
{
assert(m_category);
return m_category->add_column(name);
}
......@@ -53,6 +57,9 @@ uint16_t row_handle::add_column(std::string_view name)
row_initializer::row_initializer(row_handle rh)
{
assert(rh.m_category);
assert(rh.m_row);
row *r = rh;
auto &cat = *rh.m_category;
......
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