summaryrefslogtreecommitdiffstats
path: root/pcilib/unit.h
blob: 2351f26f6f33cdf8698df4c307a6b5b061dcbfbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef _PCILIB_UNIT_H
#define _PCILIB_UNIT_H

#include <uthash.h>

#include <pcilib.h>

#define PCILIB_UNIT_INVALID ((pcilib_unit_t)-1)
#define PCILIB_UNIT_TRANSFORM_INVALID ((pcilib_unit_transform_t)-1)

#define PCILIB_MAX_TRANSFORMS_PER_UNIT 16			/**< Limits number of supported transforms per unit */

typedef struct pcilib_unit_context_s pcilib_unit_context_t;

/**
 * unit transformation routines
 */
typedef struct {
    char *unit;								                /**< Name of the resulting unit */
    char *transform;							                /**< String, similar to view formula, explaining transform to this unit */
} pcilib_unit_transform_t;

typedef struct {
    char *name;										/**< Unit name */
    pcilib_unit_transform_t transforms[PCILIB_MAX_TRANSFORMS_PER_UNIT + 1];		/**< Transforms to other units */
} pcilib_unit_description_t;

struct pcilib_unit_context_s {
    const char *name;
    pcilib_unit_t unit;
    UT_hash_handle hh;
};

#ifdef __cplusplus
extern "C" {
#endif

/**
 * Use this function to add new unit definitions into the model. It is error to re-register
 * already registered unit. The function will copy the context of unit description, but name, 
 * transform, and other strings in the structure are considered to have static duration 
 * and will not be copied. On error no new units are initalized.
 * @param[in,out] ctx - pcilib context
 * @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)
 * @param[in] desc - unit descriptions
 * @return - error or 0 on success
 */
int pcilib_add_units(pcilib_t *ctx, size_t n, const pcilib_unit_description_t *desc);

/**
 * Destroys data associated with units. This is an internal function and will
 * be called during clean-up.
 * @param[in,out] ctx - pcilib context
 * @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)
 */
void pcilib_clean_units(pcilib_t *ctx, pcilib_unit_t start);

pcilib_unit_t pcilib_find_unit_by_name(pcilib_t *ctx, const char *unit);
pcilib_unit_transform_t *pcilib_find_transform_by_unit_names(pcilib_t *ctx, const char *from, const char *to);

/**
 * Converts value to the requested units. It is error to convert values with unspecified units.
 * This is internal function, use pcilib_value_convert_value_unit instead.
 * @param[in,out] ctx - pcilib context
 * @param[in] trans - the requested unit transform 
 * @param[in,out] value - the value to be converted (changed on success)
 * @return - error or 0 on success
 */
int pcilib_transform_unit(pcilib_t *ctx, const pcilib_unit_transform_t *trans, pcilib_value_t *value);

/**
 * Converts value to the requested units. It is error to convert values with unspecified units.
 * This is internal function, use pcilib_value_convert_value_unit instead.
 * @param[in,out] ctx - pcilib context
 * @param[in] name - specifies the requested unit of the value
 * @param[in,out] value - the value to be converted (changed on success)
 * @return - error or 0 on success
 */
int pcilib_transform_unit_by_name(pcilib_t *ctx, const char *to, pcilib_value_t *value);


#ifdef __cplusplus
}
#endif


#endif /* _PCILIB_UNIT_H */