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

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: Serialization with strings as ID 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 that can be serialized without type checki ng
33 */
34 #define SK_DEFINE_FLATTENABLE_SERIALIZABLE(flattenable) \
mtklein 2013/10/07 19:29:56 Maybe SK_DEFINE_FLATTENABLE_DESERIALIZATION_PROCS?
sugoi1 2013/10/08 20:23:10 Done.
33 virtual Factory getFactory() SK_OVERRIDE { return CreateProc; } \ 35 virtual Factory getFactory() SK_OVERRIDE { return CreateProc; } \
34 static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { \ 36 static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { \
35 return SkNEW_ARGS(flattenable, (buffer)); \ 37 return SkNEW_ARGS(flattenable, (buffer)); \
36 } 38 }
37 39
40 /** For SkFlattenable derived objects that need type checking
41 */
42 #define SK_DEFINE_FLATTENABLE_TYPE_CHECKING(flattenable) \
43 static bool IsA(const char* name) { \
44 return (strcmp(flattenable::GetName(), name) == 0) || INHERITED::IsA(nam e); \
sugoi1 2013/10/07 15:54:08 These type checks are now done using string compar
45 } \
46 virtual const char* getFlattenableName() SK_OVERRIDE { \
47 return flattenable::GetName(); \
48 }
49
50 /** For SkFlattenable derived objects with a valid type
51 This macros would typically be used by virtual classes, which need a type, b ut can't
52 be constructed or serialized directly without going through a derived class
53 */
54 #define SK_DEFINE_FLATTENABLE_TYPE(flattenable) \
mtklein 2013/10/07 19:29:56 Is there any case where we need to call SK_DEFINE_
sugoi1 2013/10/08 20:23:10 Yeah, I had already done this locally.
55 static const char* GetName() { \
56 return #flattenable; \
57 } \
58 SK_DEFINE_FLATTENABLE_TYPE_CHECKING(flattenable)
59
60 /** For SkFlattenable derived objects defined within the skia library
61 */
62 #define SK_DEFINE_FLATTENABLE_SERIALIZABLE_TYPE(flattenable) \
mtklein 2013/10/07 19:29:56 I think I may have been unclear before. It looks
sugoi1 2013/10/08 20:23:10 Done.
63 SK_DEFINE_FLATTENABLE_SERIALIZABLE(flattenable) \
64 SK_DEFINE_FLATTENABLE_TYPE(flattenable)
65
66 /** Deprecated, remove once noone is using it anymore
67 */
68 #define SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(flattenable) \
69 SK_DEFINE_FLATTENABLE_SERIALIZABLE_TYPE(flattenable)
70
71 /** This is for an instantiatable SkFlattenable derived class
72 with derived classes available in header files
73 */
74 #define SK_DECLARE_FLATTENABLE_GROUP_TYPE(flattenable) \
75 SK_DEFINE_FLATTENABLE_TYPE(flattenable) \
76 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
77
38 /** \class SkFlattenable 78 /** \class SkFlattenable
39 79
40 SkFlattenable is the base class for objects that need to be flattened 80 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 81 into a data stream for either transport or as part of the key to the
42 font cache. 82 font cache.
43 */ 83 */
44 class SK_API SkFlattenable : public SkRefCnt { 84 class SK_API SkFlattenable : public SkRefCnt {
45 public: 85 public:
86
46 SK_DECLARE_INST_COUNT(SkFlattenable) 87 SK_DECLARE_INST_COUNT(SkFlattenable)
47 88
48 typedef SkFlattenable* (*Factory)(SkFlattenableReadBuffer&); 89 typedef SkFlattenable* (*Factory)(SkFlattenableReadBuffer&);
90 typedef bool (*TypeCheck)(const char*);
49 91
50 SkFlattenable() {} 92 SkFlattenable() {}
51 93
52 /** Implement this to return a factory function pointer that can be called 94 /** 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 95 to recreate your class given a buffer (previously written to by your
54 override of flatten(). 96 override of flatten().
55 */ 97 */
56 virtual Factory getFactory() = 0; 98 virtual Factory getFactory() = 0;
57 99
58 static Factory NameToFactory(const char name[]); 100 static Factory NameToFactory(const char name[]);
59 static const char* FactoryToName(Factory); 101 static const char* FactoryToName(Factory);
60 static void Register(const char name[], Factory); 102
103 static void Register(const char name[], Factory, TypeCheck);
104
105 /** Returns the class id
106 GetName() methods on classes descending from SkFlattenable should return
107 the name associated with that class, or SkFlattenable::kNoneType if
mtklein 2013/10/07 19:29:56 kNoneType? comment generally needs an update?
sugoi1 2013/10/08 20:23:10 Done.
108 they're not part of the subset of SkFlattenables that can be used with
109 SkFlattenable::TypeToFactory(). Typycally, SK_DEFINE_FLATTENABLE_TYPE() will
110 define GetName(), getFlattenableType() and IsA() in the derived class.
111 * Note that SK_DEFINE_FLATTENABLE_TYPE() is included in other macros tha t include
112 the "_TYPE" suffix, so most classes will have these declared by defaul t.
mtklein 2013/10/07 19:29:56 I get that this seems convenient, but the implicit
sugoi1 2013/10/08 20:23:10 Done. I did leave it in SK_DEFINE_FLATTENABLE_DESE
113 External classes can use SK_DEFINE_FLATTENABLE_EXTERN(), which will do the same,
114 but set the class type to SkFlattenable::kNone_Type.
115 */
116 static const char* GetName() { return "SkFlattenable"; }
mtklein 2013/10/07 19:29:56 GetTypeName?
sugoi1 2013/10/08 20:23:10 Done.
117
118 /** Returns the class id of an object
mtklein 2013/10/07 19:29:56 class id of an object -> name of the object's clas
sugoi1 2013/10/08 20:23:10 Done.
119 */
120 virtual const char* getFlattenableName() = 0;
mtklein 2013/10/07 19:29:56 getTypeName()?
sugoi1 2013/10/08 20:23:10 Done.
121
122 /** Returns whether the current object is of the given type
mtklein 2013/10/07 19:29:56 There's no current object here? How about /** Re
sugoi1 2013/10/08 20:23:10 Done.
123 */
124 static bool IsA(const char* name) {
125 return strcmp(GetName(), name) == 0;
126 }
127
128 /** Checks if typeA is a typeB
129 * For example, if typeA is kSkImageFilter and typeB is kSkFlattenable, thi s would return true
mtklein 2013/10/07 19:29:56 "SkImageFilter" and "SkFlattenable"?
sugoi1 2013/10/08 20:23:10 Done.
130 * Inversely, if typeA is kSkFlattenable and typeB is kSkImageFilter, this would return false
131 */
132 static bool TypeIsA(const char* nameA, const char* nameB) {
133 TypeCheck typeCheck = NameToTypeCheck(nameA);
134 return typeCheck ? (*typeCheck)(nameB) : false;
135 }
61 136
62 class Registrar { 137 class Registrar {
63 public: 138 public:
64 Registrar(const char name[], Factory factory) { 139 Registrar(const char name[], Factory factory, TypeCheck typeCheck) {
65 SkFlattenable::Register(name, factory); 140 SkFlattenable::Register(name, factory, typeCheck);
66 } 141 }
67 }; 142 };
68 143
69 protected: 144 protected:
70 SkFlattenable(SkFlattenableReadBuffer&) {} 145 SkFlattenable(SkFlattenableReadBuffer&) {}
71 /** Override this to write data specific to your subclass into the buffer, 146 /** 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 147 being sure to call your super-class' version first. This data will later
73 be passed to your Factory function, returned by getFactory(). 148 be passed to your Factory function, returned by getFactory().
74 */ 149 */
75 virtual void flatten(SkFlattenableWriteBuffer&) const; 150 virtual void flatten(SkFlattenableWriteBuffer&) const;
76 151
77 private: 152 private:
78 static void InitializeFlattenables(); 153 static void InitializeFlattenables();
154 static TypeCheck NameToTypeCheck(const char name[]);
79 155
80 friend class SkGraphics; 156 friend class SkGraphics;
81 friend class SkFlattenableWriteBuffer; 157 friend class SkFlattenableWriteBuffer;
82 158
83 typedef SkRefCnt INHERITED; 159 typedef SkRefCnt INHERITED;
84 }; 160 };
85 161
86 #endif 162 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698