// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using UnityEngine; namespace Microsoft.MixedReality.Toolkit { /// /// The base service implements and provides default properties for all services. /// public abstract class BaseService : IMixedRealityService, IMixedRealityServiceState { public const uint DefaultPriority = 10; public BaseService() { typeName = new[] { GetType().ToString() }; } #region IMixedRealityService Implementation /// public virtual string Name { get; protected set; } /// public virtual uint Priority { get; protected set; } = DefaultPriority; /// public virtual BaseMixedRealityProfile ConfigurationProfile { get; protected set; } = null; /// public virtual void Initialize() { IsInitialized = true; } /// public virtual void Reset() { IsInitialized = false; } /// public virtual void Enable() { IsEnabled = true; } /// public virtual void Update() { } /// public virtual void LateUpdate() { } /// public virtual void Disable() { IsEnabled = false; } /// public virtual void Destroy() { IsInitialized = false; IsEnabled = false; IsMarkedDestroyed = true; } #endregion IMixedRealityService Implementation #region IMixedRealityServiceState Implementation private bool? isInitialized = null; private readonly string[] typeName = null; private const string IsInitializedAssert = "{0} has not set a value for IsInitialized; returning false."; private const string IsEnabledAssert = "{0} has not set a value for IsEnabled; returning false."; private const string IsMarkedDestroyedAssert = "{0} has not set a value for IsMarkedDestroyed; returning false."; /// public virtual bool IsInitialized { get { Debug.AssertFormat(isInitialized.HasValue, IsInitializedAssert, typeName); return isInitialized ?? false; } protected set => isInitialized = value; } private bool? isEnabled = null; /// public virtual bool IsEnabled { get { Debug.AssertFormat(isEnabled.HasValue, IsEnabledAssert, typeName); return isEnabled ?? false; } protected set => isEnabled = value; } private bool? isMarkedDestroyed = null; /// public virtual bool IsMarkedDestroyed { get { Debug.AssertFormat(isMarkedDestroyed.HasValue, IsMarkedDestroyedAssert, typeName); return isMarkedDestroyed ?? false; } protected set => isMarkedDestroyed = value; } #endregion IMixedRealityServiceState Implementation #region IDisposable Implementation /// /// Value indicating if the object has completed disposal. /// /// /// Set by derived classes to indicate that disposal has been completed. /// protected bool disposed = false; /// /// Finalizer /// ~BaseService() { Dispose(); } /// /// Cleanup resources used by this object. /// public void Dispose() { // Clean up our resources (managed and unmanaged resources) Dispose(true); // Suppress finalization as the finalizer also calls our cleanup code. GC.SuppressFinalize(this); } /// /// Cleanup resources used by the object /// /// Are we fully disposing the object? /// True will release all managed resources, unmanaged resources are always released. /// protected virtual void Dispose(bool disposing) { } #endregion IDisposable Implementation } }