summaryrefslogtreecommitdiff
path: root/src/components/rpc_base/include/rpc_base/rpc_base_inl.h
blob: 4373f0ea2636e50356bf6b31a7f70ef094f077df (plain)
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/**
* Copyright (c) 2014, Ford Motor Company
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of the Ford Motor Company nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef VALIDATED_TYPES_INL_H_
#define VALIDATED_TYPES_INL_H_

#include "rpc_base.h"

#include <cassert>
#include <cstdio>

#include "rpc_base/validation_report.h"

namespace rpc {

/*
 * Range helper class
 */
template <typename T>
Range<T>::Range(T min, T max)
    : min_(min),
      max_(max) {
}

template <typename T>
T Range<T>::min() const {
  return min_;
}

template <typename T>
T Range<T>::max() const {
  return max_;
}

template <typename T>
template <typename U>
bool Range<T>::Includes(U val) const {
  return min() <= val && val <= max();
}


/*
 * PrimitiveType base class
 */
inline PrimitiveType::PrimitiveType(ValueState value_state)
    : value_state_(value_state),
      policy_table_type_(policy_table_interface_base::PT_PRELOADED) {
}

inline bool PrimitiveType::is_initialized() const {
  return value_state_ != kUninitialized;
}

inline bool PrimitiveType::is_valid() const {
  return value_state_ == kValid;
}

inline void PrimitiveType::ReportErrors(ValidationReport* report) const {
  switch (value_state_) {
    case kUninitialized: {
      report->set_validation_info("value is not initialized");
      break;
    }
    case kInvalid: {
      report->set_validation_info("value initialized incorrectly");
      break;
    }
    case kValid: {
      // No error
      break;
    }
    default: {
      assert(!"Unexpected value state");
      break;
    }
  }
}

/*
 * CompositeType base class
 */
inline void CompositeType::mark_initialized() {
  initialization_state__ = kInitialized;
}

inline CompositeType::CompositeType(InitializationState init_state)
    : initialization_state__(init_state),
      policy_table_type_(policy_table_interface_base::PT_PRELOADED) {
}

inline void CompositeType::ReportErrors(ValidationReport* report) const {
  switch (initialization_state__) {
    case kUninitialized: {
      report->set_validation_info("object is not initialized");
      break;
    }
    case kInvalidInitialized: {
      report->set_validation_info("object initialized incorrectly");
      break;
    }
    case kInitialized: {
      // No error
      break;
    }
    default:
      assert(!"Unexpected initialization state");
      break;
  }
}

/*
 * Boolean class
 */
inline Boolean::Boolean()
    : PrimitiveType(kUninitialized),
      value_(false) {
}

inline Boolean::Boolean(bool value)
    : PrimitiveType(kValid), value_(value) {
}

inline Boolean& Boolean::operator=(bool new_val) {
  value_ = new_val;
  value_state_ = kValid;
  return *this;
}

inline Boolean::operator bool() const {
  return value_;
}

/*
 * Integer class
 */
template<typename T, T minval, T maxval>
const Range<T> Integer<T, minval, maxval>::range_(minval, maxval);

template<typename T, T minval, T maxval>
Integer<T, minval, maxval>::Integer()
    : PrimitiveType(kUninitialized),
      value_(range_.min()) {
}

template<typename T, T minval, T maxval>
Integer<T, minval, maxval>::Integer(IntType value)
    : PrimitiveType(range_.Includes(value) ? kValid : kInvalid),
      value_(value) {
}

template<typename T, T minval, T maxval>
Integer<T, minval, maxval>& Integer<T, minval, maxval>::operator=(IntType new_val) {
  value_ = new_val;
  value_state_ = range_.Includes(value_) ? kValid : kInvalid;
  return *this;
}

template<typename T, T minval, T maxval>
Integer<T, minval, maxval>& Integer<T, minval, maxval>::operator++() {
  ++value_;
  return *this;
}

template<typename T, T minval, T maxval>
Integer<T, minval, maxval>& Integer<T, minval, maxval>::operator+=(int value) {
  value_ += value;
  return *this;
}

template<typename T, T minval, T maxval>
Integer<T, minval, maxval>::operator IntType() const {
  return value_;
}

/*
 * Float class
 */
template<int64_t minnum, int64_t maxnum, int64_t minden, int64_t maxden>
const Range<double> Float<minnum, maxnum, minden, maxden>::range_(
    (double(minnum)/minden), (double(maxnum)/maxden));


