Пример #1
0
        private bool AddReferenceInfo(ReferenceInfo referenceInfo)
        {
            lock (_lock)
            {
                var id       = referenceInfo.Id;
                var instance = referenceInfo.Instance;

                if (id.HasValue && _usedIds.Contains(id.Value))
                {
                    return(false);
                }

                if (_referenceInfoByInstance.TryGetValue(instance, out var existingReferenceInfo))
                {
                    existingReferenceInfo.Id = id;
                }
                else
                {
                    _referenceInfoByInstance.Add(instance, referenceInfo);
                }

                // We might be adding a late id
                var added = false;
                if (id.HasValue)
                {
                    _referenceInfoById.Add(id.Value, existingReferenceInfo ?? referenceInfo);
                    _usedIds.Add(id.Value);

                    added = true;
                }

                return(added);
            }
        }
Пример #2
0
        /// <summary>
        /// Gets the info for the specified instance.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <returns>The <see cref="ReferenceInfo" /> or <c>null</c> if <paramref name="instance" /> is <c>null</c>.</returns>
        public ReferenceInfo GetInfo(object instance)
        {
            if (instance == null)
            {
                return(null);
            }

            lock (_lock)
            {
                ReferenceInfo referenceInfo = null;

                if (!_referenceInfoByInstance.ContainsKey(instance))
                {
                    var id = GetNextId();
                    referenceInfo = new ReferenceInfo(instance, id, true);

                    AddReferenceInfo(referenceInfo);
                }
                else
                {
                    referenceInfo = _referenceInfoByInstance[instance];
                    referenceInfo.IsFirstUsage = false;
                }

                return(referenceInfo);
            }
        }
Пример #3
0
        private void AddReferenceInfo(ReferenceInfo referenceInfo)
        {
            lock (_lock)
            {
                _referenceInfoByInstance.Add(referenceInfo.Instance, referenceInfo);
                _referenceInfoById.Add(referenceInfo.Id, referenceInfo);

                _usedIds.Add(referenceInfo.Id);
            }
        }
Пример #4
0
        /// <summary>
        /// Registers the specified instance manually.
        /// </summary>
        /// <param name="id">The unique identifier.</param>
        /// <param name="instance">The instance.</param>
        public void RegisterManually(int id, object instance)
        {
            if (instance is null)
            {
                return;
            }

            lock (_lock)
            {
                var referenceInfo = new ReferenceInfo(instance, id, false);

                AddReferenceInfo(referenceInfo);
            }
        }
Пример #5
0
        private bool AddReferenceInfo(ReferenceInfo referenceInfo)
        {
            lock (_lock)
            {
                if (_referenceInfoByInstance.ContainsKey(referenceInfo) || _usedIds.Contains(referenceInfo.Id))
                {
                    return(false);
                }

                _referenceInfoByInstance.Add(referenceInfo.Instance, referenceInfo);
                _referenceInfoById.Add(referenceInfo.Id, referenceInfo);

                _usedIds.Add(referenceInfo.Id);

                return(true);
            }
        }
Пример #6
0
        /// <summary>
        /// Gets the info for the specified instance.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="autoAssignId">If set to <c>true</c>, a unique graph id will automatically be reserved for this object. Note that it's recommended to set this to <c>false</c> during deserialization.</param>
        /// <returns>The <see cref="ReferenceInfo" /> or <c>null</c> if <paramref name="instance" /> is <c>null</c>.</returns>
        public ReferenceInfo GetInfo(object instance, bool autoAssignId = true)
        {
            if (instance is null)
            {
                return(null);
            }

            lock (_lock)
            {
                if (_referenceInfoByInstance.TryGetValue(instance, out var referenceInfo))
                {
                    if (!referenceInfo.Id.HasValue)
                    {
                        if (autoAssignId)
                        {
                            referenceInfo.Id = GetNextId();

                            AddReferenceInfo(referenceInfo);
                        }
                    }
                    else
                    {
                        // Only treat as non-first usage if we already had a valid id
                        referenceInfo.IsFirstUsage = false;
                    }
                }
                else
                {
                    int?id = null;

                    if (autoAssignId)
                    {
                        id = GetNextId();
                    }

                    referenceInfo = new ReferenceInfo(instance, id, true);

                    AddReferenceInfo(referenceInfo);
                }

                return(referenceInfo);
            }
        }
