-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blob.hpp
118 lines (87 loc) · 2.98 KB
/
Blob.hpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef _MINILIB_BLOB_HPP
# define _MINILIB_BLOB_HPP
extern "C" {
#include "mltypes.h"
#include "blob.h"
}
#include <stdexcept>
namespace ml {
template <class T>
struct BlobSet
{
blobset *bs;
BlobSet(ub1_t hashsize, T defaultdata)
{
bs = (blobset *)blob_new(hashsize, defaultdata);
if (!bs) {
throw std::bad_alloc();
}
}
~BlobSet() {
blob_destroy(bs);
}
inline void add(const ub1_t *s, ub2_t len, T dat, void (*destruct)(void*)) {
if (blob_register(bs, s, len, dat, destruct))
throw std::bad_alloc();
}
inline void add(const ub1_t *s, ub2_t len, T dat)
{ add((const ub1_t*)s, len, dat, 0); }
inline void add(const char *s, ub2_t len, T dat)
{ add((const ub1_t*)s, len, dat); }
inline int add_nothrow(const ub1_t *s, ub2_t len, T dat, void (*destruct)(void*)) {
return blob_register(bs, s, len, dat, destruct);
}
inline int add_nothrow(const ub1_t *s, ub2_t len, T dat)
{ return add_nothrow((const ub1_t*)s, len, dat, 0); }
inline int add_nothrow(const char *s, ub2_t len, T dat)
{ return add_nothrow((const ub1_t*)s, len, dat); }
inline int remove(const ub1_t *s, ub2_t len)
{ return blob_unregister(bs, s, len); }
inline int remove(const char *s, ub2_t len)
{ return remove((const ub1_t *)s, len); }
inline T pop(const ub1_t *s, ub2_t len)
{
T dat;
blobcursor bc;
blob_cursor_init(&bc, bs);
if ( !(dat = (T)blob_cursor_get(&bc, s, len)) )
return (T)0;
blob_cursor_unregister(&bc);
return dat;
}
inline T pop(const char *s, ub2_t len)
{ return pop((const ub1_t *)s, len); }
inline T get(const ub1_t *s, ub2_t len)
{ return (T)blob_get(bs, s, len); }
inline T get(const char *s, ub2_t len)
{ return get((const ub1_t*)s, len); }
inline void empty()
{ blob_empty(bs); }
};
template <class T>
struct BlobCursor : blobcursor {
BlobCursor(BlobSet<T> *bsp)
{
blob_cursor_init(this, bsp->bs);
}
inline void reset()
{ blob_cursor_init(this, bs); }
inline T get(const ub1_t *s, ub2_t len)
{ return (T)blob_cursor_get(this, s, len); }
inline T get(const char *s, ub2_t len)
{ return get((const ub1_t*)s, len); }
inline T value()
{ return (T)blob_cursor_value(this); }
inline ub1_t find_first(const ub1_t *name, ub2_t namesize, T *data_out)
{ return blob_cursor_find_first(this, name, namesize, data_out); }
inline ub1_t find_first(const char *name, ub2_t namesize, T *data_out)
{ return find_first((const ub1_t*)name, namesize, data_out); }
inline ub1_t next(T *data_out, ub1_t **name_out, ub2_t *namesize_out)
{ return blob_cursor_next(this, data_out, name_out, namesize_out); }
inline ub1_t remove()
{ return blob_cursor_unregister(this); }
inline void switcheroo(T data)
{ blob_cursor_switcheroo(this, data); }
};
} // namespace ml
#endif /* _MINILIB_BLOB_HPP */