SharpLibEar/Object.cs
author StephaneLenclud
Sat, 20 Aug 2016 21:00:35 +0200
changeset 246 30a221eecc06
parent 244 2e4d2558bb21
child 250 b2121d03f6f0
permissions -rw-r--r--
Generic HID event with key recognition functional.
StephaneLenclud@238
     1
using System;
StephaneLenclud@238
     2
using System.Collections.Generic;
StephaneLenclud@238
     3
using System.Linq;
StephaneLenclud@238
     4
using System.Text;
StephaneLenclud@238
     5
using System.Threading.Tasks;
StephaneLenclud@238
     6
StephaneLenclud@238
     7
StephaneLenclud@238
     8
using System;
StephaneLenclud@238
     9
using System.Collections.Generic;
StephaneLenclud@238
    10
using System.Runtime.Serialization;
StephaneLenclud@244
    11
using System.ComponentModel;
StephaneLenclud@238
    12
StephaneLenclud@238
    13
namespace SharpLib.Ear
StephaneLenclud@238
    14
{
StephaneLenclud@238
    15
StephaneLenclud@238
    16
    /// <summary>
StephaneLenclud@238
    17
    /// EAR object provides serialization support.
StephaneLenclud@238
    18
    /// It assumes most derived class is decorated with AttributeObject.
StephaneLenclud@238
    19
    /// </summary>
StephaneLenclud@238
    20
    [DataContract]
StephaneLenclud@238
    21
    [KnownType("DerivedTypes")]
StephaneLenclud@244
    22
    public abstract class Object: IComparable, INotifyPropertyChanged
StephaneLenclud@238
    23
    {
Stephane@243
    24
        private bool iConstructed = false;
Stephane@243
    25
StephaneLenclud@244
    26
        protected Object()
Stephane@243
    27
        {
Stephane@243
    28
            Construct();
Stephane@243
    29
        }
Stephane@243
    30
Stephane@243
    31
        /// <summary>
Stephane@243
    32
        /// Needed as our constructor is not called following internalization.
Stephane@243
    33
        /// </summary>
Stephane@243
    34
        public void Construct()
Stephane@243
    35
        {
Stephane@243
    36
            if (!iConstructed)
Stephane@243
    37
            {
Stephane@243
    38
                DoConstruct();
Stephane@243
    39
                iConstructed = true;
Stephane@243
    40
            }
Stephane@243
    41
        }
Stephane@243
    42
Stephane@243
    43
        /// <summary>
Stephane@243
    44
        /// 
Stephane@243
    45
        /// </summary>
Stephane@243
    46
        protected virtual void DoConstruct()
Stephane@243
    47
        {
Stephane@243
    48
Stephane@243
    49
        }
Stephane@243
    50
StephaneLenclud@246
    51
StephaneLenclud@246
    52
        public enum State
StephaneLenclud@246
    53
        {
StephaneLenclud@246
    54
            Rest=0,
StephaneLenclud@246
    55
            Edit
StephaneLenclud@246
    56
        }
StephaneLenclud@246
    57
StephaneLenclud@246
    58
        State iCurrentState = State.Rest;
StephaneLenclud@246
    59
        public State CurrentState { get { return iCurrentState; } set { OnStateLeave(); iCurrentState = value; OnStateEnter(); } }
StephaneLenclud@246
    60
StephaneLenclud@246
    61
        /// <summary>
StephaneLenclud@246
    62
        /// 
StephaneLenclud@246
    63
        /// </summary>
StephaneLenclud@246
    64
        protected virtual void OnStateLeave()
StephaneLenclud@246
    65
        {
StephaneLenclud@246
    66
StephaneLenclud@246
    67
        }
StephaneLenclud@246
    68
StephaneLenclud@246
    69
        /// <summary>
StephaneLenclud@246
    70
        /// 
StephaneLenclud@246
    71
        /// </summary>
StephaneLenclud@246
    72
        protected virtual void OnStateEnter()
StephaneLenclud@246
    73
        {
StephaneLenclud@246
    74
StephaneLenclud@246
    75
        }
StephaneLenclud@246
    76
StephaneLenclud@239
    77
        /// <summary>
StephaneLenclud@239
    78
        /// Static object name.
StephaneLenclud@239
    79
        /// </summary>
StephaneLenclud@238
    80
        public string Name
StephaneLenclud@238
    81
        {
StephaneLenclud@238
    82
            //Get the name of this object attribute
StephaneLenclud@238
    83
            get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name; }
StephaneLenclud@238
    84
            private set { }
StephaneLenclud@238
    85
        }
StephaneLenclud@238
    86
StephaneLenclud@239
    87
        /// <summary>
StephaneLenclud@239
    88
        /// Static object description.
StephaneLenclud@239
    89
        /// </summary>
StephaneLenclud@238
    90
        public string Description
StephaneLenclud@238
    91
        {
StephaneLenclud@238
    92
            //Get the description of this object attribute
StephaneLenclud@238
    93
            get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Description; }
StephaneLenclud@238
    94
            private set { }
StephaneLenclud@238
    95
        }
