2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef _SSLCONTAINERS_H
17 #define _SSLCONTAINERS_H
19 #include <openssl/x509v3.h>
20 #include <openssl/ocsp.h>
23 * default deleter functor with no overloaded operator()
29 * macro for defining custom deleters for openssl structs
30 * usage DECLARE_DELETER(OpenSSLType)
32 #define DECLARE_DELETER(Type) template<> \
33 struct MySSLFree <Type> \
35 void operator() (Type* p) \
43 * declare custom deleter for X509 structs
47 * declare custom deleter for OCSP_REQUEST structs
49 DECLARE_DELETER(OCSP_REQUEST)
51 * declare custom deleter for OCSP_RESPONSE structs
53 DECLARE_DELETER(OCSP_RESPONSE)
55 * declare custom deleter for OCSP_CERTID structs
57 DECLARE_DELETER(OCSP_CERTID)
59 * declare custom deleter for OCSP_BASICRESP structs
61 DECLARE_DELETER(OCSP_BASICRESP)
63 * declare custom deleter for X509_STORE structs
65 DECLARE_DELETER(X509_STORE)
68 * undef it, so anyone could use that macro name
70 #undef DECLARE_DELETER
73 * OpenSSL smart container
74 * usage SSLSmartContainer <OpenSSLType> smartptr = ptrToOpenSSLType
75 * remember to add OpenSSLType to macro list few lines above
77 template <typename T, typename deleter = MySSLFree<T> >
78 class SSLSmartContainer
81 SSLSmartContainer() : m_pData(NULL)
86 * explicit constructor, we don't want any auto casting
88 explicit SSLSmartContainer(T* pData)
94 * overloaded assignment operator
96 SSLSmartContainer & operator=(SSLSmartContainer& pContainer)
99 * check if no assignment was done before
101 if (this != &pContainer) {
102 // if so, free internal data
107 m_pData = pContainer.m_pData;
109 pContainer.m_pData = NULL;
115 SSLSmartContainer & operator=(T* pData)
118 * check if no assignment was done before
120 if (m_pData != pData) {
121 // if so, free internal data
139 * overloaded operators for standardptr - like usage
141 SSLSmartContainer & operator*()
145 SSLSmartContainer* operator->()
151 * auto cast to T operator
153 operator T *() const { return m_pData;
157 * detachs internal pointer from smart pointer
168 * blocked assignment from another types operator
170 template <typename S>
171 T & operator = (S& pContainer)
182 #endif /* _SSLCONTAINERS_H */