Fix compilation with SK_ENABLE_INST_COUNT=1

Add INHERITED declarations to class declarations that prevent
compilation with the flag.

Remove SK_DEFINE_INST_COUNT from all class implementations.  Instead,
use function-local static variables in the reference count helper
classes to create the global instances to store the needed info. The
accessor functions are defined inline in the helper classes, so
definitions are not needed. The initialization point of the variables
should be as well defined as previously.

Remove SK_DECLARE_INST_COUNT_TEMPLATE and use SK_DECLARE_INST_COUNT
instead. This avoids possible future compilation errors further.

For SK_ENABLE_INST_COUNT=0 compilation, add an empty static member
function to all classes that use SK_DECLARE_INST_COUNT and
SK_DECLARE_INST_COUNT_ROOT macros. The function ensures that classes
contain public INHERITED typedef. This member function seems to be
compiled away. This shou─║d ensure that part of the compilation errors
are caught earlier.

Also adds DSK_DECLARE_INST_COUNT to few SkPDFDict subclasses.

R=robertphillips@google.com, richardlin@chromium.org, bsalomon@google.com

Author: kkinnunen@nvidia.com

Review URL: https://codereview.chromium.org/98703002

git-svn-id: http://skia.googlecode.com/svn/trunk/include@12501 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/core/SkInstCnt.h b/core/SkInstCnt.h
index cab8ebb..e38c42d 100644
--- a/core/SkInstCnt.h
+++ b/core/SkInstCnt.h
@@ -14,7 +14,6 @@
  * instance counting machinery. A class is added to the system by adding:
  *   SK_DECLARE_INST_COUNT at the top of its declaration for derived classes
  *   SK_DECLARE_INST_COUNT_ROOT at the top of its declaration for a root class
- *   SK_DEFINE_INST_COUNT at the top of its .cpp file (for both kinds).
  * At the end of an application a call to all the "root" objects'
  * CheckInstanceCount methods should be made
  */
@@ -29,47 +28,49 @@
 // The non-root classes just register themselves with their parent
 #define SK_DECLARE_INST_COUNT(className)                                    \
     SK_DECLARE_INST_COUNT_INTERNAL(className,                               \
-                               INHERITED::AddInstChild(CheckInstanceCount);,\
-                               /**/)
-
-#define SK_DECLARE_INST_COUNT_TEMPLATE(className)                           \
-    SK_DECLARE_INST_COUNT_INTERNAL(className,                               \
-                              INHERITED::AddInstChild(CheckInstanceCount);, \
-                              typename)
+                                   INHERITED::AddInstChild(CheckInstanceCount);)
 
 // The root classes registers a function to print out the memory stats when
 // the app ends
 #define SK_DECLARE_INST_COUNT_ROOT(className)                               \
-    SK_DECLARE_INST_COUNT_INTERNAL(className, atexit(exitPrint);, /**/)
+    SK_DECLARE_INST_COUNT_INTERNAL(className, atexit(exitPrint);)
 
