63287daa94c81985f4a5d1008c1250fa4359fc4f
[platform/core/test/security-tests.git] / tests / framework / include / dpl / scoped_resource.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        scoped_resource.h
18  * @author      Piotr Marcinkiewicz (p.marcinkiew@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of scoped resource pattern
21  */
22 #ifndef DPL_SCOPED_RESOURCE_H
23 #define DPL_SCOPED_RESOURCE_H
24
25 #include <dpl/noncopyable.h>
26
27 namespace DPL {
28 template<typename ClassPolicy>
29 class ScopedResource :
30     private Noncopyable
31 {
32   public:
33     typedef typename ClassPolicy::Type ValueType;
34     typedef ScopedResource<ClassPolicy> ThisType;
35
36   protected:
37     ValueType m_value;
38
39   public:
40     explicit ScopedResource(ValueType value) : m_value(value) { }
41
42     ~ScopedResource()
43     {
44         ClassPolicy::Destroy(m_value);
45     }
46
47     ValueType Get() const
48     {
49         return m_value;
50     }
51
52     void Reset(ValueType value = ClassPolicy::NullValue())
53     {
54         ClassPolicy::Destroy(m_value);
55         m_value = value;
56     }
57
58     ValueType Release()
59     {
60         ValueType value = m_value;
61         m_value = ClassPolicy::NullValue();
62         return value;
63     }
64     typedef ValueType ThisType::*UnknownBoolType;
65
66     operator UnknownBoolType() const
67     {
68         return m_value == ClassPolicy::NullValue() ?
69                0 : //0 is valid here because it converts to false
70                &ThisType::m_value; //it converts to true
71     }
72
73     bool operator !() const
74     {
75         return m_value == ClassPolicy::NullValue();
76     }
77 };
78 } // namespace DPL
79
80 #endif // DPL_SCOPED_RESOURCE_H