Oracle Coherence for C++ API
Release 3.7.1.0
E22845-01
00001 /* 00002 * BoxHandle.hpp 00003 * 00004 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. 00005 * 00006 * Oracle is a registered trademarks of Oracle Corporation and/or its 00007 * affiliates. 00008 * 00009 * This software is the confidential and proprietary information of Oracle 00010 * Corporation. You shall not disclose such confidential and proprietary 00011 * information and shall use it only in accordance with the terms of the 00012 * license agreement you entered into with Oracle. 00013 * 00014 * This notice may not be removed or altered. 00015 */ 00016 #ifndef COH_BOX_HANDLE_HPP 00017 #define COH_BOX_HANDLE_HPP 00018 00019 #include "coherence/lang/compatibility.hpp" 00020 00021 #include "coherence/lang/Object.hpp" 00022 #include "coherence/lang/TypedHandle.hpp" 00023 00024 COH_OPEN_NAMESPACE2(coherence,lang) 00025 00026 00027 /** 00028 * A TypedHandle implementation which supports auto-boxing. 00029 * 00030 * To support boxing a managed class defines an inner-type named "BoxedType", 00031 * and includes a public static "create" method which takes that type. The 00032 * BoxedType defines the type which the BoxHandle can use to create a managed 00033 * object from. Alternatively the BoxHandle declaration can identify the 00034 * "BoxedType" via a secondary template parameter. 00035 * 00036 * Managed classes which wish to be auto-boxing may use this handle type 00037 * when defining their Handle and View definitions. See String as an 00038 * example. 00039 * 00040 * When used with integral types a natural ambiguity is introduced when 00041 * setting or comparing a BoxHandle to NULL, as it also an integral type. In 00042 * such cases one may use the is_null(h) and clear_handle(h) helper functions 00043 * to ensure the correct behavior. 00044 * 00045 * BoxHandle can be configured as strict, or non-strict. In strict mode 00046 * (the default), derefencing a NULL BoxHandle will result in a 00047 * NullPointerException. In non-strict mode, dereferencing a BoxHandle will 00048 * result in the BoxedType's default value being returned. 00049 * 00050 * @author mf 2007.07.05 00051 */ 00052 template<class T, class B = typename T::BoxedType, bool fStrict = true> 00053 class BoxHandle 00054 : public TypedHandle<T> 00055 { 00056 // ----- typedefs ------------------------------------------------------- 00057 00058 public: 00059 /* 00060 * The boxed type. 00061 */ 00062 typedef B BoxedType; 00063 00064 00065 // ----- constructors --------------------------------------------------- 00066 00067 public: 00068 /** 00069 * Create an empty BoxHandle. 00070 */ 00071 BoxHandle() 00072 : TypedHandle<T>() 00073 { 00074 } 00075 00076 /** 00077 * Create a new BoxHandle from the supplied BoxedType 00078 */ 00079 BoxHandle(const B& b) 00080 : TypedHandle<T>(T::create(b)) 00081 { 00082 } 00083 00084 /** 00085 * Create a new BoxHandle from the TypedHandle with a type conversion. 00086 */ 00087 template<class O> BoxHandle(const TypedHandle<O>& that) 00088 : TypedHandle<T>(that) 00089 { 00090 } 00091 00092 /** 00093 * Create a new BoxHandle from the raw pointer. 00094 */ 00095 BoxHandle(T* o) 00096 : TypedHandle<T>(o) 00097 { 00098 } 00099 00100 /** 00101 * The copy constructor. 00102 */ 00103 BoxHandle(const BoxHandle& h) 00104 : TypedHandle<T>(h) 00105 { 00106 } 00107 00108 // ----- operators ------------------------------------------------------ 00109 00110 public: 00111 /** 00112 * The "boxing" operator. 00113 */ 00114 BoxHandle& operator=(const BoxedType& b) 00115 { 00116 TypedHandle<T>::operator=(T::create(b)); 00117 return *this; 00118 } 00119 00120 /** 00121 * The "unboxing" operator. 00122 * 00123 * @return a copy of the referenced Object 00124 */ 00125 operator BoxedType() const 00126 { 00127 const T* pT = TypedHandle<T>::get(); 00128 if (NULL == pT) 00129 { 00130 if (fStrict) 00131 { 00132 coh_throw_npe(typeid(T)); 00133 } 00134 return BoxedType(); 00135 } 00136 return (BoxedType) *pT; 00137 } 00138 }; 00139 00140 template<class T, class B, bool S> 00141 bool operator==(const BoxHandle<T, B, S>& h1, const BoxHandle<T, B, S>& h2) 00142 { 00143 return operator==(Object::Holder(h1), Object::Holder(h2)); 00144 } 00145 00146 template<class T, class B, bool S> 00147 bool operator==(const BoxHandle<T, B, S>& h, const B& b) 00148 { 00149 const T* pT = get_pointer(h); 00150 if (S && pT == NULL) 00151 { 00152 coh_throw_npe(typeid(T)); 00153 } 00154 return NULL == pT ? B() == b : ((B) *pT) == b; 00155 } 00156 00157 template<class T, class B, bool S> 00158 bool operator==(const B& b, const BoxHandle<T, B, S>& h) 00159 { 00160 return operator==(h, b); 00161 } 00162 00163 template<class T, class B, bool S> 00164 bool operator!=(const BoxHandle<T, B, S>& h1, const BoxHandle<T, B, S>& h2) 00165 { 00166 return !operator==(h1, h2); 00167 } 00168 00169 template<class T, class B, bool S> 00170 bool operator!=(const BoxHandle<T, B, S>& h, const B& b) 00171 { 00172 return !operator==(h, b); 00173 } 00174 00175 template<class T, class B, bool S> 00176 bool operator!=(const B& b, const BoxHandle<T, B, S>& h) 00177 { 00178 return !operator==(h, b); 00179 } 00180 00181 COH_CLOSE_NAMESPACE2 00182 00183 #endif // COH_BOX_HANDLE_HPP