Exemple #1
0
        /// <summary>
        /// Add handle to handles map.
        /// </summary>
        /// <param name="pos">Position in stream.</param>
        /// <param name="obj">Object.</param>
        /// <returns><c>true</c> if object was written as handle.</returns>
        private bool WriteHandle(long pos, object obj)
        {
            if (_hnds == null)
            {
                // Cache absolute handle position.
                _hnds = new BinaryHandleDictionary <object, long>(obj, pos, ReferenceEqualityComparer <object> .Instance);

                return(false);
            }

            long hndPos;

            if (!_hnds.TryGetValue(obj, out hndPos))
            {
                // Cache absolute handle position.
                _hnds.Add(obj, pos);

                return(false);
            }

            _stream.WriteByte(BinaryUtils.HdrHnd);

            // Handle is written as difference between position before header and handle position.
            _stream.WriteInt((int)(pos - hndPos));

            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Perform action with detached semantics.
        /// </summary>
        internal void WriteObjectDetached <T>(T o)
        {
            if (_detaching)
            {
                Write(o);
            }
            else
            {
                _detaching = true;

                BinaryHandleDictionary <object, long> oldHnds = _hnds;
                _hnds = null;

                try
                {
                    Write(o);
                }
                finally
                {
                    _detaching = false;

                    if (oldHnds != null)
                    {
                        // Merge newly recorded handles with old ones and restore old on the stack.
                        // Otherwise we can use current handles right away.
                        if (_hnds != null)
                        {
                            oldHnds.Merge(_hnds);
                        }

                        _hnds = oldHnds;
                    }
                }
            }
        }
        /// <summary>
        /// Merge data from another dictionary without overwrite.
        /// </summary>
        /// <param name="that">Other dictionary.</param>
        public void Merge(BinaryHandleDictionary <TK, TV> that)
        {
            if (that == null)
            {
                return;
            }

            AddIfAbsent(that._key1, that._val1);
            AddIfAbsent(that._key2, that._val2);
            AddIfAbsent(that._key3, that._val3);

            if (that._dict == null)
            {
                return;
            }

            foreach (var pair in that._dict)
            {
                AddIfAbsent(pair.Key, pair.Value);
            }
        }