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