GDCM 3.0.24
gdcmDataElement.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: GDCM (Grassroots DICOM). A DICOM library
4
5 Copyright (c) 2006-2011 Mathieu Malaterre
6 All rights reserved.
7 See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notice for more information.
12
13=========================================================================*/
14#ifndef GDCMDATAELEMENT_H
15#define GDCMDATAELEMENT_H
16
17#include "gdcmTag.h"
18#include "gdcmVL.h"
19#include "gdcmVR.h"
20#include "gdcmByteValue.h"
21#include "gdcmSmartPointer.h"
22
23#include <set>
24
25namespace gdcm_ns
26{
27// Data Element
28// Contains multiple fields:
29// -> Tag
30// -> Optional VR (Explicit Transfer Syntax)
31// -> ValueLength
32// -> Value
33// TODO: This class SHOULD be pure virtual. I don't want a user
34// to shoot himself in the foot.
35
36class SequenceOfItems;
37class SequenceOfFragments;
59{
60public:
61 DataElement(const Tag& t = Tag(0), const VL& vl = 0, const VR &vr = VR::INVALID):TagField(t),ValueLengthField(vl),VRField(vr),ValueField(nullptr) {}
62 //DataElement( Attribute const &att );
63
64 friend std::ostream& operator<<(std::ostream &_os, const DataElement &_val);
65
67 const Tag& GetTag() const { return TagField; }
68 Tag& GetTag() { return TagField; }
71 void SetTag(const Tag &t) { TagField = t; }
72
74 const VL& GetVL() const { return ValueLengthField; }
75 VL& GetVL() { return ValueLengthField; }
79 void SetVL(const VL &vl) { ValueLengthField = vl; }
81
84 VR const &GetVR() const { return VRField; }
88 void SetVR(VR const &vr) {
89 if( vr.IsVRFile() )
90 VRField = vr;
91 }
92
94 Value const &GetValue() const { gdcmAssertAlwaysMacro(ValueField); return *ValueField; }
96 gdcmAssertAlwaysMacro(ValueField);
97 return *ValueField;
98 }
100 void SetValue(Value const & vl) {
101 //assert( ValueField == 0 );
102 ValueField = vl;
103 ValueLengthField = vl.GetLength();
104 }
106 bool IsEmpty() const { return ValueField == nullptr || (GetByteValue() && GetByteValue()->IsEmpty()); }
107
109 void Empty() { ValueField = nullptr; ValueLengthField = 0; }
110
112 void Clear()
113 {
114 TagField = 0;
115 VRField = VR::INVALID;
116 ValueField = nullptr;
117 ValueLengthField = 0;
118 }
119
120 // Helper:
126 void SetByteValue(const char *array, VL length)
127 {
128 ByteValue *bv = new ByteValue(array,length);
129 SetValue( *bv );
130 }
133 const ByteValue* GetByteValue() const {
134 // Get the raw pointer from the gdcm::SmartPointer
135 const ByteValue *bv = dynamic_cast<const ByteValue*>(ValueField.GetPointer());
136 return bv; // Will return NULL if not ByteValue
137 }
138
146
151
153 bool IsUndefinedLength() const {
154 return ValueLengthField.IsUndefined();
155 }
156
158 {
159 if( this != &_val)
160 {
161 *this = _val;
162 }
163 }
164
165 bool operator<(const DataElement &de) const
166 {
167 return GetTag() < de.GetTag();
168 }
170 = default;
171
172 bool operator==(const DataElement &de) const
173 {
174 bool b = TagField == de.TagField
175 && ValueLengthField == de.ValueLengthField
176 && VRField == de.VRField;
177 if( !ValueField && !de.ValueField )
178 {
179 return b;
180 }
181 if( ValueField && de.ValueField )
182 {
183 return b && (*ValueField == *de.ValueField);
184 }
185 // ValueField != de.ValueField
186 return false;
187 }
188
189 // The following functionalities are dependent on:
190 // # The Transfer Syntax: Explicit or Implicit
191 // # The Byte encoding: Little Endian / Big Endian
192
193 /*
194 * The following was inspired by a C++ idiom: Curiously Recurring Template Pattern
195 * Ref: http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern
196 * The typename TDE is typically a derived class *without* any data
197 * while TSwap is a simple template parameter to achieve byteswapping (and allow factorization of
198 * highly identical code)
199 */
200 template <typename TDE>
201 VL GetLength() const {
202 return static_cast<const TDE*>(this)->GetLength();
203 }
204
205 template <typename TDE, typename TSwap>
206 std::istream &Read(std::istream &is) {
207 return static_cast<TDE*>(this)->template Read<TSwap>(is);
208 }
209
210 template <typename TDE, typename TSwap>
211 std::istream &ReadOrSkip(std::istream &is, std::set<Tag> const &skiptags) {
212 (void)skiptags;
213 return static_cast<TDE*>(this)->template Read<TSwap>(is);
214 }
215
216 template <typename TDE, typename TSwap>
217 std::istream &ReadPreValue(std::istream &is, std::set<Tag> const &skiptags) {
218 (void)skiptags;
219 return static_cast<TDE*>(this)->template ReadPreValue<TSwap>(is);
220 }
221 template <typename TDE, typename TSwap>
222 std::istream &ReadValue(std::istream &is, std::set<Tag> const &skiptags) {
223 (void)skiptags;
224 return static_cast<TDE*>(this)->template ReadValue<TSwap>(is);
225 }
226 template <typename TDE, typename TSwap>
227 std::istream &ReadValueWithLength(std::istream &is, VL & length, std::set<Tag> const &skiptags) {
228 (void)skiptags;
229 return static_cast<TDE*>(this)->template ReadValueWithLength<TSwap>(is, length);
230 }
231
232 template <typename TDE, typename TSwap>
233 std::istream &ReadWithLength(std::istream &is, VL &length) {
234 return static_cast<TDE*>(this)->template ReadWithLength<TSwap>(is,length);
235 }
236
237 template <typename TDE, typename TSwap>
238 const std::ostream &Write(std::ostream &os) const {
239 return static_cast<const TDE*>(this)->template Write<TSwap>(os);
240 }
241
242protected:
244 // This is the value read from the file, might be different from the length of Value Field
245 VL ValueLengthField; // Can be 0xFFFFFFFF
246
247 // Value Representation
251
252 void SetValueFieldLength( VL vl, bool readvalues );
253};
254//-----------------------------------------------------------------------------
255inline std::ostream& operator<<(std::ostream &os, const DataElement &val)
256{
257 os << val.TagField;
258 os << "\t" << val.VRField;
259 os << "\t" << val.ValueLengthField;
260 if( val.ValueField )
261 {
262 val.ValueField->Print( os << "\t" );
263 }
264 return os;
265}
266
267inline bool operator!=(const DataElement& lhs, const DataElement& rhs)
268{
269 return ! ( lhs == rhs );
270}
271
272} // end namespace gdcm_ns
273
274#endif //GDCMDATAELEMENT_H
Class to represent binary value (array of bytes)
Definition gdcmByteValue.h:35
Class to represent a Data Element either Implicit or Explicit.
Definition gdcmDataElement.h:59
const VL & GetVL() const
Get VL.
Definition gdcmDataElement.h:74
VL GetLength() const
Definition gdcmDataElement.h:201
SmartPointer< Value > ValuePtr
Definition gdcmDataElement.h:249
const Tag & GetTag() const
Get Tag.
Definition gdcmDataElement.h:67
const std::ostream & Write(std::ostream &os) const
Definition gdcmDataElement.h:238
void Clear()
Clear Data Element (make Value empty and invalidate Tag & VR)
Definition gdcmDataElement.h:112
bool IsUndefinedLength() const
return if Value Length if of undefined length
Definition gdcmDataElement.h:153
void SetVL(const VL &vl)
Definition gdcmDataElement.h:79
void Empty()
Make Data Element empty (no Value)
Definition gdcmDataElement.h:109
VR const & GetVR() const
Definition gdcmDataElement.h:84
std::istream & ReadOrSkip(std::istream &is, std::set< Tag > const &skiptags)
Definition gdcmDataElement.h:211
ValuePtr ValueField
Definition gdcmDataElement.h:250
bool operator<(const DataElement &de) const
Definition gdcmDataElement.h:165
std::istream & ReadValueWithLength(std::istream &is, VL &length, std::set< Tag > const &skiptags)
Definition gdcmDataElement.h:227
const ByteValue * GetByteValue() const
Definition gdcmDataElement.h:133
void SetTag(const Tag &t)
Definition gdcmDataElement.h:71
Tag TagField
Definition gdcmDataElement.h:243
bool IsEmpty() const
Check if Data Element is empty.
Definition gdcmDataElement.h:106
VL & GetVL()
Definition gdcmDataElement.h:75
bool operator==(const DataElement &de) const
Definition gdcmDataElement.h:172
std::istream & ReadWithLength(std::istream &is, VL &length)
Definition gdcmDataElement.h:233
VL ValueLengthField
Definition gdcmDataElement.h:245
DataElement(const DataElement &_val)
Definition gdcmDataElement.h:157
const SequenceOfFragments * GetSequenceOfFragments() const
DataElement & operator=(const DataElement &)=default
DataElement(const Tag &t=Tag(0), const VL &vl=0, const VR &vr=VR::INVALID)
Definition gdcmDataElement.h:61
Value const & GetValue() const
Set/Get Value (bytes array, SQ of items, SQ of fragments):
Definition gdcmDataElement.h:94
void SetValue(Value const &vl)
Definition gdcmDataElement.h:100
SequenceOfFragments * GetSequenceOfFragments()
SmartPointer< SequenceOfItems > GetValueAsSQ() const
void SetByteValue(const char *array, VL length)
Definition gdcmDataElement.h:126
VR VRField
Definition gdcmDataElement.h:248
Tag & GetTag()
Definition gdcmDataElement.h:68
Value & GetValue()
Definition gdcmDataElement.h:95
std::istream & ReadPreValue(std::istream &is, std::set< Tag > const &skiptags)
Definition gdcmDataElement.h:217
std::istream & ReadValue(std::istream &is, std::set< Tag > const &skiptags)
Definition gdcmDataElement.h:222
std::istream & Read(std::istream &is)
Definition gdcmDataElement.h:206
void SetVR(VR const &vr)
Definition gdcmDataElement.h:88
void SetValueFieldLength(VL vl, bool readvalues)
virtual void Print(std::ostream &) const
Definition gdcmObject.h:87
Class to represent a Sequence Of Fragments.
Definition gdcmSequenceOfFragments.h:32
Class for Smart Pointer.
Definition gdcmSmartPointer.h:40
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39
Value Length.
Definition gdcmVL.h:30
VR class.
Definition gdcmVR.h:55
bool IsVRFile() const
Class to represent the value of a Data Element.
Definition gdcmValue.h:32
virtual VL GetLength() const =0
#define gdcmAssertAlwaysMacro(arg)
AssertAlways.
Definition gdcmTrace.h:228
#define GDCM_EXPORT
Definition gdcmWin32.h:34
std::ostream & operator<<(std::ostream &os, const Directory &d)
Definition gdcmDirectory.h:88
bool operator!=(const CodeString &ref, const CodeString &cs)
Definition gdcmCodeString.h:97