Author: peterw
Date: 2005-03-05 01:10:53 -0500 (Sat, 05 Mar 2005)
New Revision: 41464

Added:
   trunk/mbuild/Mono.Build/Mono.Build/CompositeResult.cs
Log:
Right, this file might be helpful.



Added: trunk/mbuild/Mono.Build/Mono.Build/CompositeResult.cs
===================================================================
--- trunk/mbuild/Mono.Build/Mono.Build/CompositeResult.cs       2005-03-05 
06:10:32 UTC (rev 41463)
+++ trunk/mbuild/Mono.Build/Mono.Build/CompositeResult.cs       2005-03-05 
06:10:53 UTC (rev 41464)
@@ -0,0 +1,119 @@
+//
+// A result that is an agglomeration of others. Ooh.
+//
+
+using System;
+using System.Collections;
+using System.Text;
+using System.Runtime.Serialization;
+
+namespace Mono.Build {
+
+       [Serializable]
+       public abstract class CompositeResult : Result {
+
+               public CompositeResult () : base () {
+               }
+
+               // A result that is made out of several sub-Results.
+               // They are implemented as fields or whatever in
+               // subclasses. This class implements the serialization
+               // logic. We serialize with a CompositeFingerprint, which
+               // takes an array of Results. The TotalItems / CopyItems
+               // mechanism provides a quasi-efficient way for creating
+               // that array of Result members across the class heirarchy.
+
+               // Return base.TotalItems plus the number
+               // of Result members of your particular
+               // CompositeResult subclass.
+
+               protected virtual int TotalItems {
+                       get { return 0; }
+               }
+
+               // Copy the Result members of your implementation
+               // into the array r, starting at the index given
+               // by base.TotalItems.
+
+               protected virtual void CopyItems (Result[] r) {
+               }
+
+               // A composite result can also have a 'default' member.
+               // If it does, then if this result is passed as an argument
+               // to a rule, the default member is tried as an argument
+               // if the result itself can't be applied to an argument.
+               //
+               // This gives you a transparent way to expand a Result:
+               // For instance, the CSharpCompile rule should return
+               // a ClrExecutable result. But MCS also produces an associated
+               // .mdb debug information file that should be installed
+               // if possible. The single ClrExecutable result can be
+               // replaced with a composite result holding a ClrExecutable
+               // and an MDB MBFile, with the ClrExecutable being the 
+               // default member. Old rules will continue to function,
+               // but a smart installer can handle the MDB file.
+
+               public virtual bool HasDefault { get { return false; } }
+
+               public virtual Result Default { get { return null; } }
+
+               // Helper functions
+
+               protected Result FieldAsResult (object o) {
+                       if (o == null)
+                               return null;
+                       if (o is Result)
+                               return (Result) o;
+                       if (o is string)
+                               return new MBString ((string) o);
+                       if (o is bool)
+                               return new MBBool ((bool) o);
+
+                       throw new InvalidOperationException ("Don't know how to 
make object " + o.ToString () +
+                                                            "into a Result.");
+               }
+
+               protected object CloneField (object o) {
+                       if (o == null)
+                               return null;
+                       if (o is Result)
+                               return (o as Result).Clone ();
+                       if (o is string)
+                               return o;
+                       if (o is bool)
+                               return o;
+
+                       throw new InvalidOperationException ("Don't know how to 
clone object " + o.ToString ());
+               }
+
+               protected override void CloneTo (Result r) {
+               }
+
+               // Fingerprinting
+
+               public override Fingerprint GetFingerprint (IBuildContext ctxt, 
Fingerprint cached) {
+                       int t = TotalItems;
+                       Result[] r = new Result[t];
+
+                       CopyItems (r);
+                       return new CompositeFingerprint (r, ctxt, cached);
+               }
+
+               // Utility printing function
+
+               public override string ToString () {
+                       int t = TotalItems;
+                       Result[] r = new Result[t];
+
+                       CopyItems (r);
+
+                       StringBuilder sb = new StringBuilder (GetType 
().ToString ());
+                       sb.Append (" {");
+                       for (int i = 0; i < t; i++)
+                               sb.AppendFormat (" {0}: {1}", i, r[i]);
+                       sb.Append (" }");
+
+                       return sb.ToString ();
+               }
+       }
+}

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to