[svn] r6022: nemerle/trunk: macros/DesignPatterns.n ncc/Makefile snippets/designpatt/proxy-p.n

nazgul svnadmin at nemerle.org
Mon Dec 19 18:26:51 CET 2005


Log:
Add ProxyDefaultMembers design pattern macro to standard macros

Author: nazgul
Date: Mon Dec 19 18:26:30 2005
New Revision: 6022

Added:
   nemerle/trunk/macros/DesignPatterns.n   (contents, props changed)
Modified:
   nemerle/trunk/ncc/Makefile
   nemerle/trunk/snippets/designpatt/proxy-p.n

Added: nemerle/trunk/macros/DesignPatterns.n
==============================================================================
--- (empty file)
+++ nemerle/trunk/macros/DesignPatterns.n	Mon Dec 19 18:26:30 2005
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2005 The University of Wroclaw.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *    1. Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *    2. Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in the
+ *       documentation and/or other materials provided with the distribution.
+ *    3. The name of the University may not be used to endorse or promote
+ *       products derived from this software without specific prior
+ *       written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
+ * NO EVENT SHALL THE UNIVERSITY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+using Nemerle.Compiler;
+
+namespace Nemerle.DesignPatterns
+{
+  [Nemerle.MacroUsage (Nemerle.MacroPhase.WithTypedMembers,
+                       Nemerle.MacroTargets.Field)]
+  macro ProxyPublicMembers (t : TypeBuilder, f : FieldBuilder)
+  {
+    def classty = match (f.GetMemType()) {
+      | Class (typeinfo, _) => typeinfo
+      | _ => Message.FatalError ("expected simple class type")
+    }
+    foreach (mem in classty.GetMembers (BindingFlags.Instance |
+                                        BindingFlags.DeclaredOnly | BindingFlags.Public))
+    {
+      // create Name object for name of created object
+      def member_name = t.ParsedName.NewName (mem.Name);
+      
+      match (mem) {
+        | meth is IMethod
+          // we must avoid property method here, a little bit hacking solution
+          when meth.Attributes & NemerleAttributes.SpecialName == 0 =>
+      
+          // prepare method invocation parameters
+          def parms = meth.GetParameters ().Map(fun (p) {
+            <[ $(t.ParsedName.NewName (p.name) : name) : $(p.ty : typed) ]>
+          });
+          // prepare created method function parameters
+          def fparms = parms.Map (Parsetree.Fun_parm);
+
+
+          // define the wrapper method
+          t.Define (<[ decl:
+            public virtual $(member_name : name) (..$fparms) : $(meth.ReturnType : typed) {
+              this.$(f.Name : dyn).$(member_name : name) (..$parms)
+            }
+          ]>)
+
+        | prop is IProperty when prop.GetGetter() != null && !prop.IsIndexer =>
+          t.Define (<[ decl:
+            public virtual $(member_name : name) : $(prop.GetMemType() : typed) {
+              get { 
+                this.$(f.Name : dyn).$(member_name : name)
+              }
+            }
+          ]>)
+
+        | _ => ()
+      }
+    }
+  }
+}

Modified: nemerle/trunk/ncc/Makefile
==============================================================================
--- nemerle/trunk/ncc/Makefile	(original)
+++ nemerle/trunk/ncc/Makefile	Mon Dec 19 18:26:30 2005
@@ -159,6 +159,7 @@
 	../macros/Internals.n \
 	../macros/English.n \
 	../macros/Data.n \
+	../macros/DesignPatterns.n \
 	../macros/AssemblyInfo.n \
 
 ############################################################

Modified: nemerle/trunk/snippets/designpatt/proxy-p.n
==============================================================================
--- nemerle/trunk/snippets/designpatt/proxy-p.n	(original)
+++ nemerle/trunk/snippets/designpatt/proxy-p.n	Mon Dec 19 18:26:30 2005
@@ -50,6 +50,23 @@
   }
 }
 
+variant Bubba {
+  | Foo { x : string; }
+  | Goo
+
+  public Length : int {
+    get { 1 } 
+  }
+  public Fire (_x : int) : void {
+  }
+}
+
+[Record]
+class BubbaExtend {
+  [Nemerle.DesignPatterns.ProxyPublicMembers ()]
+  my_bubba : Bubba;
+}
+
 /// <summary>
 /// ProxyApp test
 /// </summary>
@@ -65,6 +82,11 @@
     Console.WriteLine( "4 - 2 = {0}", p.Sub( 4.0, 2.0 ) );
     Console.WriteLine( "4 * 2 = {0}", p.Mul( 4.0, 2.0 ) );
     Console.WriteLine( "4 / 2 = {0}", p.Div( 4.0, 2.0 ) );
+
+    // ProxyPublicMembers macro test
+    def x = BubbaExtend (Bubba.Foo("a"));
+    _ = x.Length;
+    _ = x.Fire (1);
   }
 }
 



More information about the svn mailing list