473c26f64d85c2ccc3bfcca90dfc586425ab7aac
[framework/security/security-server.git] / ace / include / ace / TreeNode.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 //
17 //
18 //
19 //  @ Project : Access Control Engine
20 //  @ File Name : TreeNode.h
21 //  @ Date : 2009-05-06
22 //  @ Author : Samsung
23 //
24 //
25
26 #ifndef _TREE_NODE_H
27 #define _TREE_NODE_H
28
29 #include <iostream>
30 #include <list>
31
32 #include <ace/AbstractTreeElement.h>
33
34 class TreeNode;
35
36 typedef std::list<TreeNode *> ChildrenSet;
37 typedef std::list<TreeNode *>::iterator ChildrenIterator;
38 typedef std::list<TreeNode *>::const_iterator ChildrenConstIterator;
39
40 class TreeNode
41 {
42   public:
43     //TODO nazwac pozadnie TYPY - moze jakas konwencja ... ??!!
44     enum TypeID { Policy =0, PolicySet=1, Rule=2};
45
46     const ChildrenSet  & getChildrenSet() const
47     {
48         return children;
49     }
50
51     TreeNode * getParent() const
52     {
53         return this->parent;
54     }
55
56     void setParent(TreeNode *parent)
57     {
58         this->parent = parent;
59     }
60
61     TypeID getTypeID() const
62     {
63         return this->typeID;
64     }
65
66     void addChild(TreeNode *child)
67     {
68         child->setParent(this);
69         children.push_back(child);
70     }
71
72     /**
73      * Clone the node
74      */
75     // KW        TreeNode * clone() { return new TreeNode(NULL,this->getTypeID(),this->getElement()); }
76
77     TreeNode(TreeNode * parent,
78             TypeID type,
79             AbstractTreeElement * element) :
80         parent(parent),
81         typeID(type),
82         element(element)
83     {
84     }
85
86     AbstractTreeElement * getElement() const
87     {
88         return element;
89     }
90
91   private:
92     virtual ~TreeNode();
93
94   public:
95     /*
96      * It is common that we create a copy of tree structure created out of xml file. However we don't want to
97      * copy abstract elements ( Policies and Rules ) because we need them only for reading. We want to modify the
98      * tree structure though. Therefore we copy TreeNode. When the copy of the original tree is being destroyed method
99      * releaseTheSubtree should be called on "root". It automatically traverse the tree and call TreeNode destructors for
100      * each TreeNode in the tree. It doesn't remove the abstract elements in the tree ( there is always at most one abstract
101      * element instance, when tree is copied it is a shallow copy.
102      * When we want to completely get rid of the the tree and abstract elements we have to call releaseResources on tree root.
103      * We may want to do this for instance when we want to serialize the tree to disc. releaseResource method traverses the tree
104      * and releses the resources, as well as the TreeNode so NO releaseTheSubtree is required any more
105      */
106     void releaseResources();
107
108     /**
109      * Used to delete the copies of tree structure. The original tree structure should be removed with releaseResources method.
110      * ReleaseTheSubtree method doesn't delete the abstract elements, only TreeNodes. It traverses the whole tree, so it should be
111      * called on behalf of root of the tree
112      */
113     // KW        void releaseTheSubtree();
114
115     friend std::ostream & operator<<(std::ostream & out,
116             const TreeNode * node);
117     // KW        void printSubtree();
118
119   private:
120     // KW    TreeNode(const TreeNode& pattern){ (void)pattern; }
121
122     std::list<TreeNode *> children;
123     TreeNode * parent;
124     //TODO standarize ID case
125     TypeID typeID;
126     AbstractTreeElement * element;
127     static int level;
128 };
129
130 #endif  //_TREE_NODE_H