Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
CompressedTimeSpan.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Runtime.InteropServices;
5 using SiliconStudio.Core.Serialization;
6 using SiliconStudio.Core.Serialization.Serializers;
7 
8 namespace SiliconStudio.Paradox.DataModel
9 {
10  [DataSerializer(typeof(CompressedTimeSpanSerializer))]
11  [StructLayout(LayoutKind.Sequential)]
12  public struct CompressedTimeSpan : IComparable, IComparable<CompressedTimeSpan>, IEquatable<CompressedTimeSpan>
13  {
14  private readonly int ticks;
15 
16  public CompressedTimeSpan(int ticks)
17  {
18  this.ticks = ticks;
19  }
20 
21  public static implicit operator TimeSpan(CompressedTimeSpan t)
22  {
24  return TimeSpan.MinValue;
26  return TimeSpan.MaxValue;
27  return new TimeSpan(t.Ticks * TicksCompressedRatio);
28  }
29 
30  public static explicit operator CompressedTimeSpan(TimeSpan t)
31  {
32  if (t == TimeSpan.MinValue)
33  return CompressedTimeSpan.MinValue;
34  if (t == TimeSpan.MaxValue)
35  return CompressedTimeSpan.MaxValue;
36  return new CompressedTimeSpan((int)(t.Ticks / TicksCompressedRatio));
37  }
38 
39  public const int TicksPerMillisecond = 100;
40  public const int TicksPerSecond = TicksPerMillisecond * 1000;
41 
42  private const long TicksCompressedRatio = TimeSpan.TicksPerMillisecond / (long)TicksPerMillisecond;
43 
44  public static readonly CompressedTimeSpan Zero = new CompressedTimeSpan(0);
45  public static readonly CompressedTimeSpan MinValue = new CompressedTimeSpan(int.MinValue);
46  public static readonly CompressedTimeSpan MaxValue = new CompressedTimeSpan(int.MaxValue);
47 
48  public int Ticks
49  {
50  get { return ticks; }
51  }
52 
53  public static CompressedTimeSpan FromSeconds(double seconds)
54  {
55  return new CompressedTimeSpan((int)(seconds * TicksPerSecond));
56  }
57 
58  public bool Equals(CompressedTimeSpan other)
59  {
60  return other.ticks == ticks;
61  }
62 
63  public override bool Equals(object obj)
64  {
65  if (ReferenceEquals(null, obj)) return false;
66  if (obj.GetType() != typeof(CompressedTimeSpan)) return false;
67  return Equals((CompressedTimeSpan)obj);
68  }
69 
70  public override int GetHashCode()
71  {
72  return ticks;
73  }
74 
75  public static bool operator ==(CompressedTimeSpan left, CompressedTimeSpan right)
76  {
77  return left.Equals(right);
78  }
79 
80  public static bool operator !=(CompressedTimeSpan left, CompressedTimeSpan right)
81  {
82  return !left.Equals(right);
83  }
84 
85  public static bool operator<(CompressedTimeSpan t1, CompressedTimeSpan t2)
86  {
87  return t1.Ticks < t2.Ticks;
88  }
89 
90  public static bool operator >(CompressedTimeSpan t1, CompressedTimeSpan t2)
91  {
92  return t1.Ticks > t2.Ticks;
93  }
94 
95  public static bool operator <=(CompressedTimeSpan t1, CompressedTimeSpan t2)
96  {
97  return t1.Ticks <= t2.Ticks;
98  }
99 
100  public static bool operator >=(CompressedTimeSpan t1, CompressedTimeSpan t2)
101  {
102  return t1.Ticks >= t2.Ticks;
103  }
104 
106  {
107  // TODO: Overflow check?
108  return new CompressedTimeSpan(t1.Ticks + t2.Ticks);
109  }
110 
112  {
113  // TODO: Overflow check?
114  return new CompressedTimeSpan(t1.Ticks - t2.Ticks);
115  }
116 
117  public static CompressedTimeSpan operator *(CompressedTimeSpan t1, int factor)
118  {
119  // TODO: Overflow check?
120  return new CompressedTimeSpan(t1.Ticks / factor);
121  }
122 
123  public static CompressedTimeSpan operator /(CompressedTimeSpan t1, int factor)
124  {
125  // TODO: Overflow check?
126  return new CompressedTimeSpan(t1.Ticks / factor);
127  }
128 
129  public static CompressedTimeSpan operator *(CompressedTimeSpan t1, float factor)
130  {
131  // TODO: Overflow check?
132  return new CompressedTimeSpan((int)((float)t1.Ticks / factor));
133  }
134 
135  public static CompressedTimeSpan operator /(CompressedTimeSpan t1, float factor)
136  {
137  // TODO: Overflow check?
138  return new CompressedTimeSpan((int)((float)t1.Ticks / factor));
139  }
140 
141  public int CompareTo(CompressedTimeSpan other)
142  {
143  if (ticks > other.ticks) return 1;
144  if (ticks < other.ticks) return -1;
145  return 0;
146  }
147 
148  public int CompareTo(object obj)
149  {
150  if (obj == null)
151  return 1;
152 
153  if (!(obj is CompressedTimeSpan))
154  throw new ArgumentException();
155 
156  return CompareTo((CompressedTimeSpan)obj);
157  }
158 
159  public override string ToString()
160  {
161  return Ticks.ToString();
162  }
163  }
164 
165  /// <summary>
166  /// Data serializer for TimeSpan.
167  /// </summary>
168  internal class CompressedTimeSpanSerializer : DataSerializer<CompressedTimeSpan>
169  {
170  public override void Serialize(ref CompressedTimeSpan timeSpan, ArchiveMode mode, SerializationStream stream)
171  {
172  if (mode == ArchiveMode.Serialize)
173  {
174  stream.Write(timeSpan.Ticks);
175  }
176  else if (mode == ArchiveMode.Deserialize)
177  {
178  timeSpan = new CompressedTimeSpan(stream.ReadInt32());
179  }
180  }
181  }
182 }
FbxDouble3 operator*(double factor, FbxDouble3 vector)
Base class for implementation of SerializationStream.
static CompressedTimeSpan FromSeconds(double seconds)
Describes how to serialize and deserialize an object without knowing its type. Used as a common base ...
ArchiveMode
Enumerates the different mode of serialization (either serialization or deserialization).
Definition: ArchiveMode.cs:8
static bool operator<(CompressedTimeSpan t1, CompressedTimeSpan t2)