unit.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef _PCILIB_UNIT_H
  2. #define _PCILIB_UNIT_H
  3. #include <uthash.h>
  4. #include <pcilib.h>
  5. #define PCILIB_UNIT_INVALID ((pcilib_unit_t)-1)
  6. #define PCILIB_UNIT_TRANSFORM_INVALID ((pcilib_unit_transform_t)-1)
  7. #define PCILIB_MAX_TRANSFORMS_PER_UNIT 16 /**< Limits number of supported transforms per unit */
  8. typedef struct pcilib_unit_context_s pcilib_unit_context_t;
  9. /**
  10. * unit transformation routines
  11. */
  12. typedef struct {
  13. char *unit; /**< Name of the resulting unit */
  14. char *transform; /**< String, similar to view formula, explaining transform to this unit */
  15. } pcilib_unit_transform_t;
  16. typedef struct {
  17. char *name; /**< Unit name */
  18. pcilib_unit_transform_t transforms[PCILIB_MAX_TRANSFORMS_PER_UNIT + 1]; /**< Transforms to other units */
  19. } pcilib_unit_description_t;
  20. struct pcilib_unit_context_s {
  21. const char *name;
  22. pcilib_unit_t unit;
  23. UT_hash_handle hh;
  24. };
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /**
  29. * Use this function to add new unit definitions into the model. It is error to re-register
  30. * already registered unit. The function will copy the context of unit description, but name,
  31. * transform, and other strings in the structure are considered to have static duration
  32. * and will not be copied. On error no new units are initalized.
  33. * @param[in,out] ctx - pcilib context
  34. * @param[in] n - number of units to initialize. It is OK to pass 0 if protocols variable is NULL terminated (last member of protocols array have all members set to 0)
  35. * @param[in] desc - unit descriptions
  36. * @return - error or 0 on success
  37. */
  38. int pcilib_add_units(pcilib_t *ctx, size_t n, const pcilib_unit_description_t *desc);
  39. /**
  40. * Destroys data associated with units. This is an internal function and will
  41. * be called during clean-up.
  42. * @param[in,out] ctx - pcilib context
  43. * @param[in] start - specifies first unit to clean (used to clean only part of the units to keep the defined state if pcilib_add_units has failed)
  44. */
  45. void pcilib_clean_units(pcilib_t *ctx, pcilib_unit_t start);
  46. /**
  47. * Find unit id using supplied name.
  48. * @param[in] ctx - pcilib context
  49. * @param[in] unit - the requested unit name
  50. * @return - unit id or PCILIB_UNIT_INVALID if error not found or error has occured
  51. */
  52. pcilib_unit_t pcilib_find_unit_by_name(pcilib_t *ctx, const char *unit);
  53. /**
  54. * Find the required transform between two specified units.
  55. * @param[in] ctx - pcilib context
  56. * @param[in] from - the source unit
  57. * @param[in] to - destination unit
  58. * @return - the pointer to unit transform or NULL in case of error. If no transform required (i.e. \a from = \a to),
  59. the function will return #pcilib_unit_transform_t structure with \a transform field set to NULL.
  60. */
  61. pcilib_unit_transform_t *pcilib_find_transform_by_unit_names(pcilib_t *ctx, const char *from, const char *to);
  62. /**
  63. * Converts value to the requested units. It is error to convert values with unspecified units.
  64. * This is internal function, use pcilib_value_convert_value_unit instead.
  65. * @param[in,out] ctx - pcilib context
  66. * @param[in] trans - the requested unit transform
  67. * @param[in,out] value - the value to be converted (changed on success)
  68. * @return - error or 0 on success
  69. */
  70. int pcilib_transform_unit(pcilib_t *ctx, const pcilib_unit_transform_t *trans, pcilib_value_t *value);
  71. /**
  72. * Converts value to the requested units. It is error to convert values with unspecified units.
  73. * This is internal function, use pcilib_value_convert_value_unit instead.
  74. * @param[in,out] ctx - pcilib context
  75. * @param[in] to - specifies the requested unit of the value
  76. * @param[in,out] value - the value to be converted (changed on success)
  77. * @return - error or 0 on success
  78. */
  79. int pcilib_transform_unit_by_name(pcilib_t *ctx, const char *to, pcilib_value_t *value);
  80. #ifdef __cplusplus
  81. }
  82. #endif
  83. #endif /* _PCILIB_UNIT_H */