Design Pattern Examples
Overview of object-oriented design patterns
Adapter_Exercise.cpp
Go to the documentation of this file.
1
5
6#include "Adapter_Exercise.h"
7
9
10#include <iostream>
11
13{
14
22 // ! [Using Adapter in C++]
24 {
25 std::cout << std::endl;
26 std::cout << "Adapter Exercise" << std::endl;
27 try
28 {
30
31 uint32_t memoryBlockSize = dataReaderWriter.GetMemoryBlockByteSize();
32
33 std::vector<uint8_t> readData = dataReaderWriter.Read(0, memoryBlockSize);
34 std::string dataDump =
35 dataReaderWriter.BufferToString(readData, memoryBlockSize, 2);
36 std::cout << " Initial memory block contents:" << std::endl;
37 std::cout << dataDump << std::endl;
38
39 // Create the data to be written
40 uint32_t dataSize = 16;
41 int byteOffset = 41;
42 std::vector<uint8_t> writeData(dataSize);
43 for (uint32_t index = 0; index < dataSize; ++index)
44 {
45 writeData[index] = static_cast<uint8_t>(index+1);
46 }
47
48 // Display the data to be written
49 dataDump = dataReaderWriter.BufferToString(writeData, dataSize, 2);
50 std::cout << " Data to be written to memory block:" << std::endl;
51 std::cout << dataDump << std::endl;
52
53 std::cout << " Writing data to byte offset " << byteOffset << "..." << std::endl;
54 // Write the data to the external component
55 dataReaderWriter.Write(byteOffset, writeData, dataSize);
56
57 std::cout << " Reading back the memory block..." << std::endl;
58 // Read the data from the external component
59 readData = dataReaderWriter.Read(0, memoryBlockSize);
60 std::cout << std::endl;
61
62 // Display the data read back.
63 dataDump = dataReaderWriter.BufferToString(readData, memoryBlockSize, 2);
64 std::cout << " Current memory block contents:" << std::endl;
65 std::cout << dataDump << std::endl;
66 }
68 {
69 std::cout << "Error with startup or shutdown! " << e.what()
70 << std::endl;
71 }
73 {
74 std::cout << "Error with reading or writing! " << e.what()
75 << std::endl;
76 }
77
78 std::cout << " Done." << std::endl;
79 }
80 // ! [Using Adapter in C++]
81
82
83} // end namespace
Declaration of the DataReaderWriter class used in the Adapter Pattern.
Represents an error that occurred when reading or writing data in the Data reader/writer.
Represents a data reader/writer to a caller.
uint32_t GetMemoryBlockByteSize()
Retrieve the size of the currently opened memory block in bytes.
std::string BufferToString(const ByteArray &data, uint32_t maxBytes, int indent)
Convert the specified data up to the specified number of bytes into a string by performing a "hex dum...
void Write(int byteOffset, const ByteArray &data, uint32_t maxBytes)
Write a specified number of bytes.
ByteArray Read(int byteOffset, uint32_t maxBytes)
Read a specified number of bytes.
Represents an error that occurred during initialization or shut down of the Data reader/writer.
const char * what()
Override exception::what() to return the message we are tracking.
Declaration of the Adapter_Exercise() function as used in the Adapter Pattern.
The namespace containing all Design Pattern Examples implemented in C++.
void Adapter_Exercise()
Example of using the Adapter Pattern.