Update User Agent String
[framework/web/wrt-commons.git] / modules / core / 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 {
29 template<typename ClassPolicy>
30 class ScopedResource
31     : private Noncopyable
32 {
33   public:
34     typedef typename ClassPolicy::Type ValueType;
35     typedef ScopedResource<ClassPolicy> ThisType;
36
37   protected:
38     ValueType m_value;
39
40   public:
41     explicit ScopedResource(ValueType value) : m_value(value) { }
42
43     ~ScopedResource()
44     {
45         ClassPolicy::Destroy(m_value);
46     }
47
48     ValueType Get() const { return m_value; }
49
50     void Reset(ValueType value = ClassPolicy::NullValue())
51     {
52         ClassPolicy::Destroy(m_value);
53         m_value = value;
54     }
55
56     ValueType Release()
57     {
58         ValueType value = m_value;
59         m_value = ClassPolicy::NullValue();
60         return value;
61     }
62     typedef ValueType ThisType::*UnknownBoolType;
63
64     operator UnknownBoolType() const
65     {
66         return m_value == ClassPolicy::NullValue() ?
67                0 : //0 is valid here because it converts to false
68                &ThisType::m_value; //it converts to true
69     }
70
71     bool operator !() const
72     {
73         return m_value == ClassPolicy::NullValue();
74     }
75
76 };
77 } // namespace DPL
78
79 #endif // DPL_SCOPED_RESOURCE_H