|
| 1 | +/** @file |
| 2 | + * |
| 3 | + * @brief Example code demonstrating use of @c chops::shared_buffer and |
| 4 | + * @c chops::repeat. See @c threaded_wait_shared_demo.cpp for multithreaded |
| 5 | + * example. |
| 6 | + * |
| 7 | + * @ingroup example_module |
| 8 | + * |
| 9 | + * @author Thurman Gillespy |
| 10 | + * |
| 11 | + * Copyright (c)2019 by Thurman Gillespy |
| 12 | + * 3/22/19 |
| 13 | + * |
| 14 | + * Distributed under the Boost Software License, Version 1.0. |
| 15 | + * (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 16 | + * |
| 17 | + * Sample make file: |
| 18 | + * g++ -std=c++17 -I ~/Projects/utility-rack/include/ \ |
| 19 | + * -I ~/Projects/boost_1_69_0/ \ |
| 20 | + * shared_buffer_demo.cpp |
| 21 | + * |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +#include <iostream> |
| 26 | +#include <cstdlib> // EXIT_SUCCESS |
| 27 | +#include <cstddef> // std::byte |
| 28 | +#include <cstdint> // std::uint16_t |
| 29 | +#include <string> |
| 30 | + |
| 31 | +#include "marshall/extract_append.hpp" |
| 32 | +#include "marshall/shared_buffer.hpp" |
| 33 | +#include "utility/repeat.hpp" |
| 34 | + |
| 35 | +// tasty utility lambda function |
| 36 | +constexpr auto printLn = [] () { std::cout << std::endl; }; |
| 37 | + |
| 38 | +// utility functions for casting @c wait_queue.data() |
| 39 | +template <class C> |
| 40 | +const char* cast_to_char_ptr (const C& buf) { |
| 41 | + return static_cast<const char*> (static_cast<const void*> (buf.data())); |
| 42 | +} |
| 43 | + |
| 44 | +template <class C> |
| 45 | +const std::uint16_t* cast_to_uint16_ptr (const C& buf) { |
| 46 | + return static_cast<const std::uint16_t*> (static_cast<const void*> (buf.data())); |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +int main() { |
| 51 | + |
| 52 | + // create empty shared buffer1 |
| 53 | + chops::mutable_shared_buffer buf1; |
| 54 | + |
| 55 | + std::cout << "buffer1 contains " << buf1.size() << " bytes" << std::endl; |
| 56 | + // c-string to add to buffer1 |
| 57 | + constexpr char str1[] = "A cat in the hat."; |
| 58 | + const char* strptr = str1; |
| 59 | + |
| 60 | + // add one char at a time, inside chops::repeat |
| 61 | + chops::repeat(sizeof(str1), |
| 62 | + [&] () { buf1.append(static_cast<std::byte> (*strptr++)); }); |
| 63 | + |
| 64 | + // what str1 and chops::repeat replaces |
| 65 | + // buf1.append(static_cast<std::byte>('A'); |
| 66 | + // buf1.append((static_cast<std::byte>(' '); |
| 67 | + // buf1.append((static_cast<std::byte>('c'); |
| 68 | + // etc. |
| 69 | + |
| 70 | + std::cout << "buffer1 contains " << buf1.size() << " bytes" << std::endl; |
| 71 | + // print the output, one char at a time |
| 72 | + const char* byte = cast_to_char_ptr (buf1); // data starts here |
| 73 | + for (unsigned int i = 0; i < buf1.size(); ++i) { |
| 74 | + std::cout << *byte++; |
| 75 | + } |
| 76 | + printLn(); |
| 77 | + |
| 78 | + // append a string with one call to append |
| 79 | + buf1.clear(); // empty the buffer |
| 80 | + std::cout << "buffer1 contains " << buf1.size() << " bytes" << std::endl; |
| 81 | + const std::string str = "Green eggs and ham."; |
| 82 | + // convert str to C-string, add to buffer1 |
| 83 | + buf1.append(str.c_str(), str.size() + 1); |
| 84 | + std::cout << "buffer1 contains " << buf1.size() << " bytes" << std::endl; |
| 85 | + // print c-string |
| 86 | + std::cout << cast_to_char_ptr (buf1) << std::endl; |
| 87 | + |
| 88 | + |
| 89 | + // write some short ints to a buffer |
| 90 | + constexpr int NUM_INTS = 15; |
| 91 | + chops::mutable_shared_buffer buf2(NUM_INTS * sizeof(std::uint16_t)); |
| 92 | + std::cout << "buffer2 contains " << buf2.size() << " bytes and "; |
| 93 | + std::cout << (buf2.size()/sizeof(std::uint16_t)) << " short integers\n"; |
| 94 | + |
| 95 | + // input some numbers using chops::repeat |
| 96 | + const std::uint16_t* data = cast_to_uint16_ptr (buf2);// data starts here |
| 97 | + std::uint16_t* valptr = const_cast<std::uint16_t*>(data); // remove const* |
| 98 | + |
| 99 | + // create number, convert to 'network' (big endian) byte order, place into buf2 |
| 100 | + std::uint16_t count = 1; |
| 101 | + chops::repeat(NUM_INTS, [count, x = buf2.data()] () mutable {auto sz = |
| 102 | + chops::append_val <std::uint16_t> (x, count++ * 5); x += sz; }); |
| 103 | + |
| 104 | + // print them out |
| 105 | + valptr = const_cast<std::uint16_t*> (data); |
| 106 | + // read 2 bytes, convert back to proper endian order, print |
| 107 | + auto f = [x = buf2.data()] () mutable { std::cout << chops::extract_val<std::uint16_t>(x) << " "; x+=2;}; |
| 108 | + chops::repeat(NUM_INTS, f); |
| 109 | + printLn(); |
| 110 | + |
| 111 | + // swap the buffers, print result |
| 112 | + buf2.swap(buf1); |
| 113 | + std::cout << "buffer2 contents after swap" << std::endl; |
| 114 | + std::cout << cast_to_char_ptr (buf2) << std::endl; |
| 115 | + std::cout << "buffer1 contents after swap" << std::endl; |
| 116 | + valptr = const_cast<std::uint16_t*> (data); |
| 117 | + chops::repeat(NUM_INTS, f); |
| 118 | + printLn(); |
| 119 | + |
| 120 | + return EXIT_SUCCESS; |
| 121 | +} |
0 commit comments