Bug 462525 - username truncation code is unnecessarily duplicated in nsLoginManagerP...
[wine-gecko.git] / dom / src / threads / nsAutoJSObjectHolder.h
blobc0aaa35ce0fb114db49bba624a5ccfa2190e6458
1 /* -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is worker threads.
17 * The Initial Developer of the Original Code is
18 * Mozilla Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2008
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Ben Turner <bent.mozilla@gmail.com> (Original Author)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef __NSAUTOJSOBJECTHOLDER_H__
40 #define __NSAUTOJSOBJECTHOLDER_H__
42 #include "jsapi.h"
44 /**
45 * Simple class that looks and acts like a JSObject* except that it unroots
46 * itself automatically if Root() is ever called. Designed to be rooted on the
47 * context or runtime (but not both!). Also automatically nulls its JSObject*
48 * on Unroot and asserts that Root has been called prior to assigning an object.
50 class nsAutoJSObjectHolder
52 public:
53 /**
54 * Default constructor, no holding.
56 nsAutoJSObjectHolder()
57 : mRt(NULL), mObj(NULL), mHeld(PR_FALSE) { }
59 /**
60 * Hold by rooting on the context's runtime in the constructor, passing the
61 * result out.
63 nsAutoJSObjectHolder(JSContext* aCx, JSBool* aRv = NULL,
64 JSObject* aObj = NULL)
65 : mRt(NULL), mObj(aObj), mHeld(JS_FALSE) {
66 JSBool rv = Hold(aCx);
67 if (aRv) {
68 *aRv = rv;
72 /**
73 * Hold by rooting on the runtime in the constructor, passing the result out.
75 nsAutoJSObjectHolder(JSRuntime* aRt, JSBool* aRv = NULL,
76 JSObject* aObj = NULL)
77 : mRt(aRt), mObj(aObj), mHeld(JS_FALSE) {
78 JSBool rv = Hold(aRt);
79 if (aRv) {
80 *aRv = rv;
84 /**
85 * Always release on destruction.
87 ~nsAutoJSObjectHolder() {
88 Release();
91 /**
92 * Hold by rooting on the context's runtime.
94 JSBool Hold(JSContext* aCx) {
95 return Hold(JS_GetRuntime(aCx));
98 /**
99 * Hold by rooting on the runtime.
101 JSBool Hold(JSRuntime* aRt) {
102 if (!mHeld) {
103 mHeld = JS_AddNamedRootRT(aRt, &mObj, "nsAutoRootedJSObject");
104 if (mHeld) {
105 mRt = aRt;
108 return mHeld;
112 * Manually release.
114 void Release() {
115 NS_ASSERTION(!mHeld || mRt, "Bad!");
116 if (mHeld) {
117 mHeld = !JS_RemoveRootRT(mRt, &mObj);
118 if (!mHeld) {
119 mRt = NULL;
121 mObj = NULL;
126 * Determine if Hold has been called.
128 JSBool IsHeld() {
129 return mHeld;
133 * Pretend to be a JSObject*.
135 JSObject* get() const {
136 return mObj;
140 * Pretend to be a JSObject*.
142 operator JSObject*() const {
143 return get();
147 * Pretend to be a JSObject*. Assert if not held.
149 JSObject* operator=(JSObject* aOther) {
150 NS_ASSERTION(mHeld, "Not rooted!");
151 return mObj = aOther;
154 private:
155 JSRuntime* mRt;
156 JSObject* mObj;
157 JSBool mHeld;
160 #endif /* __NSAUTOJSOBJECTHOLDER_H__ */