template<int64_t minnum, int64_t maxnum, int64_t minden, int64_t maxden>
Float<minnum, maxnum, minden, maxden>::Float()
    : PrimitiveType(kUninitialized),
      value_(range_.min()) {
}

template<int64_t minnum, int64_t maxnum, int64_t minden, int64_t maxden>
Float<minnum, maxnum, minden, maxden>::Float(double value)
    : PrimitiveType(range_.Includes(value) ? kValid : kInvalid),
      value_(value) {
}

template<int64_t minnum, int64_t maxnum, int64_t minden, int64_t maxden>
Float<minnum, maxnum, minden, maxden>&
Float<minnum, maxnum, minden, maxden>::operator=(double new_val) {
  value_ = new_val;
  value_state_ = range_.Includes(new_val) ? kValid : kInvalid;
  return *this;
}

template<int64_t minnum, int64_t maxnum, int64_t minden, int64_t maxden>
Float<minnum, maxnum, minden, maxden>::operator double() const {
  return value_;
}

/*
 * String class
 */
template<size_t minlen, size_t maxlen>
const Range<size_t> String<minlen, maxlen>::length_range_(minlen, maxlen);

template<size_t minlen, size_t maxlen>
String<minlen, maxlen>::String()
    : PrimitiveType(kUninitialized) {
}

template<size_t minlen, size_t maxlen>
String<minlen, maxlen>::String(const std::string& value)
    : PrimitiveType(length_range_.Includes(value.length()) ? kValid : kInvalid),
      value_(value) {
}

template<size_t minlen, size_t maxlen>
String<minlen, maxlen>::String(const char* value)
    : PrimitiveType(kUninitialized),
      value_(value) {
  value_state_ = length_range_.Includes(value_.length()) ? kValid : kInvalid;
}

template<size_t minlen, size_t maxlen>
bool String<minlen, maxlen>::operator<(String new_val) {
  return value_ < new_val.value_;
}

template<size_t minlen, size_t maxlen>
String<minlen, maxlen>& String<minlen, maxlen>::operator=(const std::string& new_val) {
  value_ = new_val;
  value_state_ = length_range_.Includes(new_val.length()) ? kValid : kInvalid;
  return *this;
}

template<size_t minlen, size_t maxlen>
String<minlen, maxlen>::operator const std::string&() const {
  return value_;
}

/*
 * Enum class
 */
template<typename T>
Enum<T>::Enum()
    : PrimitiveType(kUninitialized),
      value_(EnumType()) {
}

template<typename T>
Enum<T>::Enum(EnumType value)
    : PrimitiveType(IsValidEnum(value) ? kValid : kInvalid),
      value_(value) {
}

template<typename T>
Enum<T>& Enum<T>::operator=(EnumType new_val) {
  value_ = new_val;
  value_state_ = IsValidEnum(value_) ? kValid : kInvalid;
  return *this;
}

template<typename T>
Enum<T>::operator EnumType() const {
  return value_;
}

/*
 * Array class
 */
template<typename T, size_t minsize, size_t maxsize>
Array<T, minsize, maxsize>::Array()
    : CompositeType(kUninitialized) {
}

template<typename T, size_t minsize, size_t maxsize>
template<typename U>
Array<T, minsize, maxsize>::Array(const U& value)
    : ArrayType(value.begin(), value.end()),
      CompositeType(kUninitialized) {
}

template<typename T, size_t minsize, size_t maxsize>
template<typename U>
Array<T, minsize, maxsize>&
Array<T, minsize, maxsize>::operator=(const U& that) {
  this->assign(that.begin(), that.end());
  return *this;
}

template<typename T, size_t minsize, size_t maxsize>
template<typename U>
void Array<T, minsize, maxsize>::push_back(const U& value) {
  ArrayType::push_back(T(value));
}

template<typename T, size_t minsize, size_t maxsize>
bool Array<T, minsize, maxsize>::is_valid() const {
  // Empty array might be valid only if marked initialized
  if (this->empty() && (initialization_state__ != kInitialized)) {
    return false;
  }
  // Array size must be within allowed range
  if (!Range<size_t>(minsize, maxsize).Includes(this->size())) {
    return false;
  }
  // All array elements must be valid
  for (typename ArrayType::const_iterator i = this->begin();
      i != this->end(); ++i) {
    if (!i->is_valid()) {
      return false;
    }
  }
  return true;
}

