Welcome to mirror list, hosted at ThFree Co, Russian Federation.

WorkspaceObject.cs « MonoDevelop.Projects « MonoDevelop.Core « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 53a974ac55646387a4b6fd3cc62aedc7fa134624 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// IWorkspaceObject.cs
//
// Author:
//   Lluis Sanchez Gual <lluis@novell.com>
//
// Copyright (c) 2008 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//

using System;
using System.Collections.Generic;
using MonoDevelop.Core;
using MonoDevelop.Core.Serialization;
using System.Collections;
using MonoDevelop.Projects.Extensions;
using Mono.Addins;
using System.Linq;
using System.Threading.Tasks;
using System.Runtime.Remoting.Messaging;
using MonoDevelop.Core.StringParsing;


namespace MonoDevelop.Projects
{
	public abstract class WorkspaceObject: IExtendedDataItem, IFolderItem, IDisposable
	{
		Hashtable extendedProperties;
		bool initializeCalled;
		bool isShared;
		object localLock = new object ();
		ExtensionContext extensionContext;

		internal protected void Initialize<T> (T instance)
		{
			if (instance.GetType () != typeof(T))
				return;
			var delayedInitialize = CallContext.LogicalGetData ("MonoDevelop.DelayItemInitialization");
			if (delayedInitialize != null && (bool)delayedInitialize)
				return;
			EnsureInitialized ();
		}

		internal void EnsureInitialized ()
		{
			if (!initializeCalled) {
				initializeCalled = true;

				extensionContext = AddinManager.CreateExtensionContext ();
				extensionContext.RegisterCondition ("ItemType", new ItemTypeCondition (GetType ()));

				OnInitialize ();
				InitializeExtensionChain ();
				OnExtensionChainInitialized ();
			}
		}

		protected void AssertMainThread ()
		{
			if (isShared)
				Runtime.AssertMainThread ();
		}

		/// <summary>
		/// Gets a value indicating whether this instance is shared.
		/// </summary>
		/// <remarks>Shared objects can only be modified in the main thread</remarks>
		public bool IsShared {
			get { return isShared; }
		}

		/// <summary>
		/// Sets this object as shared, which means that it is accessible from several threads for reading,
		/// but it can only be modified in the main thread
		/// </summary>
		public void SetShared ()
		{
			OnSetShared ();
		}

		protected virtual void OnSetShared ()
		{
			isShared = true;
			ItemExtension.NotifyShared ();
		}

		public string Name {
			get { return OnGetName (); }
		}

		public FilePath ItemDirectory {
			get { return OnGetItemDirectory (); }
		}

		public FilePath BaseDirectory {
			get { return OnGetBaseDirectory (); }
		}

		public WorkspaceObject ParentObject { get; protected set; }

		public IEnumerable<WorkspaceObject> GetChildren ()
		{
			return OnGetChildren ();
		}

		public IEnumerable<T> GetAllItems<T> () where T: WorkspaceObject
		{
			if (this is T)
				yield return (T)this;
			foreach (var c in OnGetChildren ()) {
				foreach (var r in c.GetAllItems<T> ())
					yield return r;
			}
		}

		/// <summary>
		/// Gets extended properties.
		/// </summary>
		/// <remarks>
		/// This dictionary can be used by add-ins to store arbitrary information about this solution item.
		/// Keys and values can be of any type.
		/// If a value implements IDisposable, the value will be disposed when this item is disposed.
		/// </remarks>
		public IDictionary ExtendedProperties {
			get {
				return OnGetExtendedProperties ();
			}
		}

		/// <summary>
		/// Gets a value indicating whether this <see cref="MonoDevelop.Projects.SolutionItem"/> has been disposed.
		/// </summary>
		/// <value>
		/// <c>true</c> if disposed; otherwise, <c>false</c>.
		/// </value>
		internal protected bool Disposed { get; private set; }

		public virtual void Dispose ()
		{
			AssertMainThread ();

			if (Disposed)
				return;

			Disposed = true;

			if (extensionChain != null) {
				extensionChain.Dispose ();
				extensionChain = null;
			}

			if (extendedProperties != null) {
				foreach (object ob in extendedProperties.Values) {
					IDisposable disp = ob as IDisposable;
					if (disp != null)
						disp.Dispose ();
				}
				extendedProperties = null;
			}
		}

		/// <summary>
		/// Gets a service instance of a given type
		/// </summary>
		/// <returns>
		/// The service.
		/// </returns>
		/// <typeparam name='T'>
		/// Type of the service
		/// </typeparam>
		/// <remarks>
		/// This method looks for an imlpementation of a service of the given type.
		/// </remarks>
		public T GetService<T> ()
		{
			return (T) GetService (typeof(T));
		}

