IntegratedSubsystemDescriptor.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System;
  12. using System.Collections.Generic;
  13. /// <summary>
  14. /// Information about a subsystem that can be queried before creating a subsystem instance.
  15. /// </summary>
  16. public class IntegratedSubsystemDescriptor : ISubsystemDescriptor
  17. {
  18. protected IntegratedSubsystemDescriptor() { }
  19. /// <summary>
  20. /// A unique string that identifies the subsystem that this Descriptor can create.
  21. /// </summary>
  22. public virtual string id { get; }
  23. public ISubsystem subsystem { get; protected set; }
  24. public virtual ISubsystem Create() => null;
  25. }
  26. public class IntegratedSubsystemDescriptor<TSubsystem> : IntegratedSubsystemDescriptor where TSubsystem : IntegratedSubsystem
  27. {
  28. protected static Dictionary<string, TSubsystem> m_SubsystemDict = new Dictionary<string, TSubsystem>();
  29. public IntegratedSubsystemDescriptor() { }
  30. public virtual new TSubsystem Create()
  31. {
  32. if (!m_SubsystemDict.ContainsKey(id))
  33. {
  34. try
  35. {
  36. subsystem = (TSubsystem)Activator.CreateInstance(typeof(TSubsystem), this);
  37. m_SubsystemDict.Add(id, (TSubsystem)subsystem);
  38. }
  39. catch (Exception e)
  40. {
  41. NRDebugger.Error("Get the instance of Class({0}) faild.", typeof(TSubsystem).FullName);
  42. throw e;
  43. }
  44. }
  45. return m_SubsystemDict[id];
  46. }
  47. }
  48. }