SkFlattenable.h revision 22d69f9973a7aa942b3ecb91931fbed4b52ecb98
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#define SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(flattenable) \
19        SkFlattenable::Registrar(#flattenable, flattenable::CreateProc);
20
21#define SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() static void InitializeFlattenables();
22
23#define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(flattenable) \
24    void flattenable::InitializeFlattenables() {
25
26#define SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END \
27    }
28
29#define SK_DECLARE_UNFLATTENABLE_OBJECT() \
30    virtual Factory getFactory() SK_OVERRIDE { return NULL; }
31
32#define SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(flattenable) \
33    virtual Factory getFactory() SK_OVERRIDE { return CreateProc; } \
34    static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { \
35        return SkNEW_ARGS(flattenable, (buffer)); \
36    }
37
38/** \class SkFlattenable
39
40 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
42 font cache.
43 */
44class SK_API SkFlattenable : public SkRefCnt {
45public:
46    SK_DECLARE_INST_COUNT(SkFlattenable)
47
48    typedef SkFlattenable* (*Factory)(SkFlattenableReadBuffer&);
49
50    SkFlattenable() {}
51
52    /** 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
54     override of flatten().
55     */
56    virtual Factory getFactory() = 0;
57
58    static Factory NameToFactory(const char name[]);
59    static const char* FactoryToName(Factory);
60    static void Register(const char name[], Factory);
61
62    class Registrar {
63    public:
64        Registrar(const char name[], Factory factory) {
65            SkFlattenable::Register(name, factory);
66        }
67    };
68
69protected:
70    SkFlattenable(SkFlattenableReadBuffer&) {}
71    /** 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
73     be passed to your Factory function, returned by getFactory().
74     */
75    virtual void flatten(SkFlattenableWriteBuffer&) const;
76
77private:
78    static void InitializeFlattenables();
79
80    friend class SkGraphics;
81    friend class SkFlattenableWriteBuffer;
82
83    typedef SkRefCnt INHERITED;
84};
85
86#endif
87