C++ Client API Reference for Oracle Coherence
14c (14.1.2.0.0)

F79659-03

coherence/lang/BoxHandle.hpp

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