Allow attributes to be used on enums

wip/source-generators
copygirl 2 years ago
parent 4a5494bfa3
commit 18615fbb3f
  1. 24
      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
/// are also marked with this attribute, the symbol won't be set.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum)]
public class PrivateAttribute : Attribute { }
/// <summary>
@ -51,15 +51,15 @@ public class TagAttribute : AddAttribute<Tag>, ICreateEntityAttribute { }
/// the "relation" in a pair. However, this attribute is purely informational.
/// </summary>
/// <remarks>
/// The relationship may have component data associated with
/// it when added to an entity under these circumstances:
/// <list type="bullet">
/// <item>If marked as a <see cref="TagAttribute"/>, does not carry data.</item>
/// <item>If marked as a <see cref="ComponentAttribute"/>, carries the relation's data.</item>
/// <item>If marked with neither, will carry the target's data, if it's a component.</item>
/// </list>
/// The relationship may have component data associated with
/// it when added to an entity under these circumstances:
/// <list type="bullet">
/// <item>If marked as a <see cref="TagAttribute"/>, does not carry data.</item>
/// <item>If marked as a <see cref="ComponentAttribute"/>, carries the relation's data.</item>
/// <item>If marked with neither, will carry the target's data, if it's a component.</item>
/// </list>
/// </remarks>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum)]
public class RelationAttribute : Attribute, ICreateEntityAttribute { }
@ -67,7 +67,7 @@ public class RelationAttribute : Attribute, ICreateEntityAttribute { }
public class IsAAttribute<TTarget> : AddAttribute<IsA, TTarget> { }
/// <seealso cref="ChildOf"/>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum)]
public class ChildOfAttribute<TTarget> : AddAttribute<ChildOf, TTarget> { }
/// <seealso cref="DependsOn"/>
@ -90,14 +90,14 @@ public class ProxyAttribute : Attribute
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 Type Entity { get; }
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 Type Relation { get; }

@ -5,7 +5,7 @@ using static flecs_hub.flecs;
namespace gaemstone.ECS;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum)]
public class ComponentAttribute : EntityAttribute { }
public static class ComponentExtensions
@ -17,7 +17,13 @@ public static class ComponentExtensions
// TODO: Do some additional sanity checking for this type.
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);
if (!wrapper.IsUnmanaged) throw new Exception(
"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
/// to <see cref="SourceAttribute{}"/> with itself as the generic type parameter.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum)]
public class SingletonAttribute : EntityAttribute { }
public readonly struct Entity

Loading…
Cancel
Save