vega
data_set.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdint.h>
4 #include <memory>
5 #include <map>
6 
7 #include "vega/vega.h"
8 #include "vega/tag.h"
9 #include "vega/logger.h"
10 #include "vega/dicom/element.h"
11 
12 namespace vega {
13  namespace dictionary {
14  class Page;
15  class PrivateOwnerBlocks;
16  }
17 
18  namespace dicom {
19  class DataElement;
20 
29  class DataSet {
30  public:
31  typedef uint32_t length_type;
32 
33  length_type length;
34 
35  private:
36  std::weak_ptr<DataElement> m_parent;
37  std::map<Tag, std::shared_ptr<DataElement>> m_elements;
38  std::shared_ptr<dictionary::PrivateOwnerBlocks> m_private_owner_blocks;
39 
40  public:
41  static const length_type UNDEFINED_LENGTH;
42 
43  class iterator
44  {
45  public:
47  typedef decltype(m_elements)::iterator iterator_type;
48 
49  iterator(iterator_type it);
50 
51  self_type& operator++();
52  std::shared_ptr<DataElement>& operator*();
53  bool operator==(const self_type& rhs) const;
54  bool operator!=(const self_type& rhs) const;
55 
56  private:
57  iterator_type m_it;
58  };
59 
61  {
62  public:
64  typedef decltype(m_elements)::const_iterator iterator_type;
65 
66  const_iterator(iterator_type it);
67 
68  self_type& operator++();
69  std::shared_ptr<const DataElement> operator*();
70  bool operator==(const self_type& rhs) const;
71  bool operator!=(const self_type& rhs) const;
72 
73  private:
74  iterator_type m_it;
75  };
76 
81  DataSet(std::shared_ptr<DataElement> parent = nullptr);
82 
83  const std::weak_ptr<DataElement>& parent() const;
84  std::weak_ptr<DataElement>& parent();
85 
87  void add_data_element(std::shared_ptr<DataElement> data_element);
88 
90 
96  std::shared_ptr<const dictionary::Page> page_for(const Tag& tag) const;
98 
100  template <typename T>
101  void add_element(std::shared_ptr<Element<T>> element) {
102  if (this->data_element(element->tag())) throw vega::Exception("DataSet::add_element() -- Cannot add new element as it already exists");
103  this->m_elements.emplace(element->tag(), element->underlying_data_element());
104  }
105 
106  bool is_undefined_length() const;
107 
109  return iterator(m_elements.begin());
110  }
112  return const_iterator(m_elements.begin());
113  }
114 
116  return iterator(m_elements.end());
117  }
118  const_iterator end() const {
119  return const_iterator(m_elements.end());
120  }
121 
122  size_t size() const;
123  std::shared_ptr<DataElement> data_element(const std::string& name);
124  std::shared_ptr<DataElement> data_element(const Tag& tag);
125  std::shared_ptr<DataElement> data_element(const TagMask& tag_mask);
126  std::shared_ptr<const DataElement> data_element(const std::string& tag) const;
127  std::shared_ptr<const DataElement> data_element(const Tag& tag) const;
128 
129  template <typename T>
130  std::shared_ptr<Element<T>> new_element() {
131  if (this->data_element(T::tag_mask)) throw vega::Exception("DataSet::new_element() -- Cannot create new element as it already exists");
132 
133  auto element = std::make_shared<Element<T>>();
134  this->add_data_element(element->underlying_data_element());
135  return element;
136  }
137 
138  template <typename T>
139  std::shared_ptr<Element<T>> new_element(const Tag& tag) {
140  if (this->data_element(T::tag_mask)) throw vega::Exception("DataSet::new_element() -- Cannot create new element as it already exists");
141 
142  auto element = std::make_shared<Element<T>>(tag);
143  this->add_data_element(element->underlying_data_element());
144  return element;
145  }
146 
147  template <typename T>
148  std::shared_ptr<Element<T>> element() {
149  auto data_element = this->data_element(T::tag_mask);
150 
151  if (data_element) {
152  return std::make_shared<Element<T>>(data_element);
153  }
154  else {
155  return nullptr;
156  }
157  }
158 
159  bool operator==(const DataSet& other) const;
160  bool operator!=(const DataSet& other) const;
161 
162  void log(Logger& logger) const;
163 
164  private:
165  void add_private_owner_block_if_relevant(std::shared_ptr<DataElement> data_element);
166  };
167  }
168 }
Definition: logger.h:7
void add_element(std::shared_ptr< Element< T >> element)
Adds a new Element<T> to this DataSet.
Definition: data_set.h:101
std::shared_ptr< Element< T > > new_element(const Tag &tag)
Definition: data_set.h:139
std::shared_ptr< Element< T > > element()
Definition: data_set.h:148
The base class for exceptions that are raised by the vega library.
Definition: vega.h:11
uint32_t length_type
Definition: data_set.h:31
A useful wrapper class over the more standard DataElement.
Definition: element.h:21
length_type length
Definition: data_set.h:33
const_iterator begin() const
Definition: data_set.h:111
static const length_type UNDEFINED_LENGTH
Definition: data_set.h:41
Stores a set of DataElements.
Definition: data_set.h:29
iterator self_type
Definition: data_set.h:46
Definition: page.h:13
Definition: tag_mask.h:11
iterator end()
Definition: data_set.h:115
Definition: age.h:6
Definition: data_set.h:43
Definition: data_set.h:60
const_iterator end() const
Definition: data_set.h:118
Definition: private_owner_blocks.h:7
Class for working with DICOM data element tags.
Definition: tag.h:15
std::shared_ptr< Element< T > > new_element()
Definition: data_set.h:130
const_iterator self_type
Definition: data_set.h:63
iterator begin()
Definition: data_set.h:108