SharpLibEar/Object.cs
author StephaneLenclud
Fri, 19 Aug 2016 19:28:03 +0200
changeset 245 448e6a616c22
parent 243 cc2251d065db
child 246 30a221eecc06
permissions -rw-r--r--
Removing Input tab as we don't need to specific features anymore.
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@239
    51
        /// <summary>
StephaneLenclud@239
    52
        /// Static object name.
StephaneLenclud@239
    53
        /// </summary>
StephaneLenclud@238
    54
        public string Name
StephaneLenclud@238
    55
        {
StephaneLenclud@238
    56
            //Get the name of this object attribute
StephaneLenclud@238
    57
            get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name; }
StephaneLenclud@238
    58
            private set { }
StephaneLenclud@238
    59
        }
StephaneLenclud@238
    60
StephaneLenclud@239
    61
        /// <summary>
StephaneLenclud@239
    62
        /// Static object description.
StephaneLenclud@239
    63
        /// </summary>
StephaneLenclud@238
    64
        public string Description
StephaneLenclud@238
    65
        {
StephaneLenclud@238
    66
            //Get the description of this object attribute
StephaneLenclud@238
    67
            get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Description; }
StephaneLenclud@238
    68
            private set { }
StephaneLenclud@238
    69
        }
StephaneLenclud@238
    70
StephaneLenclud@239
    71
        /// <summary>
StephaneLenclud@239
    72
        /// Dynamic object description.
StephaneLenclud@239
    73
        /// </summary>
StephaneLenclud@239
    74
        /// <returns></returns>
StephaneLenclud@238
    75
        public virtual string Brief()
StephaneLenclud@238
    76
        {
StephaneLenclud@238
    77
            return Name;
StephaneLenclud@238
    78
        }
StephaneLenclud@238
    79
StephaneLenclud@239
    80
        /// <summary>
StephaneLenclud@239
    81
        /// Needed to make sure our sorting makes sense
StephaneLenclud@239
    82
        /// </summary>
StephaneLenclud@239
    83
        /// <param name="obj"></param>
StephaneLenclud@239
    84
        /// <returns></returns>
StephaneLenclud@238
    85
        public int CompareTo(object obj)
StephaneLenclud@238
    86
        {
StephaneLenclud@238
    87
            //Sort by object name
StephaneLenclud@238
    88
            return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name.CompareTo(obj.GetType());
StephaneLenclud@238
    89
        }
StephaneLenclud@238
    90
StephaneLenclud@238
    91
        /// <summary>
StephaneLenclud@239
    92
        /// Tells whether the current object configuration is valid.
StephaneLenclud@239
    93
        /// </summary>
StephaneLenclud@239
    94
        /// <returns></returns>
StephaneLenclud@239
    95
        public virtual bool IsValid()
StephaneLenclud@239
    96
        {
StephaneLenclud@239
    97
            return true;
StephaneLenclud@239
    98
        }
StephaneLenclud@239
    99
StephaneLenclud@239
   100
        /// <summary>
StephaneLenclud@238
   101
        /// So that data contract knows all our types.
StephaneLenclud@238
   102
        /// </summary>
StephaneLenclud@238
   103
        /// <returns></returns>
StephaneLenclud@238
   104
        private static IEnumerable<Type> DerivedTypes()
StephaneLenclud@238
   105
        {
StephaneLenclud@238
   106
            return SharpLib.Utils.Reflection.GetDerivedTypes<Object>();
StephaneLenclud@238
   107
        }
StephaneLenclud@238
   108
StephaneLenclud@244
   109
        /// <summary>
StephaneLenclud@244
   110
        /// 
StephaneLenclud@244
   111
        /// </summary>
StephaneLenclud@244
   112
        public event PropertyChangedEventHandler PropertyChanged;
StephaneLenclud@244
   113
StephaneLenclud@244
   114
        /// <summary>
StephaneLenclud@244
   115
        /// Invoke our event.
StephaneLenclud@244
   116
        /// </summary>
StephaneLenclud@244
   117
        /// <param name="name"></param>
StephaneLenclud@244
   118
        protected void OnPropertyChanged(string name)
StephaneLenclud@244
   119
        {
StephaneLenclud@244
   120
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
StephaneLenclud@244
   121
        }
StephaneLenclud@244
   122
StephaneLenclud@244
   123
StephaneLenclud@238
   124
    }
StephaneLenclud@238
   125
}