Micro template library A library for building device drivers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

98 lines
2.9 KiB

  1. /*!
  2. * \file /utl/com/_1wire_id.h
  3. * \brief An 1-wire Rom ID type
  4. *
  5. * Copyright (C) 2018 Christos Choutouridis
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #ifndef __utl_com_1wire_id_h__
  22. #define __utl_com_1wire_id_h__
  23. #include <utl/core/impl.h>
  24. #include <utl/container/id.h>
  25. namespace utl {
  26. /*!
  27. * 1-wire Rom-ID type
  28. */
  29. class _1wire_id_t : public id_t<uint8_t, 8> {
  30. /*!
  31. * \name Constructors
  32. */
  33. //!@{
  34. public:
  35. _1wire_id_t () noexcept
  36. : id_t<uint8_t, 8> {0, 0, 0, 0, 0, 0, 0, 0} { }
  37. constexpr _1wire_id_t (const _1wire_id_t& id) noexcept {
  38. std::copy(id.begin(), id.end(), this->begin());
  39. }
  40. _1wire_id_t operator=(const _1wire_id_t& id) noexcept {
  41. _1wire_id_t r;
  42. std::copy(id.begin(), id.end(), r.begin());
  43. return r;
  44. }
  45. //!@}
  46. /*!
  47. * \name User functionality provided by the interface
  48. */
  49. //!@{
  50. //! Return nullDev reference
  51. static constexpr const _1wire_id_t& nullDev () { return nullDev_; }
  52. //!@{
  53. //! Return the RomID family code (The LSB)
  54. uint8_t& family() noexcept { return front(); }
  55. const uint8_t& family() const noexcept { return front(); }
  56. //!@}
  57. //!@{
  58. //! Access the CRC8 byte (The MSB).
  59. uint8_t& crc8() noexcept { return back(); }
  60. const uint8_t& crc8() const noexcept { return back(); }
  61. //!@}
  62. /*!
  63. * \brief
  64. * Full compare two dev_ids
  65. * \return The comparison result
  66. * \arg 0 dev_ids are equal
  67. * \arg -1 dev_id lhs is smaller than dev_id rhs
  68. * \arg 1 dev_id rhs is smaller than dev_id lhs
  69. */
  70. static int compare (const _1wire_id_t& lhs, const _1wire_id_t& rhs) noexcept {
  71. auto p = std::mismatch (lhs.rbegin(), lhs.rend(), rhs.rbegin());
  72. if (p.first == lhs.rend()) return 0;
  73. else if (*p.first < *p.second) return -1;
  74. else return 1;
  75. }
  76. //!@{
  77. //!@{
  78. //! Self referenced special null Rom_ID object as static
  79. private:
  80. static const _1wire_id_t nullDev_;
  81. //!@}
  82. };
  83. // Init static member
  84. const _1wire_id_t _1wire_id_t::nullDev_ { };
  85. } // namespace tbx
  86. #endif /* __utl_com_1wire_id_h__ */