template<typename T, size_t minsize, size_t maxsize>
bool Array<T, minsize, maxsize>::is_initialized() const {
  // Array that is not empty is initialized for sure
  if (!this->empty()) {
    return true;
  }
  // Empty array is initialized if not marked as unitialized
  if (initialization_state__ != kUninitialized) {
    return true;
  }
  return false;
}

template<typename T, size_t minsize, size_t maxsize>
void Array<T, minsize, maxsize>::ReportErrors(ValidationReport* report) const {
  if (this->empty()) {
    CompositeType::ReportErrors(report);
  } else {
    if (!Range<size_t>(minsize, maxsize).Includes(this->size())) {
      report->set_validation_info("array has invalid size");
    } else {
      // No error
    }
  }
  for (size_t i = 0; i != this->size(); ++i) {
    const T& elem = this->operator [](i);
    if (!elem.is_valid()) {
      char elem_idx[32] = {};
      snprintf(elem_idx, 32, "[%zu]", i);
      ValidationReport& elem_report =
          report->ReportSubobject(elem_idx);
      elem.ReportErrors(&elem_report);
    }
  }
}

template<typename T, size_t minsize, size_t maxsize>
void Array<T, minsize, maxsize>::SetPolicyTableType(
    rpc::policy_table_interface_base::PolicyTableType pt_type) {

  for (typename ArrayType::iterator it = this->begin();
      it != this->end(); ++it) {
      it->SetPolicyTableType(pt_type);
  }
}

/*
 * Map class
 */
template<typename T, size_t minsize, size_t maxsize>
Map<T, minsize, maxsize>::Map()
    : CompositeType(kUninitialized) {
}

template<typename T, size_t minsize, size_t maxsize>
template<typename U>
Map<T, minsize, maxsize>::Map(const U& value)
    : CompositeType(kUninitialized) {
  for (typename U::const_iterator i = value.begin(), e = value.end(); i != e;
      ++i) {
    // Explicitly convert that value to T because all rpc_types have explicit
    // constructors
    insert(typename MapType::value_type(i->first, T(i->second)));
  }
}

template<typename T, size_t minsize, size_t maxsize>
template<typename U>
Map<T, minsize, maxsize>&
Map<T, minsize, maxsize>::operator=(const U& that) {
  this->clear();
  for (typename U::const_iterator i = that.begin(), e = that.end(); i != e;
      ++i) {
    // Explicitly convert that value to T because all rpc_types have explicit
    // constructors
    insert(typename MapType::value_type(i->first, T(i->second)));
  }
  return *this;
}

template<typename T, size_t minsize, size_t maxsize>
template<typename U>
void Map<T, minsize, maxsize>::insert(const std::pair<std::string, U>& value) {
  MapType::insert(typename MapType::value_type(value.first, T(value.second)));
}

template<typename T, size_t minsize, size_t maxsize>
bool Map<T, minsize, maxsize>::is_valid() const {
  // Empty map might be valid only if marked initialized
  if (this->empty() && (initialization_state__ != kInitialized)) {
    return false;
  }
  // Maps size must be within allowed range
  if (!Range<size_t>(minsize, maxsize).Includes(this->size())) {
    return false;
  }
  // All map elements must be valid
  for (typename Map::const_iterator i = this->begin();
      i != this->end(); ++i) {
    if (!i->second.is_valid()) {
      return false;
    }
  }
  return true;
}

template<typename T, size_t minsize, size_t maxsize>
bool Map<T, minsize, maxsize>::is_initialized() const {
  // Map that is not empty is initialized for sure
  if (!this->empty()) {
    return true;
  }
  // Empty map might be initialized only if not marked as unitialized
  if (initialization_state__ != kUninitialized) {
    return true;
  }
  return false;
}

template<typename T, size_t minsize, size_t maxsize>
void Map<T, minsize, maxsize>::ReportErrors(ValidationReport* report) const {
  if (this->empty()) {
    CompositeType::ReportErrors(report);
  } else {
    if (!Range<size_t>(minsize, maxsize).Includes(this->size())) {
      report->set_validation_info("map has invalid size");
    } else {
      // No error
    }
  }
  for (typename Map::const_iterator i = this->begin();
      i != this->end(); ++i) {
    if (!i->second.is_valid()) {
      std::string elem_name = "[\"" + i->first + "\"]";
      ValidationReport& elem_report = report->ReportSubobject(elem_name);
      i->second.ReportErrors(&elem_report);
    }
  }
}

