Ejemplo n.º 1
0
        private static void PopulateHash(HashParameterDictionary hash, object from)
        {
            var descriptor = ObjectDescriptor.Create(from);

            if (descriptor == ObjectDescriptor.Empty)
            {
                return;
            }

            var accessor   = descriptor.MemberAccessor;
            var properties = descriptor.GetProperties(descriptor, from);
            var enumerator = properties.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var segment = ChainSegment.Create(enumerator.Current);
                if (hash.ContainsKey(segment))
                {
                    continue;
                }
                if (!accessor.TryGetValue(@from, segment, out var value))
                {
                    continue;
                }
                hash[segment] = value;
            }
        }
Ejemplo n.º 2
0
        public bool TryGetValue(object instance, Type instanceType, string memberName, out object value)
        {
            var bindingContext = (BindingContext)instance;
            var segment        = ChainSegment.Create(memberName);

            return(bindingContext.TryGetContextVariable(ref segment, out value));
        }
        public virtual bool TryGetValue(ref ChainSegment segment, out object value)
        {
            switch (segment.LowerInvariant)
            {
            case "index":
                value = Index;
                return(true);

            case "first":
                value = First;
                return(true);

            case "last":
                value = Last;
                return(true);

            case "value":
                value = Value;
                return(true);

            default:
                value = null;
                return(false);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        ///    Update the details of an existing chain element.
        /// </summary>
        /// <param name="chainIndex> The index of the chain</param>
        /// <param name="elementIndex"> The element index within the chain, measured from the 'head' of the chain</param>
        /// <param name="billboardChainElement"> The details to set</param>
        protected void UpdateChainElement(int chainIndex, int elementIndex, Element dtls)
        {
            if (chainIndex >= numberOfChains)
            {
                log.Error("BillboardChain.UpdateChainElement: chainIndex " + chainIndex + " out of bounds");
                return;
            }
            ChainSegment seg = chainSegmentList[chainIndex];

            if (seg.head == SEGMENT_EMPTY)
            {
                log.Error("BillboardChain.UpdateChainElement: Chain segment is empty " + chainIndex + " is empty");
                return;
            }

            int idx = seg.head + elementIndex;

            // adjust for the edge and start
            idx = (idx % maxElementsPerChain) + seg.start;

            chainElementList[idx] = dtls;

            boundsDirty = true;
            // tell parent node to update bounds
            if (parentNode != null)
            {
                parentNode.NeedUpdate();
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 ///		Perform any fading / width delta required; internal method
 /// </summary>
 /// <param name="time"> The time of the update</param>
 ///
 public virtual void TimeUpdate(float time)
 {
     // Apply all segment effects
     for (int s = 0; s < chainSegmentList.Count; s++)
     {
         ChainSegment    seg        = chainSegmentList[s];
         TrailAttributes attributes = trailAttributes[s];
         if (seg.head != SEGMENT_EMPTY && seg.head != seg.tail)
         {
             for (int e = seg.head + 1;; ++e)
             {
                 e = e % maxElementsPerChain;
                 Element elem = chainElementList[seg.start + e];
                 elem.width = elem.width - (time * attributes.deltaWidth);
                 elem.width = Math.Max(0.0f, elem.width);
                 elem.color = elem.color - (attributes.deltaColor * time);
                 elem.color.Saturate();
                 if (e == seg.tail)
                 {
                     break;
                 }
             }
         }
     }
 }
Ejemplo n.º 6
0
        public object this[ChainSegment segment]
        {
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            get
            {
                var knownVariableIndex = (int)segment.WellKnownVariable;
                if (segment.WellKnownVariable != WellKnownVariable.None && _wellKnownVariables[knownVariableIndex].IsNotEmpty)
                {
                    return(_data[_wellKnownVariables[knownVariableIndex]]);
                }

                return(_data.TryGetValue(segment, out var value) ? value : null);
            }

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            set
            {
                var knownVariableIndex = (int)segment.WellKnownVariable;
                if (segment.WellKnownVariable != WellKnownVariable.None)
                {
                    _data.AddOrReplace(segment, value, out _wellKnownVariables[knownVariableIndex]);
                    return;
                }

                _data.AddOrReplace(segment, value, out _);
            }
        }
Ejemplo n.º 7
0
        public bool TryGetValue(object instance, ChainSegment memberName, out object?value)
        {
            var element = (JsonElement)instance;

            if (element.ValueKind == JsonValueKind.Object && element.TryGetProperty(memberName.TrimmedValue, out var property))
            {
                value = Utils.ExtractProperty(property);
                return(true);
            }

            if (element.ValueKind == JsonValueKind.Array && int.TryParse(memberName, out var index))
            {
                var indexedElement = element.EnumerateArray().Skip(index).FirstOrDefault();

                if (indexedElement.ValueKind == JsonValueKind.Undefined)
                {
                    value = null;
                    return(false);
                }

                value = Utils.ExtractProperty(indexedElement);
                return(true);
            }

            if (_aliasProvider.TryGetMemberByAlias(element, typeof(JsonElement), memberName, out value))
            {
                return(true);
            }

            value = null;
            return(false);
        }
Ejemplo n.º 8
0
 private int GetLength(ChainSegment ChainSeg)
 {
     if (ChainSeg.head <= ChainSeg.tail)
     {
         return(ChainSeg.tail - ChainSeg.head + 1);
     }
     return(ChainSeg.tail + (MaxChainElements - ChainSeg.head) + 1);
 }
Ejemplo n.º 9
0
        private static void IterateObject(
            BindingContext context,
            EncodedTextWriter writer,
            ObjectDescriptor descriptor,
            ChainSegment[] blockParamsVariables,
            object target,
            IEnumerable properties,
            Type targetType,
            TemplateDelegate template,
            TemplateDelegate ifEmpty)
        {
            using var innerContext = context.CreateFrame();
            var iterator    = new ObjectIteratorValues(innerContext);
            var blockParams = new BlockParamsValues(innerContext, blockParamsVariables);

            blockParams.CreateProperty(0, out var _0);
            blockParams.CreateProperty(1, out var _1);

            var accessor   = new MemberAccessor(target, descriptor);
            var enumerable = new ExtendedEnumerator <object>(properties.GetEnumerator());
            var enumerated = false;

            object       iteratorValue;
            ChainSegment iteratorKey;

            while (enumerable.MoveNext())
            {
                enumerated = true;
                var enumerableValue = enumerable.Current;
                iteratorKey = ChainSegment.Create(enumerableValue.Value);

                iterator.Key   = iteratorKey;
                iterator.Index = enumerableValue.Index;
                if (enumerableValue.Index == 1)
                {
                    iterator.First = BoxedValues.False;
                }
                if (enumerableValue.IsLast)
                {
                    iterator.Last = BoxedValues.True;
                }

                iteratorValue      = accessor[iteratorKey];
                iterator.Value     = iteratorValue;
                innerContext.Value = iteratorValue;

                blockParams[_0] = iteratorValue;
                blockParams[_1] = iteratorKey;

                template(writer, innerContext);
            }

            if (!enumerated)
            {
                innerContext.Value = context.Value;
                ifEmpty(writer, innerContext);
            }
        }
        public bool TryGetMemberByAlias(object instance, Type targetType, string memberAlias, out object value)
        {
            var segment = ChainSegment.Create(memberAlias);

            switch (instance)
            {
            case Array array:
                switch (segment.LowerInvariant)
                {
                case "count":
                    value = array.Length;
                    return(true);

                default:
                    value = null;
                    return(false);
                }

            case ICollection array:
                switch (segment.LowerInvariant)
                {
                case "length":
                    value = array.Count;
                    return(true);

                default:
                    value = null;
                    return(false);
                }

            case IEnumerable enumerable:
                if (!_configuration.ObjectDescriptorProvider.TryGetDescriptor(targetType, out var descriptor))
                {
                    value = null;
                    return(false);
                }

                var properties = descriptor.GetProperties(enumerable);
                var property   = properties.FirstOrDefault(o =>
                {
                    var name = o.ToString().ToLowerInvariant();
                    return(name.Equals("length") || name.Equals("count"));
                });

                if (property != null && descriptor.MemberAccessor.TryGetValue(enumerable, targetType, property.ToString(), out value))
                {
                    return(true);
                }

                value = null;
                return(false);

            default:
                value = null;
                return(false);
            }
        }
Ejemplo n.º 11
0
        public bool TryGetValue(ChainSegment segment, out object value)
        {
            if (_memberAccessor == null)
            {
                value = null;
                return(false);
            }

            return(_memberAccessor.TryGetValue(_data, segment, out value));
        }
        public virtual bool TryGetValue(object instance, ChainSegment memberName, out object value)
        {
            if (int.TryParse(memberName.LowerInvariant, out var index))
            {
                return(TryGetValueInternal(instance, index, out value));
            }

            value = null;
            return(false);
        }
Ejemplo n.º 13
0
        /// <summary>
        ///    Add an element to the 'head' of a chain.
        /// </summary>
        /// <remarks>
        ///    If this causes the number of elements to exceed the maximum elements
        ///    per chain, the last element in the chain (the 'tail') will be removed
        ///    to allow the additional element to be added.
        /// </remarks>
        /// <param name="chainIndex"> The index of the chain</param>
        /// <param name="billboardChainElement"> The details to add</param>
        protected void AddChainElement(int chainIndex, Element dtls)
        {
            if (chainIndex >= numberOfChains)
            {
                log.Error("BillboardChain.AddChainElement: chainIndex " + chainIndex + " out of bounds");
                return;
            }
            ChainSegment seg = chainSegmentList[chainIndex];

            if (seg.head == SEGMENT_EMPTY)
            {
                // Tail starts at end, head grows backwards
                seg.tail          = maxElementsPerChain - 1;
                seg.head          = seg.tail;
                indexContentDirty = true;
            }
            else
            {
                if (seg.head == 0)
                {
                    // Wrap backwards
                    seg.head = maxElementsPerChain - 1;
                }
                else
                {
                    // Just step backward
                    --seg.head;
                }
                // Run out of elements?
                if (seg.head == seg.tail)
                {
                    // Move tail backwards too, losing the end of the segment and re-using
                    // it in the head
                    if (seg.tail == 0)
                    {
                        seg.tail = maxElementsPerChain - 1;
                    }
                    else
                    {
                        --seg.tail;
                    }
                }
            }

            // Set the details
            chainElementList[seg.start + seg.head] = dtls;

            indexContentDirty = true;
            boundsDirty       = true;
            // tell parent node to update bounds
            if (parentNode != null)
            {
                parentNode.NeedUpdate();
            }
        }
        public bool TryGetValue(object instance, ChainSegment memberName, out object value)
        {
            value = null;
            // Check if the instance is IDictionary (ie, System.Collections.Hashtable)
            // Only string keys supported - indexer takes an object, but no nice
            // way to check if the hashtable check if it should be a different type.
            var dictionary = (IDictionary)instance;

            value = dictionary[memberName.LowerInvariant];
            return(true);
        }
        public bool TryGetValue(ref ChainSegment segment, out object value)
        {
            if (_accessors.TryGetValue(segment.LowerInvariant, out var provider))
            {
                value = provider.Value(provider.Key);
                return(true);
            }

            value = null;
            return(false);
        }
        private bool TryGetValueImpl(object instance, Type instanceType, ChainSegment memberName, out object value)
        {
            if (!_descriptors.TryGetValue(instanceType, out var deferredValue))
            {
                deferredValue = _descriptors.GetOrAdd(instanceType, DescriptorsValueFactory);
            }

            var accessor = deferredValue.Value.GetOrCreateAccessor(memberName);

            value = accessor?.Invoke(instance);
            return(accessor != null);
        }
Ejemplo n.º 17
0
        internal bool TryGetContextVariable(ChainSegment segment, out object value)
        {
            if (segment.WellKnownVariable != WellKnownVariable.None)
            {
                var wellKnownVariable = WellKnownVariables[(int)segment.WellKnownVariable];
                return(BlockParamsObject.TryGetValue(segment, out value) ||
                       ContextDataObject.TryGetValue(wellKnownVariable, out value));
            }

            return(BlockParamsObject.TryGetValue(segment, out value) ||
                   ContextDataObject.TryGetValue(segment, out value));
        }
Ejemplo n.º 18
0
        public object this[ChainSegment segment]
        {
            get
            {
                if (_accessor.TryGetValue(_instance, segment, out var value))
                {
                    return(value);
                }

                return(null);
            }
        }
Ejemplo n.º 19
0
        internal bool TryGetVariable(ChainSegment segment, out object value)
        {
            if (segment.WellKnownVariable != WellKnownVariable.None)
            {
                var wellKnownVariable = WellKnownVariables[(int)segment.WellKnownVariable];
                return(BlockParamsObject.TryGetValue(wellKnownVariable, out value) ||
                       (Descriptor.Value.MemberAccessor?.TryGetValue(Value, segment, out value) ?? false));
            }

            return(BlockParamsObject.TryGetValue(segment, out value) ||
                   (Descriptor.Value.MemberAccessor?.TryGetValue(Value, segment, out value) ?? false));
        }
        public override object Invoke(object context, params object[] arguments)
        {
            if (arguments.Length != 2)
            {
                throw new HandlebarsException("{{lookup}} helper must have exactly two argument");
            }

            var segment = ChainSegment.Create(arguments[1]);

            return(!PathResolver.TryAccessMember(arguments[0], segment, _configuration, out var value)
                ? segment.GetUndefinedBindingResult(_configuration)
                : value);
        }
Ejemplo n.º 21
0
        public bool TryGetValue(object instance, ChainSegment memberName, out object value)
        {
            var dictionary = (T)instance;

            if (dictionary.TryGetValue(memberName.TrimmedValue, out var v))
            {
                value = v;
                return(true);
            }

            value = null;
            return(false);
        }
Ejemplo n.º 22
0
        public bool TryGetValue(object instance, ChainSegment memberName, out object value)
        {
            for (var index = 0; index < _accessors.Length; index++)
            {
                if (_accessors[index].TryGetValue(instance, memberName, out value))
                {
                    return(true);
                }
            }

            value = default;
            return(false);
        }
Ejemplo n.º 23
0
        private int GetIndex(ChainSegment chainSeg, float percentFromTail, ref float fPercentToLast)
        {
            float fLength = (GetLength(chainSeg) - 1) * percentFromTail;
            int   nLast   = (int)(fLength);

            fPercentToLast = fLength - nLast;
            nLast          = chainSeg.tail - nLast;
            if (nLast < 0)
            {
                nLast = MaxChainElements + nLast;
            }
            return(nLast);
        }
        public bool TryGetValue(object instance, ChainSegment memberName, out object value)
        {
            var key        = (TK)TypeConverter.ConvertFromString(memberName.TrimmedValue);
            var dictionary = (T)instance;

            if (key != null && dictionary.TryGetValue(key, out var v))
            {
                value = v;
                return(true);
            }

            value = null;
            return(false);
        }
        private object Lookup(dynamic context, params object[] arguments)
        {
            if (arguments.Length != 2)
            {
                throw new HandlebarsException("{{lookup}} helper must have exactly two argument");
            }

            var memberName = arguments[1].ToString();
            var segment    = ChainSegment.Create(memberName);

            return(!PathResolver.TryAccessMember(arguments[0], ref segment, _configuration, out var value)
                ? new UndefinedBindingResult(memberName, _configuration)
                : value);
        }
Ejemplo n.º 26
0
        /// <summary>
        ///    Get the detail of a chain element.
        /// </summary>
        /// <param name="chainIndex> The index of the chain</param>
        /// <param name="elementIndex"> The element index within the chain, measured from the 'head' of the chain</param>
        protected Element GetChainElement(int chainIndex, int elementIndex)
        {
            if (chainIndex >= numberOfChains)
            {
                log.Error("BillboardChain.GetChainElement: chainIndex " + chainIndex + " out of bounds");
                return(null);
            }
            ChainSegment seg = chainSegmentList[chainIndex];
            int          idx = seg.head + elementIndex;

            // adjust for the edge and start
            idx = (idx % maxElementsPerChain) + seg.start;

            return(chainElementList[idx]);
        }
Ejemplo n.º 27
0
        public bool TryGetMemberByAlias(JsonElement instance, Type targetType, ChainSegment memberAlias, out object?value)
        {
            if (!EqualsIgnoreCase("count", memberAlias) && !EqualsIgnoreCase("length", memberAlias))
            {
                value = null;
                return(false);
            }

            if (instance.ValueKind != JsonValueKind.Array)
            {
                value = null;
                return(false);
            }

            value = instance.GetArrayLength();
            return(true);
Ejemplo n.º 28
0
        //-----------------------------------------------------------------------
        public void ResetTrail(int index, Node node)
        {
            Debug.Assert(index < numberOfChains);

            ChainSegment seg = chainSegmentList[index];

            // set up this segment
            seg.head = seg.tail = SEGMENT_EMPTY;
            // Create new element, v coord is always 0.0f
            Element e = new Element(node.DerivedPosition, trailAttributes[index].initialWidth, 0.0f, trailAttributes[index].initialColor);

            // Add the start position
            AddChainElement(index, e);
            // Add another on the same spot, this will extend
            AddChainElement(index, e);
        }
Ejemplo n.º 29
0
        public bool TryGetValue(object instance, ChainSegment memberName, out object value)
        {
            value = null;
            //crude handling for dynamic objects that don't have metadata
            var metaObjectProvider = (IDynamicMetaObjectProvider)instance;

            try
            {
                value = GetProperty(metaObjectProvider, memberName.TrimmedValue);
                return(value != null);
            }
            catch
            {
                return(false);
            }
        }
Ejemplo n.º 30
0
        public override bool TryGetValue(ref ChainSegment segment, out object value)
        {
            switch (segment.LowerInvariant)
            {
            case "key":
                value = Key;
                return(true);

            case "last" when !_configuration.Compatibility.SupportLastInObjectIterations:
                value = null;
                return(true);

            default:
                return(base.TryGetValue(ref segment, out value));
            }
        }