Пример #1
0
        public bool Remove(object o, INamedVersionedUniqueId p, string k)
        {
            if (o == null)
            {
                throw new ArgumentNullException("o");
            }
            if (p == null)
            {
                throw new ArgumentNullException("p");
            }
            if (k == null)
            {
                throw new ArgumentNullException("k");
            }

            SharedDictionaryEntry e = new SharedDictionaryEntry(o, p, k);
            SharedDictionaryEntry result;

            if (_values.TryGetValue(e, out result))
            {
                PluginConfigByObject co = _byObject[e.Obj];
                co.Remove(result);
                PluginConfigByPlugin cp = _byPlugin[e.PluginId.UniqueId];
                cp.Remove(result);
                _finalDictionary[e].Count--;
                _values.Remove(result);
                result.Value = null;
                if (Changed != null)
                {
                    Changed(this, new ConfigChangedEventArgs(e, e, ChangeStatus.Delete));
                }
                return(true);
            }
            return(false);
        }
Пример #2
0
        public T GetOrSet <T>(object o, INamedVersionedUniqueId p, string k, Func <T> value)
        {
            if (o == null)
            {
                throw new ArgumentNullException("o");
            }
            if (p == null)
            {
                throw new ArgumentNullException("p");
            }
            if (k == null)
            {
                throw new ArgumentNullException("k");
            }

            SharedDictionaryEntry e = new SharedDictionaryEntry(o, p, k);
            SharedDictionaryEntry result;

            if (_values.TryGetValue(e, out result))
            {
                return((T)result.Value);
            }
            T val = value == null ? default(T) : value();

            e.Value = val;
            Add(e);
            return(val);
        }
Пример #3
0
        public ChangeStatus Set(object o, INamedVersionedUniqueId p, string k, object value)
        {
            if (o == null)
            {
                throw new ArgumentNullException("o");
            }
            if (p == null)
            {
                throw new ArgumentNullException("p");
            }
            if (k == null)
            {
                throw new ArgumentNullException("k");
            }

            SharedDictionaryEntry e = new SharedDictionaryEntry(o, p, k);
            SharedDictionaryEntry result;

            if (_values.TryGetValue(e, out result))
            {
                if (!Equals(result.Value, value))
                {
                    result.Value = value;
                    if (Changed != null)
                    {
                        Changed(this, new ConfigChangedEventArgs(result, result, ChangeStatus.Update));
                    }
                    return(ChangeStatus.Update);
                }
                return(ChangeStatus.None);
            }
            e.Value = value;
            Add(e);
            return(ChangeStatus.Add);
        }
Пример #4
0
        public object this[object o, INamedVersionedUniqueId p, string k]
        {
            get
            {
                if (o == null)
                {
                    throw new ArgumentNullException("o");
                }
                if (p == null)
                {
                    throw new ArgumentNullException("p");
                }
                if (k == null)
                {
                    throw new ArgumentNullException("k");
                }

                SharedDictionaryEntry e = new SharedDictionaryEntry(o, p, k);
                SharedDictionaryEntry result;
                return(_values.TryGetValue(e, out result) ? result.Value : null);
            }
            set
            {
                Set(o, p, k, value);
            }
        }
Пример #5
0
        public override bool Equals(object o)
        {
            Debug.Assert(_key != null, "Entries are compared by its Equals method only for real entries (where key is not null) when searching in SharedDictionaryImpl._values. Search for FinalDictionary use the FinalDictionaryComparer that ignores the key.");
            SharedDictionaryEntry e = o as SharedDictionaryEntry;

            if (e == null)
            {
                return(false);
            }
            return(_obj == e._obj && _pluginId == e._pluginId && _key == e._key);
        }
Пример #6
0
        bool Merge(SharedDictionaryEntry e)
        {
            SharedDictionaryEntry result;

            if (_values.TryGetValue(e, out result))
            {
                IMergeable existing = result.Value as IMergeable;
                if (existing != null)
                {
                    return(existing.Merge(e.Value));
                }
            }
            return(false);
        }
Пример #7
0
        public T GetOrSet <T>(object o, INamedVersionedUniqueId p, string k, Func <T> value, Func <object, T> converter)
        {
            if (o == null)
            {
                throw new ArgumentNullException("o");
            }
            if (p == null)
            {
                throw new ArgumentNullException("p");
            }
            if (k == null)
            {
                throw new ArgumentNullException("k");
            }

            if (converter == null)
            {
                throw new ArgumentNullException("converter");
            }
            SharedDictionaryEntry e = new SharedDictionaryEntry(o, p, k);
            SharedDictionaryEntry result;

            if (_values.TryGetValue(e, out result))
            {
                if (result.Value is T)
                {
                    return((T)result.Value);
                }
                T valT = converter(result.Value);
                result.Value = valT;
                if (Changed != null)
                {
                    Changed(this, new ConfigChangedEventArgs(result, result, ChangeStatus.Update));
                }
                return(valT);
            }
            T val = value == null ? default(T) : value();

            e.Value = val;
            Add(e);
            return(val);
        }
