ImageResizer  3.4.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Public Member Functions | Protected Member Functions | Protected Attributes | Properties | Events | List of all members
ImageResizer.Collections.SafeList< T > Class Template Reference

SafeList is mutable, but it uses immutable data structures to minimize the need for locking. The provided manipulation Exposes a immutable list. Changes are made by copying the lists. SafeList is Never perform logic on SafeList directly, always use GetList() or GetCollection() first, followed by SetList(). If you need involved list-fu, use ModifyList and specify a callback. It will execute inside a lock, preventing changes on other threads from overwriting each other. More...

Inheritance diagram for ImageResizer.Collections.SafeList< T >:
Inheritance graph
[legend]
Collaboration diagram for ImageResizer.Collections.SafeList< T >:
Collaboration graph
[legend]

Public Member Functions

delegate void ChangedHandler (SafeList< T > sender)
 
delegate IEnumerable< T > ListEditor (IList< T > items)
 
 SafeList (IEnumerable< T > items)
 
ReadOnlyCollection< T > GetCollection ()
 Returns an immutable snapshot of the collection More...
 
IList< T > GetList ()
 Returns a mutable snapshot of the list More...
 
void SetList (IEnumerable< T > list)
 Replaces the current collection with a new one. (copied to ensure safety) Use ModifyList when modifying the list. Use this only when the previous or current state of the list is irrelevant. More...
 
void Add (T item)
 Adds the specified item to the end of the list More...
 
bool Remove (T item)
 Removes the item from the list More...
 
void AddFirst (T item)
 Adds the specified item to the beginning of the list More...
 
void ModifyList (ListEditor callback)
 Allows a caller to perform logic on the list inside a lock, and return a modified list. Callbacks should be fast, and should reference the IList they are fed, not this SafeList instance. Calling methods on the SafeList instance will cause a deadlock. More...
 
bool Contains (T item)
 Returns true if the collection contains the specified item at the moment. More...
 
IEnumerator< T > GetEnumerator ()
 

Protected Member Functions

void FireChanged ()
 

Protected Attributes

volatile ReadOnlyCollection< T > items
 
object writeLock = new object()
 

Properties

First [get]
 Returns the first item in the list. May return null if the list is empty. More...
 
Last [get]
 Returns the first item in the list. May return null if the list is empty. More...
 
IEnumerable< T > Reversed [get]
 

Events

ChangedHandler Changed
 

Detailed Description

SafeList is mutable, but it uses immutable data structures to minimize the need for locking. The provided manipulation Exposes a immutable list. Changes are made by copying the lists. SafeList is Never perform logic on SafeList directly, always use GetList() or GetCollection() first, followed by SetList(). If you need involved list-fu, use ModifyList and specify a callback. It will execute inside a lock, preventing changes on other threads from overwriting each other.

Template Parameters
T

Definition at line 19 of file SafeList.cs.

Member Function Documentation

void ImageResizer.Collections.SafeList< T >.Add ( item)
inline

Adds the specified item to the end of the list

Parameters
item

Definition at line 73 of file SafeList.cs.

73  {
74  lock (writeLock) {
75  IList<T> newList = GetList();
76  newList.Add(item);
77  items = new ReadOnlyCollection<T>(newList);
78  }
79  FireChanged();
80  }
IList< T > GetList()
Returns a mutable snapshot of the list
Definition: SafeList.cs:54
void ImageResizer.Collections.SafeList< T >.AddFirst ( item)
inline

Adds the specified item to the beginning of the list

Parameters
item

Definition at line 122 of file SafeList.cs.

122  {
123  lock (writeLock) {
124  IList<T> newList = GetList();
125  newList.Insert(0, item);
126  items = new ReadOnlyCollection<T>(newList);
127  }
128  FireChanged();
129  }
IList< T > GetList()
Returns a mutable snapshot of the list
Definition: SafeList.cs:54
bool ImageResizer.Collections.SafeList< T >.Contains ( item)
inline

Returns true if the collection contains the specified item at the moment.

Parameters
item
Returns

Definition at line 150 of file SafeList.cs.

150  {
151  return items.Contains(item);
152  }
ReadOnlyCollection<T> ImageResizer.Collections.SafeList< T >.GetCollection ( )
inline

Returns an immutable snapshot of the collection

Returns

Definition at line 47 of file SafeList.cs.

47  {
48  return items;
49  }
IList<T> ImageResizer.Collections.SafeList< T >.GetList ( )
inline

Returns a mutable snapshot of the list

Returns

Definition at line 54 of file SafeList.cs.

54  {
55  return new List<T>(items);
56  }
void ImageResizer.Collections.SafeList< T >.ModifyList ( ListEditor  callback)
inline

Allows a caller to perform logic on the list inside a lock, and return a modified list. Callbacks should be fast, and should reference the IList they are fed, not this SafeList instance. Calling methods on the SafeList instance will cause a deadlock.

Parameters
callback

Definition at line 138 of file SafeList.cs.

138  {
139  lock (writeLock) {
140  items = new ReadOnlyCollection<T>(new List<T>(callback(GetList())));
141  }
142  FireChanged();
143  }
IList< T > GetList()
Returns a mutable snapshot of the list
Definition: SafeList.cs:54
bool ImageResizer.Collections.SafeList< T >.Remove ( item)
inline

Removes the item from the list

Parameters
item

Definition at line 86 of file SafeList.cs.

86  {
87  lock (writeLock) {
88  IList<T> newList = GetList();
89  bool removed = newList.Remove(item);
90  if (!removed) return false; //The item didn't exist, don't fire changed events.
91  items = new ReadOnlyCollection<T>(newList);
92  }
93  FireChanged();
94  return true;
95  }
IList< T > GetList()
Returns a mutable snapshot of the list
Definition: SafeList.cs:54
void ImageResizer.Collections.SafeList< T >.SetList ( IEnumerable< T >  list)
inline

Replaces the current collection with a new one. (copied to ensure safety) Use ModifyList when modifying the list. Use this only when the previous or current state of the list is irrelevant.

Parameters
list

Definition at line 63 of file SafeList.cs.

63  {
64  lock (writeLock) {
65  items = new ReadOnlyCollection<T>(new List<T>(list));
66  }
67  FireChanged();
68  }

Property Documentation

T ImageResizer.Collections.SafeList< T >.First
get

Returns the first item in the list. May return null if the list is empty.

Definition at line 99 of file SafeList.cs.

T ImageResizer.Collections.SafeList< T >.Last
get

Returns the first item in the list. May return null if the list is empty.

Definition at line 110 of file SafeList.cs.


The documentation for this class was generated from the following file: