Use bool instead of bool:1 if your compiler doesn't create correct code
[awesomized/libmemcached] / libhashkit / hashkit.h
1 /* HashKit
2 * Copyright (C) 2009-2010 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 */
8
9 #ifndef HASHKIT_H
10 #define HASHKIT_H
11
12
13 #if !defined(__cplusplus)
14 # include <stdbool.h>
15 #endif
16 #include <inttypes.h>
17 #include <sys/types.h>
18 #include <libhashkit/visibility.h>
19 #include <libhashkit/configure.h>
20 #include <libhashkit/types.h>
21 #include <libhashkit/algorithm.h>
22 #include <libhashkit/behavior.h>
23 #include <libhashkit/digest.h>
24 #include <libhashkit/function.h>
25 #include <libhashkit/strerror.h>
26
27 #ifdef __cplusplus
28
29 #include <string>
30
31 extern "C" {
32 #endif
33
34 HASHKIT_API
35 hashkit_st *hashkit_create(hashkit_st *hash);
36
37 HASHKIT_API
38 hashkit_st *hashkit_clone(hashkit_st *destination, const hashkit_st *ptr);
39
40 HASHKIT_API
41 bool hashkit_compare(const hashkit_st *first, const hashkit_st *second);
42
43 HASHKIT_API
44 void hashkit_free(hashkit_st *hash);
45
46 #define hashkit_is_allocated(__object) ((__object)->options.is_allocated)
47 #define hashkit_is_initialized(__object) ((__object)->options.is_initialized)
48
49 #ifdef __cplusplus
50 } // extern "C"
51 #endif
52
53 struct hashkit_st
54 {
55 struct hashkit_function_st {
56 hashkit_hash_fn function;
57 void *context;
58 } base_hash, distribution_hash;
59
60 struct {
61 bool is_base_same_distributed HASHKIT_BITFIELD;
62 } flags;
63
64 struct {
65 bool is_allocated HASHKIT_BITFIELD;
66 } options;
67 };
68
69 #ifdef __cplusplus
70 class Hashkit : private hashkit_st {
71
72 public:
73
74 Hashkit()
75 {
76 hashkit_create(this);
77 }
78
79 Hashkit(const Hashkit& source)
80 {
81 hashkit_clone(this, &source);
82 }
83
84 Hashkit& operator=(const Hashkit& source)
85 {
86 hashkit_free(this);
87 hashkit_clone(this, &source);
88
89 return *this;
90 }
91
92 friend bool operator==(const Hashkit &left, const Hashkit &right)
93 {
94 return hashkit_compare(&left, &right);
95 }
96
97 uint32_t digest(std::string& str)
98 {
99 return hashkit_digest(this, str.c_str(), str.length());
100 }
101
102 uint32_t digest(const char *key, size_t key_length)
103 {
104 return hashkit_digest(this, key, key_length);
105 }
106
107 hashkit_return_t set_function(hashkit_hash_algorithm_t hash_algorithm)
108 {
109 return hashkit_set_function(this, hash_algorithm);
110 }
111
112 hashkit_return_t set_distribution_function(hashkit_hash_algorithm_t hash_algorithm)
113 {
114 return hashkit_set_function(this, hash_algorithm);
115 }
116
117 ~Hashkit()
118 {
119 hashkit_free(this);
120 }
121 };
122 #endif
123
124
125 #endif /* HASHKIT_H */