Oracle Coherence for C++ API
Release 3.7.1.0
E22845-01
00001 /* 00002 * AbstractTypedClass.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_ABSTRACT_TYPED_CLASS_HPP 00017 #define COH_ABSTRACT_TYPED_CLASS_HPP 00018 00019 #include "coherence/lang/compatibility.hpp" 00020 00021 #include "coherence/lang/Class.hpp" 00022 #include "coherence/lang/ObjectArray.hpp" 00023 00024 #include <typeinfo> 00025 00026 COH_OPEN_NAMESPACE2(coherence,lang) 00027 00028 /** 00029 * AbstractTypedClass provides an abstract templated implementation of Class. 00030 * 00031 * @author pf 2010.08.11 00032 * @since Coherence 3.7 00033 */ 00034 template<class T> class AbstractTypedClass 00035 : public abstract_spec<AbstractTypedClass<T>, 00036 extends<Class> > 00037 { 00038 // ----- typedefs ------------------------------------------------------- 00039 00040 public: 00041 /** 00042 * Class type. 00043 */ 00044 typedef T Type; 00045 00046 00047 // ----- constructors --------------------------------------------------- 00048 00049 protected: 00050 /** 00051 * Initialize an AbstractTypedClass. 00052 */ 00053 AbstractTypedClass<T>() 00054 : abstract_spec<AbstractTypedClass<T>, extends<Class> >(typeid(T)) 00055 { 00056 } 00057 00058 private: 00059 /** 00060 * Blocked copy constructor 00061 */ 00062 AbstractTypedClass<T>(const AbstractTypedClass<T>&); 00063 00064 00065 // ----- AbstractTypedClass interface ------------------------------------------- 00066 00067 public: 00068 /** 00069 * {@inheritDoc} 00070 */ 00071 virtual const std::type_info& getTypeInfo() const 00072 { 00073 return typeid(T); 00074 } 00075 00076 /** 00077 * {@inheritDoc} 00078 */ 00079 virtual size32_t getSize() const 00080 { 00081 return (size32_t) sizeof(T); 00082 } 00083 00084 /** 00085 * {@inheritDoc} 00086 */ 00087 virtual bool isInstance(Object::View v) const 00088 { 00089 return instanceof<typename T::View>(v); 00090 } 00091 00092 /** 00093 * {@inheritDoc} 00094 */ 00095 virtual bool isAssignableFrom(Class::View vClass) const 00096 { 00097 if (typeid(T) == typeid(Object)) 00098 { 00099 return true; 00100 } 00101 00102 while (vClass != NULL) 00103 { 00104 if (vClass == this) 00105 { 00106 return true; 00107 } 00108 ObjectArray::View va = vClass->getInterfaces(); 00109 for (size32_t i = 0, c = va->length; i < c; ++i) 00110 { 00111 if (va[i] == this) 00112 { 00113 return true; 00114 } 00115 } 00116 try 00117 { 00118 vClass = vClass->getSuperclass(); 00119 } 00120 catch (... /* ClassNotFound */) 00121 { 00122 return false; 00123 } 00124 } 00125 00126 return false; 00127 } 00128 00129 /** 00130 * {@inheritDoc} 00131 */ 00132 virtual Class::View getSuperclass() const 00133 { 00134 const std::type_info& ti = typeid(typename T::inherited); 00135 if (ti == typeid(Void<>)) 00136 { 00137 return NULL; 00138 } 00139 return coh_loadClassByType(ti); 00140 } 00141 00142 #define COH_GET_INTERFACE(I) \ 00143 if (typeid(typename T::interface_##I) != typeid(void)) \ 00144 try \ 00145 { \ 00146 ha[i] = coh_loadClassByType(typeid(typename T::interface_##I)); \ 00147 ++i; \ 00148 } \ 00149 catch (... /*ClassNotFound*/) {} 00150 00151 /** 00152 * {@inheritDoc} 00153 */ 00154 virtual ObjectArray::View getInterfaces() const 00155 { 00156 if (typeid(typename T::interface_1) == typeid(void)) 00157 { 00158 // no interfaces 00159 return ObjectArray::create(0); 00160 } 00161 // else there can be up to 16 interfaces, see interface_spec 00162 00163 ObjectArray::Handle ha = ObjectArray::create(16); 00164 size32_t i = 0; 00165 00166 COH_GET_INTERFACE(1) 00167 COH_GET_INTERFACE(2) 00168 COH_GET_INTERFACE(3) 00169 COH_GET_INTERFACE(4) 00170 COH_GET_INTERFACE(5) 00171 COH_GET_INTERFACE(6) 00172 COH_GET_INTERFACE(7) 00173 COH_GET_INTERFACE(8) 00174 COH_GET_INTERFACE(9) 00175 COH_GET_INTERFACE(10) 00176 COH_GET_INTERFACE(11) 00177 COH_GET_INTERFACE(12) 00178 COH_GET_INTERFACE(13) 00179 COH_GET_INTERFACE(14) 00180 COH_GET_INTERFACE(15) 00181 COH_GET_INTERFACE(16) 00182 00183 if (i == ha->length) 00184 { 00185 return ha; 00186 } 00187 return ObjectArray::copy(ha, 0, ObjectArray::create(i)); 00188 } 00189 }; 00190 00191 COH_CLOSE_NAMESPACE2 00192 00193 #endif // COH_ABSTRACT_TYPED_CLASS_HPP