• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-dotnet / 9881710012

10 Jul 2024 09:26PM UTC coverage: 81.928% (+0.6%) from 81.376%
9881710012

Pull #3644

github

59a21e
nirinchev
Prepare a debug build
Pull Request #3644: Prepare a debug build

2317 of 3019 branches covered (76.75%)

Branch coverage included in aggregate %.

9402 of 11285 relevant lines covered (83.31%)

42998.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.89
/Realm/Realm/DatabaseTypes/RealmInteger.cs
1
////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright 2017 Realm Inc.
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
//
17
////////////////////////////////////////////////////////////////////////////
18

19
using System;
20
using System.Diagnostics.CodeAnalysis;
21
using Realms.Helpers;
22

23
namespace Realms
24
{
25
    /// <summary>
26
    /// A structure representing an integer value in the database. It offers API to increment the value, which produces
27
    /// correct merges during conflicts.
28
    /// </summary>
29
    /// <remarks>
30
    /// <see cref="RealmInteger{T}"/> is implicitly convertible to and from T/>.
31
    /// <br/>
32
    /// Calling <see cref="Increment()"/> on a managed <see cref="RealmObject"/>/<see cref="EmbeddedObject"/>'s property must be done in a write
33
    /// transaction. When calling <see cref="Increment()"/> on a <see cref="RealmObject"/>/<see cref="EmbeddedObject"/> property, it will increment
34
    /// the property's value in the database, so the change will be reflected the next time this property is accessed.
35
    /// </remarks>
36
    /// <typeparam name="T">
37
    /// The integer type, represented by this <see cref="RealmInteger{T}"/>. Supported types are <see cref="byte"/>,
38
    /// <see cref="short"/>, <see cref="int"/>, and <see cref="long"/>.
39
    /// </typeparam>
40
    [SuppressMessage("Design", "CA1066:Implement IEquatable when overriding Object.Equals", Justification = "We already implement IEquatable<T> and RealmInteger<T> implicitly converts to T.")]
41
    public readonly struct RealmInteger<T> :
42
        IEquatable<T>,
43
        IComparable<RealmInteger<T>>,
44
        IComparable<T>,
45
        IConvertible,
46
        IFormattable
47
        where T : struct, IComparable<T>, IFormattable, IConvertible, IEquatable<T>
48
    {
49
        private readonly T _value;
50
        private readonly ObjectHandle? _objectHandle;
51
        private readonly IntPtr _propertyIndex;
52

53
        [MemberNotNullWhen(true, nameof(_objectHandle))]
54
        private bool IsManaged => _objectHandle != null;
175✔
55

56
        internal RealmInteger(T value)
57
        {
1,677✔
58
            _value = value;
1,677✔
59
            _objectHandle = null;
1,677✔
60
            _propertyIndex = default;
1,677✔
61
        }
1,677✔
62

63
        internal RealmInteger(T value, ObjectHandle objectHandle, IntPtr propertyIndex)
64
        {
848✔
65
            _value = value;
848✔
66
            _objectHandle = objectHandle;
848✔
67
            _propertyIndex = propertyIndex;
848✔
68
        }
848✔
69

70
        /// <summary>
71
        /// Increments the integer value by 1. Inverse of <see cref="Decrement"/>.
72
        /// </summary>
73
        /// <returns>The incremented value.</returns>
74
        public RealmInteger<T> Increment()
75
        {
3✔
76
            return Increment(Operator.Convert<int, T>(1));
3✔
77
        }
1✔
78

79
        /// <summary>
80
        /// Decrements the integer value by 1. Inverse of <see cref="Increment()"/>.
81
        /// </summary>
82
        /// <returns>The decremented value.</returns>
83
        public RealmInteger<T> Decrement()
84
        {
3✔
85
            return Increment(Operator.Convert<int, T>(-1));
3✔
86
        }
1✔
87

88
        /// <summary>
89
        /// Increment the integer value by a specified amount.
90
        /// </summary>
91
        /// <returns>The incremented value.</returns>
92
        /// <param name="value">Value by which to increment.</param>
93
        public RealmInteger<T> Increment(T value)
94
        {
175✔
95
            if (IsManaged)
175✔
96
            {
86✔
97
                var result = _objectHandle.AddInt64(_propertyIndex, Operator.Convert<T, long>(value));
86✔
98
                return new RealmInteger<T>(Operator.Convert<long, T>(result), _objectHandle, _propertyIndex);
86✔
99
            }
100

101
            throw new NotSupportedException("Increment should only be called on RealmInteger properties of managed objects.");
89✔
102
        }
86✔
103

104
        #region Equals
105

106
        /// <inheritdoc />
107
        public override bool Equals(object? obj)
108
        {
14✔
109
            if (obj is RealmInteger<T> realmInteger)
14!
110
            {
14✔
111
                return _value.Equals(realmInteger._value);
14✔
112
            }
113

114
            if (obj is T value)
×
115
            {
×
116
                return _value.Equals(value);
×
117
            }
118

119
            return false;
×
120
        }
14✔
121

122
        /// <summary>
123
        /// Indicates whether this instance represents the same numeric value as the provided object.
124
        /// </summary>
125
        /// <param name="other">The object to compare with the current instance.</param>
126
        /// <returns>true if obj and this instance represent the same numeric value; otherwise, false.</returns>
127
        public bool Equals(T other) => _value.Equals(other);
2,937✔
128

129
        /// <inheritdoc />
130
        public override int GetHashCode() => _value.GetHashCode();
×
131

132
        /// <summary>
133
        /// Returns the string representation of the underlying numeric value.
134
        /// </summary>
135
        /// <returns>The string representation of the numeric value.</returns>
136
        public override string? ToString() => _value.ToString();
×
137

138
        #endregion
139

140
        #region IComparable
141

142
        /// <summary>
143
        /// Compares this instance to another <see cref="RealmInteger{T}"/> value.
144
        /// </summary>
145
        /// <param name="other">The value to compare to.</param>
146
        /// <returns>1 if this instance is greater than <c>other</c>, 0 if the two values are equal, and -1 if <c>other</c> is larger.</returns>
147
        public int CompareTo(RealmInteger<T> other) => _value.CompareTo(other._value);
14✔
148

149
        /// <summary>
150
        /// Compares this instance to another numeric value.
151
        /// </summary>
152
        /// <param name="other">The value to compare to.</param>
153
        /// <returns>1 if this instance is greater than <c>other</c>, 0 if the two values are equal, and -1 if <c>other</c> is larger.</returns>
154
        public int CompareTo(T other) => _value.CompareTo(other);
×
155

156
        #endregion
157

158
        #region IConvertible
159

160
        /// <summary>
161
        /// Returns the <see cref="TypeCode"/> of the value represented by this <see cref="RealmInteger{T}"/>.
162
        /// </summary>
163
        /// <returns>The enumerated constant that is the System.TypeCode of the class or value type that implements this interface.</returns>
164
        TypeCode IConvertible.GetTypeCode() => _value.GetTypeCode();
×
165

166
        /// <summary>
167
        /// Converts the value of this instance to an equivalent Boolean value using the
168
        /// specified culture-specific formatting information.
169
        /// </summary>
170
        /// <param name="provider">An <see cref="IFormatProvider"/>interface implementation that supplies culture-specific formatting information.</param>
171
        /// <returns>A Boolean value equivalent to the value of this instance.</returns>
172
        bool IConvertible.ToBoolean(IFormatProvider? provider) => _value.ToBoolean(provider);
×
173

174
        /// <summary>
175
        /// Converts the value of this instance to an equivalent 8-bit unsigned integer using the
176
        /// specified culture-specific formatting information.
177
        /// </summary>
178
        /// <param name="provider">An <see cref="IFormatProvider"/>interface implementation that supplies culture-specific formatting information.</param>
179
        /// <returns>An 8-bit unsigned integer value equivalent to the value of this instance.</returns>
180
        byte IConvertible.ToByte(IFormatProvider? provider) => _value.ToByte(provider);
×
181

182
        /// <summary>
183
        /// Converts the value of this instance to an equivalent Unicode character using the
184
        /// specified culture-specific formatting information.
185
        /// </summary>
186
        /// <param name="provider">An <see cref="IFormatProvider"/>interface implementation that supplies culture-specific formatting information.</param>
187
        /// <returns>A Unicode character value equivalent to the value of this instance.</returns>
188
        char IConvertible.ToChar(IFormatProvider? provider) => _value.ToChar(provider);
×
189

190
        /// <summary>
191
        /// Converts the value of this instance to an equivalent <see cref="DateTime"/> value using the
192
        /// specified culture-specific formatting information.
193
        /// </summary>
194
        /// <param name="provider">An <see cref="IFormatProvider"/>interface implementation that supplies culture-specific formatting information.</param>
195
        /// <returns>A <see cref="DateTime"/> value equivalent to the value of this instance.</returns>
196
        DateTime IConvertible.ToDateTime(IFormatProvider? provider) => _value.ToDateTime(provider);
×
197

198
        /// <summary>
199
        /// Converts the value of this instance to an equivalent <see cref="decimal"/> value using the
200
        /// specified culture-specific formatting information.
201
        /// </summary>
202
        /// <param name="provider">An <see cref="IFormatProvider"/>interface implementation that supplies culture-specific formatting information.</param>
203
        /// <returns>A <see cref="decimal"/> value equivalent to the value of this instance.</returns>
204
        decimal IConvertible.ToDecimal(IFormatProvider? provider) => _value.ToDecimal(provider);
×
205

206
        /// <summary>
207
        /// Converts the value of this instance to an equivalent double-precision floating-point number using the
208
        /// specified culture-specific formatting information.
209
        /// </summary>
210
        /// <param name="provider">An <see cref="IFormatProvider"/>interface implementation that supplies culture-specific formatting information.</param>
211
        /// <returns>A double-precision floating-point number equivalent to the value of this instance.</returns>
212
        double IConvertible.ToDouble(IFormatProvider? provider) => _value.ToDouble(provider);
×
213

214
        /// <summary>
215
        /// Converts the value of this instance to an equivalent 16-bit signed integer using the
216
        /// specified culture-specific formatting information.
217
        /// </summary>
218
        /// <param name="provider">An <see cref="IFormatProvider"/>interface implementation that supplies culture-specific formatting information.</param>
219
        /// <returns>A 16-bit signed integer equivalent to the value of this instance.</returns>
220
        short IConvertible.ToInt16(IFormatProvider? provider) => _value.ToInt16(provider);
×
221

222
        /// <summary>
223
        /// Converts the value of this instance to an equivalent 32-bit signed integer using the
224
        /// specified culture-specific formatting information.
225
        /// </summary>
226
        /// <param name="provider">An <see cref="IFormatProvider"/>interface implementation that supplies culture-specific formatting information.</param>
227
        /// <returns>A 32-bit signed integer equivalent to the value of this instance.</returns>
228
        int IConvertible.ToInt32(IFormatProvider? provider) => _value.ToInt32(provider);
×
229

230
        /// <summary>
231
        /// Converts the value of this instance to an equivalent 64-bit signed integer using the
232
        /// specified culture-specific formatting information.
233
        /// </summary>
234
        /// <param name="provider">An <see cref="IFormatProvider"/>interface implementation that supplies culture-specific formatting information.</param>
235
        /// <returns>A 64-bit signed integer equivalent to the value of this instance.</returns>
236
        long IConvertible.ToInt64(IFormatProvider? provider) => _value.ToInt64(provider);
×
237

238
        /// <summary>
239
        /// Converts the value of this instance to an equivalent 8-bit signed integer using the
240
        /// specified culture-specific formatting information.
241
        /// </summary>
242
        /// <param name="provider">An <see cref="IFormatProvider"/>interface implementation that supplies culture-specific formatting information.</param>
243
        /// <returns>An 8-bit signed integer equivalent to the value of this instance.</returns>
244
        sbyte IConvertible.ToSByte(IFormatProvider? provider) => _value.ToSByte(provider);
×
245

246
        /// <summary>
247
        /// Converts the value of this instance to an equivalent single-precision floating-point number using the
248
        /// specified culture-specific formatting information.
249
        /// </summary>
250
        /// <param name="provider">An <see cref="IFormatProvider"/>interface implementation that supplies culture-specific formatting information.</param>
251
        /// <returns>A single-precision floating-point number equivalent to the value of this instance.</returns>
252
        float IConvertible.ToSingle(IFormatProvider? provider) => _value.ToSingle(provider);
×
253

254
        /// <summary>
255
        /// Converts the value of this instance to an equivalent <see cref="string"/> value using the
256
        /// specified culture-specific formatting information.
257
        /// </summary>
258
        /// <param name="provider">An <see cref="IFormatProvider"/>interface implementation that supplies culture-specific formatting information.</param>
259
        /// <returns>A <see cref="string"/> value equivalent to the value of this instance.</returns>
260
        string IConvertible.ToString(IFormatProvider? provider) => _value.ToString(provider);
×
261

262
        /// <summary>
263
        /// Converts the value of this instance to an <see cref="object"/> of the specified <see cref="Type"/>
264
        /// that has an equivalent value, using the specified culture-specific formatting information.
265
        /// </summary>
266
        /// <param name="conversionType">The <see cref="Type"/> to which the value of this instance is converted.</param>
267
        /// <param name="provider">An <see cref="IFormatProvider"/>interface implementation that supplies culture-specific formatting information.</param>
268
        /// <returns>An <see cref="object"/> instance of type <paramref name="conversionType"/> whose value equivalent to the value of this instance.</returns>
269
        object IConvertible.ToType(Type conversionType, IFormatProvider? provider) => _value.ToType(conversionType, provider);
×
270

271
        /// <summary>
272
        /// Converts the value of this instance to an equivalent 16-bit unsigned integer using the
273
        /// specified culture-specific formatting information.
274
        /// </summary>
275
        /// <param name="provider">An <see cref="IFormatProvider"/>interface implementation that supplies culture-specific formatting information.</param>
276
        /// <returns>A 16-bit unsigned integer equivalent to the value of this instance.</returns>
277
        ushort IConvertible.ToUInt16(IFormatProvider? provider) => _value.ToUInt16(provider);
×
278

279
        /// <summary>
280
        /// Converts the value of this instance to an equivalent 32-bit unsigned integer using the
281
        /// specified culture-specific formatting information.
282
        /// </summary>
283
        /// <param name="provider">An <see cref="IFormatProvider"/>interface implementation that supplies culture-specific formatting information.</param>
284
        /// <returns>A 32-bit unsigned integer equivalent to the value of this instance.</returns>
285
        uint IConvertible.ToUInt32(IFormatProvider? provider) => _value.ToUInt32(provider);
×
286

287
        /// <summary>
288
        /// Converts the value of this instance to an equivalent 64-bit unsigned integer using the
289
        /// specified culture-specific formatting information.
290
        /// </summary>
291
        /// <param name="provider">An <see cref="IFormatProvider"/>interface implementation that supplies culture-specific formatting information.</param>
292
        /// <returns>A 64-bit unsigned integer equivalent to the value of this instance.</returns>
293
        ulong IConvertible.ToUInt64(IFormatProvider? provider) => _value.ToUInt64(provider);
×
294

295
        #endregion
296

297
        #region IFormattable
298

299
        /// <summary>
300
        /// Formats the value of the current instance using the specified format.
301
        /// </summary>
302
        /// <param name="format">
303
        /// The format to use. -or- A null reference to use the default format defined for the type of the <see cref="IFormattable"/> implementation.
304
        /// </param>
305
        /// <param name="formatProvider">
306
        /// The provider to use to format the value. -or- A null reference to obtain the numeric format
307
        /// information from the current locale setting of the operating system.</param>
308
        /// <returns>The value of the current instance in the specified format.</returns>
309
        public string ToString(string? format, IFormatProvider? formatProvider) => _value.ToString(format, formatProvider);
1,032✔
310

311
        #endregion
312

313
        #region Operators
314

315
        /// <summary>
316
        /// Increments the value of the integer by 1. Equivalent to calling <see cref="Increment()"/>.
317
        /// </summary>
318
        /// <param name="source">The <see cref="RealmInteger{T}"/> that will be incremented.</param>
319
        /// <returns>The incremented value.</returns>
320
        public static RealmInteger<T> operator ++(RealmInteger<T> source) => source.Increment();
1✔
321

322
        /// <summary>
323
        /// Decrements the value of the integer by 1. Equivalent to calling <see cref="Decrement()"/>.
324
        /// </summary>
325
        /// <param name="source">The <see cref="RealmInteger{T}"/> that will be decremented.</param>
326
        /// <returns>The decremented value.</returns>
327
        public static RealmInteger<T> operator --(RealmInteger<T> source) => source.Decrement();
1✔
328

329
        /// <summary>
330
        /// Converts a <see cref="RealmInteger{T}"/> to its underlying value.
331
        /// </summary>
332
        /// <param name="i">The <see cref="RealmInteger{T}"/>.</param>
333
        public static implicit operator T(RealmInteger<T> i) => i._value;
4,768✔
334

335
        /// <summary>
336
        /// Constructs a <see cref="RealmInteger{T}"/> from its underlying value.
337
        /// </summary>
338
        /// <param name="i">The value.</param>
339
        public static implicit operator RealmInteger<T>(T i) => new(i);
1,474✔
340

341
        /// <summary>
342
        /// Compares two <see cref="RealmInteger{T}"/> instances for equality.
343
        /// </summary>
344
        /// <param name="first">The first <see cref="RealmInteger{T}"/>.</param>
345
        /// <param name="second">The second <see cref="RealmInteger{T}"/>.</param>
346
        /// <returns><c>true</c> if their underlying values are equal; <c>false</c> otherwise.</returns>
347
        public static bool operator ==(RealmInteger<T> first, RealmInteger<T> second) => first.Equals(second);
2,936✔
348

349
        /// <summary>
350
        /// Compares two <see cref="RealmInteger{T}"/> instances for inequality.
351
        /// </summary>
352
        /// <param name="first">The first <see cref="RealmInteger{T}"/>.</param>
353
        /// <param name="second">The second <see cref="RealmInteger{T}"/>.</param>
354
        /// <returns><c>true</c> if their underlying values are equal; <c>false</c> otherwise.</returns>
355
        public static bool operator !=(RealmInteger<T> first, RealmInteger<T> second) => !(first == second);
2,522✔
356

357
        /// <summary>
358
        /// Compares two <see cref="RealmInteger{T}"/> values.
359
        /// </summary>
360
        /// <param name="left">The first <see cref="RealmInteger{T}"/>.</param>
361
        /// <param name="right">The second <see cref="RealmInteger{T}"/>.</param>
362
        /// <returns><c>true</c> if <paramref name="left"/> is less than <paramref name="right"/>.</returns>
363
        public static bool operator <(RealmInteger<T> left, RealmInteger<T> right) => left.CompareTo(right) < 0;
4✔
364

365
        /// <summary>
366
        /// Compares two <see cref="RealmInteger{T}"/> values.
367
        /// </summary>
368
        /// <param name="left">The first <see cref="RealmInteger{T}"/>.</param>
369
        /// <param name="right">The second <see cref="RealmInteger{T}"/>.</param>
370
        /// <returns><c>true</c> if <paramref name="left"/> is less than or equal to <paramref name="right"/>.</returns>
371
        public static bool operator <=(RealmInteger<T> left, RealmInteger<T> right) => left.CompareTo(right) <= 0;
×
372

373
        /// <summary>
374
        /// Compares two <see cref="RealmInteger{T}"/> values.
375
        /// </summary>
376
        /// <param name="left">The first <see cref="RealmInteger{T}"/>.</param>
377
        /// <param name="right">The second <see cref="RealmInteger{T}"/>.</param>
378
        /// <returns><c>true</c> if <paramref name="left"/> is greater than <paramref name="right"/>.</returns>
379
        public static bool operator >(RealmInteger<T> left, RealmInteger<T> right) => left.CompareTo(right) > 0;
4✔
380

381
        /// <summary>
382
        /// Compares two <see cref="RealmInteger{T}"/> values.
383
        /// </summary>
384
        /// <param name="left">The first <see cref="RealmInteger{T}"/>.</param>
385
        /// <param name="right">The second <see cref="RealmInteger{T}"/>.</param>
386
        /// <returns><c>true</c> if <paramref name="left"/> is greater than or equal to <paramref name="right"/>.</returns>
387
        public static bool operator >=(RealmInteger<T> left, RealmInteger<T> right) => left.CompareTo(right) >= 0;
×
388

389
        #endregion
390
    }
391
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc