Basic Types

Daabli has built-in support for loading the following basic types: bool, byte, sbyte, char, decimal, double, float, int, uint, long, ulong, short, ushort, string.

A bool object is described with either true or false. Numeric objects are described directly (without any decoration). A char object can be described with a numeric value or with a character literal enclosed in single quotes. A string object is described with a string literal enclosed in double quotes.

Let's look at an example of how to load some properties of a space ship from a description.

using System;
using Daabli;

namespace ConsoleApplication
{
    struct SpaceShip
    {
        public char _type;
        public float _speed;
        public string _desc;
        public bool _isCloaked;
    };

    class Program
    {
        static void Main(string[] args)
        {
            // Create a Reader
            var r = new DefaultReader();

            // Specify which file to read from
            if (!r.FromFile("input.txt"))
                return;

            var ship = new SpaceShip();

            // Read the space ship's properties
            if (!r.Read("type",      ref ship._type                   ) || // required
                !r.Read("speed",     ref ship._speed                  ) || // required
                !r.Read("desc",      ref ship._desc,      string.Empty) || // optional
                !r.Read("isCloaked", ref ship._isCloaked, false       ))   // optional
                return;

            // Display the space ship's properties
            Console.WriteLine("type      : {0}", ship._type);
            Console.WriteLine("speed     : {0}", ship._speed);
            Console.WriteLine("desc      : {0}", ship._desc);
            Console.WriteLine("isCloaked : {0}", ship._isCloaked);
        }
    }
}

Given the description in input.txt:
/* Ship properties */
type      = 'D';
speed     = 10.2;
desc      = "drone ship";
//isCloaked = true;

The output generated would be:
type      : D
speed     : 10.2
desc      : drone ship
isCloaked : False

Note from the input.txt file that Daabli supports C/C++ style comments.

Next: Enumerations

Last edited Apr 20, 2012 at 5:59 AM by francisxavier, version 7

Comments

No comments yet.