|
1 /* vim: se cin sw=2 ts=2 et : */ |
|
2 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
3 * |
|
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/. */ |
|
7 |
|
8 #ifndef __mozilla_widget_GfxInfoCollector_h__ |
|
9 #define __mozilla_widget_GfxInfoCollector_h__ |
|
10 |
|
11 #include "mozilla/Attributes.h" |
|
12 #include "nsStringFwd.h" |
|
13 #include "js/RootingAPI.h" |
|
14 |
|
15 namespace mozilla { |
|
16 namespace widget { |
|
17 |
|
18 |
|
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 |
|
22 { |
|
23 friend class GfxInfoBase; |
|
24 |
|
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); |
|
29 |
|
30 private: |
|
31 // We need to ensure that this object lives on the stack so that GC sees it properly |
|
32 InfoObject(JSContext *aCx); |
|
33 InfoObject(InfoObject&); |
|
34 |
|
35 JSContext *mCx; |
|
36 JS::Rooted<JSObject*> mObj; |
|
37 bool mOk; |
|
38 }; |
|
39 |
|
40 /* |
|
41 |
|
42 Here's an example usage: |
|
43 |
|
44 class Foo { |
|
45 Foo::Foo() : mInfoCollector(this, &Foo::GetAweseomeness) {} |
|
46 |
|
47 void GetAwesomeness(InfoObject &obj) { |
|
48 obj.DefineProperty("awesome", mAwesome); |
|
49 } |
|
50 |
|
51 int mAwesome; |
|
52 |
|
53 GfxInfoCollector<Foo> mInfoCollector; |
|
54 } |
|
55 |
|
56 This will define a property on the object |
|
57 returned from calling getInfo() on a |
|
58 GfxInfo object. e.g. |
|
59 |
|
60 gfxInfo = Cc["@mozilla.org/gfx/info;1"].getService(Ci.nsIGfxInfo); |
|
61 info = gfxInfo.getInfo(); |
|
62 if (info.awesome) |
|
63 alert(info.awesome); |
|
64 |
|
65 */ |
|
66 |
|
67 class GfxInfoCollectorBase |
|
68 { |
|
69 public: |
|
70 GfxInfoCollectorBase(); |
|
71 virtual void GetInfo(InfoObject &obj) = 0; |
|
72 virtual ~GfxInfoCollectorBase(); |
|
73 }; |
|
74 |
|
75 template<class T> |
|
76 class GfxInfoCollector : public GfxInfoCollectorBase |
|
77 { |
|
78 public: |
|
79 GfxInfoCollector(T* aPointer, void (T::*aFunc)(InfoObject &obj)) : mPointer(aPointer), mFunc(aFunc) { |
|
80 } |
|
81 virtual void GetInfo(InfoObject &obj) { |
|
82 (mPointer->*mFunc)(obj); |
|
83 } |
|
84 |
|
85 protected: |
|
86 T* mPointer; |
|
87 void (T::*mFunc)(InfoObject &obj); |
|
88 |
|
89 }; |
|
90 |
|
91 } |
|
92 } |
|
93 |
|
94 #endif |