Пример #1
0
        public IEnumConfigurator WriteEnum(string name, EnumValueType valueType = EnumValueType.Number)
        {
            var enumInfo = new EnumInfo(name, valueType);
            var part     = _options.EnumWriterFactory.Build(enumInfo);

            _parts.Add(part);
            return(enumInfo);
        }
Пример #2
0
        CCodeExpression generate_enum_value_to_string(EnumValueType type, CCodeExpression expr)
        {
            var en             = type.type_symbol as ValaEnum;
            var to_string_name = "%s_to_string".printf(get_ccode_lower_case_name(en, null));

            var to_string_call = new CCodeFunctionCall(new CCodeIdentifier(to_string_name));

            to_string_call.add_argument(expr);

            return(to_string_call);
        }
Пример #3
0
 public double GetLast(EnumValueType valueType)
 {
     if (Count != 0)
     {
         return(valueDict[valueType.ToString()][Count - 1]);
     }
     else
     {
         return(JPR.NaN);
     }
 }
Пример #4
0
        CCodeExpression generate_enum_value_from_string(EnumValueType type, CCodeExpression expr, CCodeExpression error_expr)
        {
            var en = type.type_symbol as ValaEnum;
            var from_string_name = "%s_from_string".printf(get_ccode_lower_case_name(en, null));

            var from_string_call = new CCodeFunctionCall(new CCodeIdentifier(from_string_name));

            from_string_call.add_argument(expr);
            from_string_call.add_argument(error_expr != null ? error_expr : new CCodeConstant("NULL"));

            return(from_string_call);
        }
Пример #5
0
        public static Func <byte[], object> GetBytesToValueFromEnum(EnumValueType enumType)
        {
            switch (enumType)
            {
            case EnumValueType.Byte:
                return(ValueConverter.FromByteBytes);

            case EnumValueType.Short:
                return(ValueConverter.FromShortBytes);

            case EnumValueType.Int:
                return(ValueConverter.FromIntBytes);

            case EnumValueType.Long:
                return(ValueConverter.FromLongBytes);

            case EnumValueType.Bool:
                return(ValueConverter.FromBoolBytes);

            case EnumValueType.DateTime:
                return(ValueConverter.FromDateTimeBytes);

            case EnumValueType.Decimal:
                return(ValueConverter.FromDecimalBytes);

            case EnumValueType.Float:
                return(ValueConverter.FromFloatBytes);

            case EnumValueType.Double:
                return(ValueConverter.FromDoubleBytes);

            case EnumValueType.Guid:
                return(ValueConverter.FromGuidBytes);

            case EnumValueType.Bytes:
                return(ValueConverter.FromByteArrayBytes);

            case EnumValueType.String:
                return(ValueConverter.FromStringBytes);

            case EnumValueType.Object:
                return(ValueConverter.FromObjectBytes);

            default:
                return(null);
            }
        }
Пример #6
0
        public static Type GetTypeFromEnumType(EnumValueType enumvalue)
        {
            switch (enumvalue)
            {
            case EnumValueType.Byte:
                return(typeof(byte));

            case EnumValueType.Short:
                return(typeof(short));

            case EnumValueType.Int:
                return(typeof(int));

            case EnumValueType.Long:
                return(typeof(long));

            case EnumValueType.Bool:
                return(typeof(bool));

            case EnumValueType.DateTime:
                return(typeof(DateTime));

            case EnumValueType.Decimal:
                return(typeof(decimal));

            case EnumValueType.Float:
                return(typeof(float));

            case EnumValueType.Double:
                return(typeof(double));

            case EnumValueType.Guid:
                return(typeof(Guid));

            case EnumValueType.Bytes:
                return(typeof(byte[]));

            case EnumValueType.String:
                return(typeof(string));

            default:
                return(null);
            }
        }
