• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  endian/example/uses_cases.cpp  -----------------------------------------------------//
2 
3 //  Copyright Beman Dawes 2014
4 
5 //  Distributed under the Boost Software License, Version 1.0.
6 //  See http://www.boost.org/LICENSE_1_0.txt
7 
8 //--------------------------------------------------------------------------------------//
9 
10 #ifndef _SCL_SECURE_NO_WARNINGS
11 # define _SCL_SECURE_NO_WARNINGS
12 #endif
13 
14 #ifndef _CRT_SECURE_NO_WARNINGS
15 # define _CRT_SECURE_NO_WARNINGS
16 #endif
17 
18 
19 #include <boost/endian/conversion.hpp>
20 #include <boost/endian/buffers.hpp>
21 #include <boost/endian/arithmetic.hpp>
22 #include <iostream>
23 
24 using namespace boost::endian;
25 
26 using std::cout;
27 using std::endl;
28 
29 
30 
31 
32   { // Use case 2 - Endian buffer types
33 
34     struct Record
35     {
36       big_ubuf32_t count;  // big endian
37       big_buf32_t  value;  // big endian
38     };
39 
40     Record rec;
41 
42     read(&rec, sizeof(Record));
43 
44     uint32_t count = rec.count.value();
45     int32_t value = rec.value.value();
46 
47     ++count;
48     value += fee;
49 
50     rec.count = count;
51     rec.value = value;
52 
53     write(&rec, sizeof(Record));
54   }
55 
56   { // Use case 3a - Endian arithmetic types
57 
58     struct Record
59     {
60       big_uint32_t count;  // big endian
61       big_int32_t  value;  // big endian
62     };
63 
64     Record rec;
65 
66     read(&rec, sizeof(Record));
67 
68     ++rec.count;
69     rec.value += fee;
70 
71     write(&rec, sizeof(Record));
72   }
73 
74   { // Use case 3b - Endian arithmetic types
75 
76     struct Record
77     {
78       big_uint32_t count;  // big endian
79       big_int32_t  value;  // big endian
80     };
81 
82     Record rec;
83 
84     read(&rec, sizeof(Record));
85 
86     uint32_t count = rec.count;
87     int32_t value = rec.value;
88 
89     ++count;
90     value += fee;
91 
92     rec.count = count;
93     rec.value = value;
94 
95     write(&rec, sizeof(Record));
96   }
97 
98   //  Recommended approach when conversion time is not a concern
99   //
100   //  Conversion time is not a concert with this application because the minimum
101   //  possible number of conversions is performed and because I/O time will be
102   //  much greater than conversion time.
103 
104   {
105     struct Record
106     {
107       big_uint32_t count;  // big endian
108       big_int32_t  value;  // big endian
109     };
110 
111     Record rec;
112 
113     read(&rec, sizeof(Record));
114 
115     ++rec.count;
116     rec.value += fee;
117 
118     write(&rec, sizeof(Record));
119   }
120 
121 //  Recommended approach when conversion time is a concern
122   //
123   //  Conversion time is a concert with this application because (1) any conversions
124   //  performed in the loop will consume a great deal of time and because (2)
125   //  computation time will be much greater than I/O time.
126 
127   {
128     struct Record
129     {
130       big_uint32_t count;  // big endian
131       big_int32_t  value;  // big endian
132     };
133 
134     Record rec;
135 
136     read(&rec, sizeof(Record));
137 
138     uint32_t count = rec.count;
139     int32_t value = rec.value;
140 
141     for (long long i = 0; i < several_gazillion; ++i)  // (1)
142     {
143       ... immensely complex computation using rec variables many times // (2)
144     }
145 
146     rec.count = count;
147     rec.value = value;
148 
149     write(&rec, sizeof(Record));
150   }
151 
152 }
153