]> Creatis software - creaImageIO.git/blob - src2/creaImageIOTreeNode.cpp
*** empty log message ***
[creaImageIO.git] / src2 / creaImageIOTreeNode.cpp
1 #include <creaImageIOTreeNode.h>
2 #include <creaImageIOTree.h>
3 #include <creaMessageManager.h>
4 #include <algorithm>
5
6 namespace creaImageIO
7 {
8   namespace tree
9   {
10
11     //=============================================================
12     /// Ctor with parent
13     Node::Node(Node* parent)
14       : mParent(parent),
15         mData(0),
16         mChildrenLoaded(false)
17     {
18       if (parent) 
19         {
20           // Insert into parent's children list
21           parent->GetChildrenList().push_back(this);
22           // Initialize attributes
23           LevelDescriptor::AttributeDescriptorListType::const_iterator a;
24           for (a = GetTree()->GetAttributeDescriptorList(GetLevel()).begin();
25                a!= GetTree()->GetAttributeDescriptorList(GetLevel()).end();
26                ++a)
27             {
28               UnsafeSetAttribute( a->GetKey(), "" );
29             }
30         }
31     }
32     //=============================================================
33
34     //=============================================================
35     /// Ctor with parent and attributes map 
36     Node::Node(Node* parent, const AttributeMapType& attr)
37      : mParent(parent),
38         mData(0),
39         mChildrenLoaded(false)
40     {
41       if (parent) 
42         {
43           // Insert into parent's children list
44           parent->GetChildrenList().push_back(this);
45           // Initialize attributes
46           LevelDescriptor::AttributeDescriptorListType::const_iterator a;
47           for (a = GetTree()->GetAttributeDescriptorList(GetLevel()).begin();
48                a!= GetTree()->GetAttributeDescriptorList(GetLevel()).end();
49                ++a)
50             {
51               UnsafeSetAttribute( a->GetKey(), attr[a->GetKey()] );
52             }
53         }
54       
55     }
56     //=============================================================
57
58
59     //=============================================================
60     Node::~Node()
61     {
62       ChildrenListType::iterator i;
63       for (i=GetChildrenList().begin(); i!=GetChildrenList().end(); i++)
64         {
65           delete *i;
66         }
67       if (mData) 
68         {
69           delete mData;
70           mData = 0;
71         }
72     }
73     //=============================================================
74
75   //=============================================================
76     void Node::RemoveChildrenFromList(Node* node)
77     {
78       ChildrenListType::iterator i = find(GetChildrenList().begin(),
79                                           GetChildrenList().end(),
80                                           node);
81       if (i != GetChildrenList().end())
82         {
83           GetChildrenList().erase(i);
84         }
85     }
86     //=============================================================
87
88     //=============================================================
89     const std::string& Node::GetAttribute(const std::string& k) const
90     {
91       //    std::cout << "this = "<<(void*)this<<std::endl;
92       //    std::cout << "mFieldValueMap="<<(void*)(&mFieldValueMap)<<std::endl;
93       AttributeMapType::const_iterator i = mAttributeMap.find(k);
94       if (i == mAttributeMap.end())
95         {
96           static std::string def("");
97           return def;
98           //    CREAIMAGEIO_ERROR("DicomNode::GetFieldValue : no field with key '"<<k<<"'");
99         }
100       return i->second;
101   }
102   //=============================================================
103
104   //=============================================================
105     void Node::SetAttribute(const std::string& k, 
106                                  const std::string& v)
107   {
108     AttributeMapType::iterator i = mAttributeMap.find(k);
109     if (i==mAttributeMap.end())
110       {
111         std::cout<<"[Gimmick!] Node::SetAttribute : no attribute with key '"
112                  <<k<<"'"<<std::endl;
113         creaError( "[Gimmick!] Node::SetAttribute : no attribute with key '"
114                    <<k<<"'");
115       }
116     i->second = v;
117   }
118     //=============================================================
119
120     //=============================================================
121     bool Node::Matches(  const AttributeMapType& ) const
122     {
123       // TO DO 
124       return false;
125     }
126     //=============================================================
127   }
128
129 }
130