Allow XyzEncoder.Default to be settable.

This commit is contained in:
Levi B 2015-03-10 13:57:55 -07:00
parent ae456401a8
commit 332900b175
3 changed files with 66 additions and 24 deletions

View File

@ -4,6 +4,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
namespace Microsoft.Framework.WebEncoders
@ -57,20 +58,33 @@ namespace Microsoft.Framework.WebEncoders
}
/// <summary>
/// The default <see cref="HtmlEncoder"/>, which uses <see cref="UnicodeRanges.BasicLatin"/> as its allow list.
/// A default instance of <see cref="HtmlEncoder"/>.
/// </summary>
/// <remarks>
/// This normally corresponds to <see cref="UnicodeRanges.BasicLatin"/>. However, this property is
/// settable so that a developer can change the default implementation application-wide.
/// </remarks>
public static HtmlEncoder Default
{
get
{
HtmlEncoder defaultEncoder = Volatile.Read(ref _defaultEncoder);
if (defaultEncoder == null)
{
defaultEncoder = new HtmlEncoder();
Volatile.Write(ref _defaultEncoder, defaultEncoder);
}
return defaultEncoder;
return Volatile.Read(ref _defaultEncoder) ?? CreateDefaultEncoderSlow();
}
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
Volatile.Write(ref _defaultEncoder, value);
}
}
[MethodImpl(MethodImplOptions.NoInlining)] // the JITter can attempt to inline the caller itself without worrying about us
private static HtmlEncoder CreateDefaultEncoderSlow()
{
var onDemandEncoder = new HtmlEncoder();
return Interlocked.CompareExchange(ref _defaultEncoder, onDemandEncoder, null) ?? onDemandEncoder;
}
/// <summary>

View File

@ -4,6 +4,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
namespace Microsoft.Framework.WebEncoders
@ -57,20 +58,33 @@ namespace Microsoft.Framework.WebEncoders
}
/// <summary>
/// The default <see cref="JavaScriptStringEncoder"/>, which uses <see cref="UnicodeRanges.BasicLatin"/> as its allow list.
/// A default instance of <see cref="JavaScriptStringEncoder"/>.
/// </summary>
/// <remarks>
/// This normally corresponds to <see cref="UnicodeRanges.BasicLatin"/>. However, this property is
/// settable so that a developer can change the default implementation application-wide.
/// </remarks>
public static JavaScriptStringEncoder Default
{
get
{
JavaScriptStringEncoder defaultEncoder = Volatile.Read(ref _defaultEncoder);
if (defaultEncoder == null)
{
defaultEncoder = new JavaScriptStringEncoder();
Volatile.Write(ref _defaultEncoder, defaultEncoder);
}
return defaultEncoder;
return Volatile.Read(ref _defaultEncoder) ?? CreateDefaultEncoderSlow();
}
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
Volatile.Write(ref _defaultEncoder, value);
}
}
[MethodImpl(MethodImplOptions.NoInlining)] // the JITter can attempt to inline the caller itself without worrying about us
private static JavaScriptStringEncoder CreateDefaultEncoderSlow()
{
var onDemandEncoder = new JavaScriptStringEncoder();
return Interlocked.CompareExchange(ref _defaultEncoder, onDemandEncoder, null) ?? onDemandEncoder;
}
/// <summary>

View File

@ -4,6 +4,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
namespace Microsoft.Framework.WebEncoders
@ -57,20 +58,33 @@ namespace Microsoft.Framework.WebEncoders
}
/// <summary>
/// The default <see cref="UrlEncoder"/> which uses <see cref="UnicodeRanges.BasicLatin"/> as its allow list.
/// A default instance of <see cref="UrlEncoder"/>.
/// </summary>
/// <remarks>
/// This normally corresponds to <see cref="UnicodeRanges.BasicLatin"/>. However, this property is
/// settable so that a developer can change the default implementation application-wide.
/// </remarks>
public static UrlEncoder Default
{
get
{
UrlEncoder defaultEncoder = Volatile.Read(ref _defaultEncoder);
if (defaultEncoder == null)
{
defaultEncoder = new UrlEncoder();
Volatile.Write(ref _defaultEncoder, defaultEncoder);
}
return defaultEncoder;
return Volatile.Read(ref _defaultEncoder) ?? CreateDefaultEncoderSlow();
}
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
Volatile.Write(ref _defaultEncoder, value);
}
}
[MethodImpl(MethodImplOptions.NoInlining)] // the JITter can attempt to inline the caller itself without worrying about us
private static UrlEncoder CreateDefaultEncoderSlow()
{
var onDemandEncoder = new UrlEncoder();
return Interlocked.CompareExchange(ref _defaultEncoder, onDemandEncoder, null) ?? onDemandEncoder;
}
/// <summary>