Пример #7
0
 public static List <TimeCode> SubList(EnumValueType type)
 {
     return(ListForCompany().Where(x => x.ValueType == type).ToList());
 }
        public override void ElementPropertyChanged(ElementPropertyChangedEventArgs e)
        {
            base.ElementPropertyChanged(e);

            ModelEnum element = (ModelEnum)e.ModelElement;

            if (element.IsDeleted)
            {
                return;
            }

            Store       store = element.Store;
            Transaction currentTransaction = store.TransactionManager.CurrentTransaction;

            if (currentTransaction.IsSerializing)
            {
                return;
            }

            if (Equals(e.NewValue, e.OldValue))
            {
                return;
            }

            string errorMessage = null;

            switch (e.DomainProperty.Name)
            {
            case "Name":

                if (currentTransaction.Name.ToLowerInvariant() == "paste")
                {
                    return;
                }

                if (string.IsNullOrWhiteSpace(element.Name) || !CodeGenerator.IsValidLanguageIndependentIdentifier(element.Name))
                {
                    errorMessage = "Name must be a valid .NET identifier";
                }
                else if (store.GetAll <ModelClass>().Any(x => x.FullName == element.FullName))
                {
                    errorMessage = "Enum name already in use by a class";
                }
                else if (store.GetAll <ModelEnum>().Except(new[] { element }).Any(x => x.FullName == element.FullName))
                {
                    errorMessage = "Enum name already in use by another enum";
                }
                else
                {
                    // rename type names for ModelAttributes that reference this enum
                    foreach (ModelAttribute modelAttribute in store.GetAll <ModelAttribute>().Where(a => a.Type == (string)e.OldValue))
                    {
                        modelAttribute.Type = element.Name;

                        if (!string.IsNullOrEmpty(modelAttribute.InitialValue))
                        {
                            string[] parts = modelAttribute.InitialValue.Split('.');
                            parts[0] = (string)e.NewValue;
                            modelAttribute.InitialValue = string.Join(".", parts);
                        }
                    }
                }

                break;

            case "Namespace":

                if (currentTransaction.Name.ToLowerInvariant() != "paste")
                {
                    errorMessage = CommonRules.ValidateNamespace(element.Namespace, CodeGenerator.IsValidLanguageIndependentIdentifier);
                }

                break;

            case "IsFlags":

                element.SetFlagValues();

                break;

            case "ValueType":

                EnumValueType         newValueType    = (EnumValueType)e.NewValue;
                List <ModelAttribute> modelAttributes = store.ElementDirectory
                                                        .AllElements
                                                        .OfType <ModelAttribute>()
                                                        .Where(a => a.Type == element.Name && a.IsIdentity)
                                                        .ToList();

                if (modelAttributes.Any())
                {
                    string classList = string.Join(", ", modelAttributes.Select(a => a.ModelClass.Name + "." + a.Name));
                    errorMessage = $"Can't change {element.Name} value type to {newValueType}. It's not a valid identity type, and {element.Name} is used as an identity type in {classList}";
                }

                break;
            }

            if (errorMessage != null)
            {
                currentTransaction.Rollback();
                ErrorDisplay.Show(store, errorMessage);
            }
        }
Пример #9
0
 public List <double> GetValues(EnumValueType valueType)
 {
     return(valueDict[valueType.ToString()]);
 }
Пример #10
0
        /// <summary>
        /// Creates a new variable.
        /// </summary>
        private DataItemState CreateMultiStateValueDiscreteItemVariable(NodeState parent, string path, string name, params string[] enumNames)
        {
            MultiStateValueDiscreteState variable = new MultiStateValueDiscreteState(parent);

            variable.NodeId = new NodeId(path, NamespaceIndex);
            variable.BrowseName = new QualifiedName(path, NamespaceIndex);
            variable.DisplayName = new LocalizedText("en", name);
            variable.WriteMask = AttributeWriteMask.None;
            variable.UserWriteMask = AttributeWriteMask.None;

            variable.Create(
                SystemContext,
                null,
                variable.BrowseName,
                null,
                true);

            variable.SymbolicName = name;
            variable.ReferenceTypeId = ReferenceTypes.Organizes;
            variable.DataType = DataTypeIds.UInt32;
            variable.ValueRank = ValueRanks.Scalar;
            variable.AccessLevel = AccessLevels.CurrentReadOrWrite;
            variable.UserAccessLevel = AccessLevels.CurrentReadOrWrite;
            variable.Historizing = false;
            variable.Value = (uint)0;
            variable.StatusCode = StatusCodes.Good;
            variable.Timestamp = DateTime.UtcNow;
            // variable.OnWriteValue = OnWriteDiscrete;

            // there are two enumerations for this type:
            // EnumStrings = the string representations for enumerated values
            // EnumValues  = the actual enumerated value

            // set the enumerated strings
            LocalizedText[] strings = new LocalizedText[enumNames.Length];
            for (int ii = 0; ii < strings.Length; ii++)
            {
                strings[ii] = enumNames[ii];
            }

            //variable.EnumStrings = new PropertyState<LocalizedText[]>( variable );
            //variable.EnumStrings.Value = strings;
            //variable.EnumStrings.AccessLevel = AccessLevels.CurrentReadOrWrite;
            //variable.EnumStrings.UserAccessLevel = AccessLevels.CurrentReadOrWrite;

            // set the enumerated values
            EnumValueType[] values = new EnumValueType[enumNames.Length];
            for (int ii = 0; ii < values.Length; ii++)
            {
                values[ii] = new EnumValueType();
                values[ii].Value = ii;
                values[ii].Description = strings[ii];
                values[ii].DisplayName = strings[ii];
            }
            variable.EnumValues.Value = values;
            variable.EnumValues.AccessLevel = AccessLevels.CurrentReadOrWrite;
            variable.EnumValues.UserAccessLevel = AccessLevels.CurrentReadOrWrite;
            variable.ValueAsText.Value = strings[0];

            if (parent != null)
            {
                parent.AddChild(variable);
            }

            return variable;
        }
