1 //
2 // Copyright (c) 2010-2018 Antmicro
3 //
4 //  This file is licensed under the MIT License.
5 //  Full license text is available in 'licenses/MIT.txt'.
6 //
7 using System;
8 using System.Collections.Generic;
9 
10 namespace Antmicro.Renode.Utilities
11 {
12     public class DisposableWrapper : IDisposable
13     {
New(Action a)14         public static DisposableWrapper New(Action a)
15         {
16             return new DisposableWrapper().RegisterDisposeAction(a);
17         }
18 
DisposableWrapper()19         public DisposableWrapper()
20         {
21             disposeActions = new List<Action>();
22         }
23 
RegisterDisposeAction(Action a)24         public DisposableWrapper RegisterDisposeAction(Action a)
25         {
26             disposeActions.Add(a);
27             return this;
28         }
29 
Disable()30         public void Disable()
31         {
32             disabled = true;
33         }
34 
Dispose()35         public void Dispose()
36         {
37             if(disabled)
38             {
39                 return;
40             }
41 
42             foreach(var a in disposeActions)
43             {
44                 a();
45             }
46         }
47 
48         private bool disabled;
49 
50         private readonly List<Action> disposeActions;
51     }
52 }
53