Exemplo n.º 1
0
        ///// <summary>
        ///// imposta oggetto con chiavi multiple
        ///// </summary>
        ///// <param name="keys"></param>
        ///// <param name="value"></param>
        //public void SetM(IEnumerable<TKEY> keys, TVALUE value)
        //{
        //    lock (this.mSyncRoot)
        //    {
        //        if (!this.mIsActive)
        //            return;

        //        InnerReference oRefItem = null;

        //        foreach (var key in keys)
        //        {
        //            InnerReference oRef = null;
        //            if (this.mStore.TryGetValue(key, out oRef))
        //            {
        //                //esiste, verifichiamo se medesimo riferimento
        //                if (!oRef.Wref.IsAlive)
        //                {
        //                    //Oggetto scaduto: lo reimpostiamo e usciamo
        //                    oRef.Wref.Target = value;
        //                    return;
        //                }
        //            }
        //            else
        //            {
        //                //Non esiste: lo aggiungiamo ma creando un solo riferimento
        //                if (oRefItem == null)
        //                    oRefItem = new InnerReference(System.Threading.Interlocked.Increment(ref this.mRefCounter), keys, value);

        //                this.mStore.Add(key, oRefItem);
        //            }
        //        }
        //    }
        //}


        /// <summary>
        /// Cerca oggetto
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public TVALUE Get(TKEY key)
        {
            lock (this.mSyncRoot)
            {
                if (!this.mIsActive)
                {
                    return(default(TVALUE));
                }

                InnerReference oRef = null;

                if (this.mStore.TryGetValue(key, out oRef))
                {
                    //esiste, verifichiamo se medesimo riferimento
                    if (oRef.Wref.IsAlive)
                    {
                        //oggetto vivo: lo ritorniamo
                        return((TVALUE)oRef.Wref.Target);
                    }
                    else
                    {
                        //Oggetto scaduto: lo eliminiamo
                        this.mStore.Remove(key);
                    }
                }

                return(default(TVALUE));
            }
        }
Exemplo n.º 2
0
        ///// <summary>
        ///// Rimuove oggetti multipli
        ///// </summary>
        ///// <param name="keys"></param>
        //public void RemoveM(IEnumerable<TKEY> keys)
        //{
        //    lock (this.mSyncRoot)
        //    {
        //        if (!this.mIsActive)
        //            return;

        //        foreach (var key in keys)
        //        {
        //            this.mStore.Remove(key);
        //        }
        //    }
        //}


        /// <summary>
        /// imposta oggetto con chiave
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Set(TKEY key, TVALUE value)
        {
            lock (this.mSyncRoot)
            {
                if (!this.mIsActive)
                {
                    return;
                }

                InnerReference oRefItem = null;

                InnerReference oRef = null;
                if (this.mStore.TryGetValue(key, out oRef))
                {
                    //esiste, verifichiamo se medesimo riferimento
                    if (!oRef.Wref.IsAlive)
                    {
                        //Oggetto scaduto: lo reimpostiamo e usciamo
                        oRef.Wref.Target = value;
                        return;
                    }
                }
                else
                {
                    //Non esiste: lo aggiungiamo ma creando un solo riferimento
                    if (oRefItem == null)
                    {
                        oRefItem = new InnerReference(System.Threading.Interlocked.Increment(ref this.mRefCounter), key, value);
                    }

                    this.mStore.Add(key, oRefItem);
                }
            }
        }
Exemplo n.º 3
0
 internal override void SetXML(XmlElement xml, BaseClassIfc host, Dictionary <int, XmlElement> processed)
 {
     base.SetXML(xml, host, processed);
     setAttribute(xml, "TypeIdentifier", TypeIdentifier);
     setAttribute(xml, "AttributeIdentifier", AttributeIdentifier);
     setAttribute(xml, "InstanceName", InstanceName);
     if (mListPositions.Count > 0)
     {
         xml.SetAttribute("ListPositions", String.Join(" ", mListPositions));
     }
     if (mInnerReference > 0)
     {
         xml.AppendChild(InnerReference.GetXML(xml.OwnerDocument, "InnerReference", this, processed));
     }
 }
Exemplo n.º 4
0
 protected override void setJSON(JObject obj, BaseClassIfc host, SetJsonOptions options)
 {
     base.setJSON(obj, host, options);
     setAttribute(obj, "TypeIdentifier", TypeIdentifier);
     setAttribute(obj, "AttributeIdentifier", AttributeIdentifier);
     setAttribute(obj, "InstanceName", InstanceName);
     if (mListPositions.Count > 0)
     {
         obj["ListPositions"] = new JArray(mListPositions.ToArray());
     }
     if (mInnerReference > 0)
     {
         obj["InnerReference"] = InnerReference.getJson(this, options);
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a new reference.
        /// </summary>
        /// <returns>The reference identifier.</returns>
        /// <param name="entity">Referencable object to store.</param>
        public override EntityId CreateReference(Entity entity, bool force = false)
        {
            // TODO: maybe initiate a request for an server generated unique id

            long nextIndex = ThreadSaveIdGenerator.NextId;
            // as client we are unable to assing uuids so we use 0 indicating it isn't set
            EntityId newReference = new EntityId(0, nextIndex);

            if (entity.Id != new EntityId() && !force)
            {
                throw new Exception("The entity already had an id, it may already have been registered. Call with force = true to ignore");
            }
            entity.Id = newReference;

            InnerReference <Entity> referenceObject = new InnerReference <Entity>(entity);

            if (!AddReference(referenceObject))
            {
                throw new Exception("adding failed");
            }
            return(newReference);
        }