Пример #11
0
        private List <int> XMLOnce(XElement nowElement, int parentid)
        {
            List <DataType> DataTemp       = new List <DataType>();
            List <int>      IDList         = new List <int>();
            List <int>      childrenIDList = new List <int>();
            var             keys           = from k in nowElement.Elements("key")
                                             select k;
            var values = from v in nowElement.Elements()
                         where v.Name != "key"
                         select v;
            var valList = values.ToList();

            for (int i = 0; i < valList.Count; i++)
            {
                int           id        = ++count;
                EnumValueType valuetype = (EnumValueType)Enum.Parse(typeof(EnumValueType), valList[i].Name.LocalName.ToString().ToUpper(), true);
                string        value     = null;
                if (valuetype == EnumValueType.ARRAY)
                {
                    XElement newElement = nowElement.Elements().Except(nowElement.Elements("key")).ElementAt(i);
                    int      num        = newElement.Elements().Count();
                    for (int j = 0; j < num; j++)
                    {
                        newElement.AddFirst(new XElement("key", "item"));
                    }
                    childrenIDList = XMLOnce(newElement, id);
                }
                else if (valuetype == EnumValueType.DICT)
                {
                    XElement newElement = nowElement.Elements().Except(nowElement.Elements("key")).ElementAt(i);
                    childrenIDList = XMLOnce(newElement, id);
                }
                else if (valuetype == EnumValueType.TRUE)
                {
                    value = "true";
                }
                else if (valuetype == EnumValueType.FALSE)
                {
                    value = "false";
                }
                else
                {
                    value = valList[i].Value.ToString();
                }

                try
                {
                    DataTemp.Add(new DataType()
                    {
                        DataName   = keys.ToList()[i].Value.ToString(),
                        ValueType  = valuetype,
                        ID         = id,
                        Value      = value,
                        parentID   = parentid,
                        childrenID = childrenIDList
                    });
                }
                catch (System.Exception ex)
                {
                    DataTemp.Add(new DataType()
                    {
                        DataName   = "itemContent",
                        ValueType  = valuetype,
                        ID         = id,
                        Value      = value,
                        parentID   = parentid,
                        childrenID = childrenIDList
                    });
                }
            }
            foreach (var item in DataTemp)
            {
                IDList.Add(item.ID);
            }
            DataList.AddRange(DataTemp);
            return(IDList);
        }
Пример #12
0
        public static DataType get_data_type_for_symbol(TypeSymbol sym)
        {
            DataType type = null;

            List <TypeParameter> type_parameters = null;

            if (sym is ObjectTypeSymbol)
            {
                type            = new ObjectType((ObjectTypeSymbol)sym);
                type_parameters = ((ObjectTypeSymbol)sym).get_type_parameters();
            }
            else if (sym is Struct)
            {
                var st = (Struct)sym;
                if (st.is_boolean_type())
                {
                    type = new BooleanType(st);
                }
                else if (st.is_integer_type())
                {
                    type = new IntegerType(st);
                }
                else if (st.is_floating_type())
                {
                    type = new FloatingType(st);
                }
                else
                {
                    type = new StructValueType(st);
                }
                type_parameters = st.get_type_parameters();
            }
            else if (sym is ValaEnum)
            {
                type = new EnumValueType((ValaEnum)sym);
            }
            else if (sym is ErrorDomain)
            {
                type = new ErrorType((ErrorDomain)sym, null);
            }
            else if (sym is ErrorCode)
            {
                type = new ErrorType((ErrorDomain)sym.parent_symbol, (ErrorCode)sym);
            }
            else
            {
                Report.error(null, "internal error: `%s' is not a supported type".printf(sym.get_full_name()));
                return(new InvalidType());
            }

            if (type_parameters != null)
            {
                foreach (var type_param in type_parameters)
                {
                    var type_arg = new GenericType(type_param);
                    type_arg.value_owned = true;
                    type.add_type_argument(type_arg);
                }
            }

            return(type);
        }
