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  /* add the given tags to this, NO copy, 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);
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 
329  std::set<RawTagId> _deletedtags;
330 
336  Const_TreePtr _parent;
337 
339  Const_DictPtr _dictionary;
340  };
341 
342 
344  inline std::ostream &operator << (std::ostream &out,const Tree &t)
345  {
346  t.toStream (out, false);
347  return out;
348  }
349 
350 
352  inline std::istream &operator >> (std::istream &in,Tree &t)
353  {
354  t.fromStream (in, false);
355  return in;
356  }
357 
358  template <typename T, T (Tag::*Access)(unsigned) const>
359  T value_or(const Tree& tree, TagId id, unsigned index, T alternative)
360  {
361  T result;
362  if (const auto tag = tree.getTag(id); tag && index < tag->numberOfValues() && !tag->isNull(index))
363  {
364  result = ((*tag).*Access)(index);
365  }
366  else
367  {
368  result = std::move(alternative);
369  }
370  return result;
371  }
372 
373  DCMTREE_EXPORT int value_or(const Tree& tree, TagId id, unsigned index, int alternative);
374 
375  DCMTREE_EXPORT unsigned int value_or(const Tree& tree, TagId id, unsigned index, unsigned int alternative);
376 
377  DCMTREE_EXPORT signed short value_or(const Tree& tree, TagId id, unsigned index, signed short alternative);
378 
379  DCMTREE_EXPORT unsigned short value_or(const Tree& tree, TagId id, unsigned index, unsigned short alternative);
380 
381  DCMTREE_EXPORT double value_or(const Tree& tree, TagId id, unsigned index, double alternative);
382 
383  DCMTREE_EXPORT std::string value_or(const Tree& tree, TagId id, unsigned index, std::string alternative);
384 
385  template <typename T>
386  T value_or(const Tree& tree, TagId id, T alternative)
387  {
388  return value_or(tree, id, 0, std::move(alternative));
389  }
390 }
391 
392 #endif
393 
394 
#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)
convert 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
Get read access to the private tag with the given TagId.
TreePtr mergedCopy(TagSelector sel) const
make a copy of this making a parent-less tree
Const_TagPtr getTag(const std::string &tagName, TagSelector sel=AllTags) const
get read access to the tag with the given tagName returns empty pointer if not found
bool hasParent() const
check if this has a parent
unsigned short getPrivateSlot(unsigned short groupid, const std::string &creator)
search for a private creator tag with specified group id and private creator string and returns its e...
void removePrivateTags(unsigned short groupId, unsigned short slot)
remove all tags associated with a private slot in the specified group (0x10 <= slot <= 0xff)
const TagPtrMap & constTags_rw() const
access tags map of this tree even if it is a const one to avoid expensive copy operations.
Const_TagPtr getTag(const TagId &tagid, TagSelector sel=AllTags) const
get read access to the tag with the given TagId returns empty pointer if not found
boost::uint32_t tagsSize() const
get size of tags map
std::string getPrivateCreator(TagId tagId) const
Return the private creator string for the given tag id.
void removeTag(const TagId &tagid)
drop a tag with the given Tagid
void toStream(std::ostream &out, bool writeName=false) const
copy this instance recursively into the given stream writeName = true writes tag name(s) to stream,...
Const_DictPtr getDict() const
returns the pointer to the dictionary, which can be NULL
TreePtr copy(const TagId &fromtag, const TagId &totag) const
make a deep-copy of this including only tags between (including) fromtag and totag
Const_TagPtrMapPtr getTags(const TagId &fromtag, const TagId &totag, TagSelector sel=AllTags) const
access tags map of this tree, ranging from fromtag to totag (const, returns a map with const pointers...
void addTag(TagPtr tagdata)
Convenience function for the above - uses the tag id contained in the tagdata.
Tree & operator=(Tree &&other) noexcept=default
Const_TreePtr parent() const
get the parent as unchangeable object
void removeTags(TagIdVectorPtr tags)
drops all given tags
Tree(Const_TreePtr parent)
initialize empty tree representing an incremental modification to the specified parent tree.
const TagPtrMap & tags()
access tags map of this tree, parent tags are ignored (non-const, the map allows write access to the ...
~Tree() override=default
dtor
Tree(const Tree &other)
copy all values from other to this uses operator=
unsigned short findPrivateSlot(unsigned short groupid, const std::string &creator, bool newOrExisting=false) const
search for a private creator tag with specified group id and private creator string.
TagPtr getTag_rw(const TagId &tagid)
get read/write access to the tag with the given TagId, search is restricted to owned tags returns emp...
void fromStream(std::istream &in, bool readName=false)
extract this instance from the given stream readName = true reads tag name(s) from stream,...
void addTags(TreePtr tags)
void addTag(const TagId &tagid, TagPtr tagdata)
add a tag to this, tagdata is NOT copied
TreePtr copy() const
make a deep-copy of this, including reference to 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
check if this has the given tag it searches only on the top-level, not in sequences!
void setTags(Const_TreePtr tags)
sets / overwrites all tags in this from the given data.
Tree(Tree &&other) noexcept=default
TagIdVectorPtr tagIds(TagSelector sel=AllTags) const
get all tagids of this tree, ordered ascending
Const_TagPtrMapPtr getTags(TagSelector sel=AllTags) const
access tags map of this tree (const, returns a map with const pointers to the tag objects)
bool hasTag(const std::string &tagName, TagSelector sel=AllTags) const
check if this has the given tag it searches only on the top-level, not in sequences!
Const_TreePtr root() const
get the unchangeable root object of this
bool hasDict() const
check if 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)
set the tag with the given TagId overwrites / discards a previously existing tag the given tag is dee...
TagIdVectorPtr tagIds(const TagId &fromtag, const TagId &totag, TagSelector sel=AllTags) const
get all tagids of this tree ranging from fromtag to totag, ordered ascending
Tree & operator=(const Tree &other)
assignment operator copy all values from other to this handles self-assignment well
TreePtr copy(TagIdVectorPtr tags) const
make 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 - uses the tag id contained in the tagdata.
Interface which provides a method to deserialize an object.
Interface which 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)
convert val from the original character set to the new character set returns true if val has been cha...
class to dump a DCMTree dicom message into a human readable file this class could be used as a starte...
boost::shared_ptr< const Tag > Const_TagPtr
Definition: DCMTree_Lib.h:63
Vr
DICOM VR.
Definition: DCMTree_Lib.h:208
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:280
@ AllTags
Select all tags from all trees in the chain that are not deleted in a successor.
Definition: DCMTree_Lib.h:281
boost::shared_ptr< const CharacterSetConverter > Const_CharacterSetConverterPtr
std::map< TagId, TagPtr > TagPtrMap
a map from tag-id to TagPtr
Definition: DCMTree_Lib.h:157
boost::shared_ptr< const Tree > Const_TreePtr
Definition: DCMTree_Lib.h:73
std::map< TagId, Const_TagPtr > Const_TagPtrMap
Definition: DCMTree_Lib.h:158
boost::shared_ptr< Const_TagPtrMap > Const_TagPtrMapPtr
Definition: DCMTree_Lib.h:159
boost::shared_ptr< Tag > TagPtr
shared-pointer to tag
Definition: DCMTree_Lib.h:59
T value_or(const Tree &tree, TagId id, unsigned index, T alternative)
Definition: DCMTree_Tree.h:359
boost::shared_ptr< const Dict > Const_DictPtr
Definition: DCMTree_Dict.h:53
boost::shared_ptr< Tree > TreePtr
shared pointer to a DCMTree::Tree
Definition: DCMTree_Lib.h:70
boost::shared_ptr< TagIdVector > TagIdVectorPtr
a shared-pointer to a vector of TagIds
Definition: DCMTree_Lib.h:153
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.