- Nova.0.1.zip
- Nova.CodeDOM
- CodeDOM
- Annotations
- Base
- CodeUnit.cs
- Expressions
- AnonymousMethods
- Base
- Operators
- Other
- References
- Base
- GotoTargets
- Methods
- Namespaces
- Other
- Properties
- Types
- Variables
- Namespaces
- Statements
- Base
- Conditionals
- Exceptions
- Generics
- Iterators
- Jumps
- Loops
- Methods
- Miscellaneous
- Namespaces
- Properties
- Types
- Variables
- Configuration.cs
- Log.cs
- Nova.CodeDOM.csproj
- Properties
- Rendering
- Utilities
- Nova.Examples
- Nova.sln
- Nova.Test
|
// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php
using Nova.Rendering;
namespace Nova.CodeDOM
{
/// <summary>
/// Represents one or more contraints on a type parameter.
/// </summary>
/// <remarks>
/// The constraints may consist of:
/// - a <see cref="ClassConstraint"/>
/// - a <see cref="StructConstraint"/>
/// - a <see cref="TypeConstraint"/> for a base class
/// - one or more <see cref="TypeConstraint"/>s for interfaces (which can also be generic)
/// - a <see cref="TypeConstraint"/> for a base class followed by one or more <see cref="TypeConstraint"/>s for interfaces
/// - a <see cref="TypeConstraint"/> for another <see cref="TypeParameter"/> (a "naked type constraint")
/// - a <see cref="NewConstraint"/> by itself or in addition to any of the above (must be at the end)
/// </remarks>
public class ConstraintClause : CodeObject
{
#region /* FIELDS */
protected SymbolicRef _typeParameter;
protected ChildList<TypeParameterConstraint> _constraints;
#endregion
#region /* CONSTRUCTORS */
/// <summary>
/// Create a <see cref="ConstraintClause"/>.
/// </summary>
public ConstraintClause(SymbolicRef symbolicRef, params TypeParameterConstraint[] constraints)
{
TypeParameter = symbolicRef;
CreateConstraints().AddRange(constraints);
}
/// <summary>
/// Create a <see cref="ConstraintClause"/>.
/// </summary>
public ConstraintClause(TypeParameterRef typeParameterRef, params TypeParameterConstraint[] constraints)
: this((SymbolicRef)typeParameterRef, constraints)
{ }
/// <summary>
/// Create a <see cref="ConstraintClause"/>.
/// </summary>
public ConstraintClause(TypeParameter typeParameter, params TypeParameterConstraint[] constraints)
: this(typeParameter.CreateRef(), constraints)
{ }
#endregion
#region /* PROPERTIES */
/// <summary>
/// The <see cref="TypeParameter"/> being constrained.
/// </summary>
public SymbolicRef TypeParameter
{
get { return _typeParameter; }
set { SetField(ref _typeParameter, value, true); }
}
/// <summary>
/// The list of <see cref="TypeParameterConstraint"/>s.
/// </summary>
public ChildList<TypeParameterConstraint> Constraints
{
get { return _constraints; }
}
#endregion
#region /* METHODS */
/// <summary>
/// Create the list of <see cref="TypeParameterConstraint"/>s, or return the existing one.
/// </summary>
public ChildList<TypeParameterConstraint> CreateConstraints()
{
if (_constraints == null)
_constraints = new ChildList<TypeParameterConstraint>(this);
return _constraints;
}
/// <summary>
/// Deep-clone the code object.
/// </summary>
public override CodeObject Clone()
{
ConstraintClause clone = (ConstraintClause)base.Clone();
clone.CloneField(ref clone._typeParameter, _typeParameter);
clone._constraints = ChildListHelpers.Clone(_constraints, clone);
return clone;
}
#endregion
#region /* FORMATTING */
/// <summary>
/// True if the code object defaults to starting on a new line.
/// </summary>
public override bool IsFirstOnLineDefault
{
get { return false; }
}
/// <summary>
/// Determines if the code object only requires a single line for display.
/// </summary>
public override bool IsSingleLine
{
get
{
return (base.IsSingleLine && (_typeParameter == null || (!_typeParameter.IsFirstOnLine && _typeParameter.IsSingleLine))
&& (_constraints == null || _constraints.Count == 0 || (!_constraints[0].IsFirstOnLine && _constraints.IsSingleLine)));
}
set
{
base.IsSingleLine = value;
if (value)
{
if (_typeParameter != null)
{
_typeParameter.IsFirstOnLine = false;
_typeParameter.IsSingleLine = true;
}
if (_constraints != null && _constraints.Count > 0)
{
_constraints[0].IsFirstOnLine = false;
_constraints.IsSingleLine = true;
}
}
}
}
#endregion
#region /* RENDERING */
public override void AsText(CodeWriter writer, RenderFlags flags)
{
int newLines = NewLines;
if (newLines > 0)
{
if (!flags.HasFlag(RenderFlags.SuppressNewLine))
writer.WriteLines(newLines);
}
else if (flags.HasFlag(RenderFlags.PrefixSpace))
writer.Write(" ");
flags &= ~RenderFlags.SuppressNewLine;
RenderFlags passFlags = (flags & RenderFlags.PassMask);
AsTextBefore(writer, passFlags | RenderFlags.IsPrefix);
UpdateLineCol(writer, flags);
// Increase the indent level for any newlines that occur within the statement if the flag is set
bool increaseIndent = flags.HasFlag(RenderFlags.IncreaseIndent);
if (increaseIndent)
writer.BeginIndentOnNewLine(this);
writer.Write("where");
RenderFlags noDescFlags = passFlags & ~(RenderFlags.Description | RenderFlags.ShowParentTypes);
if (_typeParameter != null)
_typeParameter.AsText(writer, noDescFlags | RenderFlags.IsPrefix | RenderFlags.PrefixSpace);
writer.Write(": ");
writer.WriteList(_constraints, noDescFlags, this);
AsTextEOLComments(writer, flags);
if (increaseIndent)
writer.EndIndentation(this);
AsTextAfter(writer, passFlags | (flags & RenderFlags.NoPostAnnotations));
}
public static void AsTextConstraints(CodeWriter writer, ChildList<ConstraintClause> constraints, RenderFlags flags)
{
if (constraints != null && constraints.Count > 0)
writer.WriteList(constraints, flags | RenderFlags.NoItemSeparators | (constraints[0].IsFirstOnLine ? 0 : RenderFlags.PrefixSpace), constraints.Parent);
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I've been writing software since the late 70's, currently focusing mainly on C#.NET. I also like to travel around the world, and I own a Chocolate Factory (sadly, none of my employees are oompa loompas).