template<typename T, size_t minsize, size_t maxsize>
void Map<T, minsize, maxsize>::SetPolicyTableType(
    rpc::policy_table_interface_base::PolicyTableType pt_type) {
  for (typename Map::iterator it = this->begin();
      it != this->end(); ++it) {
    it->second.SetPolicyTableType(pt_type);
  }
}

/*
 * Nullable class
 */
template<typename T>
Nullable<T>::Nullable()
    : marked_null_(false) {
}

template<typename T>
template<typename U>
Nullable<T>::Nullable(const U& value)
    : T(value),
      marked_null_(false) {
}

template<typename T>
template<typename U>
Nullable<T>& Nullable<T>::operator=(const U& new_val) {
  this->T::operator=(new_val);
  return *this;
}

template<typename T>
bool Nullable<T>::is_valid() const {
  return is_null() || T::is_valid();
}

template<typename T>
bool Nullable<T>::is_initialized() const {
  return is_null() || T::is_initialized();
}

template<typename T>
bool Nullable<T>::is_null() const {
  return marked_null_;
}

template<typename T>
void Nullable<T>::set_to_null() {
  marked_null_ = true;
}

template<typename T>
void Nullable<T>::ReportErrors(ValidationReport* report) const {
  if (marked_null_) {
    // No error
  } else {
    T::ReportErrors(report);
  }
}

/*
 * Optional class
 */
template<typename T>
Optional<T>::Optional() {
}

template<typename T>
template<typename U>
Optional<T>::Optional(const U& value)
    : value_(value) {
}

template<typename T>
T& Optional<T>::operator*() {
  return value_;
}

template<typename T>
const T& Optional<T>::operator*() const {
  return value_;
}

template<typename T>
T* Optional<T>::operator->() {
  return &value_;
}

template<typename T>
const T* Optional<T>::operator->() const {
  return &value_;
}

template<typename T>
Optional<T>::operator const void*() const {
  return is_initialized() ? &value_ : NULL;
}

template<typename T>
bool Optional<T>::is_valid() const {
  return !value_.is_initialized() || value_.is_valid();
}

template<typename T>
bool Optional<T>::is_initialized() const {
  return value_.is_initialized();
}

template<typename T>
void Optional<T>::ReportErrors(ValidationReport* report) const {
  if (!is_initialized()) {
    // No error
  } else {
    value_.ReportErrors(report);
  }
}
template<typename T>
inline rpc::policy_table_interface_base::PolicyTableType Optional<T>::GetPolicyTableType() const {
  return policy_table_type_;
}

template<typename T>
void rpc::Optional<T>::SetPolicyTableType(rpc::policy_table_interface_base::PolicyTableType pt_type) {
  policy_table_type_ = pt_type;
  value_.SetPolicyTableType(pt_type);
}

/*
 * Stringifyable class
 */
template<typename T>
Stringifyable<T>::Stringifyable()
  : predefined_string_("") {
}

template<typename T>
template<typename U>
Stringifyable<T>::Stringifyable(const U& value)
  : T(value),
    predefined_string_("") {
}

template<typename T>
template<typename U>
Stringifyable<T>& Stringifyable<T>::operator=(const U& new_val) {
  this->T::operator=(new_val);
  return *this;
}

template<typename T>
bool Stringifyable<T>::is_valid() const {
  return is_string() || T::is_valid();
}

template<typename T>
bool Stringifyable<T>::is_initialized() const {
  return is_string() || T::is_initialized();
}

template<typename T>
bool Stringifyable<T>::is_string() const {
  return !predefined_string_.empty();
}

template<typename T>
std::string Stringifyable<T>::get_string() const {
  return predefined_string_;
}

template<typename T>
void Stringifyable<T>::set_to_string(const std::string& input) {
  predefined_string_ = input;
}

template<typename T>
void Stringifyable<T>::ReportErrors(ValidationReport* report) const {
  if (is_string()) {
    // No error
  } else {
    T::ReportErrors(report);
  }
}

}  // namespace rpc



#endif /* VALIDATED_TYPES_INL_H_ */