1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/xpwidgets/GfxInfoCollector.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,94 @@ 1.4 +/* vim: se cin sw=2 ts=2 et : */ 1.5 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.6 + * 1.7 + * This Source Code Form is subject to the terms of the Mozilla Public 1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.10 + 1.11 +#ifndef __mozilla_widget_GfxInfoCollector_h__ 1.12 +#define __mozilla_widget_GfxInfoCollector_h__ 1.13 + 1.14 +#include "mozilla/Attributes.h" 1.15 +#include "nsStringFwd.h" 1.16 +#include "js/RootingAPI.h" 1.17 + 1.18 +namespace mozilla { 1.19 +namespace widget { 1.20 + 1.21 + 1.22 +/* this is handy wrapper around JSAPI to make it more pleasant to use. 1.23 + * We collect the JSAPI errors and so that callers don't need to */ 1.24 +class MOZ_STACK_CLASS InfoObject 1.25 +{ 1.26 + friend class GfxInfoBase; 1.27 + 1.28 + public: 1.29 + void DefineProperty(const char *name, int value); 1.30 + void DefineProperty(const char *name, nsAString &value); 1.31 + void DefineProperty(const char *name, const char *value); 1.32 + 1.33 + private: 1.34 + // We need to ensure that this object lives on the stack so that GC sees it properly 1.35 + InfoObject(JSContext *aCx); 1.36 + InfoObject(InfoObject&); 1.37 + 1.38 + JSContext *mCx; 1.39 + JS::Rooted<JSObject*> mObj; 1.40 + bool mOk; 1.41 +}; 1.42 + 1.43 +/* 1.44 + 1.45 + Here's an example usage: 1.46 + 1.47 + class Foo { 1.48 + Foo::Foo() : mInfoCollector(this, &Foo::GetAweseomeness) {} 1.49 + 1.50 + void GetAwesomeness(InfoObject &obj) { 1.51 + obj.DefineProperty("awesome", mAwesome); 1.52 + } 1.53 + 1.54 + int mAwesome; 1.55 + 1.56 + GfxInfoCollector<Foo> mInfoCollector; 1.57 + } 1.58 + 1.59 + This will define a property on the object 1.60 + returned from calling getInfo() on a 1.61 + GfxInfo object. e.g. 1.62 + 1.63 + gfxInfo = Cc["@mozilla.org/gfx/info;1"].getService(Ci.nsIGfxInfo); 1.64 + info = gfxInfo.getInfo(); 1.65 + if (info.awesome) 1.66 + alert(info.awesome); 1.67 + 1.68 +*/ 1.69 + 1.70 +class GfxInfoCollectorBase 1.71 +{ 1.72 + public: 1.73 + GfxInfoCollectorBase(); 1.74 + virtual void GetInfo(InfoObject &obj) = 0; 1.75 + virtual ~GfxInfoCollectorBase(); 1.76 +}; 1.77 + 1.78 +template<class T> 1.79 +class GfxInfoCollector : public GfxInfoCollectorBase 1.80 +{ 1.81 + public: 1.82 + GfxInfoCollector(T* aPointer, void (T::*aFunc)(InfoObject &obj)) : mPointer(aPointer), mFunc(aFunc) { 1.83 + } 1.84 + virtual void GetInfo(InfoObject &obj) { 1.85 + (mPointer->*mFunc)(obj); 1.86 + } 1.87 + 1.88 + protected: 1.89 + T* mPointer; 1.90 + void (T::*mFunc)(InfoObject &obj); 1.91 + 1.92 +}; 1.93 + 1.94 +} 1.95 +} 1.96 + 1.97 +#endif