Bug 1889091 - Part 4: Remove extra stack pointer move. r=jandem
[gecko.git] / widget / GfxInfoCollector.h
blob7108a7fe2df4d1b5472bd59cbba710d5de810e28
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 {
18 /* this is handy wrapper around JSAPI to make it more pleasant to use.
19 * We collect the JSAPI errors and so that callers don't need to */
20 class MOZ_STACK_CLASS InfoObject {
21 friend class GfxInfoBase;
23 public:
24 void DefineProperty(const char* name, int value);
25 void DefineProperty(const char* name, const nsAString& value);
26 void DefineProperty(const char* name, const char* value);
28 private:
29 // We need to ensure that this object lives on the stack so that GC sees it
30 // properly
31 explicit InfoObject(JSContext* aCx);
32 InfoObject(InfoObject&);
34 JSContext* mCx;
35 JS::Rooted<JSObject*> mObj;
36 bool mOk;
41 Here's an example usage:
43 class Foo {
44 Foo::Foo() : mInfoCollector(this, &Foo::GetAweseomeness) {}
46 void GetAwesomeness(InfoObject &obj) {
47 obj.DefineProperty("awesome", mAwesome);
50 int mAwesome;
52 GfxInfoCollector<Foo> mInfoCollector;
55 This will define a property on the object
56 returned from calling getInfo() on a
57 GfxInfo object. e.g.
59 gfxInfo = Cc["@mozilla.org/gfx/info;1"].getService(Ci.nsIGfxInfo);
60 info = gfxInfo.getInfo();
61 if (info.awesome)
62 alert(info.awesome);
66 class GfxInfoCollectorBase {
67 public:
68 GfxInfoCollectorBase();
69 virtual void GetInfo(InfoObject& obj) = 0;
70 virtual ~GfxInfoCollectorBase();
73 template <class T>
74 class GfxInfoCollector : public GfxInfoCollectorBase {
75 public:
76 GfxInfoCollector(T* aPointer, void (T::*aFunc)(InfoObject& obj))
77 : mPointer(aPointer), mFunc(aFunc) {}
78 virtual void GetInfo(InfoObject& obj) override { (mPointer->*mFunc)(obj); }
80 protected:
81 T* mPointer;
82 void (T::*mFunc)(InfoObject& obj);
85 } // namespace widget
86 } // namespace mozilla
88 #endif