1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2014 Couchbase, Inc.
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 
18 #ifndef LCB_CXXWRAP_H
19 #define LCB_CXXWRAP_H
20 
21 /* Note that the contents of this file are here only to provide inline
22  * definitions for backwards compatibility with older code. Users of the library
23  * should assume that structures do _not_ behave differently under C++ and must
24  * be explicitly initialized with their appropriate members.
25  *
26  * For newer code which wants to use pure "C-Style" initializers, define the
27  * LCB_NO_DEPR_CXX_CTORS macro so that the structures remain to function
28  * as they do in plain C.
29  */
30 
31 #if defined(__cplusplus) && !defined(LCB_NO_DEPR_CXX_CTORS)
32 
33 #include <cstdlib>
34 #include <cstring>
35 
36 
37 #define LCB_DEPR_CTORS_GET \
38     lcb_get_cmd_st() { std::memset(this, 0, sizeof(*this)); } \
39     lcb_get_cmd_st(const void *key, lcb_size_t nkey=0, lcb_time_t exptime=0, int lock=0) { \
40         version = 0; \
41         v.v0.key = key; \
42         if (key != NULL && nkey == 0) { \
43             v.v0.nkey = std::strlen((const char *)key); \
44         } else { \
45             v.v0.nkey = nkey; \
46         } \
47         v.v0.exptime = exptime; v.v0.lock = lock; v.v0.hashkey = NULL; v.v0.nhashkey = 0; \
48     }
49 
50 
51 #define LCB_DEPR_CTORS_RGET \
52     lcb_get_replica_cmd_st() { std::memset(this, 0, sizeof(*this)); } \
53     lcb_get_replica_cmd_st(const void *key, lcb_size_t nkey, lcb_replica_t strategy=LCB_REPLICA_FIRST, int index=0) { \
54         version = 1; v.v1.key = key; v.v1.nkey = nkey; v.v1.hashkey = NULL; v.v1.nhashkey = 0; v.v1.strategy = strategy; v.v1.index = index; \
55     }
56 
57 #define LCB_DEPR_CTORS_UNL \
58     lcb_unlock_cmd_st() { std::memset(this, 0, sizeof(*this)); } \
59     lcb_unlock_cmd_st(const void *key, lcb_size_t nkey, lcb_cas_t cas) { \
60         version = 0; v.v0.key = key; v.v0.nkey = nkey; v.v0.cas = cas; v.v0.hashkey = NULL; v.v0.nhashkey = 0; \
61     }
62 
63 #define LCB_DEPR_CTORS_STORE \
64     lcb_store_cmd_st() { std::memset(this, 0, sizeof(*this)); } \
65     lcb_store_cmd_st(lcb_storage_t operation, const void *key, lcb_size_t nkey, \
66         const void *bytes=NULL, lcb_size_t nbytes=0, lcb_uint32_t flags=0, \
67         lcb_time_t exptime=0, lcb_cas_t cas=0, lcb_datatype_t datatype=0) { \
68         version = 0; v.v0.operation = operation; v.v0.key = key; v.v0.nkey = nkey; v.v0.cas = cas; \
69         v.v0.bytes = bytes; v.v0.nbytes = nbytes; v.v0.flags = flags; v.v0.datatype = datatype; \
70         v.v0.exptime = exptime; v.v0.hashkey = NULL; v.v0.nhashkey = 0; \
71     }
72 
73 #define LCB_DEPR_CTORS_ARITH \
74     lcb_arithmetic_cmd_st() { std::memset(this, 0, sizeof(*this)); } \
75     lcb_arithmetic_cmd_st(const void *key, lcb_size_t nkey, lcb_int64_t delta, int create=0, \
76         lcb_uint64_t initial=0, lcb_time_t exptime=0) { \
77         version = 0; v.v0.key = key; v.v0.nkey = nkey; v.v0.exptime = exptime; \
78         v.v0.delta = delta; v.v0.create = create; v.v0.initial = initial; \
79         v.v0.hashkey = NULL; v.v0.nhashkey = 0; \
80     }
81 
82 
83 #define LCB_DEPR_CTORS_OBS \
84     lcb_observe_cmd_st() { std::memset(this, 0, sizeof(*this)); } \
85     lcb_observe_cmd_st(const void *key, lcb_size_t nkey) { \
86         version = 0; v.v0.key = key; v.v0.nkey = nkey; v.v0.hashkey = NULL; v.v0.nhashkey = 0; \
87     }
88 
89 #define LCB_DEPR_CTORS_RM \
90     lcb_remove_cmd_st() { std::memset(this, 0, sizeof(*this)); } \
91     lcb_remove_cmd_st(const void *key, lcb_size_t nkey=0, lcb_cas_t cas=0) { \
92             version = 0; v.v0.key = key; \
93         if (key != NULL && nkey == 0) { v.v0.nkey = std::strlen((const char *)key);}\
94         else { v.v0.nkey = nkey; } \
95         v.v0.cas = cas; v.v0.hashkey = NULL; v.v0.nhashkey = 0; \
96     }
97 
98 #define LCB_DEPR_CTORS_STATS \
99     lcb_server_stats_cmd_st(const char *name=NULL, lcb_size_t nname=0) { \
100         version = 0; v.v0.name = name; v.v0.nname = nname; \
101         if (name != NULL && nname == 0) { v.v0.nname = std::strlen(name); } \
102         else { v.v0.nname = nname; } \
103     }
104 
105 #define LCB_DEPR_CTORS_VERBOSITY \
106     lcb_verbosity_cmd_st(lcb_verbosity_level_t level=LCB_VERBOSITY_WARNING, const char *server=NULL) { \
107         version = 0; v.v0.server = server; v.v0.level = level; \
108     }
109 
110 #define LCB_DEPR_CTORS_VERSIONS \
111     lcb_server_version_cmd_st() { std::memset(this, 0, sizeof(*this)); }
112 
113 #define LCB_DEPR_CTORS_FLUSH \
114     lcb_flush_cmd_st() { std::memset(this, 0, sizeof(*this)); }
115 
116 #define LCB_DEPR_CTORS_CRST \
117     lcb_create_st(const char *host=NULL, const char *user=NULL,\
118         const char *passwd=NULL, const char *bucket=NULL, \
119         struct lcb_io_opt_st *io=NULL, lcb_type_t type=LCB_TYPE_BUCKET) { \
120         version = 2; v.v2.host = host; v.v2.user = user; v.v2.passwd = passwd; \
121         v.v2.bucket = bucket; v.v2.io = io; v.v2.type = type; v.v2.mchosts = NULL; \
122         v.v2.transports = NULL; \
123     }
124 
125 #define LCB_DEPR_CTORS_HTTP \
126     lcb_http_cmd_st() { std::memset(this, 0, sizeof(*this)); } \
127     lcb_http_cmd_st(const char *path, lcb_size_t npath, const void *body, \
128                 lcb_size_t nbody, lcb_http_method_t method, \
129                 int chunked, const char *content_type) { \
130         version = 0; v.v0.path = path; v.v0.npath = npath; v.v0.body = body; \
131         v.v0.nbody = nbody; v.v0.method = method; v.v0.chunked = chunked; \
132         v.v0.content_type = content_type; \
133     }
134 #else
135 /* Not C++ */
136 #define LCB_DEPR_CTORS_GET
137 #define LCB_DEPR_CTORS_RGET
138 #define LCB_DEPR_CTORS_UNL
139 #define LCB_DEPR_CTORS_STORE
140 #define LCB_DEPR_CTORS_ARITH
141 #define LCB_DEPR_CTORS_OBS
142 #define LCB_DEPR_CTORS_RM
143 #define LCB_DEPR_CTORS_STATS
144 #define LCB_DEPR_CTORS_VERBOSITY
145 #define LCB_DEPR_CTORS_VERSIONS
146 #define LCB_DEPR_CTORS_FLUSH
147 #define LCB_DEPR_CTORS_HTTP
148 #define LCB_DEPR_CTORS_CRST
149 #endif
150 #endif /* LCB_CXXWRAP_H */
151