Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 5a7c0e6 in mainline for uspace/app/bithenge/transform.h


Ignore:
Timestamp:
2012-07-27T01:09:05Z (10 years ago)
Author:
Sean Bartell <wingedtachikoma@…>
Branches:
lfn, master
Children:
6e34bd0
Parents:
43788b2
Message:

Bithenge: rename transform parameters "scopes" and add room for parameters

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bithenge/transform.h

    r43788b2 r5a7c0e6  
    5151typedef struct {
    5252        /** @privatesection */
    53 } bithenge_transform_context_t;
     53        bithenge_node_t **params;
     54        int num_params;
     55} bithenge_scope_t;
    5456
    5557/** Operations that may be provided by a transform. */
    5658typedef struct bithenge_transform_ops {
    5759        /** @copydoc bithenge_transform_t::bithenge_transform_apply */
    58         int (*apply)(bithenge_transform_t *self,
    59             bithenge_transform_context_t *context, bithenge_node_t *in,
    60             bithenge_node_t **out);
     60        int (*apply)(bithenge_transform_t *self, bithenge_scope_t *scope,
     61            bithenge_node_t *in, bithenge_node_t **out);
    6162        /** @copydoc bithenge_transform_t::bithenge_transform_prefix_length */
    6263        int (*prefix_length)(bithenge_transform_t *self,
    63             bithenge_transform_context_t *context, bithenge_blob_t *blob,
    64             aoff64_t *out);
     64            bithenge_scope_t *scope, bithenge_blob_t *blob, aoff64_t *out);
    6565        /** Destroy the transform.
    6666         * @param self The transform. */
    6767        void (*destroy)(bithenge_transform_t *self);
     68        /** The number of parameters required. */
     69        int num_params;
    6870} bithenge_transform_ops_t;
    6971
    70 /** Initialize a transform context. It must be destroyed with @a
    71  * bithenge_transform_context_destroy after it is used.
    72  * @param[out] context The context to initialize. */
    73 static inline void bithenge_transform_context_init(
    74     bithenge_transform_context_t *context)
    75 {
    76 }
    77 
    78 /** Destroy a transform context.
    79  * @param context The context to destroy.
    80  * @return EOK on success or an error code from errno.h. */
    81 static inline void bithenge_transform_context_destroy(
    82     bithenge_transform_context_t *context)
    83 {
     72/** Initialize a transform scope. It must be destroyed with @a
     73 * bithenge_scope_destroy after it is used.
     74 * @param[out] scope The scope to initialize. */
     75static inline void bithenge_scope_init(bithenge_scope_t *scope)
     76{
     77        scope->params = NULL;
     78        scope->num_params = 0;
     79}
     80
     81/** Destroy a transform scope.
     82 * @param scope The scope to destroy.
     83 * @return EOK on success or an error code from errno.h. */
     84static inline void bithenge_scope_destroy(bithenge_scope_t *scope)
     85{
     86        for (int i = 0; i < scope->num_params; i++)
     87                bithenge_node_dec_ref(scope->params[i]);
     88        free(scope->params);
     89}
     90
     91/** Allocate parameters. The parameters must then be set with @a
     92 * bithenge_scope_set_param.
     93 * @param scope The scope in which to allocate parameters.
     94 * @param num_params The number of parameters to allocate.
     95 * @return EOK on success or an error code from errno.h. */
     96static inline int bithenge_scope_alloc_params(bithenge_scope_t *scope,
     97    int num_params)
     98{
     99        scope->params = malloc(sizeof(*scope->params) * num_params);
     100        if (!scope->params)
     101                return ENOMEM;
     102        scope->num_params = num_params;
     103        return EOK;
     104}
     105
     106/** Set a parameter. Takes a reference to @a value. Note that range checking is
     107 * not done in release builds.
     108 * @param scope The scope in which to allocate parameters.
     109 * @param i The index of the parameter to set.
     110 * @param value The value to store in the parameter.
     111 * @return EOK on success or an error code from errno.h. */
     112static inline int bithenge_scope_set_param( bithenge_scope_t *scope, int i,
     113    bithenge_node_t *node)
     114{
     115        assert(scope);
     116        assert(i >= 0 && i < scope->num_params);
     117        scope->params[i] = node;
     118        return EOK;
     119}
     120
     121/** Get a parameter. Note that range checking is not done in release builds.
     122 * @param scope The scope to get the parameter from.
     123 * @param i The index of the parameter to set.
     124 * @param[out] out Stores a new reference to the parameter.
     125 * @return EOK on success or an error code from errno.h. */
     126static inline int bithenge_scope_get_param(bithenge_scope_t *scope, int i,
     127    bithenge_node_t **out)
     128{
     129        assert(scope);
     130        assert(i >= 0 && i < scope->num_params);
     131        *out = scope->params[i];
     132        bithenge_node_inc_ref(*out);
     133        return EOK;
     134}
     135
     136/** Get the number of parameters required by a transform. Takes ownership of
     137 * nothing.
     138 * @param self The transform.
     139 * @return The number of parameters required. */
     140static inline int bithenge_transform_num_params(bithenge_transform_t *self)
     141{
     142        assert(self);
     143        assert(self->ops);
     144        return self->ops->num_params;
    84145}
    85146
     
    87148 * @memberof bithenge_transform_t
    88149 * @param self The transform.
    89  * @param context The context.
     150 * @param scope The scope.
    90151 * @param in The input tree.
    91152 * @param[out] out Where the output tree will be stored.
    92153 * @return EOK on success or an error code from errno.h. */
    93154static inline int bithenge_transform_apply(bithenge_transform_t *self,
    94     bithenge_transform_context_t *context, bithenge_node_t *in,
    95     bithenge_node_t **out)
    96 {
    97         assert(self);
    98         assert(self->ops);
    99         return self->ops->apply(self, context, in, out);
     155    bithenge_scope_t *scope, bithenge_node_t *in, bithenge_node_t **out)
     156{
     157        assert(self);
     158        assert(self->ops);
     159        return self->ops->apply(self, scope, in, out);
    100160}
    101161
     
    106166 * @memberof bithenge_transform_t
    107167 * @param self The transform.
    108  * @param context The context.
     168 * @param scope The scope.
    109169 * @param blob The blob.
    110170 * @param[out] out Where the prefix length will be stored.
     
    112172 * errno.h. */
    113173static inline int bithenge_transform_prefix_length(bithenge_transform_t *self,
    114     bithenge_transform_context_t *context, bithenge_blob_t *blob,
    115     aoff64_t *out)
     174    bithenge_scope_t *scope, bithenge_blob_t *blob, aoff64_t *out)
    116175{
    117176        assert(self);
     
    119178        if (!self->ops->prefix_length)
    120179                return ENOTSUP;
    121         return self->ops->prefix_length(self, context, blob, out);
     180        return self->ops->prefix_length(self, scope, blob, out);
    122181}
    123182
Note: See TracChangeset for help on using the changeset viewer.