aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/misc/property.h
blob: 3c42693ef3fefc6c3133695ba58f27025b685560 (plain) (blame)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#pragma once

#include <util/system/compiler.h>

////////////////////////////////////////////////////////////////////////////////

//! Declares a trivial public read-write property that is passed by reference.
#define DECLARE_BYREF_RW_PROPERTY(type, name) \
public: \
    type& name() noexcept; \
    const type& name() const noexcept

//! Defines a trivial public read-write property that is passed by reference.
//! All arguments after name are used as default value (via braced-init-list).
#define DEFINE_BYREF_RW_PROPERTY(type, name, ...) \
protected: \
    type name##_ { __VA_ARGS__ }; \
    \
public: \
    Y_FORCE_INLINE type& name() noexcept \
    { \
        return name##_; \
    } \
    \
    Y_FORCE_INLINE const type& name() const noexcept \
    { \
        return name##_; \
    } \
    static_assert(true)

//! Defines a trivial public read-write property that is passed by reference
//! and is not inline-initialized.
#define DEFINE_BYREF_RW_PROPERTY_NO_INIT(type, name) \
protected: \
    type name##_; \
    \
public: \
    Y_FORCE_INLINE type& name() noexcept \
    { \
        return name##_; \
    } \
    \
    Y_FORCE_INLINE const type& name() const noexcept \
    { \
        return name##_; \
    } \
    static_assert(true)

//! Defines a trivial public read-write property override that is passed by reference
//! and is not inline-initialized.
#define DEFINE_BYREF_RW_PROPERTY_NO_INIT_OVERRIDE(type, name) \
protected: \
    type name##_; \
    \
public: \
    Y_FORCE_INLINE type& name() noexcept override \
    { \
        return name##_; \
    } \
    \
    Y_FORCE_INLINE const type& name() const noexcept override \
    { \
        return name##_; \
    } \
    static_assert(true)

//! Forwards a trivial public read-write property that is passed by reference.
#define DELEGATE_BYREF_RW_PROPERTY(declaringType, type, name, delegateTo) \
    type& declaringType::name() noexcept \
    { \
        return (delegateTo).name(); \
    } \
    \
    const type& declaringType::name() const noexcept \
    { \
        return (delegateTo).name(); \
    } \
    static_assert(true)

////////////////////////////////////////////////////////////////////////////////

//! Declares a trivial public read-only property that is passed by reference.
#define DECLARE_BYREF_RO_PROPERTY(type, name) \
public: \
    const type& name() const noexcept

//! Defines a trivial public read-only property that is passed by reference.
//! All arguments after name are used as default value (via braced-init-list).
#define DEFINE_BYREF_RO_PROPERTY(type, name, ...) \
protected: \
    type name##_ { __VA_ARGS__ }; \
    \
public: \
    Y_FORCE_INLINE const type& name() const noexcept \
    { \
        return name##_; \
    } \
    static_assert(true)

//! Defines a trivial public read-only property that is passed by reference
//! and is not inline-initialized.
#define DEFINE_BYREF_RO_PROPERTY_NO_INIT(type, name) \
protected: \
    type name##_; \
    \
public: \
    Y_FORCE_INLINE const type& name() const noexcept \
    { \
        return name##_; \
    } \
    static_assert(true)

//! Defines a trivial public read-only property override that is passed by reference
//! and is not inline-initialized.
#define DEFINE_BYREF_RO_PROPERTY_NO_INIT_OVERRIDE(type, name) \
protected: \
    type name##_; \
    \
public: \
    Y_FORCE_INLINE const type& name() const noexcept override \
    { \
        return name##_; \
    } \
    static_assert(true)

//! Forwards a trivial public read-only property that is passed by reference.
#define DELEGATE_BYREF_RO_PROPERTY(declaringType, type, name, delegateTo) \
    const type& declaringType::name() const noexcept \
    { \
        return (delegateTo).name(); \
    } \
    static_assert(true)

