Xmipp  v3.23.11-Nereus
metadata_row_base.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: J.M. De la Rosa Trevin (jmdelarosa@cnb.csic.es)
4  * Jan Horacek (xhorace4@fi.muni.cz)
5  *
6  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
7  * Institute of Computer Science MUNI
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22  * 02111-1307 USA
23  *
24  * All comments concerning this program package may be sent to the
25  * e-mail address 'xmipp@cnb.csic.es'
26  ***************************************************************************/
27 
28 #ifndef CORE_METADATA_ROW_BASE_H
29 #define CORE_METADATA_ROW_BASE_H
30 
31 #include "metadata_label.h"
32 #include "metadata_object.h"
33 #include "choose.h"
34 #include <stdexcept>
35 
37 // template <typename T >
38 // void getValueOrAbort(MDLabel label, T &d) const
39 // {
40 // if (!getValue(label, d))
41 // REPORT_ERROR(ERR_ARG_MISSING,(String)"Cannot find label: " + MDL::label2Str(label) );
42 // //formatString("%d",label) );
43 // }
44 //weite function as macro since MDL::label2Str is not availale at class compilation time
45 #define rowGetValueOrAbort(__row,__label, __d)\
46  if (!__row.getValue(__label, __d))\
47  REPORT_ERROR(ERR_ARG_MISSING,(String)"Cannot find label: " + MDL::label2Str(__label) );
48 
49 
53 class MDRow {
54 public:
55  /* Row could be attached to metadata (contains pointers to MetaData) or detached
56  * from metadata (contains data itself). To detach row, call this method.
57  */
58  virtual void detach() {}
59 
60  virtual bool empty() const = 0;
61  virtual int size() const = 0;
62  virtual void clear() = 0;
63 
64  virtual MDRow& operator = (const MDRow&) = 0;
65  virtual ~MDRow() = default;
66 
67  virtual size_t id() const {
68  return getObject(MDL_OBJID)->getValue2(size_t());
69  }
70 
71  virtual bool containsLabel(MDLabel label) const = 0;
72  virtual std::vector<MDLabel> labels() const = 0;
73  virtual void addLabel(MDLabel label) = 0;
74 
78  virtual void resetGeo(bool addLabels = true) {
89  setValue(MDL_FLIP, false, addLabels);
91  }
92 
93  virtual MDObject* getObject(MDLabel label) = 0;
94  virtual const MDObject* getObject(MDLabel label) const = 0;
95 
96  template <typename T>
97  T& getValue(MDLabel label) {
98  MDObject* ptr = getObject(label);
99  if (ptr == nullptr)
100  throw std::logic_error("Object does not exist!");
101  return ptr->getValue2(T());
102  }
103 
104  template <typename T>
105  const T& getValue(MDLabel label) const {
106  const MDObject* ptr = getObject(label);
107  if (ptr == nullptr)
108  throw std::logic_error("Object does not exist!");
109  return ptr->getValue2(T());
110  }
111 
112  template <typename T>
113  bool getValue(MDLabel label, T &d) const { // FIXME: deprecated
114  auto *obj = getObject(label);
115  if (obj == nullptr)
116  return false;
117  d = obj->getValue2(T());
118  return true;
119  }
120 
121  bool getValue(MDObject &object) const { // FIXME: deprecated
122  const MDObject* ptr = this->getObject(object.label);
123  if (ptr != nullptr)
124  object = *ptr;
125  return (ptr != nullptr);
126  }
127 
128  template <typename T>
129  const T& getValueOrDefault(MDLabel label, const T& def) const {
130  const MDObject* ptr = getObject(label);
131  if (ptr == nullptr)
132  return def;
133  return ptr->getValue2(T());
134  }
135 
136  template <typename T>
137  T& getValueOrDefault(MDLabel label, const T& def) {
138  const MDObject* ptr = getObject(label);
139  if (ptr == nullptr)
140  return def;
141  return ptr->getValue2(T());
142  }
143 
144  template <typename T, typename T1>
145  void getValueOrDefault(MDLabel label, T &d, T1 def) const { // FIXME: deprecated
146  if (!getValue(label, d))
147  d = (T) def;
148  }
149 
150  template <typename T>
151  void setValue(MDLabel label, const T &d, bool addLabel = true) {
152  if (this->containsLabel(label) || addLabel)
153  this->setValue(MDObject(label, d));
154  }
155 
156  virtual void setValue(const MDObject &object) = 0;
157 
158  virtual void setValueFromStr(MDLabel label, const String &value) {
159  MDObject mdValue(label);
160  mdValue.fromString(value);
161  setValue(mdValue);
162  }
163 
164  friend std::ostream& operator << (std::ostream &out, const MDRow &row);
165 
166  template <bool IsConst>
167  class iterator_ptr {
168  size_t i;
169  const MDRow* row;
170 
171  public:
172  iterator_ptr(size_t i, const MDRow& row) : i(i), row(&row) {}
173  iterator_ptr(iterator_ptr const& right) : i(right.i), row(right.row) {}
175  i = right.i;
176  row = right.row;
177  return *this;
178  }
180  ++i;
181  return *this;
182  }
184  return row->iteratorValue(i);
185  }
186  bool operator==(const iterator_ptr& other) const { return other.i == this->i; }
187  bool operator!=(const iterator_ptr& other) const { return !(*this == other); }
188  };
189 
192 
193  iterator begin() { return iterator_ptr<false>(0, *this); }
194  iterator end() { return iterator_ptr<false>(this->size(), *this); }
195 
196  const_iterator begin() const { return iterator_ptr<true>(0, *this); }
197  const_iterator end() const { return iterator_ptr<true>(this->size(), *this); }
198 
199 private:
200 
201  virtual MDObject* iteratorValue(size_t i) = 0;
202  virtual const MDObject* iteratorValue(size_t i) const = 0;
203 };
204 
205 #endif
object id (int), NOTE: This label is special and shouldn&#39;t be used
Rotation angle of an image (double,degrees)
const_iterator end() const
virtual bool empty() const =0
bool operator!=(const iterator_ptr &other) const
iterator_ptr(size_t i, const MDRow &row)
void addLabels(PyObject *dict)
bool getValue(MDObject &object) const
const int & getValue2(int) const
Tilting angle of an image (double,degrees)
Shift for the image in the X axis (double)
virtual std::vector< MDLabel > labels() const =0
virtual ~MDRow()=default
Special label to be used when gathering MDs in MpiMetadataPrograms.
auxiliary label to be used as an index (long)
virtual size_t id() const
doublereal * d
virtual void addLabel(MDLabel label)=0
T & getValue(MDLabel label)
Origin for the image in the Y axis (double)
iterator_ptr & operator++()
Flip the image? (bool)
virtual void clear()=0
void getValueOrDefault(MDLabel label, T &d, T1 def) const
virtual MDRow & operator=(const MDRow &)=0
virtual void detach()
bool fromString(const String &str)
scaling factor for an image or volume (double)
friend std::ostream & operator<<(std::ostream &out, const MDRow &row)
virtual void resetGeo(bool addLabels=true)
iterator end()
virtual int size() const =0
const T & getValueOrDefault(MDLabel label, const T &def) const
T & getValueOrDefault(MDLabel label, const T &def)
void setValue(MDLabel label, const T &d, bool addLabel=true)
virtual bool containsLabel(MDLabel label) const =0
iterator_ptr & operator=(iterator_ptr const &right)
bool operator==(const iterator_ptr &other) const
std::string String
Definition: xmipp_strings.h:34
iterator_ptr(iterator_ptr const &right)
TypeHelpers::choose< IsConst, const MDObject *, MDObject * >::type operator*() const
const T & getValue(MDLabel label) const
Shift for the image in the Z axis (double)
iterator begin()
Origin for the image in the Z axis (double)
void(* obj)()
virtual void setValueFromStr(MDLabel label, const String &value)
bool getValue(MDLabel label, T &d) const
Shift for the image in the Y axis (double)
virtual MDObject * getObject(MDLabel label)=0
MDLabel
const_iterator begin() const
< Score 4 for volumes