Odil
A C++11 library for the DICOM standard
Item.h
Go to the documentation of this file.
1 /*************************************************************************
2  * odil - Copyright (C) Universite de Strasbourg
3  * Distributed under the terms of the CeCILL-B license, as published by
4  * the CEA-CNRS-INRIA. Refer to the LICENSE file or to
5  * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
6  * for details.
7  ************************************************************************/
8 
9 #ifndef _1c7b57cc_79f5_497c_815c_920e0711a864
10 #define _1c7b57cc_79f5_497c_815c_920e0711a864
11 
12 #include <cstdint>
13 #include <istream>
14 #include <ostream>
15 #include <string>
16 #include <utility>
17 #include <vector>
18 
19 #include "odil/odil.h"
20 
21 namespace odil
22 {
23 
24 namespace pdu
25 {
26 
29 {
30 public:
33  {
34  public:
36  enum class Type
37  {
38  unsigned_int_8,
39  unsigned_int_16,
40  unsigned_int_32,
41  string,
42  items
43  };
44 
46  Field(uint8_t value);
47 
49  Field(uint16_t value);
50 
52  Field(uint32_t value);
53 
55  Field(std::string const & value);
56 
58  Field(std::vector<Item> const & value);
59 
61  Type get_type() const;
62 
68  uint8_t as_unsigned_int_8() const;
69 
75  uint8_t & as_unsigned_int_8();
76 
82  uint16_t as_unsigned_int_16() const;
83 
89  uint16_t & as_unsigned_int_16();
90 
96  uint32_t as_unsigned_int_32() const;
97 
103  uint32_t & as_unsigned_int_32();
104 
110  std::string const & as_string() const;
111 
117  std::string & as_string();
118 
124  std::vector<Item> const & as_items() const;
125 
131  std::vector<Item> & as_items();
132 
133  private:
134  Type _type;
135  uint8_t _uint8;
136  uint16_t _uint16;
137  uint32_t _uint32;
138  std::string _string;
139  std::vector<Item> _items;
140  };
141 
142  typedef std::vector<std::pair<std::string, Field>> Container;
143 
145  Item();
146 
148  Item(std::vector<std::pair<std::string, Field>> const & fields);
149 
151  Item & add(std::string const & name, Field const & field);
152 
154  Container::size_type size() const;
155 
157  bool empty() const;
158 
160  bool has_field(std::string const & name) const;
161 
163  Field const & operator[](std::string const & name) const;
164 
166  Field & operator[](std::string const & name);
167 
174  uint8_t as_unsigned_int_8(std::string const & name) const;
175 
182  uint8_t & as_unsigned_int_8(std::string const & name);
183 
190  uint16_t as_unsigned_int_16(std::string const & name) const;
191 
198  uint16_t & as_unsigned_int_16(std::string const & name);
199 
206  uint32_t as_unsigned_int_32(std::string const & name) const;
207 
214  uint32_t & as_unsigned_int_32(std::string const & name);
215 
222  std::string const & as_string(std::string const & name) const;
223 
230  std::string & as_string(std::string const & name);
231 
232  std::vector<Item> const & as_items(std::string const & name) const;
233 
240  std::vector<Item> & as_items(std::string const & name);
241 
242  typedef Container::const_iterator const_iterator;
245 
251  void read(
252  std::istream & stream,std::string const & name, Field::Type type,
253  std::streamsize size=0);
254 
255 private:
256  std::vector<std::pair<std::string, Field>> _fields;
257 };
258 
259 // No operator>> since we need explicit names and types.
260 
261 ODIL_API
262 std::ostream &
263 operator<<(std::ostream & stream, Item const & item);
264 
265 }
266 
267 }
268 
269 #endif // _1c7b57cc_79f5_497c_815c_920e0711a864
odil::pdu::Item::as_unsigned_int_8
uint8_t & as_unsigned_int_8(std::string const &name)
Return the named field as an uint8_t.
odil::pdu::Item::Field::as_unsigned_int_16
uint16_t as_unsigned_int_16() const
Return the uint16_t stored in the field.
odil::pdu::Item::Field::Field
Field(std::string const &value)
Constructor.
odil::pdu::Item::Field::as_items
std::vector< Item > const & as_items() const
Return the items stored in the field.
odil::pdu::Item::Field::Type
Type
Possible types stored in the field.
Definition: Item.h:37
odil::pdu::Item::operator[]
Field & operator[](std::string const &name)
Return the named field. Raise an exception if no such field exists.
odil::pdu::Item::end
const_iterator end() const
odil::pdu::Item::Field::as_unsigned_int_32
uint32_t & as_unsigned_int_32()
Return the uint32_t stored in the field.
odil::pdu::Item::Field::as_string
std::string const & as_string() const
Return the string stored in the field.
odil::pdu::Item::Container
std::vector< std::pair< std::string, Field > > Container
Definition: Item.h:142
odil
Definition: Association.h:25
odil::pdu::Item::empty
bool empty() const
Test whether the container is empty.
ODIL_API
#define ODIL_API
Definition: odil.h:28
odil::pdu::Item::Field::get_type
Type get_type() const
Return the concrete type stored in the field.
odil::pdu::Item::Field::Field
Field(uint16_t value)
Constructor.
odil::pdu::Item::Field::as_string
std::string & as_string()
Return the string stored in the field.
odil::pdu::Item::Field
Generic field.
Definition: Item.h:33
odil::pdu::Item::as_items
std::vector< Item > & as_items(std::string const &name)
Return the named field as a sequence of items.
odil::pdu::Item::begin
const_iterator begin() const
odil.h
odil::pdu::Item::add
Item & add(std::string const &name, Field const &field)
Add a new field to the PDU item.
odil::pdu::Item::Field::Field
Field(uint8_t value)
Constructor.
odil::pdu::Item::size
Container::size_type size() const
Return the number of fields.
odil::pdu::Item::Field::as_items
std::vector< Item > & as_items()
Return the items stored in the field.
odil::pdu::Item::has_field
bool has_field(std::string const &name) const
Test whether the PDU item contains a field with a given name.
odil::pdu::Item::Field::as_unsigned_int_16
uint16_t & as_unsigned_int_16()
Return the uint16_t stored in the field.
odil::pdu::Item::as_items
std::vector< Item > const & as_items(std::string const &name) const
odil::pdu::Item::Field::as_unsigned_int_32
uint32_t as_unsigned_int_32() const
Return the uint32_t stored in the field.
odil::pdu::Item::Field::as_unsigned_int_8
uint8_t & as_unsigned_int_8()
Return the uint8_t stored in the field.
odil::pdu::Item::Item
Item()
Create an empty PDU item.
odil::pdu::Item::Item
Item(std::vector< std::pair< std::string, Field >> const &fields)
Create an initialized PDU item.
odil::pdu::Item::read
void read(std::istream &stream, std::string const &name, Field::Type type, std::streamsize size=0)
Read a field from a stream.
odil::pdu::Item::const_iterator
Container::const_iterator const_iterator
Definition: Item.h:242
odil::pdu::Item::as_string
std::string & as_string(std::string const &name)
Return the named field as a sequence of items.
odil::pdu::Item::as_unsigned_int_8
uint8_t as_unsigned_int_8(std::string const &name) const
Return the named field as an uint8_t.
odil::pdu::Item::as_unsigned_int_32
uint32_t & as_unsigned_int_32(std::string const &name)
Return the named field as an uint32_t.
odil::pdu::Item::as_unsigned_int_16
uint16_t as_unsigned_int_16(std::string const &name) const
Return the named field as an uint16_t.
odil::pdu::Item::as_unsigned_int_16
uint16_t & as_unsigned_int_16(std::string const &name)
Return the named field as an uint16_t.
odil::pdu::operator<<
std::ostream & operator<<(std::ostream &stream, Item const &item)
odil::pdu::Item
A sequence of fields forming a full PDU or a part of it.
Definition: Item.h:29
odil::pdu::Item::as_string
std::string const & as_string(std::string const &name) const
Return the named field as a string.
odil::pdu::Item::operator[]
Field const & operator[](std::string const &name) const
Return the named field. Raise an exception if no such field exists.
odil::pdu::Item::Field::Field
Field(uint32_t value)
Constructor.
odil::pdu::Item::Field::Field
Field(std::vector< Item > const &value)
Constructor.
odil::pdu::Item::as_unsigned_int_32
uint32_t as_unsigned_int_32(std::string const &name) const
Return the named field as an uint32_t.
odil::pdu::Item::Field::as_unsigned_int_8
uint8_t as_unsigned_int_8() const
Return the uint8_t stored in the field.