summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/endian/example/use_cases.cpp
blob: 7bbe9b28309544cf64072a2a6d9d05dc33499bde (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
//  endian/example/uses_cases.cpp  -----------------------------------------------------//

//  Copyright Beman Dawes 2014

//  Distributed under the Boost Software License, Version 1.0.
//  See http://www.boost.org/LICENSE_1_0.txt

//--------------------------------------------------------------------------------------//

#ifndef _SCL_SECURE_NO_WARNINGS
# define _SCL_SECURE_NO_WARNINGS
#endif

#ifndef _CRT_SECURE_NO_WARNINGS
# define _CRT_SECURE_NO_WARNINGS
#endif


#include <boost/endian/conversion.hpp>
#include <boost/endian/buffers.hpp>
#include <boost/endian/arithmetic.hpp>
#include <iostream>

using namespace boost::endian;

using std::cout;
using std::endl;




  { // Use case 2 - Endian buffer types

    struct Record
    {
      big_ubuf32_t count;  // big endian
      big_buf32_t  value;  // big endian
    };

    Record rec;

    read(&rec, sizeof(Record));

    uint32_t count = rec.count.value();
    int32_t value = rec.value.value();

    ++count;
    value += fee;

    rec.count = count;
    rec.value = value;

    write(&rec, sizeof(Record));
  }

  { // Use case 3a - Endian arithmetic types

    struct Record
    {
      big_uint32_t count;  // big endian
      big_int32_t  value;  // big endian
    };

    Record rec;

    read(&rec, sizeof(Record));

    ++rec.count;
    rec.value += fee;

    write(&rec, sizeof(Record));
  }

  { // Use case 3b - Endian arithmetic types

    struct Record
    {
      big_uint32_t count;  // big endian
      big_int32_t  value;  // big endian
    };

    Record rec;

    read(&rec, sizeof(Record));

    uint32_t count = rec.count;
    int32_t value = rec.value;

    ++count;
    value += fee;

    rec.count = count;
    rec.value = value;

    write(&rec, sizeof(Record));
  }

  //  Recommended approach when conversion time is not a concern
  //
  //  Conversion time is not a concert with this application because the minimum
  //  possible number of conversions is performed and because I/O time will be
  //  much greater than conversion time.

  {
    struct Record
    {
      big_uint32_t count;  // big endian
      big_int32_t  value;  // big endian
    };

    Record rec;

    read(&rec, sizeof(Record));

    ++rec.count;
    rec.value += fee;

    write(&rec, sizeof(Record));
  }

//  Recommended approach when conversion time is a concern
  //
  //  Conversion time is a concert with this application because (1) any conversions
  //  performed in the loop will consume a great deal of time and because (2)
  //  computation time will be much greater than I/O time.

  {
    struct Record
    {
      big_uint32_t count;  // big endian
      big_int32_t  value;  // big endian
    };

    Record rec;

    read(&rec, sizeof(Record));

    uint32_t count = rec.count;
    int32_t value = rec.value;

    for (long long i = 0; i < several_gazillion; ++i)  // (1)
    {
      ... immensely complex computation using rec variables many times // (2)
    }

    rec.count = count;
    rec.value = value;

    write(&rec, sizeof(Record));
  }

}