MeVisLab Toolbox Reference
DCMTree_Tree.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2007, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef DCM_TREE_TREE_H
14 #define DCM_TREE_TREE_H
15 
16 #include "DCMTree_Lib.h"
17 #include "DCMTree_TagId.h"
18 #include "DCMTree_Defines.h"
19 
20 #ifdef _MSC_VER
21  // 4365: signed/unsigned mismatch
22  // 4548: expression before comma has no effect; expected expression with side-effect
23  // 4623: default constructor was implicitly defined as deleted because a base class default constructor is inaccessible or deleted
24  #pragma warning (push)
25  #pragma warning (disable : 4365 4548 4623)
26 #endif
27 #include "DCMTree_Tag.h"
28 
29 #include <iostream>
30 #include <fstream>
31 #include <set>
32 #ifdef _MSC_VER
33  #pragma warning (pop)
34 #endif
35 
40 namespace DCMTree
41 {
43  {
44  public:
46  Tree();
47 
52  Tree(const Tree &other);
53 
54  Tree(Tree&& other) noexcept = default;
55 
62  Tree (Const_TreePtr parent);
63 
65  ~Tree() override = default;
66 
69 
71  Const_TreePtr root () const;
72 
74  bool hasParent() const;
75 
77  bool hasDict() const;
78 
81 
84  void setDict (Const_DictPtr dictPtr);
85 
91  Tree &operator=(const Tree &other);
92  Tree& operator=(Tree&& other) noexcept = default;
93 
98  const TagPtrMap &tags();
99 
106  const TagPtrMap &constTags_rw() const;
107 
111  boost::uint32_t tagsSize() const;
112 
117 
121  TagIdVectorPtr tagIds (const TagId &fromtag, const TagId &totag, TagSelector sel=AllTags) const;
122 
127  bool hasTag (const TagId &tagid, TagSelector sel=AllTags) const;
128 
133  bool hasTag (const std::string& tagName, TagSelector sel=AllTags) const;
134 
139  Const_TagPtr getTag (const TagId &tagid, TagSelector sel=AllTags) const;
140 
147  Const_TagPtr getTag (const TagId &tagid, const std::string& privateCreator, Vr privateVr=UN, TagSelector sel=AllTags) const;
148 
153  Const_TagPtr getTag (const std::string& tagName, TagSelector sel=AllTags) const;
154 
159  TagPtr getTag_rw (const TagId &tagid);
160 
166 
171  Const_TagPtrMapPtr getTags (const TagId &fromtag, const TagId &totag, TagSelector sel=AllTags) const;
172 
178  void setTag (const TagId &tagid, Const_TagPtr tagdata);
183  void setTag (Const_TagPtr tagdata);
184 
189  void setTags (Const_TreePtr tags);
190 
192  void addTag (const TagId &tagid, TagPtr tagdata);
197  void addTag (TagPtr tagdata);
198 
199  /* Adds the given tags to this. It does \e not copy and parent tags are ignored. **/
200  void addTags (TreePtr tags);
201 
205  void removeTag (const TagId &tagid);
206 
211 
215  TreePtr copy() const;
216 
220  TreePtr copy (const TagId &fromtag, const TagId &totag) const;
221 
226 
231 
232 
238  std::string getPrivateCreator (TagId tagId) const;
239 
246  unsigned short findPrivateSlot (unsigned short groupid, const std::string &creator,
247  bool newOrExisting = false) const;
248 
256  unsigned short getPrivateSlot (unsigned short groupid, const std::string &creator);
257 
261  void removePrivateTags (unsigned short groupId, unsigned short slot);
262 
266  bool convertCharacterSet (const std::string &originalcharacterset,const std::string &newcharacterset);
267 
273  void fromStream (std::istream &in, bool readName=false, size_t maxSize = 0);
274 
280  void toStream (std::ostream &out, bool writeName=false) const;
281 
282  void serializeX (DCMTree_Serialization::Sink &sink) const override;
284 
285 
286  private:
287 
291  void addTagsToSet (std::set<RawTagId> &tagIdSet, TagSelector sel, const TagId &fromtag, const TagId &totag) const;
292 
296  void addTagsToMap (Const_TagPtrMap &tagMap, TagSelector sel, const TagId &fromtag, const TagId &totag) const;
297 
299  Const_TagPtr const_tag4tagid (const TagId &tagid,const TagSelector sel) const;
300 
302  TagPtr tag4tagid (const TagId &tagid);
303 
308 
313  void checkForTag (const TagId &tagid,const TagSelector sel) const;
314 
316  bool isDeleted (const TagId &tagid) const;
317 
319  void markAsDeleted (const TagId &tagid);
320 
322  void undelete (const TagId &tagid);
323 
324 
326  TagPtrMap _tags;
327 
331  std::set<RawTagId> _deletedtags;
332 
338  Const_TreePtr _parent;
339 
341  Const_DictPtr _dictionary;
342  };
343 
344 
346  inline std::ostream &operator << (std::ostream &out,const Tree &t)
347  {
348  t.toStream (out, false);
349  return out;
350  }
351 
352 
354  inline std::istream &operator >> (std::istream &in,Tree &t)
355  {
356  t.fromStream (in, false);
357  return in;
358  }
359 
360  template <typename T, T (Tag::*Access)(unsigned) const>
361  T value_or(const Tree& tree, TagId id, unsigned index, T alternative)
362  {
363  T result;
364  if (const auto tag = tree.getTag(id); tag && index < tag->numberOfValues() && !tag->isNull(index))
365  {
366  result = ((*tag).*Access)(index);
367  }
368  else
369  {
370  result = std::move(alternative);
371  }
372  return result;
373  }
374 
375  DCMTREE_EXPORT int value_or(const Tree& tree, TagId id, unsigned index, int alternative);
376 
377  DCMTREE_EXPORT unsigned int value_or(const Tree& tree, TagId id, unsigned index, unsigned int alternative);
378 
379  DCMTREE_EXPORT signed short value_or(const Tree& tree, TagId id, unsigned index, signed short alternative);
380 
381  DCMTREE_EXPORT unsigned short value_or(const Tree& tree, TagId id, unsigned index, unsigned short alternative);
382 
383  DCMTREE_EXPORT double value_or(const Tree& tree, TagId id, unsigned index, double alternative);
384 
385  DCMTREE_EXPORT std::string value_or(const Tree& tree, TagId id, unsigned index, std::string alternative);
386 
387  template <typename T>
388  T value_or(const Tree& tree, TagId id, T alternative)
389  {
390  return value_or(tree, id, 0, std::move(alternative));
391  }
392 }
393 
394 #endif
395 
396 
#define DCMTREE_EXPORT
@ T
Definition: SoKeyGrabber.h:71
Class to wrap a tag ID.
Definition: DCMTree_TagId.h:40
bool convertCharacterSet(const std::string &originalcharacterset, const std::string &newcharacterset)
Converts from one character set to another; returns false if the conversion was not possible.
Const_TagPtr getTag(const TagId &tagid, const std::string &privateCreator, Vr privateVr=UN, TagSelector sel=AllTags) const
Returns read access to the private tag with the given TagId.
TreePtr mergedCopy(TagSelector sel) const
Makes a copy of this, creating a parentless tree.
Const_TagPtr getTag(const std::string &tagName, TagSelector sel=AllTags) const
Returns read access to the tag with the given tagName.
bool hasParent() const
Checks whether this has a parent.
void fromStream(std::istream &in, bool readName=false, size_t maxSize=0)
Extracts this instance from the given stream.
unsigned short getPrivateSlot(unsigned short groupid, const std::string &creator)
Searches for a private creator tag with specified group ID and private creator string and returns its...
void removePrivateTags(unsigned short groupId, unsigned short slot)
Removes all tags associated with a private slot in the specified group (0x10 <= slot <= 0xff).
const TagPtrMap & constTags_rw() const
Accesses tags map of this tree even if it is a const one to avoid expensive copy operations.
Tree()
Constructor.
Const_TagPtr getTag(const TagId &tagid, TagSelector sel=AllTags) const
Returns read access to the tag with the given TagId.
boost::uint32_t tagsSize() const
Returns the size of tags map.
std::string getPrivateCreator(TagId tagId) const
Returns the private creator string for the given tag ID.
void removeTag(const TagId &tagid)
Drops a tag with the given tagid.
void toStream(std::ostream &out, bool writeName=false) const
Copies this instance recursively into the given stream.
Const_DictPtr getDict() const
Returns a pointer to the dictionary, which can be NULL.
TreePtr copy(const TagId &fromtag, const TagId &totag) const
Makes a deep copy of this, including only tags between and including fromtag and totag.
Const_TagPtrMapPtr getTags(const TagId &fromtag, const TagId &totag, TagSelector sel=AllTags) const
Accesses tags map of this tree, ranging from fromtag to totag.
void addTag(TagPtr tagdata)
Convenience function for the above.
Tree & operator=(Tree &&other) noexcept=default
Const_TreePtr parent() const
Returns the parent as an immutable object.
void removeTags(TagIdVectorPtr tags)
Drops all given tags.
Tree(Const_TreePtr parent)
Initializes an empty tree representing an incremental modification to the specified parent tree.
const TagPtrMap & tags()
Accesses tags map of this tree, parent tags are ignored.
~Tree() override=default
Destructor.
Tree(const Tree &other)
Copies all values from other to this.
unsigned short findPrivateSlot(unsigned short groupid, const std::string &creator, bool newOrExisting=false) const
Searches for a private creator tag with specified group ID and private creator string.
TagPtr getTag_rw(const TagId &tagid)
Returns read/write access to the tag with the given TagId, search is restricted to owned tags.
void addTags(TreePtr tags)
void addTag(const TagId &tagid, TagPtr tagdata)
Adds a tag to this; tagdata is not copied.
TreePtr copy() const
Makes a deep copy of this, including a reference to the parent.
void serializeX(DCMTree_Serialization::Sink &sink) const override
Writes data of this object to sink.
bool hasTag(const TagId &tagid, TagSelector sel=AllTags) const
Checks whether this has the given tag.
void setTags(Const_TreePtr tags)
Sets or overwrites all tags in this from the given data.
Tree(Tree &&other) noexcept=default
TagIdVectorPtr tagIds(TagSelector sel=AllTags) const
Returns all tagIds of this tree in ascending order.
Const_TagPtrMapPtr getTags(TagSelector sel=AllTags) const
Accesses tags map of this tree.
bool hasTag(const std::string &tagName, TagSelector sel=AllTags) const
Checks whether this has the given tag.
Const_TreePtr root() const
Returns the immutable root object of this.
bool hasDict() const
Checks whether this has a pointer to a dictionary.
void deserializeX(DCMTree_Serialization::Source &source) override
Overwrites data of this object with data from source.
void setDict(Const_DictPtr dictPtr)
Sets the pointer to the dictionary.
void setTag(const TagId &tagid, Const_TagPtr tagdata)
Sets the tag with the given TagId.
TagIdVectorPtr tagIds(const TagId &fromtag, const TagId &totag, TagSelector sel=AllTags) const
Returns all tagIds of this tree ranging from fromtag to totag in ascending order.
Tree & operator=(const Tree &other)
Assignment operator.
TreePtr copy(TagIdVectorPtr tags) const
Makes a deep copy of this including, only tags whose IDs are within the given vector.
void setTag(Const_TagPtr tagdata)
Convenience function for the above.
Interface that provides a method to deserialize an object.
Interface that provides a method to serialize an object.
Interface of a data sink for the serialization of objects.
Interface of a data source for the deserialization of objects.
DCMTREE_EXPORT bool convertCharacterSet(const std::string &originalcharacterset, const std::string &newcharacterset, std::string &val)
Converts val from the original character set to the new character set.
Class to dump a DCMTree DICOM message into a human-readable file.
boost::shared_ptr< const Tag > Const_TagPtr
Definition: DCMTree_Lib.h:57
Vr
DICOM VR.
Definition: DCMTree_Lib.h:202
std::istream & operator>>(std::istream &in, Message &m)
TagSelector
Constants selecting a tag subset in a chain of incrementally modified DICOM trees.
Definition: DCMTree_Lib.h:274
@ AllTags
Select all tags from all trees in the chain that are not deleted in a successor.
Definition: DCMTree_Lib.h:275
boost::shared_ptr< const CharacterSetConverter > Const_CharacterSetConverterPtr
std::map< TagId, TagPtr > TagPtrMap
A map from tag ID to TagPtr.
Definition: DCMTree_Lib.h:151
boost::shared_ptr< const Tree > Const_TreePtr
Definition: DCMTree_Lib.h:67
std::map< TagId, Const_TagPtr > Const_TagPtrMap
Definition: DCMTree_Lib.h:152
boost::shared_ptr< Const_TagPtrMap > Const_TagPtrMapPtr
Definition: DCMTree_Lib.h:153
boost::shared_ptr< Tag > TagPtr
Shared pointer to tag.
Definition: DCMTree_Lib.h:53
T value_or(const Tree &tree, TagId id, unsigned index, T alternative)
Definition: DCMTree_Tree.h:361
boost::shared_ptr< const Dict > Const_DictPtr
Definition: DCMTree_Dict.h:41
boost::shared_ptr< Tree > TreePtr
Shared pointer to a DCMTree::Tree.
Definition: DCMTree_Lib.h:64
boost::shared_ptr< TagIdVector > TagIdVectorPtr
A shared pointer to a vector of TagIds.
Definition: DCMTree_Lib.h:147
std::ostream & operator<<(std::ostream &out, const Message &m)
boost::graph_traits< ml_graph_ptr >::vertex_descriptor source(graph_traits< ml_graph_ptr >::edge_descriptor e, const ml_graph_ptr)
Returns the vertex descriptor for u of the edge (u,v) represented by e.