Source code formating unification
[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     AutoPtr(const AutoPtr<T> &second)
56     {
57         m_data = second.m_data;
58         second.m_data = 0;
59     }
60
61     AutoPtr & operator=(const AutoPtr &second)
62     {
63         if (this != &second) {
64             UniversalFree<T> deleter;
65             deleter.universal_free(m_data);
66             m_data = second.m_data;
67             second.m_data = 0;
68         }
69         return *this;
70     }
71
72     /**
73      * overloaded -> operator, so smart ptr could act as ordinary ptr
74      */
75     T* operator->()
76     {
77         return m_data;
78     }
79
80     ~AutoPtr()
81     {
82         UniversalFree<T> deleter;
83         deleter.universal_free(m_data);
84     }
85
86     /**
87      * get internal pointer
88      */
89     T* get(void)
90     {
91         return m_data;
92     }
93
94   private:
95     mutable T *m_data;
96 };
97 } // namespace DPL
98
99 #endif // AUTO_PTR