version.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*****************************************************************************
  2. *
  3. * version - simple version handling functions for major.minor version
  4. * types
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. ****************************************************************************/
  25. #ifndef CCAN_VERSION_H
  26. #define CCAN_VERSION_H
  27. #include <stdint.h>
  28. struct version {
  29. uint32_t _v; /* major << 16 | minor */
  30. };
  31. /**
  32. * version_major - return the major version of the given struct
  33. * @v: the version number to obtain the major number from
  34. */
  35. static inline uint16_t version_major(struct version v) {
  36. return (v._v & 0xFFFF0000) >> 16;
  37. }
  38. /**
  39. * version_minor - return the minor version of the given struct
  40. * @v: the version number to obtain the minor number from
  41. */
  42. static inline uint16_t version_minor(const struct version v) {
  43. return v._v & 0xFFFF;
  44. }
  45. /**
  46. * version - create a new version number
  47. * @major: major version number
  48. * @minor: minor version number
  49. */
  50. static inline struct version version(uint16_t major, uint16_t minor)
  51. {
  52. struct version v = { ._v = major << 16 | minor };
  53. return v;
  54. }
  55. /**
  56. * version_cmp - compare two versions
  57. * @a: the first version number
  58. * @b: the second version number
  59. * @return a number greater, equal, or less than 0 if a is greater, equal or
  60. * less than b, respectively
  61. *
  62. * Example:
  63. * struct version a = version(1, 0);
  64. * struct version b = version(1, 3);
  65. * if (version_cmp(a, b) < 0)
  66. * printf("b is smaller than b\n");
  67. */
  68. static inline int version_cmp(struct version a, struct version b)
  69. {
  70. return (a._v == b._v) ? 0 : (a._v > b._v) ? 1 : - 1;
  71. }
  72. #endif /* CCAN_VERSION_H */