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