Ejemplo n.º 1
0
        public void ConvertValue(IBedValueReader reader, TypeStorage dsttype, IBedValueWriter writer)
        {
            var srctype = reader.GetFieldType();

            if (srctype == dsttype)
            {
                // no conversion needed
                writer.ReadFrom(reader);
                return;
            }
            if (srctype.IsNumber() && dsttype.IsNumber())
            {
                if (dsttype.IsInteger())
                {
                    writer.SetIntegerValue(dsttype, reader.GetIntegerValue());
                }
                else
                {
                    writer.SetRealValue(dsttype, reader.GetRealValue());
                }
                return;
            }
            if (srctype.IsDateRelated() && dsttype.IsDateRelated())
            {
                writer.SetDateTimeValue(dsttype, reader.GetDateTimeValue());
                return;
            }
            if (srctype == TypeStorage.Boolean && dsttype.IsNumber())
            {
                bool val = reader.GetBoolean();
                writer.SetIntegerValue(dsttype, val ? 1 : 0);
                return;
            }
            if (srctype.IsNumber() && dsttype == TypeStorage.Boolean)
            {
                long val = reader.GetIntegerValue();
                writer.SetBoolean(val != 0);
                return;
            }
            if (srctype == TypeStorage.String)
            {
                // parse
                m_parser.ParseValue(reader.GetString(), dsttype, writer);
                return;
            }
            if (dsttype == TypeStorage.String)
            {
                // format
                m_formatter.ReadFrom(reader);
                writer.SetString(m_formatter.GetText());
                return;
            }
            {
                // most generic case - format and than parse
                m_formatter.ReadFrom(reader);
                m_parser.ParseValue(m_formatter.GetText(), dsttype, writer);
                return;
            }
        }