Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_rpc / size_report / base_with_nanopb.cc
1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pb_decode.h"
16 #include "pb_encode.h"
17 #include "pw_assert/assert.h"
18 #include "pw_bloat/bloat_this_binary.h"
19 #include "pw_log/log.h"
20 #include "pw_sys_io/sys_io.h"
21
22 int volatile* unoptimizable;
23
24 namespace my_product {
25
26 template <typename DecodeFunction>
27 struct NanopbTraits;
28
29 template <typename FieldsType>
30 struct NanopbTraits<bool(pb_istream_t*, FieldsType, void*)> {
31   using Fields = FieldsType;
32 };
33
34 using Fields = typename NanopbTraits<decltype(pb_decode)>::Fields;
35
36 // Performs the core nanopb encode and decode operations so that those functions
37 // are included in the binary.
38 void DoNanopbStuff() {
39   std::byte buffer[128];
40   void* fields = &buffer;
41
42   auto output = pb_ostream_from_buffer(reinterpret_cast<pb_byte_t*>(buffer),
43                                        sizeof(buffer));
44   pb_encode(&output, static_cast<Fields>(fields), buffer);
45
46   auto input = pb_istream_from_buffer(
47       reinterpret_cast<const pb_byte_t*>(buffer), sizeof(buffer));
48   pb_decode(&input, static_cast<Fields>(fields), buffer);
49 }
50
51 }  // namespace my_product
52
53 int main() {
54   pw::bloat::BloatThisBinary();
55   my_product::DoNanopbStuff();
56
57   // Ensure we are paying the cost for log and assert.
58   PW_CHECK_INT_GE(*unoptimizable, 0, "Ensure this CHECK logic stays");
59   PW_LOG_INFO("We care about optimizing: %d", *unoptimizable);
60
61   std::byte packet_buffer[128];
62   pw::sys_io::ReadBytes(packet_buffer);
63   pw::sys_io::WriteBytes(packet_buffer);
64
65   return static_cast<int>(packet_buffer[92]);
66 }