| | | 1 | | //======================================================================= |
| | | 2 | | // MemoryPackableAttribute.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | |
| | | 10 | | namespace MemoryPack; |
| | | 11 | | |
| | | 12 | | /// <summary>Carries MemoryPack-compatible generation and layout metadata.</summary> |
| | | 13 | | /// <remarks>The shim stores attribute metadata for Lean builds. Serialization requires the full MemoryPack implementati |
| | | 14 | | [AttributeUsage( |
| | | 15 | | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface, |
| | | 16 | | AllowMultiple = false, |
| | | 17 | | Inherited = false)] |
| | | 18 | | public sealed class MemoryPackableAttribute : Attribute |
| | | 19 | | { |
| | | 20 | | /// <summary>Records a generation mode and selects its default layout.</summary> |
| | | 21 | | /// <param name="generateType">Defaults to ordinary object mode. Version-tolerant and circular-reference modes selec |
| | | 22 | | /// <remarks>Other modes select sequential layout.</remarks> |
| | 5 | 23 | | public MemoryPackableAttribute(GenerateType generateType = GenerateType.Object) |
| | | 24 | | { |
| | 5 | 25 | | GenerateType = generateType; |
| | 5 | 26 | | SerializeLayout = generateType is GenerateType.VersionTolerant or GenerateType.CircularReference |
| | 5 | 27 | | ? SerializeLayout.Explicit |
| | 5 | 28 | | : SerializeLayout.Sequential; |
| | 5 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary>Records a layout with ordinary object generation metadata.</summary> |
| | | 32 | | /// <param name="serializeLayout">The layout value to preserve on the attribute.</param> |
| | 2 | 33 | | public MemoryPackableAttribute(SerializeLayout serializeLayout) |
| | | 34 | | { |
| | 2 | 35 | | GenerateType = GenerateType.Object; |
| | 2 | 36 | | SerializeLayout = serializeLayout; |
| | 2 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary>Records an explicit generation mode and layout pair.</summary> |
| | | 40 | | /// <param name="generateType">The mode metadata to preserve.</param> |
| | | 41 | | /// <param name="serializeLayout">The layout metadata to preserve independently of the generation mode.</param> |
| | 2 | 42 | | public MemoryPackableAttribute(GenerateType generateType, SerializeLayout serializeLayout) |
| | | 43 | | { |
| | 2 | 44 | | GenerateType = generateType; |
| | 2 | 45 | | SerializeLayout = serializeLayout; |
| | 2 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary>Gets the recorded generation mode.</summary> |
| | | 49 | | /// <value>The supplied value, or ordinary object mode when omitted.</value> |
| | | 50 | | public GenerateType GenerateType { get; } |
| | | 51 | | |
| | | 52 | | /// <summary>Gets the recorded member layout.</summary> |
| | | 53 | | /// <value>The supplied layout or the default associated with the selected generation mode.</value> |
| | | 54 | | public SerializeLayout SerializeLayout { get; } |
| | | 55 | | } |