StephaneLenclud@238
    96
StephaneLenclud@239
    97
        /// <summary>
StephaneLenclud@239
    98
        /// Dynamic object description.
StephaneLenclud@239
    99
        /// </summary>
StephaneLenclud@239
   100
        /// <returns></returns>
StephaneLenclud@238
   101
        public virtual string Brief()
StephaneLenclud@238
   102
        {
StephaneLenclud@238
   103
            return Name;
StephaneLenclud@238
   104
        }
StephaneLenclud@238
   105
StephaneLenclud@239
   106
        /// <summary>
StephaneLenclud@239
   107
        /// Needed to make sure our sorting makes sense
StephaneLenclud@239
   108
        /// </summary>
StephaneLenclud@239
   109
        /// <param name="obj"></param>
StephaneLenclud@239
   110
        /// <returns></returns>
StephaneLenclud@238
   111
        public int CompareTo(object obj)
StephaneLenclud@238
   112
        {
StephaneLenclud@238
   113
            //Sort by object name
StephaneLenclud@238
   114
            return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name.CompareTo(obj.GetType());
StephaneLenclud@238
   115
        }
StephaneLenclud@238
   116
StephaneLenclud@238
   117
        /// <summary>
StephaneLenclud@239
   118
        /// Tells whether the current object configuration is valid.
StephaneLenclud@239
   119
        /// </summary>
StephaneLenclud@239
   120
        /// <returns></returns>
StephaneLenclud@239
   121
        public virtual bool IsValid()
StephaneLenclud@239
   122
        {
StephaneLenclud@239
   123
            return true;
StephaneLenclud@239
   124
        }
StephaneLenclud@239
   125
StephaneLenclud@239
   126
        /// <summary>
StephaneLenclud@238
   127
        /// So that data contract knows all our types.
StephaneLenclud@238
   128
        /// </summary>
StephaneLenclud@238
   129
        /// <returns></returns>
StephaneLenclud@238
   130
        private static IEnumerable<Type> DerivedTypes()
StephaneLenclud@238
   131
        {
StephaneLenclud@238
   132
            return SharpLib.Utils.Reflection.GetDerivedTypes<Object>();
StephaneLenclud@238
   133
        }
StephaneLenclud@238
   134
StephaneLenclud@244
   135
        /// <summary>
StephaneLenclud@244
   136
        /// 
StephaneLenclud@244
   137
        /// </summary>
StephaneLenclud@244
   138
        public event PropertyChangedEventHandler PropertyChanged;
StephaneLenclud@244
   139
StephaneLenclud@246
   140
StephaneLenclud@244
   141
        /// <summary>
StephaneLenclud@244
   142
        /// Invoke our event.
StephaneLenclud@244
   143
        /// </summary>
StephaneLenclud@244
   144
        /// <param name="name"></param>
StephaneLenclud@244
   145
        protected void OnPropertyChanged(string name)
StephaneLenclud@244
   146
        {
StephaneLenclud@244
   147
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
StephaneLenclud@244
   148
        }
StephaneLenclud@244
   149
StephaneLenclud@244
   150
StephaneLenclud@238
   151
    }
StephaneLenclud@238
   152
}