Allow attributes to be used on enums

wip/source-generators
copygirl 2 years ago
parent 4a5494bfa3
commit 18615fbb3f
  1. 10
      src/gaemstone/ECS/Attributes.cs
  2. 10
      src/gaemstone/ECS/Component.cs
  3. 1
      src/gaemstone/ECS/Entity.cs

@ -16,7 +16,7 @@ public interface ICreateEntityAttribute { }
/// (a globally unique identifier) set equal to their name. If they /// (a globally unique identifier) set equal to their name. If they
/// are also marked with this attribute, the symbol won't be set. /// are also marked with this attribute, the symbol won't be set.
/// </summary> /// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum)]
public class PrivateAttribute : Attribute { } public class PrivateAttribute : Attribute { }
/// <summary> /// <summary>
@ -59,7 +59,7 @@ public class TagAttribute : AddAttribute<Tag>, ICreateEntityAttribute { }
/// <item>If marked with neither, will carry the target's data, if it's a component.</item> /// <item>If marked with neither, will carry the target's data, if it's a component.</item>
/// </list> /// </list>
/// </remarks> /// </remarks>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum)]
public class RelationAttribute : Attribute, ICreateEntityAttribute { } public class RelationAttribute : Attribute, ICreateEntityAttribute { }
@ -67,7 +67,7 @@ public class RelationAttribute : Attribute, ICreateEntityAttribute { }
public class IsAAttribute<TTarget> : AddAttribute<IsA, TTarget> { } public class IsAAttribute<TTarget> : AddAttribute<IsA, TTarget> { }
/// <seealso cref="ChildOf"/> /// <seealso cref="ChildOf"/>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum)]
public class ChildOfAttribute<TTarget> : AddAttribute<ChildOf, TTarget> { } public class ChildOfAttribute<TTarget> : AddAttribute<ChildOf, TTarget> { }
/// <seealso cref="DependsOn"/> /// <seealso cref="DependsOn"/>
@ -90,14 +90,14 @@ public class ProxyAttribute : Attribute
internal ProxyAttribute(Type type) => Type = type; internal ProxyAttribute(Type type) => Type = type;
} }
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum, AllowMultiple = true)]
public class AddEntityAttribute : Attribute public class AddEntityAttribute : Attribute
{ {
public Type Entity { get; } public Type Entity { get; }
internal AddEntityAttribute(Type entity) => Entity = entity; internal AddEntityAttribute(Type entity) => Entity = entity;
} }
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum, AllowMultiple = true)]
public class AddRelationAttribute : Attribute public class AddRelationAttribute : Attribute
{ {
public Type Relation { get; } public Type Relation { get; }

@ -5,7 +5,7 @@ using static flecs_hub.flecs;
namespace gaemstone.ECS; namespace gaemstone.ECS;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum)]
public class ComponentAttribute : EntityAttribute { } public class ComponentAttribute : EntityAttribute { }
public static class ComponentExtensions public static class ComponentExtensions
@ -17,7 +17,13 @@ public static class ComponentExtensions
// TODO: Do some additional sanity checking for this type. // TODO: Do some additional sanity checking for this type.
var typeInfo = default(ecs_type_info_t); var typeInfo = default(ecs_type_info_t);
if (type.IsValueType) { if (type.IsPrimitive) throw new ArgumentException(
"Must not be primitive", nameof(type));
else if (type.IsEnum) {
var wrapper = TypeWrapper.For(type);
typeInfo.size = wrapper.Size;
typeInfo.alignment = wrapper.Size;
} else if (type.IsValueType) {
var wrapper = TypeWrapper.For(type); var wrapper = TypeWrapper.For(type);
if (!wrapper.IsUnmanaged) throw new Exception( if (!wrapper.IsUnmanaged) throw new Exception(
"Struct component must satisfy the unmanaged constraint. " + "Struct component must satisfy the unmanaged constraint. " +

@ -12,6 +12,7 @@ public class EntityAttribute : Attribute, ICreateEntityAttribute
/// without explicitly specifying an entity in a query, where it is equivalent /// without explicitly specifying an entity in a query, where it is equivalent
/// to <see cref="SourceAttribute{}"/> with itself as the generic type parameter. /// to <see cref="SourceAttribute{}"/> with itself as the generic type parameter.
/// </summary> /// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum)]
public class SingletonAttribute : EntityAttribute { } public class SingletonAttribute : EntityAttribute { }
public readonly struct Entity public readonly struct Entity

Loading…
Cancel
Save