////////////////////////////////////////////////////////////////////////////////

//! Declares a trivial public read-write property that is passed by value.
#define DECLARE_BYVAL_RW_PROPERTY(type, name) \
public: \
    type Get##name() const; \
    void Set##name(type value)

//! Defines a trivial public read-write property that is passed by value.
//! All arguments after name are used as default value (via braced-init-list).
#define DEFINE_BYVAL_RW_PROPERTY(type, name, ...) \
protected: \
    type name##_ { __VA_ARGS__ }; \
    \
public: \
    Y_FORCE_INLINE type Get##name() const \
    { \
        return name##_; \
    } \
    \
    Y_FORCE_INLINE void Set##name(type value) \
    { \
        name##_ = value; \
    } \
    static_assert(true)

//! Defines a trivial public read-write boolean property that is passed by value.
//! All arguments after name are used as default value (via braced-init-list).
#define DEFINE_BYVAL_RW_BOOLEAN_PROPERTY(name, ...) \
protected: \
    bool name##_ { __VA_ARGS__ }; \
    \
public: \
    Y_FORCE_INLINE bool Is##name() const \
    { \
        return name##_; \
    } \
    \
    Y_FORCE_INLINE void Set##name(bool value) \
    { \
        name##_ = value; \
    } \
    static_assert(true)

//! Defines a trivial public read-write property that is passed by value.
//! All arguments after name are used as default value (via braced-init-list).
#define DEFINE_BYVAL_RW_PROPERTY_WITH_FLUENT_SETTER(declaringType, type, name, ...) \
protected: \
    type name##_ { __VA_ARGS__ }; \
    \
public: \
    Y_FORCE_INLINE type Get##name() const \
    { \
        return name##_; \
    } \
    \
    Y_FORCE_INLINE void Set##name(type value) &\
    { \
        name##_ = value; \
    } \
    \
    Y_FORCE_INLINE declaringType&& Set##name(type value) &&\
    { \
        name##_ = value; \
        return std::move(*this); \
    } \
    static_assert(true)

//! Defines a trivial public read-write property that is passed by value
//! and is not inline-initialized.
#define DEFINE_BYVAL_RW_PROPERTY_NO_INIT(type, name) \
protected: \
    type name##_; \
    \
public: \
    Y_FORCE_INLINE type Get##name() const \
    { \
        return name##_; \
    } \
    \
    Y_FORCE_INLINE void Set##name(type value) \
    { \
        name##_ = value; \
    } \
    static_assert(true)

//! Defines a trivial public read-write property override that is passed by value
//! and is not inline-initialized.
#define DEFINE_BYVAL_RW_PROPERTY_NO_INIT_OVERRIDE(type, name) \
protected: \
    type name##_; \
    \
public: \
    Y_FORCE_INLINE type Get##name() const override \
    { \
        return name##_; \
    } \
    \
    Y_FORCE_INLINE void Set##name(type value) override \
    { \
        name##_ = value; \
    } \
    static_assert(true)

//! Forwards a trivial public read-write property that is passed by value.
#define DELEGATE_BYVAL_RW_PROPERTY(declaringType, type, name, delegateTo) \
    type declaringType::Get##name() const \
    { \
        return (delegateTo).Get##name(); \
    } \
    \
    void declaringType::Set##name(type value) \
    { \
        (delegateTo).Set##name(value); \
    } \
    static_assert(true)

////////////////////////////////////////////////////////////////////////////////

//! Declares a trivial public read-only property that is passed by value.
#define DECLARE_BYVAL_RO_PROPERTY(type, name) \
public: \
    type Get##name() const

//! Defines a trivial public read-only property that is passed by value.
//! All arguments after name are used as default value (via braced-init-list).
#define DEFINE_BYVAL_RO_PROPERTY(type, name, ...) \
protected: \
    type name##_ { __VA_ARGS__ }; \
    \
public: \
    Y_FORCE_INLINE type Get##name() const \
    { \
        return name##_; \
    } \
    static_assert(true)

