Xmipp  v3.23.11-Nereus
row.cpp
Go to the documentation of this file.
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2022 NKI/AVL, Netherlands Cancer Institute
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "cif++/category.hpp"
28 
29 namespace cif
30 {
31 
32 void row_handle::assign(uint16_t column, std::string_view value, bool updateLinked, bool validate)
33 {
34  if (not m_category)
35  throw std::runtime_error("uninitialized row");
36 
37  m_category->update_value(m_row, column, value, updateLinked, validate);
38 }
39 
40 uint16_t row_handle::get_column_ix(std::string_view name) const
41 {
42  if (not m_category)
43  throw std::runtime_error("uninitialized row");
44 
45  return m_category->get_column_ix(name);
46 }
47 
48 std::string_view row_handle::get_column_name(uint16_t ix) const
49 {
50  if (not m_category)
51  throw std::runtime_error("uninitialized row");
52 
53  return m_category->get_column_name(ix);
54 }
55 
56 uint16_t row_handle::add_column(std::string_view name)
57 {
58  if (not m_category)
59  throw std::runtime_error("uninitialized row");
60 
61  return m_category->add_column(name);
62 }
63 
64 void row_handle::swap(uint16_t column, row_handle &b)
65 {
66  if (not m_category)
67  throw std::runtime_error("uninitialized row");
68 
69  m_category->swap_item(column, *this, b);
70 }
71 
72 // --------------------------------------------------------------------
73 
74 row_initializer::row_initializer(row_handle rh)
75 {
76  if (not rh.m_category)
77  throw std::runtime_error("uninitialized row");
78 
79  assert(rh.m_row);
80 
81  row *r = rh.get_row();
82  auto &cat = *rh.m_category;
83 
84  for (uint16_t ix = 0; ix < r->size(); ++ix)
85  {
86  auto &i = r->operator[](ix);
87  if (not i)
88  continue;
89  emplace_back(cat.get_column_name(ix), i.text());
90  }
91 }
92 
93 void row_initializer::set_value(std::string_view name, std::string_view value)
94 {
95  for (auto &i : *this)
96  {
97  if (i.name() == name)
98  {
99  i.value(value);
100  return;
101  }
102  }
103 
104  emplace_back(name, value);
105 }
106 
107 void row_initializer::set_value_if_empty(std::string_view name, std::string_view value)
108 {
109  if (find_if(begin(), end(), [name](auto &i) { return i.name() == name; }) == end())
110  emplace_back(name, value);
111 }
112 
113 } // namespace cif
uint16_t get_column_ix(const category &cat, std::string_view col)
Definition: condition.cpp:39
#define i
doublereal * b
float rh