Пример #7
0
        private bool AddReferenceInfo(ReferenceInfo referenceInfo)
        {
            lock (_lock)
            {
                var id       = referenceInfo.Id;
                var instance = referenceInfo.Instance;

                if (_referenceInfoByInstance.ContainsKey(instance) || _usedIds.Contains(id))
                {
                    return(false);
                }

                _referenceInfoByInstance.Add(instance, referenceInfo);
                _referenceInfoById.Add(id, referenceInfo);

                _usedIds.Add(id);

                return(true);
            }
        }
Пример #8
0
        /// <summary>
        /// Gets the info for the specified instance.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <returns>The <see cref="ReferenceInfo" /> or <c>null</c> if <paramref name="instance" /> is <c>null</c>.</returns>
        public ReferenceInfo GetInfo(object instance)
        {
            if (instance is null)
            {
                return(null);
            }

            lock (_lock)
            {
                if (_referenceInfoByInstance.TryGetValue(instance, out var referenceInfo))
                {
                    referenceInfo.IsFirstUsage = false;
                }
                else
                {
                    var id = GetNextId();
                    referenceInfo = new ReferenceInfo(instance, id, true);

                    AddReferenceInfo(referenceInfo);
                }

                return(referenceInfo);
            }
        }
Пример #9
0
        /// <summary>
        /// Registers the specified instance manually.
        /// </summary>
        /// <param name="id">The unique identifier.</param>
        /// <param name="instance">The instance.</param>
        public void RegisterManually(int id, object instance)
        {
            if (instance == null)
            {
                return;
            }

            lock (_lock)
            {
                var referenceInfo = new ReferenceInfo(instance, id, false);

                AddReferenceInfo(referenceInfo);
            }
        }
Пример #10
0
        private bool AddReferenceInfo(ReferenceInfo referenceInfo)
        {
            lock (_lock)
            {
                if (_referenceInfoByInstance.ContainsKey(referenceInfo) || _usedIds.Contains(referenceInfo.Id))
                {
                    return false;
                }

                _referenceInfoByInstance.Add(referenceInfo.Instance, referenceInfo);
                _referenceInfoById.Add(referenceInfo.Id, referenceInfo);

                _usedIds.Add(referenceInfo.Id);

                return true;
            }
        }
Пример #11
0
        /// <summary>
        /// Gets the info for the specified instance.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <returns>The <see cref="ReferenceInfo" /> or <c>null</c> if <paramref name="instance" /> is <c>null</c>.</returns>
        public ReferenceInfo GetInfo(object instance)
        {
            if (instance == null)
            {
                return null;
            }

            lock (_lock)
            {
                ReferenceInfo referenceInfo = null;

                if (!_referenceInfoByInstance.ContainsKey(instance))
                {
                    var id = GetNextId();
                    referenceInfo = new ReferenceInfo(instance, id, true);

                    AddReferenceInfo(referenceInfo);
                }
                else
                {
                    referenceInfo = _referenceInfoByInstance[instance];
                    referenceInfo.IsFirstUsage = false;
                }

                return referenceInfo;
            }
        }
Пример #12
0
        private void AddReferenceInfo(ReferenceInfo referenceInfo)
        {
            lock (_lock)
            {
                _referenceInfoByInstance.Add(referenceInfo.Instance, referenceInfo);
                _referenceInfoById.Add(referenceInfo.Id, referenceInfo);

                _usedIds.Add(referenceInfo.Id);
            }
        }