##// END OF EJS Templates
Added Trace<T>.Debug(...) method for debug messages...
Added Trace<T>.Debug(...) method for debug messages Added ContainerBuilde.LoadConfig(Uri) method

File last commit:

r264:3a6e18c432be v3
r280:f07be402ab02 v3
Show More
LazyAndWeak.cs
65 lines | 2.1 KiB | text/x-csharp | CSharpLexer
cin
working on JSON parser
r178 using System;
using System.Threading;
namespace Implab.Components {
cin
refactoring complete, JSONParser rewritten
r180 /// <summary>
/// Creates an instace on-demand and allows it to be garbage collected.
/// </summary>
/// <remarks>
/// Usefull when dealing with memory-intensive objects which are frequently used.
cin
pretty print DFA, the minimization is still buggy
r182 /// This class is similar to <see cref="ObjectPool{T}"/> except it is a singleton.
cin
Added XmlToJson xsl transformation....
r264 /// This class can't be used to hold diposable objects.
cin
refactoring complete, JSONParser rewritten
r180 /// </remarks>
cin
working on JSON parser
r178 public class LazyAndWeak<T> where T : class {
readonly Func<T> m_factory;
readonly object m_lock;
WeakReference m_reference;
public LazyAndWeak(Func<T> factory, bool useLock) {
Safe.ArgumentNotNull(factory, "factory");
m_factory = factory;
m_lock = useLock ? new object() : null;
}
public LazyAndWeak(Func<T> factory) : this(factory, false) {
}
public T Value {
get {
while (true) {
var weak = m_reference;
T value;
if (weak != null) {
value = weak.Target as T;
if (value != null)
return value;
}
if (m_lock == null) {
value = m_factory();
if (Interlocked.CompareExchange(ref m_reference, new WeakReference(value), weak) == weak)
return value;
} else {
cin
refactoring complete, JSONParser rewritten
r180 lock (m_lock) {
// double check
cin
pretty print DFA, the minimization is still buggy
r182 weak = m_reference;
cin
refactoring complete, JSONParser rewritten
r180 if (weak != null) {
value = weak.Target as T;
if (value != null)
return value;
}
// we are safe to write
value = m_factory();
m_reference = new WeakReference(value);
return value;
}
cin
working on JSON parser
r178 }
}
}
}
}
}