using System;
using System.Reflection;
namespace ComputeSharp.Shaders.Translation.Models
{
///
/// A that wraps a readable member of another and provides access to it
///
internal sealed partial class ReadableMember
{
///
/// The id of the current member
///
private readonly string Id;
///
/// The object wrapped by the current instance, if present
///
private readonly FieldInfo? Field;
///
/// The object wrapped by the current instance, if present
///
private readonly PropertyInfo? Property;
///
/// Creates a new instance with the given parameters
///
/// The target object wrapped by the current instance
private ReadableMember(MemberInfo memberInfo)
{
// General properties
Name = memberInfo.Name;
Id = $"{memberInfo.DeclaringType.FullName}{Type.Delimiter}{Name}";
DeclaringType = memberInfo.DeclaringType;
// Type specific properties
if (memberInfo is FieldInfo fieldInfo)
{
Field = fieldInfo;
IsStatic = Field.IsStatic;
MemberType = Field.FieldType;
}
else if (memberInfo is PropertyInfo propertyInfo)
{
Property = propertyInfo;
IsStatic = Property.GetMethod.IsStatic;
MemberType = Property.PropertyType;
}
else throw new InvalidOperationException("Field and property can't both be null at the same time");
}
///
/// Gets the name of the wrapped member for the current instance
///
public string Name { get; }
///
/// Gets whether or not the current member is not an instance member
///
public bool IsStatic { get; }
///
/// Gets the where the current member is declared in
///
public Type DeclaringType { get; }
///
/// Gets the of the wrapped member for the current instance
///
public Type MemberType { get; }
///
/// Converts a object into a instance
///
/// The input object to wrap
public static implicit operator ReadableMember(FieldInfo field) => new ReadableMember(field);
///
/// Converts a object into a instance
///
/// The input object to wrap
public static implicit operator ReadableMember(PropertyInfo property) => new ReadableMember(property);
}
}