Пример #13
0
        private DataType resolve_type(UnresolvedType unresolved_type)
        {
            DataType type = null;

            // still required for vapigen
            if (unresolved_type.unresolved_symbol.name == "void")
            {
                return(new VoidType());
            }

            var sym = resolve_symbol(unresolved_type.unresolved_symbol);

            if (sym == null)
            {
                // don't report same error twice
                if (!unresolved_type.unresolved_symbol.error)
                {
                    Report.error(unresolved_type.source_reference, "The type name `%s' could not be found".printf(unresolved_type.unresolved_symbol.ToString()));
                }
                return(new InvalidType());
            }

            if (sym is TypeParameter)
            {
                type = new GenericType((TypeParameter)sym);
            }
            else if (sym is TypeSymbol)
            {
                if (sym is ValaDelegate)
                {
                    type = new DelegateType((ValaDelegate)sym);
                }
                else if (sym is Class)
                {
                    var cl = (Class)sym;
                    if (cl.is_error_base)
                    {
                        type = new ErrorType(null, null, unresolved_type.source_reference);
                    }
                    else
                    {
                        type = new ObjectType(cl);
                    }
                }
                else if (sym is Interface)
                {
                    type = new ObjectType((Interface)sym);
                }
                else if (sym is Struct)
                {
                    type = get_type_for_struct((Struct)sym, (Struct)sym);
                }
                else if (sym is ValaEnum)
                {
                    type = new EnumValueType((ValaEnum)sym);
                }
                else if (sym is ErrorDomain)
                {
                    type = new ErrorType((ErrorDomain)sym, null, unresolved_type.source_reference);
                }
                else if (sym is ErrorCode)
                {
                    type = new ErrorType((ErrorDomain)sym.parent_symbol, (ErrorCode)sym, unresolved_type.source_reference);
                }
                else
                {
                    Report.error(unresolved_type.source_reference, "internal error: `%s' is not a supported type".printf(sym.get_full_name()));
                    return(new InvalidType());
                }
            }
            else
            {
                Report.error(unresolved_type.source_reference, "`%s' is not a type".printf(sym.get_full_name()));
                return(new InvalidType());
            }

            type.source_reference = unresolved_type.source_reference;
            type.value_owned      = unresolved_type.value_owned;
            sym.used = true;

            if (type is GenericType)
            {
                // type parameters are always considered nullable
                // actual type argument may or may not be nullable
                type.nullable = true;
            }
            else
            {
                type.nullable = unresolved_type.nullable;
            }

            type.is_dynamic = unresolved_type.is_dynamic;
            foreach (DataType type_arg in unresolved_type.get_type_arguments())
            {
                type.add_type_argument(type_arg);
            }

            return(type);
        }
        /// <summary>
        /// Creates a new variable.
        /// </summary>
        private DataItemState CreateMultiStateValueDiscreteItemVariable(NodeState parent, string path, string name, NodeId nodeId, params string[] enumNames)
        {
            MultiStateValueDiscreteState variable = new MultiStateValueDiscreteState(parent);

            variable.NodeId        = new NodeId(path, NamespaceIndex);
            variable.BrowseName    = new QualifiedName(path, NamespaceIndex);
            variable.DisplayName   = new LocalizedText("en", name);
            variable.WriteMask     = AttributeWriteMask.None;
            variable.UserWriteMask = AttributeWriteMask.None;

            variable.Create(
                SystemContext,
                null,
                variable.BrowseName,
                null,
                true);

            variable.SymbolicName    = name;
            variable.ReferenceTypeId = ReferenceTypes.Organizes;
            variable.DataType        = (nodeId == null) ? DataTypeIds.UInt32 : nodeId;
            variable.ValueRank       = ValueRanks.Scalar;
            variable.AccessLevel     = AccessLevels.CurrentReadOrWrite;
            variable.UserAccessLevel = AccessLevels.CurrentReadOrWrite;
            variable.Historizing     = false;
            variable.Value           = (uint)0;
            variable.StatusCode      = StatusCodes.Good;
            variable.Timestamp       = DateTime.UtcNow;
            variable.OnWriteValue    = OnWriteValueDiscrete;

            // there are two enumerations for this type:
            // EnumStrings = the string representations for enumerated values
            // ValueAsText = the actual enumerated value

            // set the enumerated strings
            LocalizedText[] strings = new LocalizedText[enumNames.Length];
            for (int ii = 0; ii < strings.Length; ii++)
            {
                strings[ii] = enumNames[ii];
            }

            // set the enumerated values
            EnumValueType[] values = new EnumValueType[enumNames.Length];
            for (int ii = 0; ii < values.Length; ii++)
            {
                values[ii]             = new EnumValueType();
                values[ii].Value       = ii;
                values[ii].Description = strings[ii];
                values[ii].DisplayName = strings[ii];
            }
            variable.EnumValues.Value           = values;
            variable.EnumValues.AccessLevel     = AccessLevels.CurrentReadOrWrite;
            variable.EnumValues.UserAccessLevel = AccessLevels.CurrentReadOrWrite;
            variable.ValueAsText.Value          = variable.EnumValues.Value[0].DisplayName;

            if (parent != null)
            {
                parent.AddChild(variable);
            }

            return(variable);
        }
