00001 #ifndef GLW_OBJECT_H 00002 #define GLW_OBJECT_H 00003 00004 #include "./bookkeeping.h" 00005 #include "./noncopyable.h" 00006 #include "./objectdeleter.h" 00007 #include "./type.h" 00008 #include "./glheaders.h" 00009 00010 namespace glw 00011 { 00012 00013 class Context; 00014 00015 class ObjectArguments 00016 { 00017 public: 00018 00019 typedef void BaseType; 00020 typedef ObjectArguments ThisType; 00021 00022 ObjectArguments(void) 00023 { 00024 ; 00025 } 00026 00027 void clear(void) 00028 { 00029 ; 00030 } 00031 }; 00032 00033 class Object : public detail::NonCopyable 00034 { 00035 friend class Context; 00036 00037 public: 00038 00039 typedef detail::NonCopyable BaseType; 00040 typedef Object ThisType; 00041 00042 virtual ~Object(void) 00043 { 00044 this->destroy(); 00045 } 00046 00047 bool isValid(void) const 00048 { 00049 return ((this->m_name != 0) && this->doIsValid()); 00050 } 00051 00052 const Context * context(void) const 00053 { 00054 return this->m_context; 00055 } 00056 00057 Context * context(void) 00058 { 00059 return this->m_context; 00060 } 00061 00062 GLuint name(void) const 00063 { 00064 return this->m_name; 00065 } 00066 00067 virtual Type type(void) const = 0; 00068 00069 protected: 00070 00071 GLuint m_name; 00072 Context * m_context; 00073 00074 Object(Context * ctx) 00075 : m_name (0) 00076 , m_context (ctx) 00077 { 00078 ; 00079 } 00080 00081 void destroy(void) 00082 { 00083 if (this->m_name == 0) return; 00084 this->doDestroy(); 00085 this->m_name = 0; 00086 this->m_context = 0; 00087 } 00088 00089 virtual void doDestroy(void) = 0; 00090 virtual bool doIsValid(void) const = 0; 00091 }; 00092 00093 namespace detail { template <typename T> struct ObjectBase { typedef NoType Type; }; }; 00094 namespace detail { template <typename T> struct ObjectSafe { typedef NoType Type; }; }; 00095 namespace detail { template <typename T> struct ObjectBound { typedef NoType Type; }; }; 00096 00097 namespace detail { template <> struct BaseOf <Object> { typedef NoType Type; }; }; 00098 namespace detail { template <> struct DeleterOf <Object> { typedef ObjectDeleter Type; }; }; 00099 typedef detail::ObjectSharedPointerTraits <Object> ::Type ObjectPtr; 00100 00101 class SafeObject : public detail::NonCopyable 00102 { 00103 friend class Context; 00104 friend class BoundObject; 00105 00106 public: 00107 00108 typedef detail::NonCopyable BaseType; 00109 typedef SafeObject ThisType; 00110 00111 SafeObject(void) 00112 : m_object(0) 00113 { 00114 ; 00115 } 00116 00117 virtual ~SafeObject(void) 00118 { 00119 ; 00120 } 00121 00122 bool isNull(void) const 00123 { 00124 return this->m_object.isNull(); 00125 } 00126 00127 bool isValid(void) const 00128 { 00129 return this->m_object->isValid(); 00130 } 00131 00132 const Context * context(void) const 00133 { 00134 if (this->isNull()) return 0; 00135 return this->m_object->context(); 00136 } 00137 00138 Context * context(void) 00139 { 00140 if (this->isNull()) return 0; 00141 return this->m_object->context(); 00142 } 00143 00144 GLuint name(void) const 00145 { 00146 if (this->isNull()) return 0; 00147 return this->m_object->name(); 00148 } 00149 00150 Type type(void) const 00151 { 00152 if (this->isNull()) return InvalidType; 00153 return this->m_object->type(); 00154 } 00155 00156 protected: 00157 00158 SafeObject(const ObjectPtr & object) 00159 : m_object(object) 00160 { 00161 ; 00162 } 00163 00164 const ObjectPtr & object(void) const 00165 { 00166 return this->m_object; 00167 } 00168 00169 ObjectPtr & object(void) 00170 { 00171 return this->m_object; 00172 } 00173 00174 private: 00175 00176 ObjectPtr m_object; 00177 }; 00178 00179 namespace detail { template <> struct BaseOf <SafeObject> { typedef NoType Type; }; }; 00180 namespace detail { template <> struct DeleterOf <SafeObject> { typedef DefaultDeleter<SafeObject> Type; }; }; 00181 namespace detail { template <> struct ObjectBase <SafeObject> { typedef Object Type; }; }; 00182 namespace detail { template <> struct ObjectSafe <Object > { typedef SafeObject Type; }; }; 00183 typedef detail::ObjectSharedPointerTraits <SafeObject> ::Type ObjectHandle; 00184 00185 class ObjectBindingParams 00186 { 00187 public: 00188 00189 typedef void BaseType; 00190 typedef ObjectBindingParams ThisType; 00191 00192 GLenum target; 00193 GLint unit; 00194 00195 ObjectBindingParams(void) 00196 : target (GL_NONE) 00197 , unit (0) 00198 { 00199 ; 00200 } 00201 00202 ObjectBindingParams(GLenum aTarget, GLenum aUnit) 00203 : target (aTarget) 00204 , unit (aUnit) 00205 { 00206 ; 00207 } 00208 }; 00209 00210 class BoundObject : public detail::NonCopyable 00211 { 00212 friend class Context; 00213 00214 public: 00215 00216 typedef detail::NonCopyable BaseType; 00217 typedef BoundObject ThisType; 00218 00219 BoundObject(void) 00220 : m_handle (0) 00221 , m_target (GL_NONE) 00222 , m_unit (0) 00223 { 00224 ; 00225 } 00226 00227 virtual ~BoundObject(void) 00228 { 00229 ; 00230 } 00231 00232 bool isNull(void) const 00233 { 00234 return this->m_handle.isNull(); 00235 } 00236 00237 const ObjectHandle & handle(void) const 00238 { 00239 return this->m_handle; 00240 } 00241 00242 ObjectHandle & handle(void) 00243 { 00244 return this->m_handle; 00245 } 00246 00247 GLenum target(void) const 00248 { 00249 return this->m_target; 00250 } 00251 00252 GLint unit(void) const 00253 { 00254 return this->m_unit; 00255 } 00256 00257 protected: 00258 00259 ObjectHandle m_handle; 00260 GLenum m_target; 00261 GLint m_unit; 00262 00263 BoundObject(const ObjectHandle & handle, const ObjectBindingParams & params) 00264 : m_handle (handle) 00265 , m_target (params.target) 00266 , m_unit (params.unit) 00267 { 00268 ; 00269 } 00270 00271 const ObjectPtr & object(void) const 00272 { 00273 return this->handle()->object(); 00274 } 00275 00276 ObjectPtr & object(void) 00277 { 00278 return this->handle()->object(); 00279 } 00280 00281 virtual void bind (void) = 0; 00282 virtual void unbind (void) = 0; 00283 }; 00284 00285 namespace detail { template <typename T> struct ParamsOf { typedef NoType Type; }; }; 00286 00287 namespace detail { template <> struct ParamsOf <BoundObject> { typedef ObjectBindingParams Type; }; }; 00288 namespace detail { template <> struct BaseOf <BoundObject> { typedef NoType Type; }; }; 00289 namespace detail { template <> struct DeleterOf <BoundObject> { typedef DefaultDeleter<BoundObject> Type; }; }; 00290 namespace detail { template <> struct ObjectBase <BoundObject> { typedef Object Type; }; }; 00291 namespace detail { template <> struct ObjectBound <Object > { typedef BoundObject Type; }; }; 00292 typedef detail::ObjectSharedPointerTraits <BoundObject> ::Type BoundObjectHandle; 00293 00294 }; 00295 00296 #endif // GLW_OBJECT_H