use_cases.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // endian/example/uses_cases.cpp -----------------------------------------------------//
  2. // Copyright Beman Dawes 2014
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // See http://www.boost.org/LICENSE_1_0.txt
  5. //--------------------------------------------------------------------------------------//
  6. #ifndef _SCL_SECURE_NO_WARNINGS
  7. # define _SCL_SECURE_NO_WARNINGS
  8. #endif
  9. #ifndef _CRT_SECURE_NO_WARNINGS
  10. # define _CRT_SECURE_NO_WARNINGS
  11. #endif
  12. #include <boost/endian/conversion.hpp>
  13. #include <boost/endian/buffers.hpp>
  14. #include <boost/endian/arithmetic.hpp>
  15. #include <iostream>
  16. using namespace boost::endian;
  17. using std::cout;
  18. using std::endl;
  19. { // Use case 2 - Endian buffer types
  20. struct Record
  21. {
  22. big_ubuf32_t count; // big endian
  23. big_buf32_t value; // big endian
  24. };
  25. Record rec;
  26. read(&rec, sizeof(Record));
  27. uint32_t count = rec.count.value();
  28. int32_t value = rec.value.value();
  29. ++count;
  30. value += fee;
  31. rec.count = count;
  32. rec.value = value;
  33. write(&rec, sizeof(Record));
  34. }
  35. { // Use case 3a - Endian arithmetic types
  36. struct Record
  37. {
  38. big_uint32_t count; // big endian
  39. big_int32_t value; // big endian
  40. };
  41. Record rec;
  42. read(&rec, sizeof(Record));
  43. ++rec.count;
  44. rec.value += fee;
  45. write(&rec, sizeof(Record));
  46. }
  47. { // Use case 3b - Endian arithmetic types
  48. struct Record
  49. {
  50. big_uint32_t count; // big endian
  51. big_int32_t value; // big endian
  52. };
  53. Record rec;
  54. read(&rec, sizeof(Record));
  55. uint32_t count = rec.count;
  56. int32_t value = rec.value;
  57. ++count;
  58. value += fee;
  59. rec.count = count;
  60. rec.value = value;
  61. write(&rec, sizeof(Record));
  62. }
  63. // Recommended approach when conversion time is not a concern
  64. //
  65. // Conversion time is not a concert with this application because the minimum
  66. // possible number of conversions is performed and because I/O time will be
  67. // much greater than conversion time.
  68. {
  69. struct Record
  70. {
  71. big_uint32_t count; // big endian
  72. big_int32_t value; // big endian
  73. };
  74. Record rec;
  75. read(&rec, sizeof(Record));
  76. ++rec.count;
  77. rec.value += fee;
  78. write(&rec, sizeof(Record));
  79. }
  80. // Recommended approach when conversion time is a concern
  81. //
  82. // Conversion time is a concert with this application because (1) any conversions
  83. // performed in the loop will consume a great deal of time and because (2)
  84. // computation time will be much greater than I/O time.
  85. {
  86. struct Record
  87. {
  88. big_uint32_t count; // big endian
  89. big_int32_t value; // big endian
  90. };
  91. Record rec;
  92. read(&rec, sizeof(Record));
  93. uint32_t count = rec.count;
  94. int32_t value = rec.value;
  95. for (long long i = 0; i < several_gazillion; ++i) // (1)
  96. {
  97. ... immensely complex computation using rec variables many times // (2)
  98. }
  99. rec.count = count;
  100. rec.value = value;
  101. write(&rec, sizeof(Record));
  102. }
  103. }