		/// <summary>
		/// Gets a service instance of a given type
		/// </summary>
		/// <returns>
		/// The service.
		/// </returns>
		/// <param name='t'>
		/// Type of the service
		/// </param>
		/// <remarks>
		/// This method looks for an imlpementation of a service of the given type.
		/// </remarks>
		public object GetService (Type t)
		{
			return ItemExtension.GetService (t);
		}

		public StringTagModelDescription GetStringTagModelDescription (ConfigurationSelector conf)
		{
			return ItemExtension.OnGetStringTagModelDescription (conf);
		}

		protected virtual StringTagModelDescription OnGetStringTagModelDescription (ConfigurationSelector conf)
		{
			var m = new StringTagModelDescription ();
			m.Add (GetType ());
			return m;
		}

		/// <summary>
		/// Gets the string tag model for this solution item
		/// </summary>
		/// <returns>
		/// The string tag model
		/// </returns>
		/// <param name='conf'>
		/// Configuration for which to get the string tag model
		/// </param>
		public StringTagModel GetStringTagModel (ConfigurationSelector conf)
		{
			return ItemExtension.OnGetStringTagModel (conf);
		}

		protected virtual StringTagModel OnGetStringTagModel (ConfigurationSelector conf)
		{
			var m = new StringTagModel ();
			m.Add (this);
			return m;
		}

		/// <summary>
		/// Gets a value indicating whether the extension chain for this object has already been created and initialized
		/// </summary>
		protected bool IsExtensionChainCreated {
			get { return extensionChain != null; }
		}

		ExtensionChain extensionChain;
		protected ExtensionChain ExtensionChain {
			get {
				AssertExtensionChainCreated ();
				return extensionChain;
			}
		}

		protected void AssertExtensionChainCreated ()
		{
			if (extensionChain == null) {
				if (!initializeCalled)
					throw new InvalidOperationException ("The constructor of type " + GetType () + " must call Initialize(this)");
				else
					throw new InvalidOperationException ("The extension chain can't be used before OnExtensionChainInitialized() method is called");
			}
		}

		public ExtensionContext ExtensionContext {
			get {
				return this.extensionContext;
			}
		}

		WorkspaceObjectExtension itemExtension;

		WorkspaceObjectExtension ItemExtension {
			get {
				if (itemExtension == null)
					AssertExtensionChainCreated ();
				return itemExtension;
			}
		}

		public void AttachExtension (WorkspaceObjectExtension ext)
		{
			AssertMainThread ();
			ExtensionChain.AddExtension (ext);
			ext.Init (this);
		}

		public void DetachExtension (WorkspaceObjectExtension ext)
		{
			AssertMainThread ();
			ExtensionChain.RemoveExtension (ext);
		}

		void InitializeExtensionChain ()
		{
			// Create an initial empty extension chain. This avoid crashes in case a call to SupportsObject ends
			// calling methods from the extension

			var tempExtensions = new List<WorkspaceObjectExtension> ();
			tempExtensions.AddRange (CreateDefaultExtensions ().Reverse ());
			extensionChain = ExtensionChain.Create (tempExtensions.ToArray ());
			foreach (var e in tempExtensions)
				e.Init (this);

			// Collect extensions that support this object

			var extensions = new List<WorkspaceObjectExtension> ();
			foreach (ProjectModelExtensionNode node in GetModelExtensions (extensionContext)) {
				if (node.CanHandleObject (this)) {
					var ext = node.CreateExtension ();
					if (ext.SupportsObject (this))
						extensions.Add (ext);
					else
						ext.Dispose ();
				}
			}

			foreach (var e in tempExtensions)
				e.Dispose ();

			// Now create the final extension chain

			extensions.Reverse ();
			extensions.AddRange (CreateDefaultExtensions ().Reverse ());
			extensionChain = ExtensionChain.Create (extensions.ToArray ());
			foreach (var e in extensions)
				e.Init (this);
			foreach (var e in extensions)
				e.OnExtensionChainCreated ();
		}

		internal static IEnumerable<ProjectModelExtensionNode> GetModelExtensions (ExtensionContext ctx)
		{
			if (ctx != null)
				return ctx.GetExtensionNodes (ProjectService.ProjectModelExtensionsPath).Cast<ProjectModelExtensionNode> ().Concat (customNodes);
			else
				return AddinManager.GetExtensionNodes (ProjectService.ProjectModelExtensionsPath).Cast<ProjectModelExtensionNode> ().Concat (customNodes);
		}

		static List<ProjectModelExtensionNode> customNodes = new List<ProjectModelExtensionNode> ();

		internal static void RegisterCustomExtension (ProjectModelExtensionNode node)
		{
			customNodes.Add (node);
		}

		internal static void UnregisterCustomExtension (ProjectModelExtensionNode node)
		{
			customNodes.Remove (node);
		}

