michael@0: /* vim: se cin sw=2 ts=2 et : */ michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef __mozilla_widget_GfxInfoCollector_h__ michael@0: #define __mozilla_widget_GfxInfoCollector_h__ michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: #include "nsStringFwd.h" michael@0: #include "js/RootingAPI.h" michael@0: michael@0: namespace mozilla { michael@0: namespace widget { michael@0: michael@0: michael@0: /* this is handy wrapper around JSAPI to make it more pleasant to use. michael@0: * We collect the JSAPI errors and so that callers don't need to */ michael@0: class MOZ_STACK_CLASS InfoObject michael@0: { michael@0: friend class GfxInfoBase; michael@0: michael@0: public: michael@0: void DefineProperty(const char *name, int value); michael@0: void DefineProperty(const char *name, nsAString &value); michael@0: void DefineProperty(const char *name, const char *value); michael@0: michael@0: private: michael@0: // We need to ensure that this object lives on the stack so that GC sees it properly michael@0: InfoObject(JSContext *aCx); michael@0: InfoObject(InfoObject&); michael@0: michael@0: JSContext *mCx; michael@0: JS::Rooted mObj; michael@0: bool mOk; michael@0: }; michael@0: michael@0: /* michael@0: michael@0: Here's an example usage: michael@0: michael@0: class Foo { michael@0: Foo::Foo() : mInfoCollector(this, &Foo::GetAweseomeness) {} michael@0: michael@0: void GetAwesomeness(InfoObject &obj) { michael@0: obj.DefineProperty("awesome", mAwesome); michael@0: } michael@0: michael@0: int mAwesome; michael@0: michael@0: GfxInfoCollector mInfoCollector; michael@0: } michael@0: michael@0: This will define a property on the object michael@0: returned from calling getInfo() on a michael@0: GfxInfo object. e.g. michael@0: michael@0: gfxInfo = Cc["@mozilla.org/gfx/info;1"].getService(Ci.nsIGfxInfo); michael@0: info = gfxInfo.getInfo(); michael@0: if (info.awesome) michael@0: alert(info.awesome); michael@0: michael@0: */ michael@0: michael@0: class GfxInfoCollectorBase michael@0: { michael@0: public: michael@0: GfxInfoCollectorBase(); michael@0: virtual void GetInfo(InfoObject &obj) = 0; michael@0: virtual ~GfxInfoCollectorBase(); michael@0: }; michael@0: michael@0: template michael@0: class GfxInfoCollector : public GfxInfoCollectorBase michael@0: { michael@0: public: michael@0: GfxInfoCollector(T* aPointer, void (T::*aFunc)(InfoObject &obj)) : mPointer(aPointer), mFunc(aFunc) { michael@0: } michael@0: virtual void GetInfo(InfoObject &obj) { michael@0: (mPointer->*mFunc)(obj); michael@0: } michael@0: michael@0: protected: michael@0: T* mPointer; michael@0: void (T::*mFunc)(InfoObject &obj); michael@0: michael@0: }; michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif