// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// 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 System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis;
using SharpGen.Config;
using SharpGen.CppModel;
using SharpGen.Transform;
namespace SharpGen.Model
{
///
/// Root class for all model elements.
///
[DebuggerDisplay("Name: {" + nameof(Name) + "}")]
public abstract class CsBase
{
private ObservableCollection _items;
private string _cppElementName;
private string description;
protected CsBase(CppElement cppElement, string name)
{
CppElement = cppElement;
Name = name;
if (cppElement == null)
return;
var tag = cppElement.Rule;
Visibility = tag.Visibility ?? Visibility;
}
private void ItemsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.OldItems != null)
{
foreach (var item in e.OldItems.OfType())
{
item.Parent = null;
}
}
if (e.NewItems != null)
{
foreach (var item in e.NewItems.OfType())
{
item.Parent = this;
}
}
OnItemsChanged();
ExpireOnItemsChange();
}
private void ExpireOnItemsChange()
{
if (_items == null)
return;
foreach (var itemList in ExpiringOnItemsChange)
itemList?.Expire();
}
protected virtual void OnItemsChanged()
{
}
///
/// Gets or sets the parent of this container.
///
/// The parent.
public CsBase Parent { get; private set; }
///
/// Gets the parent of a specified type. This method goes back
/// to all parent and returns the first parent of the type T or null if no parent were found.
///
/// Type of the parent
/// a valid reference to the parent T or null if no parent of this type
public T GetParent() where T : CsBase
{
var parent = Parent;
while (parent is { } and not T)
parent = parent.Parent;
return (T) parent;
}
///
/// Gets items stored in this container.
///
/// The items.
public IReadOnlyCollection Items => ItemsImpl;
private ObservableCollection ItemsImpl
{
get
{
if (_items == null) ResetItems();
return _items;
}
}
public virtual IEnumerable AdditionalItems => Enumerable.Empty();
protected void ResetItems()
{
ExpireOnItemsChange();
_items = new ObservableCollection();
_items.CollectionChanged += ItemsChanged;
}
///
/// Adds the specified inner container to this container.
///
///
/// The Parent property of the innerContainer is set to this container.
///
/// The inner container.
public void Add(CsBase innerCs)
{
ItemsImpl.Add(innerCs);
}
///
/// Removes the specified inner container from this container
///
///
/// The Parent property of the innerContainer is set to null.
///
/// The inner container.
public void Remove(CsBase innerCs)
{
ItemsImpl.Remove(innerCs);
}
///
/// Gets or sets the name of this element.
///
/// The name.
public string Name { get; private protected set; }
///
/// Gets or sets the of this element. Default is public.
///
/// The visibility.
public Visibility Visibility { get; set; } = Visibility.Public;
public SyntaxTokenList VisibilityTokenList => ModelUtilities.VisibilityToTokenList(Visibility);
///
/// Gets the full qualified name of this type.
///
/// The full name.
public virtual string QualifiedName
{
get
{
var path = Parent?.QualifiedName;
var name = Name ?? string.Empty;
return string.IsNullOrEmpty(path) ? name : path + "." + name;
}
}
///
/// Gets or sets the C++ element associated to this container.
///
/// The C++ element.
public CppElement CppElement { get; }
///
/// Gets the name of the C++ element.
///
/// The name of the C++ element.
public string CppElementName
{
get => string.IsNullOrEmpty(_cppElementName) ? CppElement?.Name : _cppElementName;
set => _cppElementName = value;
}
///
/// Gets or sets the doc id.
///
public string DocId { get; set; }
///
/// Gets or sets the description documentation.
///
public virtual string Description
{
get => string.IsNullOrEmpty(description) ? DefaultDescription : description;
set => description = value;
}
protected virtual string DefaultDescription => "No documentation.";
///
/// Gets or sets the remarks documentation.
///
public string Remarks { get; set; } = string.Empty;
public virtual void FillDocItems(IList docItems, IDocumentationLinker manager)
{
}
public virtual string DocUnmanagedName => CppElementName;
public virtual string DocUnmanagedShortName => CppElementName;
[ExcludeFromCodeCoverage]
public override string ToString() => QualifiedName;
protected void ResetParentAfterClone()
{
Parent = null;
}
private protected virtual IEnumerable ExpiringOnItemsChange => Enumerable.Empty();
private protected static IEnumerable AppendNonNull(IEnumerable source, params CsBase[] values)
=> source.Concat(values.Where(x => x != null).Distinct());
}
}