aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/ProtocolBuffers/IMessage.cs
diff options
context:
space:
mode:
authorJon Skeet <jonskeet@google.com>2015-06-23 16:09:27 +0100
committerJon Skeet <jonskeet@google.com>2015-06-24 17:56:22 +0100
commitbfee2dfe137b07e64ebd46baf71d932d58d01b1f (patch)
tree4bf6258a990369335cc82c8a8059eaaf1dc2dd06 /csharp/src/ProtocolBuffers/IMessage.cs
parent94071b54d24d2f2f1cb5933a692b5397a047057c (diff)
downloadprotobuf-bfee2dfe137b07e64ebd46baf71d932d58d01b1f.tar.gz
protobuf-bfee2dfe137b07e64ebd46baf71d932d58d01b1f.tar.bz2
protobuf-bfee2dfe137b07e64ebd46baf71d932d58d01b1f.zip
Implement freezing for messages and repeated fields.
Fixes issue #523.
Diffstat (limited to 'csharp/src/ProtocolBuffers/IMessage.cs')
-rw-r--r--csharp/src/ProtocolBuffers/IMessage.cs36
1 files changed, 35 insertions, 1 deletions
diff --git a/csharp/src/ProtocolBuffers/IMessage.cs b/csharp/src/ProtocolBuffers/IMessage.cs
index 5d662cfd..d606aee1 100644
--- a/csharp/src/ProtocolBuffers/IMessage.cs
+++ b/csharp/src/ProtocolBuffers/IMessage.cs
@@ -41,6 +41,7 @@ namespace Google.Protobuf
{
// TODO(jonskeet): Do we want a "weak" (non-generic) version of IReflectedMessage?
+ // TODO(jonskeet): Split these interfaces into separate files when we're happy with them.
/// <summary>
/// Reflection support for a specific message type. message
@@ -85,7 +86,7 @@ namespace Google.Protobuf
/// the implementation class.
/// </summary>
/// <typeparam name="T">The message type.</typeparam>
- public interface IMessage<T> : IMessage, IEquatable<T>, IDeepCloneable<T> where T : IMessage<T>
+ public interface IMessage<T> : IMessage, IEquatable<T>, IDeepCloneable<T>, IFreezable where T : IMessage<T>
{
/// <summary>
/// Merges the given message into this one.
@@ -102,6 +103,11 @@ namespace Google.Protobuf
/// All generated messages implement this interface, but so do some non-message types.
/// Additionally, due to the type constraint on <c>T</c> in <see cref="IMessage{T}"/>,
/// it is simpler to keep this as a separate interface.
+ /// </para>
+ /// <para>
+ /// Freezable types which implement this interface should always return a mutable clone,
+ /// even if the original object is frozen.
+ /// </para>
/// </remarks>
/// <typeparam name="T">The type itself, returned by the <see cref="Clone"/> method.</typeparam>
public interface IDeepCloneable<T>
@@ -112,4 +118,32 @@ namespace Google.Protobuf
/// <returns>A deep clone of this object.</returns>
T Clone();
}
+
+ /// <summary>
+ /// Provides a mechanism for freezing a message (or repeated field collection)
+ /// to make it immutable.
+ /// </summary>
+ /// <remarks>
+ /// Implementations are under no obligation to make this thread-safe: if a freezable
+ /// type instance is shared between threads before being frozen, and one thread then
+ /// freezes it, it is possible for other threads to make changes during the freezing
+ /// operation and also to observe stale values for mutated fields. Objects should be
+ /// frozen before being made available to other threads.
+ /// </remarks>
+ public interface IFreezable
+ {
+ /// <summary>
+ /// Freezes this object.
+ /// </summary>
+ /// <remarks>
+ /// If the object is already frozen, this method has no effect.
+ /// </remarks>
+ void Freeze();
+
+ /// <summary>
+ /// Returns whether or not this object is frozen (and therefore immutable).
+ /// </summary>
+ /// <value><c>true</c> if this object is frozen; <c>false</c> otherwise.</value>
+ bool IsFrozen { get; }
+ }
}