Modified Hashkit class (turned it into an actual class).
[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
68 #ifdef __cplusplus
69 class Hashkit : private hashkit_st {
70
71 public:
72
73 Hashkit()
74 {
75 hashkit_create(this);
76 }
77
78 Hashkit(const Hashkit& source)
79 {
80 hashkit_clone(this, &source);
81 }
82
83 Hashkit& operator=(const Hashkit& source)
84 {
85 hashkit_free(this);
86 hashkit_clone(this, &source);
87
88 return *this;
89 }
90
91 friend bool operator==(const Hashkit &left, const Hashkit &right)
92 {
93 return hashkit_compare(&left, &right);
94 }
95
96 uint32_t digest(std::string& str)
97 {
98 return hashkit_digest(this, str.c_str(), str.length());
99 }
100
101 uint32_t digest(const char *key, size_t key_length)
102 {
103 return hashkit_digest(this, key, key_length);
104 }
105
106 hashkit_return_t set_function(hashkit_hash_algorithm_t hash_algorithm)
107 {
108 return hashkit_set_function(this, hash_algorithm);
109 }
110
111 hashkit_return_t set_distribution_function(hashkit_hash_algorithm_t hash_algorithm)
112 {
113 return hashkit_set_function(this, hash_algorithm);
114 }
115
116 ~Hashkit()
117 {
118 hashkit_free(this);
119 }
120 };
121 #endif
122
123
124 #endif /* HASHKIT_H */