00001 /* 00002 * class_spec.hpp 00003 * 00004 * Copyright (c) 2000, 2010, 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_CLASS_SPEC_HPP 00017 #define COH_CLASS_SPEC_HPP 00018 00019 #include "coherence/lang/compatibility.hpp" 00020 00021 #include "coherence/lang/lang_spec.hpp" 00022 #include "coherence/lang/TypedHandle.hpp" 00023 #include "coherence/lang/TypedHolder.hpp" 00024 00025 COH_OPEN_NAMESPACE2(coherence,lang) 00026 00027 class Object; 00028 00029 extern COH_EXPORT void coh_throw_clone_not_supported(const std::type_info&); 00030 00031 00032 /** 00033 * Helper for defining a non-cloneable concrete managed class. 00034 * 00035 * Managed classes are implementations of coherence::lang::Object, and include 00036 * a set of well known features, which are auto-generated by this helper class: 00037 * 00038 * - Handle/View/Holder definitions 00039 * - super class definition 00040 * - virtual interface inheritance of up to 16 interfaces 00041 * - public static create methods which delegate to protected constructors with 00042 * up to sixteen arguments 00043 * - automatic sizeOf() definition 00044 * 00045 * The template takes three parameters: 00046 * 00047 * - The name of the class being defined 00048 * - The defined classes parent class, indicated as extends<parent> 00049 * - An optional list of interfaces to implement, indicated as 00050 * implements<i1, i2, ...> 00051 * 00052 * A normal class definition would be: 00053 * @code 00054 * class Foo 00055 * : public class_spec<Foo, 00056 * extends<Bar>, 00057 * implements<SomeInterface, SomeOtherInterface> > 00058 * { 00059 * // add support for auto-generated static create methods 00060 * friend class factory<Foo>; 00061 * 00062 * protected: 00063 * // Constructors are defined as protected, and access via 00064 * // auto-generated create methods, with matching signatures 00065 * Foo() 00066 * : super() // calls Bar() 00067 * { 00068 * } 00069 * 00070 * public: 00071 * // normal class definition.... 00072 * }; 00073 * @endcode 00074 * 00075 * @see extends 00076 * @see implements 00077 * 00078 * @author mf 2008.07.14 00079 */ 00080 template<class T, class E = extends<Object, void>, class I = implements<> > 00081 class COH_EXPORT_SPEC class_spec 00082 : public E, public E::inherited, public virtual I::implements_chain 00083 { 00084 // ----- typedefs ------------------------------------------------------- 00085 00086 public: 00087 /** 00088 * Specification definition 00089 */ 00090 typedef class_spec this_spec; 00091 00092 /** 00093 * Factory for this class 00094 */ 00095 typedef factory<T> factory_spec; 00096 00097 /** 00098 * Definition T's actual parent class 00099 */ 00100 typedef class_spec super; 00101 00102 /** 00103 * Definition of the spec's parent class 00104 */ 00105 typedef typename E::inherited super_spec; 00106 00107 /** 00108 * Definition T's logical parent class 00109 */ 00110 typedef typename E::inherited inherited; 00111 00112 /** 00113 * @internal 00114 * 00115 * Definition T's alias 00116 */ 00117 typedef typename E::alias alias; 00118 00119 /** 00120 * Standard Handle definition 00121 */ 00122 typedef TypedHandle<T> Handle; 00123 00124 /** 00125 * Standard View definition 00126 */ 00127 typedef TypedHandle<const T> View; 00128 00129 /** 00130 * Standard Holder definition 00131 */ 00132 typedef TypedHolder<T> Holder; 00133 00134 00135 // ----- constructors --------------------------------------------------- 00136 00137 protected: 00138 /** 00139 * Generate a set of proxy constructors matching the signatures of the 00140 * parent class's constructors. 00141 * 00142 * NOTE: Compilation errors referencing this line likely indicate that 00143 * class being defined by this spec makes calls a "super" 00144 * constructor supplying a set of parameters for which there is 00145 * no exact match on the parent class. 00146 */ 00147 COH_DEFINE_PROXY_CONSTRUCTORS(class_spec) 00148 00149 public: 00150 /** 00151 * Generate a set of static "create" methods matching the signatures of 00152 * class T's constructors. 00153 * 00154 * NOTE: Compilation errors referencing this line likely indicate that 00155 * the parameters supplied by the caller to the create method did 00156 * not match one of the constructors. 00157 */ 00158 COH_DEFINE_CREATE_METHODS(Handle, factory_spec::create) 00159 00160 virtual TypedHandle<Object> clone() const 00161 { 00162 coh_throw_clone_not_supported(typeid(T)); 00163 return NULL; 00164 } 00165 00166 virtual size32_t sizeOf() const 00167 { 00168 return (size32_t) sizeof(T); 00169 } 00170 00171 virtual void* _cast(const std::type_info* pInfo) const 00172 { 00173 // optimistically test for class equality based on type_info 00174 // reference equality; this may result in false negatives, but 00175 // not false positives 00176 static const std::type_info* pInfoThis = &typeid(T); 00177 if (pInfoThis == pInfo) 00178 { 00179 return (void*) static_cast<const T*>(this); 00180 } 00181 // recurse down class trunk 00182 void* pCast = super_spec::_cast(pInfo); 00183 // traverse interfaces upon trunk cast failure 00184 return pCast ? pCast : I::implements_chain::_icast(pInfo); 00185 } 00186 00187 protected: 00188 /** 00189 * @internal 00190 * 00191 * Protect access to create method generated for the copy constructor. 00192 */ 00193 static inline Handle create(const T& that) 00194 { 00195 return Handle(factory_spec::create(that)); 00196 } 00197 }; 00198 00199 COH_CLOSE_NAMESPACE2 00200 00201 #endif // COH_CLASS_SPEC_HPP