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