Back to index

lightning-sunbird  0.9+nobinonly
nsXBLProtoImplMethod.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   David Hyatt <hyatt@netscape.com> (Original Author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #ifndef nsXBLProtoImplMethod_h__
00040 #define nsXBLProtoImplMethod_h__
00041 
00042 #include "nsIAtom.h"
00043 #include "nsString.h"
00044 #include "jsapi.h"
00045 #include "nsIContent.h"
00046 #include "nsString.h"
00047 #include "nsXBLProtoImplMember.h"
00048 
00049 MOZ_DECL_CTOR_COUNTER(nsXBLParameter)
00050 
00051 struct nsXBLParameter {
00052   nsXBLParameter* mNext;
00053   char* mName;
00054 
00055   nsXBLParameter(const nsAString& aName) {
00056     MOZ_COUNT_CTOR(nsXBLParameter);
00057     mName = ToNewCString(aName);
00058     mNext = nsnull;
00059   }
00060 
00061   ~nsXBLParameter() {
00062     MOZ_COUNT_DTOR(nsXBLParameter);
00063     nsMemory::Free(mName);
00064     delete mNext;
00065   }
00066 };
00067 
00068 MOZ_DECL_CTOR_COUNTER(nsXBLUncompiledMethod)
00069 
00070 struct nsXBLUncompiledMethod {
00071   nsXBLParameter* mParameters;
00072   nsXBLParameter* mLastParameter;
00073   nsXBLTextWithLineNumber mBodyText;
00074 
00075   nsXBLUncompiledMethod() :
00076     mParameters(nsnull),
00077     mLastParameter(nsnull),
00078     mBodyText()
00079   {
00080     MOZ_COUNT_CTOR(nsXBLUncompiledMethod);
00081   }
00082 
00083   ~nsXBLUncompiledMethod() {
00084     MOZ_COUNT_DTOR(nsXBLUncompiledMethod);
00085     delete mParameters;
00086   }
00087 
00088   PRInt32 GetParameterCount() {
00089     PRInt32 result = 0;
00090     for (nsXBLParameter* curr = mParameters; curr; curr=curr->mNext)
00091       result++;
00092     return result;
00093   }
00094 
00095   void AppendBodyText(const nsAString& aText) {
00096     mBodyText.AppendText(aText);
00097   }
00098 
00099   void AddParameter(const nsAString& aText) {
00100     nsXBLParameter* param = new nsXBLParameter(aText);
00101     if (!param)
00102       return;
00103     if (!mParameters)
00104       mParameters = param;
00105     else
00106       mLastParameter->mNext = param;
00107     mLastParameter = param;
00108   }
00109 
00110   void SetLineNumber(PRUint32 aLineNumber) {
00111     mBodyText.SetLineNumber(aLineNumber);
00112   }
00113 };
00114 
00115 class nsXBLProtoImplMethod: public nsXBLProtoImplMember
00116 {
00117 public:
00118   nsXBLProtoImplMethod(const PRUnichar* aName);
00119   virtual ~nsXBLProtoImplMethod();
00120   virtual void Destroy(PRBool aIsCompiled);
00121 
00122   void AppendBodyText(const nsAString& aBody);
00123   void AddParameter(const nsAString& aName);
00124 
00125   void SetLineNumber(PRUint32 aLineNumber);
00126   
00127   virtual nsresult InstallMember(nsIScriptContext* aContext,
00128                                  nsIContent* aBoundElement, 
00129                                  void* aScriptObject,
00130                                  void* aTargetClassObject,
00131                                  const nsCString& aClassStr);
00132   virtual nsresult CompileMember(nsIScriptContext* aContext,
00133                                  const nsCString& aClassStr,
00134                                  void* aClassObject);
00135 
00136 protected:
00137   union {
00138     nsXBLUncompiledMethod* mUncompiledMethod; // An object that represents the method before being compiled.
00139     JSObject * mJSMethodObject;               // The JS object for the method (after compilation)
00140   };
00141 
00142 #ifdef DEBUG
00143   PRBool mIsCompiled;
00144 #endif
00145 };
00146 
00147 class nsXBLProtoImplAnonymousMethod : public nsXBLProtoImplMethod {
00148 public:
00149   nsXBLProtoImplAnonymousMethod() :
00150     nsXBLProtoImplMethod(EmptyString().get())
00151   {}
00152   
00153   nsresult Execute(nsIContent* aBoundElement);
00154 
00155   // Override InstallMember; these methods never get installed as members on
00156   // binding instantiations (though they may hang out in mMembers on the
00157   // prototype implementation).
00158   virtual nsresult InstallMember(nsIScriptContext* aContext,
00159                                  nsIContent* aBoundElement, 
00160                                  void* aScriptObject,
00161                                  void* aTargetClassObject,
00162                                  const nsCString& aClassStr) {
00163     return NS_OK;
00164   }
00165 };
00166 
00167 #endif // nsXBLProtoImplMethod_h__