More Cleanup
[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/types.h>
20 #include <libhashkit/algorithm.h>
21 #include <libhashkit/behavior.h>
22 #include <libhashkit/digest.h>
23 #include <libhashkit/function.h>
24 #include <libhashkit/strerror.h>
25
26 #ifdef __cplusplus
27
28 #include <string>
29
30 extern "C" {
31 #endif
32
33 HASHKIT_API
34 hashkit_st *hashkit_create(hashkit_st *hash);
35
36 HASHKIT_API
37 hashkit_st *hashkit_clone(hashkit_st *destination, const hashkit_st *ptr);
38
39 HASHKIT_API
40 bool hashkit_compare(const hashkit_st *first, const hashkit_st *second);
41
42 HASHKIT_API
43 void hashkit_free(hashkit_st *hash);
44
45 #define hashkit_is_allocated(__object) ((__object)->options.is_allocated)
46 #define hashkit_is_initialized(__object) ((__object)->options.is_initialized)
47
48 #ifdef __cplusplus
49 } // extern "C"
50 #endif
51
52 struct hashkit_st
53 {
54 struct hashkit_function_st {
55 hashkit_hash_fn function;
56 void *context;
57 } base_hash, distribution_hash;
58
59 struct {
60 bool is_base_same_distributed:1;
61 } flags;
62
63 struct {
64 bool is_allocated:1;
65 } options;
66
67 #ifdef __cplusplus
68
69 hashkit_st() :
70 base_hash(),
71 distribution_hash(),
72 flags(),
73 options()
74 {
75 hashkit_create(this);
76 }
77
78 hashkit_st(const hashkit_st& source) :
79 base_hash(),
80 distribution_hash(),
81 flags(),
82 options()
83 {
84 hashkit_clone(this, &source);
85 }
86
87 hashkit_st& operator=(const hashkit_st& source)
88 {
89 hashkit_free(this);
90 hashkit_clone(this, &source);
91
92 return *this;
93 }
94
95 friend bool operator==(const hashkit_st &left, const hashkit_st &right)
96 {
97 return hashkit_compare(&left, &right);
98 }
99
100 uint32_t digest(std::string& str)
101 {
102 return hashkit_digest(this, str.c_str(), str.length());
103 }
104
105 uint32_t digest(const char *key, size_t key_length)
106 {
107 return hashkit_digest(this, key, key_length);
108 }
109
110 hashkit_return_t set_function(hashkit_hash_algorithm_t hash_algorithm)
111 {
112 return hashkit_set_function(this, hash_algorithm);
113 }
114
115 hashkit_return_t set_distribution_function(hashkit_hash_algorithm_t hash_algorithm)
116 {
117 return hashkit_set_function(this, hash_algorithm);
118 }
119
120 ~hashkit_st()
121 {
122 hashkit_free(this);
123 }
124 #endif
125 };
126
127
128 #endif /* HASHKIT_H */