		protected virtual IEnumerable<WorkspaceObjectExtension> CreateDefaultExtensions ()
		{
			yield return new DefaultWorkspaceObjectExtension ();
		}

		/// <summary>
		/// Called after the object is created, but before the extension chain has been created.
		/// </summary>
		protected virtual void OnInitialize ()
		{
		}

		/// <summary>
		/// Called when the extension chain for this object has been created. This method can be overriden
		/// to do initializations on the object that require access to the extension chain
		/// </summary>
		protected virtual void OnExtensionChainInitialized ()
		{
			itemExtension = ExtensionChain.GetExtension<WorkspaceObjectExtension> ();
		}

		internal virtual IDictionary OnGetExtendedProperties ()
		{
			lock (localLock) {
				if (extendedProperties == null)
					extendedProperties = new Hashtable ();
				return extendedProperties;
			}
		}

		protected virtual IEnumerable<WorkspaceObject> OnGetChildren ()
		{
			yield break;
		}

		protected virtual object OnGetService (Type t)
		{
			return t.IsInstanceOfType (this) ? this : null;
		}

		protected abstract string OnGetName ();

		protected abstract string OnGetItemDirectory ();

		protected abstract string OnGetBaseDirectory ();

		internal class DefaultWorkspaceObjectExtension: WorkspaceObjectExtension
		{
			internal protected override object GetService (Type t)
			{
				return Owner.OnGetService (t);
			}

			internal protected override StringTagModelDescription OnGetStringTagModelDescription (ConfigurationSelector conf)
			{
				return Owner.OnGetStringTagModelDescription (conf);
			}

			internal protected override StringTagModel OnGetStringTagModel (ConfigurationSelector conf)
			{
				return Owner.OnGetStringTagModel (conf);
			}
		}

		protected Task<IDisposable> ReadLock ()
		{
			lock (lockLock) {
				var ts = new TaskCompletionSource<IDisposable> ();
				var ol = new ObjectLock { Object = this, IsWriteLock = false, TaskSource = ts };
				if (writeLockTaken) {
					if (lockRequests == null)
						lockRequests = new Queue<ObjectLock> ();
					lockRequests.Enqueue (ol);
				} else {
					readLocksTaken++;
					ts.SetResult (ol);
				}
				return ts.Task;
			}
		}

		protected Task<IDisposable> WriteLock ()
		{
			lock (lockLock) {
				var ts = new TaskCompletionSource<IDisposable> ();
				var ol = new ObjectLock { Object = this, IsWriteLock = true, TaskSource = ts };
				if (writeLockTaken || readLocksTaken > 0) {
					if (lockRequests == null)
						lockRequests = new Queue<ObjectLock> ();
					lockRequests.Enqueue (ol);
				} else {
					writeLockTaken = true;
					ts.SetResult (ol);
				}
				return ts.Task;
			}
		}

		void ReleaseLock (bool isWriteLock)
		{
			lock (lockLock) {
				if (!isWriteLock) {
					// If there are readers still running, we can't release the lock
					if (--readLocksTaken > 0)
						return;
				}

				while (lockRequests != null && lockRequests.Count > 0) {
					// If readers have been awakened, we can't awaken a writer
					if (readLocksTaken > 0 && lockRequests.Peek ().IsWriteLock)
						return;
					var next = lockRequests.Dequeue ();
					if (next.IsWriteLock) {
						// Only one writer at a time
						next.TaskSource.SetResult (next);
						return;
					} else {
						// All readers can be awakened at once
						writeLockTaken = false;
						readLocksTaken++;
						next.TaskSource.SetResult (next);
					}
				}
				writeLockTaken = false;
			}
		}

		class ObjectLock: IDisposable
		{
			public WorkspaceObject Object;
			public bool IsWriteLock;
			public TaskCompletionSource<IDisposable> TaskSource;

			public void Dispose ()
			{
				Object.ReleaseLock (IsWriteLock);
			}
		}

		Queue<ObjectLock> lockRequests;

		int readLocksTaken;
		bool writeLockTaken;

		object lockLock = new object ();
	}

	public static class WorkspaceObjectExtensions
	{
		public static T As<T> (this WorkspaceObject ob) where T:class
		{
			return ob != null ? ob.GetService<T> () : null;
		}
	}
	
	public interface IWorkspaceFileObject: IFileItem, IDisposable
	{
		IEnumerable<FilePath> GetItemFiles (bool includeReferencedFiles);
		new FilePath FileName { get; set; }
		bool NeedsReload { get; set; }
		bool ItemFilesChanged { get; }
		Task SaveAsync (ProgressMonitor monitor);
		string Name { get; set; }
		FilePath BaseDirectory { get; }
		FilePath ItemDirectory { get; }
	}
}