SkFlattenable.h revision fbfcd5602128ec010c82cb733c9cdc0a3254f9f3
1 2/* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10#ifndef SkFlattenable_DEFINED 11#define SkFlattenable_DEFINED 12 13#include "SkRefCnt.h" 14 15class SkFlattenableReadBuffer; 16class SkFlattenableWriteBuffer; 17 18#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS 19 20#define SK_DEFINE_FLATTENABLE_REGISTRAR(flattenable) \ 21 static SkFlattenable::Registrar g##flattenable##Reg(#flattenable, \ 22 flattenable::CreateProc); 23#define SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(flattenable) \ 24 static SkFlattenable::Registrar g##flattenable##Reg(#flattenable, \ 25 flattenable::CreateProc); 26 27#define SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() 28#define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(flattenable) 29#define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END 30 31#else 32 33#define SK_DEFINE_FLATTENABLE_REGISTRAR(flattenable) 34#define SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(flattenable) \ 35 SkFlattenable::Registrar(#flattenable, flattenable::CreateProc); 36 37#define SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() static void InitializeFlattenables(); 38 39#define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(flattenable) \ 40 void flattenable::InitializeFlattenables() { 41 42#define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END \ 43 } 44 45#endif 46 47#define SK_DECLARE_UNFLATTENABLE_OBJECT() \ 48 virtual Factory getFactory() SK_OVERRIDE { return NULL; }; \ 49 50#define SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(flattenable) \ 51 virtual Factory getFactory() SK_OVERRIDE { return CreateProc; } \ 52 static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { \ 53 return SkNEW_ARGS(flattenable, (buffer)); \ 54 } 55 56/** \class SkFlattenable 57 58 SkFlattenable is the base class for objects that need to be flattened 59 into a data stream for either transport or as part of the key to the 60 font cache. 61 */ 62class SK_API SkFlattenable : public SkRefCnt { 63public: 64 SK_DECLARE_INST_COUNT(SkFlattenable) 65 66 typedef SkFlattenable* (*Factory)(SkFlattenableReadBuffer&); 67 68 SkFlattenable() {} 69 70 /** Implement this to return a factory function pointer that can be called 71 to recreate your class given a buffer (previously written to by your 72 override of flatten(). 73 */ 74 virtual Factory getFactory() = 0; 75 76 static Factory NameToFactory(const char name[]); 77 static const char* FactoryToName(Factory); 78 static void Register(const char name[], Factory); 79 80 class Registrar { 81 public: 82 Registrar(const char name[], Factory factory) { 83 SkFlattenable::Register(name, factory); 84 } 85 }; 86 87protected: 88 SkFlattenable(SkFlattenableReadBuffer&) {} 89 /** Override this to write data specific to your subclass into the buffer, 90 being sure to call your super-class' version first. This data will later 91 be passed to your Factory function, returned by getFactory(). 92 */ 93 virtual void flatten(SkFlattenableWriteBuffer&) const; 94 95private: 96#if !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS 97 static void InitializeFlattenables(); 98#endif 99 100 friend class SkGraphics; 101 friend class SkFlattenableWriteBuffer; 102 103 typedef SkRefCnt INHERITED; 104}; 105 106#endif 107