tizen beta release
[framework/web/wrt-commons.git] / modules / core / include / dpl / auto_ptr.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  * @file        auto_ptr.h
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @author      Tomasz Swierczek (t.swierczek@samsung.com)
20  * @version     1.0
21  * @brief       This file contais definictions of specific AutoPtr class.
22  */
23 #ifndef _AUTO_PTR_H_
24 #define _AUTO_PTR_H_
25
26 #include <list>
27 #include <set>
28 #include <string>
29
30 namespace DPL {
31 /*
32  * base deleter func
33  */
34 template <typename T>
35 struct UniversalFree {};
36
37 // Template Specialization
38 #define DECLARE_DELETER(type, function)           \
39     template <> \
40     struct UniversalFree <type> {           \
41         void universal_free(type *ptr){                  \
42             if (ptr) {                                      \
43                 function(ptr); }                           \
44         }                                                \
45     };
46
47 template <typename T>
48 class AutoPtr
49 {
50   public:
51     AutoPtr(T *ptr) :
52         m_data(ptr)
53     {
54     }
55
56     AutoPtr(const AutoPtr<T> &second)
57     {
58         m_data = second.m_data;
59         second.m_data = 0;
60     }
61
62     AutoPtr & operator=(const AutoPtr &second)
63     {
64         if (this != &second) {
65             UniversalFree<T> deleter;
66             deleter.universal_free(m_data);
67             m_data = second.m_data;
68             second.m_data = 0;
69         }
70         return *this;
71     }
72
73     /**
74      * overloaded -> operator, so smart ptr could act as ordinary ptr
75      */
76     T* operator->()
77     {
78         return m_data;
79     }
80
81     ~AutoPtr()
82     {
83         UniversalFree<T> deleter;
84         deleter.universal_free(m_data);
85     }
86
87     /**
88      * get internal pointer
89      */
90     T* get(void)
91     {
92         return m_data;
93     }
94
95   private:
96     mutable T *m_data;
97 };
98 } // namespace DPL
99
100 #endif // AUTO_PTR