Пример #15
0
        public override DataValue Compute(IAggregationContext context, TimeSlice bucket, AggregateState state)
        {
            int  numGood      = 0;
            int  numBad       = 0;
            int  nTransitions = 0;
            long stateCode    = -1;
            IEnumerator <DataValue> enumerator = bucket.Values.GetEnumerator();
            bool bucketValueNotEmpty           = enumerator.MoveNext();

            if (bucketValueNotEmpty && enumerator.Current != null)
            {
                if (bucket.EarlyBound != null)
                {
                    if (enumerator.Current.SourceTimestamp == bucket.EarlyBound.Timestamp && bucket.EarlyBound.PriorPoint != null)
                    {
                        stateCode = Convert.ToInt32(Convert.ToBoolean(bucket.EarlyBound.PriorPoint.Value));
                    }
                    else if (bucket.EarlyBound.Value != null)
                    {
                        stateCode = Convert.ToInt32(Convert.ToBoolean(bucket.EarlyBound.Value.Value));
                    }
                }
            }

            // viz. UA MultiStateNodeState & TwoStateNodeState,
            // assume DataValue.Value is either an EnumValueType or a bool
            if (bucketValueNotEmpty)
            {
                do
                {
                    DataValue dv = enumerator.Current;
                    if (state.RawValueIsGood(dv))
                    {
                        EnumValueType ev = dv.Value as EnumValueType;
                        if (ev == null)
                        {
                            bool b;
                            if (bool.TryParse(dv.Value.ToString(), out b))
                            {
                                if (stateCode < 0)
                                {
                                    stateCode = b ? 1 : 0;
                                }
                                else if (b.CompareTo(Convert.ToBoolean(stateCode)) != 0)
                                {
                                    nTransitions++;
                                    stateCode = b ? 1 : 0;
                                }
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            long s = ev.Value;
                            if (stateCode < 0)
                            {
                                stateCode = s;
                            }
                            else if (!s.Equals(stateCode))
                            {
                                nTransitions++;
                                stateCode = s;
                            }
                        }
                        numGood++;
                    }
                    else
                    {
                        numBad++;
                    }
                } while (enumerator.MoveNext());
            }

            StatusCode code   = ComputeStatus(context, numGood, numBad, bucket).Code;
            DataValue  retval = new DataValue {
                SourceTimestamp = bucket.From, Value = nTransitions
            };

            code.AggregateBits = AggregateBits.Calculated;
            if (bucket.Incomplete)
            {
                code.AggregateBits |= AggregateBits.Partial;
            }
            retval.StatusCode = code;
            return(retval);
        }
Пример #16
0
 public EnumInfo(string name, EnumValueType valueType)
 {
     Name      = name;
     ValueType = valueType;
 }
 public ModelEnumValueAttribute(string key, EnumValueType type = EnumValueType.ReadWrite)
 {
     Key  = key;
     Type = type;
 }