TmplMould.tlh 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. #pragma once
  3. #include <assert.h>
  4. #include <vector>
  5. #include <functional>
  6. //-----------------------------------------------------------------------------
  7. // TmplMould
  8. //-----------------------------------------------------------------------------
  9. namespace DIOS::Dev::Detail
  10. {
  11. template <typename T>
  12. class TmplMould
  13. {
  14. public:
  15. TmplMould (T init, T min, T max, T accuracy)
  16. {
  17. m_Value = init;
  18. m_LimitMin = min;
  19. m_LimitMax = max;
  20. m_Accuracy = accuracy;
  21. assert (m_LimitMin < m_LimitMax);
  22. }
  23. public:
  24. bool CanInc () const; // 能加 1 吗
  25. bool CanDec () const; // 能减 1 吗
  26. bool Verify (T value) const; // 值在界限内吗
  27. bool Inc (); // 加 1
  28. bool Dec (); // 减 1
  29. bool Update (T value); // 更新为指定值
  30. bool UpdateLimitMax(T value); // 更新为指定值
  31. bool UpdateLimitMin(T value); // 更新为指定值
  32. T Get () const; // 获得当前值
  33. T GetLimitMax() const;
  34. T GetLimitMin() const;
  35. bool ToNext (const std::vector <typename T> & ar);
  36. bool ToPrev (const std::vector <typename T> & ar);
  37. bool ToUpdate (T value, const std::vector <typename T> & ar);
  38. //change by wxx for 万东:丰富基类方法(要求ar按从小到大排列) 20230803
  39. bool CanToNext(T & value, const std::vector <typename T>& ar);
  40. bool CanToPrev(T & value, const std::vector <typename T>& ar);
  41. bool CanToUpdate(T & value, const std::vector <typename T>& ar);
  42. protected:
  43. T m_LimitMin;
  44. T m_LimitMax;
  45. T m_Accuracy;
  46. T m_Value;
  47. };
  48. template <typename T>
  49. class TmplMouldWithWarn : public TmplMould <T>
  50. {
  51. protected:
  52. T m_WarningMin;
  53. T m_WarningMax;
  54. T m_CalibWarningMin;
  55. T m_CalibWarningMax;
  56. T m_ErrorMin;
  57. T m_ErrorMax;
  58. public:
  59. std::function <void ()> m_OnWarningMin;
  60. std::function <void ()> m_OnWarningMax;
  61. std::function <void ()> m_OnCalibWarningMin;
  62. std::function <void ()> m_OnCalibWarningMax;
  63. std::function <void ()> m_OnErrorMin;
  64. std::function <void ()> m_OnErrorMax;
  65. public:
  66. using super = TmplMould;
  67. TmplMouldWithWarn(T init, T min, T WarnMin, T WarnMax, T CalibWarnMin, T CalibWarnMax, T max, T accuracy)
  68. :super(init, min, max, accuracy)
  69. {
  70. m_WarningMin = WarnMin;
  71. m_WarningMax = WarnMax;
  72. m_CalibWarningMin = CalibWarnMin;
  73. m_CalibWarningMax = CalibWarnMax;
  74. m_ErrorMin = min;
  75. m_ErrorMax = max;
  76. assert(m_CalibWarningMin < m_CalibWarningMax);
  77. assert(m_ErrorMin < m_ErrorMax);
  78. assert(m_ErrorMin < m_ErrorMax);
  79. }
  80. };
  81. }