|
| 1 | +// Copyright 2019 Florian Gather <florian.gather@tngtech.com> |
| 2 | +// Copyright 2019 Fritz Brandhuber <fritz.brandhuber@tngtech.com> |
| 3 | +// Copyright 2020 Pavel Fischer <rubbiroid@gmail.com> |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: Apache-2.0 |
| 6 | + |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using ArchUnitNET.Domain.Dependencies; |
| 10 | + |
| 11 | +namespace ArchUnitNET.Domain |
| 12 | +{ |
| 13 | + public class FunctionPointer : IType |
| 14 | + { |
| 15 | + private readonly IType _type; |
| 16 | + |
| 17 | + public FunctionPointer( |
| 18 | + IType type, |
| 19 | + ITypeInstance<IType> returnTypeInstance, |
| 20 | + List<ITypeInstance<IType>> parameterTypeInstances |
| 21 | + ) |
| 22 | + { |
| 23 | + _type = type; |
| 24 | + ReturnTypeInstance = returnTypeInstance; |
| 25 | + ParameterTypeInstances = parameterTypeInstances; |
| 26 | + } |
| 27 | + |
| 28 | + public Namespace Namespace => _type.Namespace; |
| 29 | + public Assembly Assembly => _type.Assembly; |
| 30 | + public MemberList Members => _type.Members; |
| 31 | + public IEnumerable<IType> ImplementedInterfaces => _type.ImplementedInterfaces; |
| 32 | + public bool IsNested => _type.IsNested; |
| 33 | + public bool IsStub => _type.IsStub; |
| 34 | + public bool IsGenericParameter => _type.IsGenericParameter; |
| 35 | + public string Name => _type.Name; |
| 36 | + public string FullName => _type.FullName; |
| 37 | + public Visibility Visibility => _type.Visibility; |
| 38 | + public bool IsGeneric => _type.IsGeneric; |
| 39 | + public List<GenericParameter> GenericParameters => _type.GenericParameters; |
| 40 | + public bool IsCompilerGenerated => _type.IsCompilerGenerated; |
| 41 | + public List<ITypeDependency> Dependencies => _type.Dependencies; |
| 42 | + public List<ITypeDependency> BackwardsDependencies => _type.BackwardsDependencies; |
| 43 | + public IEnumerable<Attribute> Attributes => _type.Attributes; |
| 44 | + public List<AttributeInstance> AttributeInstances => _type.AttributeInstances; |
| 45 | + public ITypeInstance<IType> ReturnTypeInstance { get; } |
| 46 | + public List<ITypeInstance<IType>> ParameterTypeInstances { get; } |
| 47 | + |
| 48 | + public bool Equals(FunctionPointer other) |
| 49 | + { |
| 50 | + if (ReferenceEquals(null, other)) |
| 51 | + { |
| 52 | + return false; |
| 53 | + } |
| 54 | + if (ReferenceEquals(this, other)) |
| 55 | + { |
| 56 | + return true; |
| 57 | + } |
| 58 | + return Equals(_type, other._type) |
| 59 | + && Equals(ReturnTypeInstance, other.ReturnTypeInstance) |
| 60 | + && ParameterTypeInstances.SequenceEqual(other.ParameterTypeInstances); |
| 61 | + } |
| 62 | + |
| 63 | + public override bool Equals(object obj) |
| 64 | + { |
| 65 | + if (ReferenceEquals(null, obj)) |
| 66 | + { |
| 67 | + return false; |
| 68 | + } |
| 69 | + if (ReferenceEquals(this, obj)) |
| 70 | + { |
| 71 | + return true; |
| 72 | + } |
| 73 | + return obj.GetType() == GetType() && Equals((FunctionPointer)obj); |
| 74 | + } |
| 75 | + |
| 76 | + public override int GetHashCode() |
| 77 | + { |
| 78 | + unchecked |
| 79 | + { |
| 80 | + var hashCode = _type.GetHashCode(); |
| 81 | + hashCode = |
| 82 | + (hashCode * 397) |
| 83 | + ^ (ReturnTypeInstance != null ? ReturnTypeInstance.GetHashCode() : 0); |
| 84 | + hashCode = ParameterTypeInstances.Aggregate( |
| 85 | + hashCode, |
| 86 | + (current, typeInstance) => |
| 87 | + (current * 397) ^ (typeInstance != null ? typeInstance.GetHashCode() : 0) |
| 88 | + ); |
| 89 | + return hashCode; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments