Bumping manifests a=b2g-bump
[gecko.git] / widget / GfxInfoCollector.h
blob66c35fe9672225bc0bba01e381e71da1ddbfb44e
1 /* vim: se cin sw=2 ts=2 et : */
2 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #ifndef __mozilla_widget_GfxInfoCollector_h__
9 #define __mozilla_widget_GfxInfoCollector_h__
11 #include "mozilla/Attributes.h"
12 #include "nsStringFwd.h"
13 #include "js/RootingAPI.h"
15 namespace mozilla {
16 namespace widget {
19 /* this is handy wrapper around JSAPI to make it more pleasant to use.
20 * We collect the JSAPI errors and so that callers don't need to */
21 class MOZ_STACK_CLASS InfoObject
23 friend class GfxInfoBase;
25 public:
26 void DefineProperty(const char *name, int value);
27 void DefineProperty(const char *name, nsAString &value);
28 void DefineProperty(const char *name, const char *value);
30 private:
31 // We need to ensure that this object lives on the stack so that GC sees it properly
32 explicit InfoObject(JSContext *aCx);
33 InfoObject(InfoObject&);
35 JSContext *mCx;
36 JS::Rooted<JSObject*> mObj;
37 bool mOk;
42 Here's an example usage:
44 class Foo {
45 Foo::Foo() : mInfoCollector(this, &Foo::GetAweseomeness) {}
47 void GetAwesomeness(InfoObject &obj) {
48 obj.DefineProperty("awesome", mAwesome);
51 int mAwesome;
53 GfxInfoCollector<Foo> mInfoCollector;
56 This will define a property on the object
57 returned from calling getInfo() on a
58 GfxInfo object. e.g.
60 gfxInfo = Cc["@mozilla.org/gfx/info;1"].getService(Ci.nsIGfxInfo);
61 info = gfxInfo.getInfo();
62 if (info.awesome)
63 alert(info.awesome);
67 class GfxInfoCollectorBase
69 public:
70 GfxInfoCollectorBase();
71 virtual void GetInfo(InfoObject &obj) = 0;
72 virtual ~GfxInfoCollectorBase();
75 template<class T>
76 class GfxInfoCollector : public GfxInfoCollectorBase
78 public:
79 GfxInfoCollector(T* aPointer, void (T::*aFunc)(InfoObject &obj)) : mPointer(aPointer), mFunc(aFunc) {
81 virtual void GetInfo(InfoObject &obj) {
82 (mPointer->*mFunc)(obj);
85 protected:
86 T* mPointer;
87 void (T::*mFunc)(InfoObject &obj);
94 #endif