EWebKit  1.0
ewk_object_private.h
1 /*
2  * Copyright (C) 2012 Intel Corporation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef ewk_object_private_h
27 #define ewk_object_private_h
28 
29 #include <wtf/RefCounted.h>
30 
31 class EwkObject : public RefCounted<EwkObject> {
32 public:
33  virtual ~EwkObject() { }
34  virtual const char* instanceClassName() const = 0;
35 };
36 
37 template <class T>
38 inline bool ewk_object_is_of_type(const EwkObject* object)
39 {
40  return (reinterpret_cast<T>(0)->className() == object->instanceClassName());
41 }
42 
43 template <class T>
44 inline bool ewk_object_cast_check(const EwkObject* object)
45 {
46  EINA_SAFETY_ON_NULL_RETURN_VAL(object, false);
47 
48  if (!ewk_object_is_of_type<T>(object)) {
49  EINA_LOG_CRIT("attempt to convert object of type %s to type %s",
50  object->instanceClassName(), reinterpret_cast<T>(0)->className());
51  ASSERT_NOT_REACHED();
52  return false;
53  }
54 
55  return true;
56 }
57 
58 template <class T>
59 inline const T ewk_object_cast(const EwkObject* object)
60 {
61  return ewk_object_cast_check<T>(object) ? static_cast<T>(object) : 0;
62 }
63 
64 template <class T>
65 inline T ewk_object_cast(EwkObject* object)
66 {
67  return ewk_object_cast_check<T>(object) ? static_cast<T>(object) : 0;
68 }
69 
70 #define EWK_OBJ_GET_IMPL_OR_RETURN(ImplClass, object, impl, ...) \
71  ImplClass* impl = ewk_object_cast<ImplClass*>(object); \
72  if (!impl) \
73  return __VA_ARGS__
74 
75 
76 #define EWK_OBJECT_DECLARE(_className) \
77 static const char* className() \
78 { \
79  static const char* name = #_className; \
80  return name; \
81 } \
82 virtual const char* instanceClassName() const \
83 { \
84  return className(); \
85 }
86 
87 #endif // ewk_object_private_h
Definition: ewk_object_private.h:31