Tizen 2.1 base
[framework/web/wrt-commons.git] / modules / ace / include / dpl / 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 <dpl/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     virtual bool serialize(std::ostream& os);
47
48     TreeNode(std::istream& is,
49             TreeNode* parent);
50
51     const ChildrenSet  & getChildrenSet() const
52     {
53         return children;
54     }
55
56     TreeNode * getParent() const
57     {
58         return this->parent;
59     }
60
61     void setParent(TreeNode *parent)
62     {
63         this->parent = parent;
64     }
65
66     TypeID getTypeID() const
67     {
68         return this->typeID;
69     }
70
71     void addChild(TreeNode *child)
72     {
73         child->setParent(this);
74         children.push_back(child);
75     }
76
77     /**
78      * Clone the node
79      */
80     // KW        TreeNode * clone() { return new TreeNode(NULL,this->getTypeID(),this->getElement()); }
81
82     TreeNode(TreeNode * parent,
83             TypeID type,
84             AbstractTreeElement * element) :
85         parent(parent),
86         typeID(type),
87         element(element)
88     {
89     }
90
91     AbstractTreeElement * getElement() const
92     {
93         return element;
94     }
95
96   private:
97     virtual ~TreeNode();
98
99   public:
100     /*
101      * It is common that we create a copy of tree structure created out of xml file. However we don't want to
102      * copy abstract elements ( Policies and Rules ) because we need them only for reading. We want to modify the
103      * tree structure though. Therefore we copy TreeNode. When the copy of the original tree is being destroyed method
104      * releaseTheSubtree should be called on "root". It automatically traverse the tree and call TreeNode destructors for
105      * each TreeNode in the tree. It doesn't remove the abstract elements in the tree ( there is always at most one abstract
106      * element instance, when tree is copied it is a shallow copy.
107      * When we want to completely get rid of the the tree and abstract elements we have to call releaseResources on tree root.
108      * We may want to do this for instance when we want to serialize the tree to disc. releaseResource method traverses the tree
109      * and releses the resources, as well as the TreeNode so NO releaseTheSubtree is required any more
110      */
111     void releaseResources();
112
113     /**
114      * Used to delete the copies of tree structure. The original tree structure should be removed with releaseResources method.
115      * ReleaseTheSubtree method doesn't delete the abstract elements, only TreeNodes. It traverses the whole tree, so it should be
116      * called on behalf of root of the tree
117      */
118     // KW        void releaseTheSubtree();
119
120     friend std::ostream & operator<<(std::ostream & out,
121             const TreeNode * node);
122     // KW        void printSubtree();
123
124   private:
125     // KW    TreeNode(const TreeNode& pattern){ (void)pattern; }
126
127     std::list<TreeNode *> children;
128     TreeNode * parent;
129     //TODO standarize ID case
130     TypeID typeID;
131     AbstractTreeElement * element;
132     static int level;
133 };
134
135 #endif  //_TREE_NODE_H