Line data Source code
1 : // Copyright 2024 Accenture. 2 : 3 : #include "estd/type_utils.h" 4 : 5 : #include <gtest/gtest.h> 6 : 7 : // clang-format off 8 : namespace 9 : { 10 : void example_cast_to_type() 11 : { 12 : // [EXAMPLE_BEGIN cast to type] 13 1 : int16_t data[4] = {55, 55 , 35, 25}; 14 1 : uint8_t* raw_data = reinterpret_cast<uint8_t*>(data); 15 : estd::type_utils<int>::cast_to_type(raw_data, 2); 16 : // [EXAMPLE_END cast to type] 17 : } 18 : void example_const_cast_to_type() 19 : { 20 : // [EXAMPLE_BEGIN cast const to type] 21 1 : int16_t data[4] = {55, 55 , 35, 25}; 22 1 : uint8_t* raw_data = reinterpret_cast<uint8_t*>(data); 23 : estd::type_utils<uint16_t>::cast_const_to_type(raw_data, 2); 24 : // *result = 12; //Cannot modify the value, since its a const data 25 : // [EXAMPLE_END cast const to type] 26 : } 27 : 28 : void example_cast_to_raw() 29 : { 30 : // [EXAMPLE_BEGIN cast to raw] 31 : int value = 42; 32 1 : int* int_ptr = &value; 33 : estd::type_utils<int>::cast_to_raw(int_ptr); 34 : // Now, you can manipulate raw_ptr to read or modify the underlying bytes. 35 : // [EXAMPLE_END cast to raw] 36 : } 37 : 38 : void example_cast_from_void() 39 : { 40 : // [EXAMPLE_BEGIN cast from void] 41 : int value = 42; 42 1 : void* void_ptr = &value; 43 : estd::type_utils<int>::cast_from_void(void_ptr); 44 : // Now, you can use int_ptr as a pointer to an integer. 45 : // [EXAMPLE_END cast from void] 46 : } 47 : 48 : void example_const_cast_from_void() 49 : { 50 : // [EXAMPLE_BEGIN const cast from void] 51 : int data = 10; 52 1 : void* vPtr = &data; 53 : estd::type_utils<int>::const_cast_from_void(vPtr); 54 : // *cPtr = 20; //Cannot modify the value, since its a const data 55 : // Now, you have a const int pointer. 56 : // [EXAMPLE_END const cast from void] 57 : } 58 : 59 : void example_cast_to_void() 60 : { 61 : // [EXAMPLE_BEGIN cast to void] 62 : int value = 42; 63 1 : int* int_ptr = &value; 64 : estd::type_utils<int>::cast_to_void(int_ptr); 65 : // Now, void_ptr can be used to store or pass the integer pointer. 66 : // [EXAMPLE_END cast to void] 67 : } 68 : 69 : void example_const_cast_to_void() 70 : { 71 : // [EXAMPLE_BEGIN const cast to void] 72 : const int value = 42; 73 1 : const int* int_ptr = &value; 74 : estd::type_utils<int>::const_cast_to_void(int_ptr); 75 : // Now, void_ptr can be used to store or pass the integer pointer. 76 : // [EXAMPLE_END const cast to void] 77 : } 78 : } // namespace 79 : 80 3 : TEST( type_utils, run_examples) 81 : { 82 1 : example_cast_to_type(); 83 1 : example_const_cast_to_type(); 84 1 : example_cast_to_void(); 85 1 : example_const_cast_from_void(); 86 1 : example_cast_from_void(); 87 1 : example_cast_to_raw(); 88 1 : example_const_cast_to_void(); 89 1 : }