[d5070ef] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Sean Bartell
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup bithenge
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * Transforms.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #ifndef BITHENGE_TRANSFORM_H_
|
---|
| 38 | #define BITHENGE_TRANSFORM_H_
|
---|
| 39 |
|
---|
| 40 | #include "tree.h"
|
---|
| 41 |
|
---|
| 42 | /** A transform that creates a new tree from an old tree. */
|
---|
| 43 | typedef struct {
|
---|
| 44 | /** @privatesection */
|
---|
| 45 | const struct bithenge_transform_ops_t *ops;
|
---|
| 46 | unsigned int refs;
|
---|
| 47 | } bithenge_transform_t;
|
---|
| 48 |
|
---|
| 49 | /** Operations that may be provided by a transform. */
|
---|
| 50 | typedef struct bithenge_transform_ops_t {
|
---|
| 51 | /** @copydoc bithenge_transform_t::bithenge_transform_apply */
|
---|
| 52 | int (*apply)(bithenge_transform_t *xform, bithenge_node_t *in, bithenge_node_t **out);
|
---|
| 53 | /** @copydoc bithenge_transform_t::bithenge_transform_prefix_length */
|
---|
| 54 | int (*prefix_length)(bithenge_transform_t *xform, bithenge_blob_t *blob, aoff64_t *out);
|
---|
| 55 | /** Destroy the transform.
|
---|
| 56 | * @param blob The transform.
|
---|
| 57 | * @return EOK on success or an error code from errno.h. */
|
---|
| 58 | int (*destroy)(bithenge_transform_t *xform);
|
---|
| 59 | } bithenge_transform_ops_t;
|
---|
| 60 |
|
---|
| 61 | /** Apply a transform.
|
---|
| 62 | * @memberof bithenge_transform_t
|
---|
| 63 | * @param xform The transform.
|
---|
| 64 | * @param in The input tree.
|
---|
| 65 | * @param[out] out Where the output tree will be stored.
|
---|
| 66 | * @return EOK on success or an error code from errno.h. */
|
---|
| 67 | static inline int bithenge_transform_apply(bithenge_transform_t *xform,
|
---|
| 68 | bithenge_node_t *in, bithenge_node_t **out)
|
---|
| 69 | {
|
---|
| 70 | assert(xform);
|
---|
| 71 | assert(xform->ops);
|
---|
| 72 | return xform->ops->apply(xform, in, out);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | /** Find the length of the prefix of a blob this transform can use as input. In
|
---|
| 76 | * other words, figure out how many bytes this transform will use up. This
|
---|
| 77 | * method is optional and can return an error, but it is required for struct
|
---|
| 78 | * subtransforms.
|
---|
| 79 | * @memberof bithenge_transform_t
|
---|
| 80 | * @param xform The transform.
|
---|
| 81 | * @param blob The blob.
|
---|
| 82 | * @param[out] out Where the prefix length will be stored.
|
---|
| 83 | * @return EOK on success, ENOTSUP if not supported, or another error code from
|
---|
| 84 | * errno.h. */
|
---|
| 85 | static inline int bithenge_transform_prefix_length(bithenge_transform_t *xform,
|
---|
| 86 | bithenge_blob_t *blob, aoff64_t *out)
|
---|
| 87 | {
|
---|
| 88 | assert(xform);
|
---|
| 89 | assert(xform->ops);
|
---|
| 90 | if (!xform->ops->prefix_length)
|
---|
| 91 | return ENOTSUP;
|
---|
| 92 | return xform->ops->prefix_length(xform, blob, out);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /** Increment a transform's reference count.
|
---|
| 96 | * @param xform The transform to reference.
|
---|
| 97 | * @return EOK on success or an error code from errno.h. */
|
---|
| 98 | static inline int bithenge_transform_inc_ref(bithenge_transform_t *xform)
|
---|
| 99 | {
|
---|
| 100 | assert(xform);
|
---|
| 101 | xform->refs++;
|
---|
| 102 | return EOK;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /** Decrement a transform's reference count.
|
---|
| 106 | * @param xform The transform to dereference.
|
---|
| 107 | * @return EOK on success or an error code from errno.h. */
|
---|
| 108 | static inline int bithenge_transform_dec_ref(bithenge_transform_t *xform)
|
---|
| 109 | {
|
---|
| 110 | assert(xform);
|
---|
| 111 | assert(xform->ops);
|
---|
| 112 | if (--xform->refs == 0)
|
---|
| 113 | return xform->ops->destroy(xform);
|
---|
| 114 | return EOK;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | int bithenge_uint32le_transform(bithenge_transform_t **out);
|
---|
| 118 |
|
---|
| 119 | #endif
|
---|
| 120 |
|
---|
| 121 | /** @}
|
---|
| 122 | */
|
---|