Coverage Report

Created: 2023-06-07 07:09

/src/LPM/external.protobuf/include/google/protobuf/stubs/callback.h
Line
Count
Source (jump to first uncovered line)
1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc.  All rights reserved.
3
// https://developers.google.com/protocol-buffers/
4
//
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
7
// met:
8
//
9
//     * Redistributions of source code must retain the above copyright
10
// notice, this list of conditions and the following disclaimer.
11
//     * Redistributions in binary form must reproduce the above
12
// copyright notice, this list of conditions and the following disclaimer
13
// in the documentation and/or other materials provided with the
14
// distribution.
15
//     * Neither the name of Google Inc. nor the names of its
16
// contributors may be used to endorse or promote products derived from
17
// this software without specific prior written permission.
18
//
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31
#ifndef GOOGLE_PROTOBUF_STUBS_CALLBACK_H_
32
#define GOOGLE_PROTOBUF_STUBS_CALLBACK_H_
33
34
#include <type_traits>
35
36
#include "google/protobuf/port_def.inc"
37
38
// ===================================================================
39
// emulates google3/base/callback.h
40
41
namespace google {
42
namespace protobuf {
43
44
// Abstract interface for a callback.  When calling an RPC, you must provide
45
// a Closure to call when the procedure completes.  See the Service interface
46
// in service.h.
47
//
48
// To automatically construct a Closure which calls a particular function or
49
// method with a particular set of parameters, use the NewCallback() function.
50
// Example:
51
//   void FooDone(const FooResponse* response) {
52
//     ...
53
//   }
54
//
55
//   void CallFoo() {
56
//     ...
57
//     // When done, call FooDone() and pass it a pointer to the response.
58
//     Closure* callback = NewCallback(&FooDone, response);
59
//     // Make the call.
60
//     service->Foo(controller, request, response, callback);
61
//   }
62
//
63
// Example that calls a method:
64
//   class Handler {
65
//    public:
66
//     ...
67
//
68
//     void FooDone(const FooResponse* response) {
69
//       ...
70
//     }
71
//
72
//     void CallFoo() {
73
//       ...
74
//       // When done, call FooDone() and pass it a pointer to the response.
75
//       Closure* callback = NewCallback(this, &Handler::FooDone, response);
76
//       // Make the call.
77
//       service->Foo(controller, request, response, callback);
78
//     }
79
//   };
80
//
81
// Currently NewCallback() supports binding zero, one, or two arguments.
82
//
83
// Callbacks created with NewCallback() automatically delete themselves when
84
// executed.  They should be used when a callback is to be called exactly
85
// once (usually the case with RPC callbacks).  If a callback may be called
86
// a different number of times (including zero), create it with
87
// NewPermanentCallback() instead.  You are then responsible for deleting the
88
// callback (using the "delete" keyword as normal).
89
//
90
// Note that NewCallback() is a bit touchy regarding argument types.  Generally,
91
// the values you provide for the parameter bindings must exactly match the
92
// types accepted by the callback function.  For example:
93
//   void Foo(std::string s);
94
//   NewCallback(&Foo, "foo");          // WON'T WORK:  const char* != string
95
//   NewCallback(&Foo, std::string("foo"));  // WORKS
96
// Also note that the arguments cannot be references:
97
//   void Foo(const std::string& s);
98
//   std::string my_str;
99
//   NewCallback(&Foo, my_str);  // WON'T WORK:  Can't use references.
100
// However, correctly-typed pointers will work just fine.
101
class PROTOBUF_EXPORT Closure {
102
 public:
103
0
  Closure() {}
104
  Closure(const Closure&) = delete;
105
  Closure& operator=(const Closure&) = delete;
106
  virtual ~Closure();
107
108
  virtual void Run() = 0;
109
};
110
111
template<typename R>
112
class ResultCallback {
113
 public:
114
  ResultCallback() {}
115
  ResultCallback(const ResultCallback&) = delete;
116
  ResultCallback& operator=(const ResultCallback&) = delete;
117
  virtual ~ResultCallback() {}
118
119
  virtual R Run() = 0;
120
};
121
122
template <typename R, typename A1>
123
class PROTOBUF_EXPORT ResultCallback1 {
124
 public:
125
  ResultCallback1() {}
126
  ResultCallback1(const ResultCallback1&) = delete;
127
  ResultCallback1& operator=(const ResultCallback1&) = delete;
128
  virtual ~ResultCallback1() {}
129
130
  virtual R Run(A1) = 0;
131
};
132
133
template <typename R, typename A1, typename A2>
134
class PROTOBUF_EXPORT ResultCallback2 {
135
 public:
136
  ResultCallback2() {}
137
  ResultCallback2(const ResultCallback2&) = delete;
138
  ResultCallback2& operator=(const ResultCallback2&) = delete;
139
  virtual ~ResultCallback2() {}
140
141
  virtual R Run(A1,A2) = 0;
142
};
143
144
namespace internal {
145
146
class PROTOBUF_EXPORT FunctionClosure0 : public Closure {
147
 public:
148
  typedef void (*FunctionType)();
149
150
  FunctionClosure0(FunctionType function, bool self_deleting)
151
0
    : function_(function), self_deleting_(self_deleting) {}
152
  ~FunctionClosure0();
153
154
0
  void Run() override {
155
0
    bool needs_delete = self_deleting_;  // read in case callback deletes
156
0
    function_();
157
0
    if (needs_delete) delete this;
158
0
  }
159
160
 private:
161
  FunctionType function_;
162
  bool self_deleting_;
163
};
164
165
template <typename Class>
166
class MethodClosure0 : public Closure {
167
 public:
168
  typedef void (Class::*MethodType)();
169
170
  MethodClosure0(Class* object, MethodType method, bool self_deleting)
171
    : object_(object), method_(method), self_deleting_(self_deleting) {}
172
  ~MethodClosure0() {}
173
174
  void Run() override {
175
    bool needs_delete = self_deleting_;  // read in case callback deletes
176
    (object_->*method_)();
177
    if (needs_delete) delete this;
178
  }
179
180
 private:
181
  Class* object_;
182
  MethodType method_;
183
  bool self_deleting_;
184
};
185
186
template <typename Arg1>
187
class FunctionClosure1 : public Closure {
188
 public:
189
  typedef void (*FunctionType)(Arg1 arg1);
190
191
  FunctionClosure1(FunctionType function, bool self_deleting,
192
                   Arg1 arg1)
193
    : function_(function), self_deleting_(self_deleting),
194
      arg1_(arg1) {}
195
  ~FunctionClosure1() {}
196
197
  void Run() override {
198
    bool needs_delete = self_deleting_;  // read in case callback deletes
199
    function_(arg1_);
200
    if (needs_delete) delete this;
201
  }
202
203
 private:
204
  FunctionType function_;
205
  bool self_deleting_;
206
  Arg1 arg1_;
207
};
208
209
template <typename Class, typename Arg1>
210
class MethodClosure1 : public Closure {
211
 public:
212
  typedef void (Class::*MethodType)(Arg1 arg1);
213
214
  MethodClosure1(Class* object, MethodType method, bool self_deleting,
215
                 Arg1 arg1)
216
    : object_(object), method_(method), self_deleting_(self_deleting),
217
      arg1_(arg1) {}
218
  ~MethodClosure1() {}
219
220
  void Run() override {
221
    bool needs_delete = self_deleting_;  // read in case callback deletes
222
    (object_->*method_)(arg1_);
223
    if (needs_delete) delete this;
224
  }
225
226
 private:
227
  Class* object_;
228
  MethodType method_;
229
  bool self_deleting_;
230
  Arg1 arg1_;
231
};
232
233
template <typename Arg1, typename Arg2>
234
class FunctionClosure2 : public Closure {
235
 public:
236
  typedef void (*FunctionType)(Arg1 arg1, Arg2 arg2);
237
238
  FunctionClosure2(FunctionType function, bool self_deleting,
239
                   Arg1 arg1, Arg2 arg2)
240
    : function_(function), self_deleting_(self_deleting),
241
      arg1_(arg1), arg2_(arg2) {}
242
  ~FunctionClosure2() {}
243
244
  void Run() override {
245
    bool needs_delete = self_deleting_;  // read in case callback deletes
246
    function_(arg1_, arg2_);
247
    if (needs_delete) delete this;
248
  }
249
250
 private:
251
  FunctionType function_;
252
  bool self_deleting_;
253
  Arg1 arg1_;
254
  Arg2 arg2_;
255
};
256
257
template <typename Class, typename Arg1, typename Arg2>
258
class MethodClosure2 : public Closure {
259
 public:
260
  typedef void (Class::*MethodType)(Arg1 arg1, Arg2 arg2);
261
262
  MethodClosure2(Class* object, MethodType method, bool self_deleting,
263
                 Arg1 arg1, Arg2 arg2)
264
    : object_(object), method_(method), self_deleting_(self_deleting),
265
      arg1_(arg1), arg2_(arg2) {}
266
  ~MethodClosure2() {}
267
268
  void Run() override {
269
    bool needs_delete = self_deleting_;  // read in case callback deletes
270
    (object_->*method_)(arg1_, arg2_);
271
    if (needs_delete) delete this;
272
  }
273
274
 private:
275
  Class* object_;
276
  MethodType method_;
277
  bool self_deleting_;
278
  Arg1 arg1_;
279
  Arg2 arg2_;
280
};
281
282
template<typename R>
283
class FunctionResultCallback_0_0 : public ResultCallback<R> {
284
 public:
285
  typedef R (*FunctionType)();
286
287
  FunctionResultCallback_0_0(FunctionType function, bool self_deleting)
288
      : function_(function), self_deleting_(self_deleting) {}
289
  ~FunctionResultCallback_0_0() {}
290
291
  R Run() override {
292
    bool needs_delete = self_deleting_;  // read in case callback deletes
293
    R result = function_();
294
    if (needs_delete) delete this;
295
    return result;
296
  }
297
298
 private:
299
  FunctionType function_;
300
  bool self_deleting_;
301
};
302
303
template<typename R, typename P1>
304
class FunctionResultCallback_1_0 : public ResultCallback<R> {
305
 public:
306
  typedef R (*FunctionType)(P1);
307
308
  FunctionResultCallback_1_0(FunctionType function, bool self_deleting,
309
                             P1 p1)
310
      : function_(function), self_deleting_(self_deleting), p1_(p1) {}
311
  ~FunctionResultCallback_1_0() {}
312
313
  R Run() override {
314
    bool needs_delete = self_deleting_;  // read in case callback deletes
315
    R result = function_(p1_);
316
    if (needs_delete) delete this;
317
    return result;
318
  }
319
320
 private:
321
  FunctionType function_;
322
  bool self_deleting_;
323
  P1 p1_;
324
};
325
326
template<typename R, typename Arg1>
327
class FunctionResultCallback_0_1 : public ResultCallback1<R, Arg1> {
328
 public:
329
  typedef R (*FunctionType)(Arg1 arg1);
330
331
  FunctionResultCallback_0_1(FunctionType function, bool self_deleting)
332
      : function_(function), self_deleting_(self_deleting) {}
333
  ~FunctionResultCallback_0_1() {}
334
335
  R Run(Arg1 a1) override {
336
    bool needs_delete = self_deleting_;  // read in case callback deletes
337
    R result = function_(a1);
338
    if (needs_delete) delete this;
339
    return result;
340
  }
341
342
 private:
343
  FunctionType function_;
344
  bool self_deleting_;
345
};
346
347
template<typename R, typename P1, typename A1>
348
class FunctionResultCallback_1_1 : public ResultCallback1<R, A1> {
349
 public:
350
  typedef R (*FunctionType)(P1, A1);
351
352
  FunctionResultCallback_1_1(FunctionType function, bool self_deleting,
353
                             P1 p1)
354
      : function_(function), self_deleting_(self_deleting), p1_(p1) {}
355
  ~FunctionResultCallback_1_1() {}
356
357
  R Run(A1 a1) override {
358
    bool needs_delete = self_deleting_;  // read in case callback deletes
359
    R result = function_(p1_, a1);
360
    if (needs_delete) delete this;
361
    return result;
362
  }
363
364
 private:
365
  FunctionType function_;
366
  bool self_deleting_;
367
  P1 p1_;
368
};
369
370
template <typename T>
371
struct InternalConstRef {
372
  typedef typename std::remove_reference<T>::type base_type;
373
  typedef const base_type& type;
374
};
375
376
template<typename R, typename T>
377
class MethodResultCallback_0_0 : public ResultCallback<R> {
378
 public:
379
  typedef R (T::*MethodType)();
380
  MethodResultCallback_0_0(T* object, MethodType method, bool self_deleting)
381
      : object_(object),
382
        method_(method),
383
        self_deleting_(self_deleting) {}
384
  ~MethodResultCallback_0_0() {}
385
386
  R Run() {
387
    bool needs_delete = self_deleting_;
388
    R result = (object_->*method_)();
389
    if (needs_delete) delete this;
390
    return result;
391
  }
392
393
 private:
394
  T* object_;
395
  MethodType method_;
396
  bool self_deleting_;
397
};
398
399
template <typename R, typename T, typename P1, typename P2, typename P3,
400
          typename P4, typename P5, typename P6, typename A1, typename A2>
401
class MethodResultCallback_6_2 : public ResultCallback2<R, A1, A2> {
402
 public:
403
  typedef R (T::*MethodType)(P1, P2, P3, P4, P5, P6, A1, A2);
404
  MethodResultCallback_6_2(T* object, MethodType method, bool self_deleting,
405
                           P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6)
406
      : object_(object),
407
        method_(method),
408
        self_deleting_(self_deleting),
409
        p1_(p1),
410
        p2_(p2),
411
        p3_(p3),
412
        p4_(p4),
413
        p5_(p5),
414
        p6_(p6) {}
415
  ~MethodResultCallback_6_2() {}
416
417
  R Run(A1 a1, A2 a2) override {
418
    bool needs_delete = self_deleting_;
419
    R result = (object_->*method_)(p1_, p2_, p3_, p4_, p5_, p6_, a1, a2);
420
    if (needs_delete) delete this;
421
    return result;
422
  }
423
424
 private:
425
  T* object_;
426
  MethodType method_;
427
  bool self_deleting_;
428
  typename std::remove_reference<P1>::type p1_;
429
  typename std::remove_reference<P2>::type p2_;
430
  typename std::remove_reference<P3>::type p3_;
431
  typename std::remove_reference<P4>::type p4_;
432
  typename std::remove_reference<P5>::type p5_;
433
  typename std::remove_reference<P6>::type p6_;
434
};
435
436
}  // namespace internal
437
438
// See Closure.
439
0
inline Closure* NewCallback(void (*function)()) {
440
0
  return new internal::FunctionClosure0(function, true);
441
0
}
442
443
// See Closure.
444
0
inline Closure* NewPermanentCallback(void (*function)()) {
445
0
  return new internal::FunctionClosure0(function, false);
446
0
}
447
448
// See Closure.
449
template <typename Class>
450
inline Closure* NewCallback(Class* object, void (Class::*method)()) {
451
  return new internal::MethodClosure0<Class>(object, method, true);
452
}
453
454
// See Closure.
455
template <typename Class>
456
inline Closure* NewPermanentCallback(Class* object, void (Class::*method)()) {
457
  return new internal::MethodClosure0<Class>(object, method, false);
458
}
459
460
// See Closure.
461
template <typename Arg1>
462
inline Closure* NewCallback(void (*function)(Arg1),
463
                            Arg1 arg1) {
464
  return new internal::FunctionClosure1<Arg1>(function, true, arg1);
465
}
466
467
// See Closure.
468
template <typename Arg1>
469
inline Closure* NewPermanentCallback(void (*function)(Arg1),
470
                                     Arg1 arg1) {
471
  return new internal::FunctionClosure1<Arg1>(function, false, arg1);
472
}
473
474
// See Closure.
475
template <typename Class, typename Arg1>
476
inline Closure* NewCallback(Class* object, void (Class::*method)(Arg1),
477
                            Arg1 arg1) {
478
  return new internal::MethodClosure1<Class, Arg1>(object, method, true, arg1);
479
}
480
481
// See Closure.
482
template <typename Class, typename Arg1>
483
inline Closure* NewPermanentCallback(Class* object, void (Class::*method)(Arg1),
484
                                     Arg1 arg1) {
485
  return new internal::MethodClosure1<Class, Arg1>(object, method, false, arg1);
486
}
487
488
// See Closure.
489
template <typename Arg1, typename Arg2>
490
inline Closure* NewCallback(void (*function)(Arg1, Arg2),
491
                            Arg1 arg1, Arg2 arg2) {
492
  return new internal::FunctionClosure2<Arg1, Arg2>(
493
    function, true, arg1, arg2);
494
}
495
496
// See Closure.
497
template <typename Arg1, typename Arg2>
498
inline Closure* NewPermanentCallback(void (*function)(Arg1, Arg2),
499
                                     Arg1 arg1, Arg2 arg2) {
500
  return new internal::FunctionClosure2<Arg1, Arg2>(
501
    function, false, arg1, arg2);
502
}
503
504
// See Closure.
505
template <typename Class, typename Arg1, typename Arg2>
506
inline Closure* NewCallback(Class* object, void (Class::*method)(Arg1, Arg2),
507
                            Arg1 arg1, Arg2 arg2) {
508
  return new internal::MethodClosure2<Class, Arg1, Arg2>(
509
    object, method, true, arg1, arg2);
510
}
511
512
// See Closure.
513
template <typename Class, typename Arg1, typename Arg2>
514
inline Closure* NewPermanentCallback(
515
    Class* object, void (Class::*method)(Arg1, Arg2),
516
    Arg1 arg1, Arg2 arg2) {
517
  return new internal::MethodClosure2<Class, Arg1, Arg2>(
518
    object, method, false, arg1, arg2);
519
}
520
521
// See ResultCallback
522
template<typename R>
523
inline ResultCallback<R>* NewCallback(R (*function)()) {
524
  return new internal::FunctionResultCallback_0_0<R>(function, true);
525
}
526
527
// See ResultCallback
528
template<typename R>
529
inline ResultCallback<R>* NewPermanentCallback(R (*function)()) {
530
  return new internal::FunctionResultCallback_0_0<R>(function, false);
531
}
532
533
// See ResultCallback
534
template<typename R, typename P1>
535
inline ResultCallback<R>* NewCallback(R (*function)(P1), P1 p1) {
536
  return new internal::FunctionResultCallback_1_0<R, P1>(
537
      function, true, p1);
538
}
539
540
// See ResultCallback
541
template<typename R, typename P1>
542
inline ResultCallback<R>* NewPermanentCallback(
543
    R (*function)(P1), P1 p1) {
544
  return new internal::FunctionResultCallback_1_0<R, P1>(
545
      function, false, p1);
546
}
547
548
// See ResultCallback1
549
template<typename R, typename A1>
550
inline ResultCallback1<R, A1>* NewCallback(R (*function)(A1)) {
551
  return new internal::FunctionResultCallback_0_1<R, A1>(function, true);
552
}
553
554
// See ResultCallback1
555
template<typename R, typename A1>
556
inline ResultCallback1<R, A1>* NewPermanentCallback(R (*function)(A1)) {
557
  return new internal::FunctionResultCallback_0_1<R, A1>(function, false);
558
}
559
560
// See ResultCallback1
561
template<typename R, typename P1, typename A1>
562
inline ResultCallback1<R, A1>* NewCallback(R (*function)(P1, A1), P1 p1) {
563
  return new internal::FunctionResultCallback_1_1<R, P1, A1>(
564
      function, true, p1);
565
}
566
567
// See ResultCallback1
568
template<typename R, typename P1, typename A1>
569
inline ResultCallback1<R, A1>* NewPermanentCallback(
570
    R (*function)(P1, A1), P1 p1) {
571
  return new internal::FunctionResultCallback_1_1<R, P1, A1>(
572
      function, false, p1);
573
}
574
575
// See MethodResultCallback_0_0
576
template <typename R, typename T1, typename T2>
577
inline ResultCallback<R>* NewPermanentCallback(
578
    T1* object, R (T2::*function)()) {
579
  return new internal::MethodResultCallback_0_0<R, T1>(object, function, false);
580
}
581
582
// See MethodResultCallback_6_2
583
template <typename R, typename T, typename P1, typename P2, typename P3,
584
          typename P4, typename P5, typename P6, typename A1, typename A2>
585
inline ResultCallback2<R, A1, A2>* NewPermanentCallback(
586
    T* object, R (T::*function)(P1, P2, P3, P4, P5, P6, A1, A2),
587
    typename internal::InternalConstRef<P1>::type p1,
588
    typename internal::InternalConstRef<P2>::type p2,
589
    typename internal::InternalConstRef<P3>::type p3,
590
    typename internal::InternalConstRef<P4>::type p4,
591
    typename internal::InternalConstRef<P5>::type p5,
592
    typename internal::InternalConstRef<P6>::type p6) {
593
  return new internal::MethodResultCallback_6_2<R, T, P1, P2, P3, P4, P5, P6,
594
                                                A1, A2>(object, function, false,
595
                                                        p1, p2, p3, p4, p5, p6);
596
}
597
598
// A function which does nothing.  Useful for creating no-op callbacks, e.g.:
599
//   Closure* nothing = NewCallback(&DoNothing);
600
void PROTOBUF_EXPORT DoNothing();
601
602
}  // namespace protobuf
603
}  // namespace google
604
605
#include "google/protobuf/port_undef.inc"
606
607
#endif  // GOOGLE_PROTOBUF_STUBS_CALLBACK_H_