Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(495)

Side by Side Diff: include/core/SkFlattenable.h

Issue 23021015: Initial error handling code (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Fixing comments Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2006 The Android Open Source Project 3 * Copyright 2006 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #ifndef SkFlattenable_DEFINED 10 #ifndef SkFlattenable_DEFINED
11 #define SkFlattenable_DEFINED 11 #define SkFlattenable_DEFINED
12 12
13 #include "SkRefCnt.h" 13 #include "SkRefCnt.h"
14 14
15 class SkFlattenableReadBuffer; 15 class SkFlattenableReadBuffer;
16 class SkFlattenableWriteBuffer; 16 class SkFlattenableWriteBuffer;
17 17
18 #define SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(flattenable) \ 18 #define SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(flattenable) \
19 SkFlattenable::Registrar(#flattenable, flattenable::CreateProc); 19 SkFlattenable::Registrar(#flattenable, flattenable::CreateProc, flattena ble::IsA);
20 20
21 #define SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() static void InitializeFlattenab les(); 21 #define SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() static void InitializeFlattenab les();
22 22
23 #define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(flattenable) \ 23 #define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(flattenable) \
24 void flattenable::InitializeFlattenables() { 24 void flattenable::InitializeFlattenables() {
25 25
26 #define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END \ 26 #define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END \
27 } 27 }
28 28
29 #define SK_DECLARE_UNFLATTENABLE_OBJECT() \ 29 #define SK_DECLARE_UNFLATTENABLE_OBJECT() \
30 virtual Factory getFactory() SK_OVERRIDE { return NULL; } 30 virtual Factory getFactory() SK_OVERRIDE { return NULL; }
31 31
32 #define SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(flattenable) \ 32 /** For SkFlattenable derived objects with a valid type
33 This macros would typically be used by virtual classes, which need a type, b ut can't
34 be constructed or serialized directly without going through a derived class
35 */
36 #define SK_DEFINE_FLATTENABLE_TYPE_CHECKING_PROCS(flattenable) \
37 static const char* GetTypeName() { \
38 return #flattenable; \
39 } \
40 static bool IsA(const char* name) { \
41 return (strcmp(flattenable::GetTypeName(), name) == 0) || INHERITED::IsA (name); \
42 } \
43 virtual const char* getTypeName() SK_OVERRIDE { \
44 return flattenable::GetTypeName(); \
45 }
46
47 /** For SkFlattenable derived classes defined within the skia library
48 */
49 #define SK_DEFINE_FLATTENABLE_DESERIALIZATION_PROCS(flattenable) \
33 virtual Factory getFactory() SK_OVERRIDE { return CreateProc; } \ 50 virtual Factory getFactory() SK_OVERRIDE { return CreateProc; } \
34 static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { \ 51 static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { \
35 return SkNEW_ARGS(flattenable, (buffer)); \ 52 return SkNEW_ARGS(flattenable, (buffer)); \
36 } 53 } \
54 SK_DEFINE_FLATTENABLE_TYPE_CHECKING_PROCS(flattenable)
55
56 /** Deprecated, remove once noone is using it anymore
57 */
58 #define SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(flattenable) \
59 SK_DEFINE_FLATTENABLE_DESERIALIZATION_PROCS(flattenable)
37 60
38 /** \class SkFlattenable 61 /** \class SkFlattenable
39 62
40 SkFlattenable is the base class for objects that need to be flattened 63 SkFlattenable is the base class for objects that need to be flattened
41 into a data stream for either transport or as part of the key to the 64 into a data stream for either transport or as part of the key to the
42 font cache. 65 font cache.
43 */ 66 */
44 class SK_API SkFlattenable : public SkRefCnt { 67 class SK_API SkFlattenable : public SkRefCnt {
45 public: 68 public:
69
46 SK_DECLARE_INST_COUNT(SkFlattenable) 70 SK_DECLARE_INST_COUNT(SkFlattenable)
47 71
48 typedef SkFlattenable* (*Factory)(SkFlattenableReadBuffer&); 72 typedef SkFlattenable* (*Factory)(SkFlattenableReadBuffer&);
73 typedef bool (*TypeCheck)(const char*);
49 74
50 SkFlattenable() {} 75 SkFlattenable() {}
51 76
52 /** Implement this to return a factory function pointer that can be called 77 /** Implement this to return a factory function pointer that can be called
53 to recreate your class given a buffer (previously written to by your 78 to recreate your class given a buffer (previously written to by your
54 override of flatten(). 79 override of flatten().
55 */ 80 */
56 virtual Factory getFactory() = 0; 81 virtual Factory getFactory() = 0;
57 82
58 static Factory NameToFactory(const char name[]); 83 static Factory NameToFactory(const char name[]);
59 static const char* FactoryToName(Factory); 84 static const char* FactoryToName(Factory);
60 static void Register(const char name[], Factory); 85
86 static void Register(const char name[], Factory, TypeCheck);
87
88 /** Returns the class id
89 GetTypeName() methods on classes descending from SkFlattenable will retu rn
90 the name associated with that class, which is a stringified version of
91 the class' name. Typically, SK_DEFINE_FLATTENABLE_TYPE_CHECKING_PROCS()
92 will define GetTypeName(), getFlattenableType() and IsA() in the derived class.
93 * Note that SK_DEFINE_FLATTENABLE_TYPE_CHECKING_PROCS() is included in
94 SK_DEFINE_FLATTENABLE_DESERIALIZATION_PROCS(), so most classes will ha ve
95 these declared by default.
96 */
97 static const char* GetTypeName() { return "SkFlattenable"; }
98
99 /** Returns the name of the object's class
100 */
101 virtual const char* getTypeName() = 0;
102
103 /** Returns whether this class is itself or inherits from a class with this type name.
104 * (SkFlattenable is the root of this type checking system.)
105 */
106 static bool IsA(const char* name) {
107 return strcmp(GetTypeName(), name) == 0;
108 }
109
110 /** Checks if typeA is a typeB
111 * For example, if typeA is "SkImageFilter" and typeB is "SkFlattenable", t his would return true
112 * Inversely, if typeA is "SkFlattenable" and typeB is "SkImageFilter", thi s would return false
113 */
114 static bool TypeIsA(const char* nameA, const char* nameB) {
115 TypeCheck typeCheck = NameToTypeCheck(nameA);
116 return typeCheck ? (*typeCheck)(nameB) : false;
117 }
61 118
62 class Registrar { 119 class Registrar {
63 public: 120 public:
64 Registrar(const char name[], Factory factory) { 121 Registrar(const char name[], Factory factory, TypeCheck typeCheck) {
65 SkFlattenable::Register(name, factory); 122 SkFlattenable::Register(name, factory, typeCheck);
66 } 123 }
67 }; 124 };
68 125
69 protected: 126 protected:
70 SkFlattenable(SkFlattenableReadBuffer&) {} 127 SkFlattenable(SkFlattenableReadBuffer&) {}
71 /** Override this to write data specific to your subclass into the buffer, 128 /** Override this to write data specific to your subclass into the buffer,
72 being sure to call your super-class' version first. This data will later 129 being sure to call your super-class' version first. This data will later
73 be passed to your Factory function, returned by getFactory(). 130 be passed to your Factory function, returned by getFactory().
74 */ 131 */
75 virtual void flatten(SkFlattenableWriteBuffer&) const; 132 virtual void flatten(SkFlattenableWriteBuffer&) const;
76 133
77 private: 134 private:
78 static void InitializeFlattenables(); 135 static void InitializeFlattenablesIfNeeded();
136 static TypeCheck NameToTypeCheck(const char name[]);
79 137
80 friend class SkGraphics; 138 friend class SkGraphics;
81 friend class SkFlattenableWriteBuffer; 139 friend class SkFlattenableWriteBuffer;
82 140
83 typedef SkRefCnt INHERITED; 141 typedef SkRefCnt INHERITED;
84 }; 142 };
85 143
86 #endif 144 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698