widget/xpwidgets/GfxInfoCollector.h

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32

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

mercurial