Пример #8
0
        private void Add(SharedDictionaryEntry eWithValue)
        {
            // First ensures that the plugin is registered.
            // This may throw an ArgumentException if the Guid is associated to another IVersionedUniqueId.
            PluginConfigByPlugin cp;

            if (!_byPlugin.TryGetValue(eWithValue.PluginId.UniqueId, out cp))
            {
                cp = CreatePluginConfigByPlugin(eWithValue.PluginId);
            }
            // If the entry already exists, an ArgumentException is thrown.
            // If this happens it means that the IVersionedUniqueId is already registered:
            // we do not have to clean up the plugin registration.
            _values.Add(eWithValue, eWithValue);
            // No exception: let us add into the other indices.

            cp.Add(eWithValue);

            PluginConfigByObject co;

            if (!_byObject.TryGetValue(eWithValue.Obj, out co))
            {
                co = CreatePluginConfigByObject(eWithValue.Obj);
            }
            co.Add(eWithValue);

            // Increment the final dictionary count.
            FinalDictionary d;

            if (!_finalDictionary.TryGetValue(eWithValue, out d))
            {
                d = new FinalDictionary(this, eWithValue.Obj, eWithValue.PluginId);
                _finalDictionary.Add(eWithValue, d);
            }
            d.Count++;
            if (Changed != null)
            {
                Changed(this, new ConfigChangedEventArgs(eWithValue, eWithValue, ChangeStatus.Add));
            }
        }
Пример #9
0
 internal void ImportValue(SharedDictionaryEntry entryWithValue, MergeMode mergeMode)
 {
     if (mergeMode == MergeMode.None || mergeMode == MergeMode.ReplaceExisting)
     {
         this[entryWithValue.Obj, entryWithValue.PluginId, entryWithValue.Key] = entryWithValue.Value;
     }
     else if (mergeMode == MergeMode.ErrorOnDuplicate)
     {
         Add(entryWithValue);
     }
     else if (mergeMode == MergeMode.PreserveExisting)
     {
         GetOrSet(entryWithValue.Obj, entryWithValue.PluginId, entryWithValue.Key, entryWithValue.Value);
     }
     else if (mergeMode == MergeMode.ReplaceExistingTryMerge)
     {
         if (!Merge(entryWithValue))
         {
             this[entryWithValue.Obj, entryWithValue.PluginId, entryWithValue.Key] = entryWithValue.Value;
         }
     }
 }
Пример #10
0
        internal FinalDictionary GetFinalDictionary(SharedDictionaryEntry e, bool ensure)
        {
            FinalDictionary d;

            if (!_finalDictionary.TryGetValue(e, out d))
            {
                if (ensure)
                {
                    if (!_byObject.ContainsKey(e.Obj))
                    {
                        CreatePluginConfigByObject(e.Obj);
                    }
                    d = new FinalDictionary(this, e.Obj, e.PluginId);
                    _finalDictionary.Add(e, d);
                    if (!_byPlugin.ContainsKey(e.PluginId.UniqueId))
                    {
                        CreatePluginConfigByPlugin(e.PluginId);
                    }
                }
            }
            return(d);
        }
Пример #11
0
        public void Clear(object o, INamedVersionedUniqueId p)
        {
            if (o == null)
            {
                throw new ArgumentNullException("o");
            }
            if (p == null)
            {
                throw new ArgumentNullException("p");
            }

            FinalDictionary d;

            if (!_finalDictionary.TryGetValue(new SharedDictionaryEntry(o, p, null), out d) || d.Count == 0)
            {
                return;
            }
            PluginConfigByObject co = _byObject[o];
            PluginConfigByPlugin cp = _byPlugin[p.UniqueId];

            for (int i = 0; i < co.Count; ++i)
            {
                SharedDictionaryEntry e = co[i];
                if (e.PluginId == p)
                {
                    co.RemoveAt(i);
                    cp.Remove(e);
                    _values.Remove(e);
                }
            }
            d.Count = 0;
            if (Changed != null)
            {
                Changed(this, new ConfigChangedEventArgs(o, p, ChangeStatus.ContainerClear));
            }
        }