//! Defines a trivial public read-only property that is passed by value
//! and is not inline-initialized.
#define DEFINE_BYVAL_RO_PROPERTY_NO_INIT(type, name) \
protected: \
    type name##_; \
    \
public: \
    Y_FORCE_INLINE type Get##name() const \
    { \
        return name##_; \
    } \
    static_assert(true)

//! Defines a trivial public read-only property override that is passed by value
//! and is not inline-initialized.
#define DEFINE_BYVAL_RO_PROPERTY_NO_INIT_OVERRIDE(type, name) \
protected: \
    type name##_; \
    \
public: \
    Y_FORCE_INLINE type Get##name() const override \
    { \
        return name##_; \
    } \
    static_assert(true)

//! Forwards a trivial public read-only property that is passed by value.
#define DELEGATE_BYVAL_RO_PROPERTY(declaringType, type, name, delegateTo) \
    type declaringType::Get##name() \
    { \
        return (delegateTo).Get##name(); \
    } \
    static_assert(true)

////////////////////////////////////////////////////////////////////////////////

//! Below are macro helpers for extra properties.
//! Extra properties should be used for lazy memory allocation for properties that
//! hold default values for the majority of objects.

//! Initializes extra property holder if it is not initialized.
#define INITIALIZE_EXTRA_PROPERTY_HOLDER(holder) \
    if (!holder##_) { \
        holder##_.reset(new decltype(holder##_)::element_type()); \
    } \
    static_assert(true)

//! Declares an extra property holder. Holder contains extra properties values.
//! Holder is not created until some property is set with a non-default value.
//! If there is no holder property getter returns default value.
#define DECLARE_EXTRA_PROPERTY_HOLDER(type, holder) \
public: \
    Y_FORCE_INLINE bool HasCustom##holder() const \
    { \
        return static_cast<bool>(holder##_); \
    } \
    Y_FORCE_INLINE const type* GetCustom##holder() const \
    { \
        return holder##_.get(); \
    } \
    Y_FORCE_INLINE type* GetCustom##holder() \
    { \
        return holder##_.get(); \
    } \
    Y_FORCE_INLINE void InitializeCustom##holder() \
    { \
        INITIALIZE_EXTRA_PROPERTY_HOLDER(holder); \
    } \
private: \
    std::unique_ptr<type> holder##_; \
    static const type Default##holder##_

//! Defines a storage for extra properties default values.
#define DEFINE_EXTRA_PROPERTY_HOLDER(class, type, holder) \
    const type class::Default##holder##_

//! Defines a public read-write extra property that is passed by value.
#define DEFINE_BYVAL_RW_EXTRA_PROPERTY(holder, name) \
public: \
    Y_FORCE_INLINE decltype(holder##_->name) Get##name() const \
    { \
        if (!holder##_) { \
            return Default##holder##_.name; \
        } \
        return holder##_->name; \
    } \
    Y_FORCE_INLINE void Set##name(decltype(holder##_->name) val) \
    { \
        if (!holder##_) { \
            if (val == Default##holder##_.name) { \
                return; \
            } \
            INITIALIZE_EXTRA_PROPERTY_HOLDER(holder); \
        } \
        holder##_->name = val; \
    } \
    static_assert(true)

//! Defines a public read-write extra property that is passed by reference.
#define DEFINE_BYREF_RW_EXTRA_PROPERTY(holder, name) \
public: \
    Y_FORCE_INLINE const decltype(holder##_->name)& name() const \
    { \
        if (!holder##_) { \
            return Default##holder##_.name; \
        } \
        return holder##_->name; \
    } \
    Y_FORCE_INLINE decltype(holder##_->name)& Mutable##name() \
    { \
        INITIALIZE_EXTRA_PROPERTY_HOLDER(holder); \
        return holder##_->name; \
    } \
    static_assert(true)

////////////////////////////////////////////////////////////////////////////////