Skip to content

Commit

Permalink
Merge pull request #71 from RCTycooner/master
Browse files Browse the repository at this point in the history
Marked the (De)SerializerDelegates as public.
  • Loading branch information
rikimaru0345 authored Oct 18, 2019
2 parents f7dd21e + 5035e1d commit cc24d60
Showing 1 changed file with 41 additions and 41 deletions.
82 changes: 41 additions & 41 deletions src/Ceras/Formatters/IFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
using System;
using Ceras.Helpers;
using Ceras.Helpers;
using System;

namespace Ceras.Formatters
{

public interface IFormatter { }
public interface IFormatter<T> : IFormatter
{
void Serialize(ref byte[] buffer, ref int offset, T value);
void Deserialize(byte[] buffer, ref int offset, ref T value);
}
delegate void SerializeDelegate<T>(ref byte[] buffer, ref int offset, T value);
delegate void DeserializeDelegate<T>(byte[] buffer, ref int offset, ref T value);
delegate void StaticSerializeDelegate(ref byte[] buffer, ref int offset);
delegate void StaticDeserializeDelegate(byte[] buffer, ref int offset);


// A formatter that is relying on the Schema of a Type in some way (either directly: SchemaDynamicFormatter, or indirectly ReferenceFormatter)
// Those formatters need to be notified when the CurrentSchema of a Type changes (because of reading an older schema, or because we're resetting to the primary schema for writing)
interface ISchemaTaintedFormatter
{
void OnSchemaChanged(TypeMetaData meta);
}

static class FormatterHelper
{
public static bool IsFormatterMatch(IFormatter formatter, Type type)
{
var closedFormatter = ReflectionHelper.FindClosedType(formatter.GetType(), typeof(IFormatter<>));
var formattedType = closedFormatter.GetGenericArguments()[0];

return type == formattedType;
}

public static void ThrowOnMismatch(IFormatter formatter, Type typeToFormat)
{
if(!IsFormatterMatch(formatter, typeToFormat))
throw new InvalidOperationException($"The given formatter '{formatter.GetType().FullName}' is not an exact match for the formatted type '{typeToFormat.FullName}'");
}
}
public interface IFormatter { }

public interface IFormatter<T> : IFormatter
{
void Serialize(ref byte[] buffer, ref int offset, T value);
void Deserialize(byte[] buffer, ref int offset, ref T value);
}

public delegate void SerializeDelegate<T>(ref byte[] buffer, ref int offset, T value);
public delegate void DeserializeDelegate<T>(byte[] buffer, ref int offset, ref T value);

public delegate void StaticSerializeDelegate(ref byte[] buffer, ref int offset);
public delegate void StaticDeserializeDelegate(byte[] buffer, ref int offset);


// A formatter that is relying on the Schema of a Type in some way (either directly: SchemaDynamicFormatter, or indirectly ReferenceFormatter)
// Those formatters need to be notified when the CurrentSchema of a Type changes (because of reading an older schema, or because we're resetting to the primary schema for writing)
interface ISchemaTaintedFormatter
{
void OnSchemaChanged(TypeMetaData meta);
}

static class FormatterHelper
{
public static bool IsFormatterMatch(IFormatter formatter, Type type)
{
var closedFormatter = ReflectionHelper.FindClosedType(formatter.GetType(), typeof(IFormatter<>));

var formattedType = closedFormatter.GetGenericArguments()[0];

return type == formattedType;
}

public static void ThrowOnMismatch(IFormatter formatter, Type typeToFormat)
{
if (!IsFormatterMatch(formatter, typeToFormat))
throw new InvalidOperationException($"The given formatter '{formatter.GetType().FullName}' is not an exact match for the formatted type '{typeToFormat.FullName}'");
}
}
}

0 comments on commit cc24d60

Please sign in to comment.