File: program.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (114 lines) | stat: -rw-r--r-- 3,016 bytes parent folder | download | duplicates (12)
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
// Example program for matching summary functions and synthetic child providers.
//
// The classes here simulate code generated by a serialization tool like, for
// example, protocol buffers. But the actual "generated" class layout is
// extremely naive to simplify the example.
//
// The idea is that we want to have generic formatters for a bunch of message
// classes, because they are all generated following common patterns, but the
// matching can't be based in the type name, because it can be anything.

#include <string>

class Message {
  // Dummy method definitions to illustrate a possible generic message API.
  std::string serialize() { return "TODO"; }
  Message* deserialize() {
    return nullptr;  // TODO.
  }
};

// This class could have been generated from a description like this. Assume
// fields are always optional, for simplicity (e.g. we don't care during
// serialization if a Customer has a name or not, we're just moving data around
// and validation happens elsewhere).
//
// message Customer {
//   string name;
//   int age;
//   string address;
// }
class Customer : public Message {
 private:
  int _internal_bookkeeping_bits_;

  // Presence bits. They are true if the field has been set.
  bool _has_name_ = false;
  bool _has_age_ = false;
  bool _has_address_ = false;

  // Actual field data.
  std::string name_;
  int age_;
  std::string address_;

 public:
  // Getters and setters.
  bool has_name() { return _has_name_; }
  bool has_age() { return _has_age_; }
  bool has_address() { return _has_address_; }

  std::string name() { return name_; }
  int age() { return age_; }
  std::string address() { return address_; }

  void set_name(std::string name) {
    name_ = name;
    _has_name_ = true;
  }
  void set_age(int age) {
    age_ = age;
    _has_age_ = true;
  }
  void set_address(std::string address) {
    address_ = address;
    _has_address_ = true;
  }
};

// message ProductOrder {
//   string product_name;
//   int amount;
// }
class ProductOrder : public Message {
 private:
  int _internal_bookkeeping_bits_;

  // Presence bits. They are true if the field has been set.
  bool _has_product_name_ = false;
  bool _has_amount_ = false;

  // Actual field data.
  std::string product_name_;
  int amount_;

 public:
  // Getters and setters.
  bool has_product_name() { return _has_product_name_; }
  bool has_amount() { return _has_amount_; }

  std::string get_product_name() { return product_name_; }
  int get_amount() { return amount_; }

  void set_product_name(std::string product_name) {
    product_name_ = product_name;
    _has_product_name_ = true;
  }
  void set_amount(int amount) {
    amount_ = amount;
    _has_amount_ = true;
  }
};

int main(int argc, char **argv) {
  Customer customer;
  customer.set_name("C. Ustomer");
  customer.set_address("123 Fake St.");
  // no age, so we can check absent fields get omitted.

  ProductOrder order;
  order.set_product_name("widget");
  order.set_amount(100);
  return 0;  // break here.
}