Savarese Software Research Corporation
CTypeWrapper.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2009 Savarese Software Research Corporation
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     https://www.savarese.com/software/ApacheLicense-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00022 #ifndef __SSRC_WSPR_UTILITY_C_TYPE_WRAPPER_H
00023 #define __SSRC_WSPR_UTILITY_C_TYPE_WRAPPER_H
00024 
00025 #include <ssrc/wispers-packages.h>
00026 
00027 #include <boost/shared_ptr.hpp>
00028 
00029 __BEGIN_NS_SSRC_WSPR_UTILITY
00030 
00031 template<typename target_type, typename error_type,
00032          typename init_type, typename delete_type,
00033          template <typename t> class smart_ptr_type = boost::shared_ptr>
00034 class CTypeWrapper : protected smart_ptr_type<target_type> {
00035   typedef smart_ptr_type<target_type> super;
00036 
00037   target_type *_target;
00038   error_type _err;
00039 
00040 public:
00041 
00042   CTypeWrapper() : super(), _target(0) {
00043     init_type initialize;
00044     delete_type deleter;
00045 
00046     _err = initialize(&_target);
00047     if(_target) {
00048       this->reset(_target, deleter);
00049     }
00050   }
00051 
00052   template<typename init_parameter>
00053   explicit CTypeWrapper(const init_parameter & value) : super(), _target(0) {
00054     init_type initialize;
00055     delete_type deleter;
00056 
00057     _err = initialize(&_target, value);
00058     if(_target) {
00059       this->reset(_target, deleter);
00060     }
00061   }
00062 
00063   template<typename init_parameter, typename custom_initializer>
00064   CTypeWrapper(const init_parameter & value,
00065                const custom_initializer & initialize) :
00066     super(), _target(0)
00067   {
00068     delete_type deleter;
00069 
00070     _err = initialize(&_target, value);
00071     if(_target) {
00072       this->reset(_target, deleter);
00073     }
00074   }
00075 
00076   template<typename init_parameter,
00077            typename custom_initializer,
00078            typename custom_deleter>
00079   explicit CTypeWrapper(const init_parameter & value,
00080                         const custom_initializer & initialize,
00081                         const custom_deleter & deleter) :
00082     super(), _target(0)
00083   {
00084     _err = initialize(&_target, value);
00085     if(_target) {
00086       this->reset(_target, deleter);
00087     }
00088   }
00089 
00090   using super::operator typename super::unspecified_bool_type;
00091   using super::operator!;
00092   using super::operator*;
00093   using super::operator->;
00094   using super::get;
00095 
00096   error_type init_error() {
00097     return _err;
00098   }
00099 };
00100 
00101 __END_NS_SSRC_WSPR_UTILITY
00102 
00103 #endif

Savarese Software Research Corporation
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.