-#define SK_DECLARE_INST_COUNT_INTERNAL(className, initStep, templateType)   \
+#define SK_DECLARE_INST_COUNT_INTERNAL(className, initStep)                 \
     class SkInstanceCountHelper {                                           \
     public:                                                                 \
         typedef int (*PFCheckInstCnt)(int level, bool cleanUp);             \
         SkInstanceCountHelper() {                                           \
+            static bool gInited;                                            \
             if (!gInited) {                                                 \
                 initStep                                                    \
-                gChildren = new SkTArray<PFCheckInstCnt>;                   \
+                GetChildren() = new SkTArray<PFCheckInstCnt>;               \
                 gInited = true;                                             \
             }                                                               \
-            sk_atomic_inc(&gInstanceCount);                                 \
+            sk_atomic_inc(GetInstanceCountPtr());                           \
         }                                                                   \
                                                                             \
         SkInstanceCountHelper(const SkInstanceCountHelper&) {               \
-            sk_atomic_inc(&gInstanceCount);                                 \
+            sk_atomic_inc(GetInstanceCountPtr());                           \
         }                                                                   \
                                                                             \
         ~SkInstanceCountHelper() {                                          \
-            sk_atomic_dec(&gInstanceCount);                                 \
+            sk_atomic_dec(GetInstanceCountPtr());                           \
         }                                                                   \
                                                                             \
-        static int32_t gInstanceCount;                                      \
-        static bool gInited;                                                \
-        static SkTArray<PFCheckInstCnt>* gChildren;                         \
+        static int32_t* GetInstanceCountPtr() {                             \
+            static int32_t gInstanceCount;                                  \
+            return &gInstanceCount;                                         \
+        }                                                                   \
+                                                                            \
+        static SkTArray<PFCheckInstCnt>*& GetChildren() {                   \
+            static SkTArray<PFCheckInstCnt>* gChildren;                     \
+            return gChildren;                                               \
+        }                                                                   \
+                                                                            \
     } fInstanceCountHelper;                                                 \
                                                                             \
     static int32_t GetInstanceCount() {                                     \
-        return SkInstanceCountHelper::gInstanceCount;                       \
+        return *SkInstanceCountHelper::GetInstanceCountPtr();               \
     }                                                                       \
                                                                             \
     static void exitPrint() {                                               \
@@ -77,57 +78,50 @@
     }                                                                       \
                                                                             \
     static int CheckInstanceCount(int level = 0, bool cleanUp = false) {    \
-        if (gPrintInstCount && 0 != SkInstanceCountHelper::gInstanceCount) {\
+        if (gPrintInstCount && 0 != GetInstanceCount()) {                   \
             SkDebugf("%*c Leaked %s: %d\n",                                 \
                      4*level, ' ', #className,                              \
-                     SkInstanceCountHelper::gInstanceCount);                \
+                     GetInstanceCount());                                   \
         }                                                                   \
-        if (NULL == SkInstanceCountHelper::gChildren) {                     \
-            return SkInstanceCountHelper::gInstanceCount;                   \
+        if (NULL == SkInstanceCountHelper::GetChildren()) {                 \
+            return GetInstanceCount();                                      \
         }                                                                   \
-        int childCount = SkInstanceCountHelper::gChildren->count();         \
-        int count = SkInstanceCountHelper::gInstanceCount;                  \
+        SkTArray<int (*)(int, bool)>* children =                            \
+            SkInstanceCountHelper::GetChildren();                           \
+        int childCount = children->count();                                 \
+        int count = GetInstanceCount();                                     \
         for (int i = 0; i < childCount; ++i) {                              \
-            count -= (*(*SkInstanceCountHelper::gChildren)[i])(level+1, cleanUp); \
+            count -= (*(*children)[i])(level+1, cleanUp);                   \
         }                                                                   \
         SkASSERT(count >= 0);                                               \
         if (gPrintInstCount && childCount > 0 && count > 0) {               \
             SkDebugf("%*c Leaked ???: %d\n", 4*(level + 1), ' ', count);    \
         }                                                                   \
         if (cleanUp) {                                                      \
-            delete SkInstanceCountHelper::gChildren;                        \
-            SkInstanceCountHelper::gChildren = NULL;                        \
+            delete children;                                                \
+            SkInstanceCountHelper::GetChildren() = NULL;                    \
         }                                                                   \
-        return SkInstanceCountHelper::gInstanceCount;                       \
+        return GetInstanceCount();                                          \
     }                                                                       \
                                                                             \
-    static void AddInstChild(templateType SkInstanceCountHelper::PFCheckInstCnt \
-                                                       childCheckInstCnt) { \
+    static void AddInstChild(int (*childCheckInstCnt)(int, bool)) {         \
         if (CheckInstanceCount != childCheckInstCnt &&                      \
-            NULL != SkInstanceCountHelper::gChildren) {                     \
-            SkInstanceCountHelper::gChildren->push_back(childCheckInstCnt); \
+            NULL != SkInstanceCountHelper::GetChildren()) {                 \
+            SkInstanceCountHelper::GetChildren()->push_back(childCheckInstCnt); \
         }                                                                   \
     }
 
-#define SK_DEFINE_INST_COUNT(className)                                     \
-    int32_t className::SkInstanceCountHelper::gInstanceCount = 0;           \
-    bool className::SkInstanceCountHelper::gInited = false;                 \
-    SkTArray<className::SkInstanceCountHelper::PFCheckInstCnt>*             \
-                        className::SkInstanceCountHelper::gChildren = NULL;
-
-#define SK_DEFINE_INST_COUNT_TEMPLATE(templateInfo, className)              \
-    templateInfo int32_t className::SkInstanceCountHelper::gInstanceCount = 0;\
-    templateInfo bool className::SkInstanceCountHelper::gInited = false;    \
-    templateInfo                                                            \
-        SkTArray<typename className::SkInstanceCountHelper::PFCheckInstCnt>*\
-                      className::SkInstanceCountHelper::gChildren = NULL;
-
 #else
-#define SK_DECLARE_INST_COUNT(className)
-#define SK_DECLARE_INST_COUNT_TEMPLATE(className)
-#define SK_DECLARE_INST_COUNT_ROOT(className)
+// Typically SK_ENABLE_INST_COUNT=0. Make sure the class declares public typedef INHERITED by
+// causing a compile-time error if the typedef is missing. This way SK_ENABLE_INST_COUNT=1 stays
+// compiling.
+#define SK_DECLARE_INST_COUNT(className) static void AddInstChild() { INHERITED::AddInstChild(); }
+#define SK_DECLARE_INST_COUNT_ROOT(className) static void AddInstChild() { }
+#endif
+
+// Following are deprecated. They are defined only for backwards API compatibility.
+#define SK_DECLARE_INST_COUNT_TEMPLATE(className) SK_DECLARE_INST_COUNT(className)
 #define SK_DEFINE_INST_COUNT(className)
 #define SK_DEFINE_INST_COUNT_TEMPLATE(templateInfo, className)
-#endif
 
 #endif // SkInstCnt_DEFINED
diff --git a/lazy/SkLruImageCache.h b/lazy/SkLruImageCache.h
index 5170a05..8509561 100644
--- a/lazy/SkLruImageCache.h
+++ b/lazy/SkLruImageCache.h
@@ -86,6 +86,7 @@
      *  Remove a set of CachedPixels. Mutex must be locked before calling.
      */
     void removePixels(CachedPixels*);
+    typedef SkImageCache INHERITED;
 };
 
 #endif // SkLruImageCache_DEFINED
diff --git a/lazy/SkPurgeableImageCache.h b/lazy/SkPurgeableImageCache.h
index 24525b0..a6889df 100644
--- a/lazy/SkPurgeableImageCache.h
+++ b/lazy/SkPurgeableImageCache.h
@@ -43,5 +43,6 @@
     int findRec(ID) const;
 #endif
     void removeRec(ID);
+    typedef SkImageCache INHERITED;
 };
 #endif // SkPurgeableImageCache_DEFINED
diff --git a/ports/SkFontConfigInterface.h b/ports/SkFontConfigInterface.h
index 661c3be..8c12a56 100644
--- a/ports/SkFontConfigInterface.h
+++ b/ports/SkFontConfigInterface.h
@@ -106,6 +106,7 @@
                                 SkTArray<FontIdentity>*) {
         return false;
     }
+    typedef SkRefCnt INHERITED;
 };
 
 #endif