1 #ifndef NETCACHE__SRV_REF__HPP
2 #define NETCACHE__SRV_REF__HPP
3 /*  $Id: srv_ref.hpp 368417 2012-07-06 18:44:04Z ivanovp $
4  * ===========================================================================
5  *
6  *                            PUBLIC DOMAIN NOTICE
7  *               National Center for Biotechnology Information
8  *
9  *  This software/database is a "United States Government Work" under the
10  *  terms of the United States Copyright Act.  It was written as part of
11  *  the author's official duties as a United States Government employee and
12  *  thus cannot be copyrighted.  This software/database is freely available
13  *  to the public for use. The National Library of Medicine and the U.S.
14  *  Government have not placed any restriction on its use or reproduction.
15  *
16  *  Although all reasonable efforts have been taken to ensure the accuracy
17  *  and reliability of the software and data, the NLM and the U.S.
18  *  Government do not and cannot warrant the performance or results that
19  *  may be obtained by using this software or data. The NLM and the U.S.
20  *  Government disclaim all warranties, express or implied, including
21  *  warranties of performance, merchantability or fitness for any particular
22  *  purpose.
23  *
24  *  Please cite the author in any work or product based on this material.
25  *
26  * ===========================================================================
27  *
28  * Authors:  Pavel Ivanov
29  *
30  * File Description:
31  */
32 
33 
34 BEGIN_NCBI_SCOPE
35 
36 
37 /// Special variant of CRef that doesn't check for NULL when dereferencing.
38 /// Segmentation fault in such case is much better than thrown exception.
39 template <class C, class Locker = typename CLockerTraits<C>::TLockerType>
40 class CSrvRef : public CRef<C, Locker>
41 {
42     typedef CSrvRef<C, Locker> TThisType;
43     typedef CRef<C, Locker> TParent;
44     typedef typename TParent::TObjectType TObjectType;
45     typedef typename TParent::locker_type locker_type;
46 
47 public:
CSrvRef(void)48     CSrvRef(void)
49     {}
CSrvRef(ENull)50     CSrvRef(ENull /*null*/)
51     {}
52     explicit
CSrvRef(TObjectType * ptr)53     CSrvRef(TObjectType* ptr)
54         : TParent(ptr)
55     {}
CSrvRef(TObjectType * ptr,const locker_type & locker_value)56     CSrvRef(TObjectType* ptr, const locker_type& locker_value)
57         : TParent(ptr, locker_value)
58     {}
CSrvRef(const TThisType & ref)59     CSrvRef(const TThisType& ref)
60         : TParent(ref)
61     {}
62 
operator =(const TThisType & ref)63     TThisType& operator= (const TThisType& ref)
64     {
65         TParent::operator= (ref);
66         return *this;
67     }
operator =(TObjectType * ptr)68     TThisType& operator= (TObjectType* ptr)
69     {
70         TParent::operator= (ptr);
71         return *this;
72     }
operator =(ENull)73     TThisType& operator= (ENull /*null*/)
74     {
75         TParent::operator= (null);
76         return *this;
77     }
78 
operator *(void)79     TObjectType& operator* (void)
80     {
81         return *TParent::GetPointerOrNull();
82     }
operator ->(void)83     TObjectType* operator-> (void)
84     {
85         return TParent::GetPointerOrNull();
86     }
operator *(void) const87     const TObjectType& operator* (void) const
88     {
89         return *TParent::GetPointerOrNull();
90     }
operator ->(void) const91     const TObjectType* operator-> (void) const
92     {
93         return TParent::GetPointerOrNull();
94     }
95 };
96 
97 
98 template<class C>
99 inline
SrvRef(C * object)100 CSrvRef<C> SrvRef(C* object)
101 {
102     return CSrvRef<C>(object);
103 }
104 
105 
106 END_NCBI_SCOPE
107 
108 #endif /* NETCACHE__SRV_REF__HPP */
109