diff options
Diffstat (limited to 'buffertest')
-rw-r--r-- | buffertest/CMakeLists.txt | 26 | ||||
-rw-r--r-- | buffertest/calendar.fbs | 12 | ||||
-rw-r--r-- | buffertest/main.cpp | 38 |
3 files changed, 76 insertions, 0 deletions
diff --git a/buffertest/CMakeLists.txt b/buffertest/CMakeLists.txt new file mode 100644 index 0000000..9505b75 --- /dev/null +++ b/buffertest/CMakeLists.txt | |||
@@ -0,0 +1,26 @@ | |||
1 | project(toynadi_buffertest) | ||
2 | |||
3 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) | ||
4 | |||
5 | set(toynadinbuffertest_SRCS | ||
6 | main.cpp | ||
7 | ) | ||
8 | |||
9 | set(SCHEMAS calendar.fbs) | ||
10 | set(SCHEMA_SOURCEFILES calendar_generated.h) | ||
11 | |||
12 | add_custom_command(OUTPUT ${SCHEMA_SOURCEFILES} | ||
13 | COMMAND flatc -c ${CMAKE_CURRENT_SOURCE_DIR}/calendar.fbs | ||
14 | COMMENT "Generating buffers" | ||
15 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} | ||
16 | DEPENDS ${SCHEMAS} | ||
17 | VERBATIM | ||
18 | ) | ||
19 | |||
20 | SET_SOURCE_FILES_PROPERTIES(${SCHEMA_SOURCEFILES} PROPERTIES GENERATED 1) | ||
21 | ADD_CUSTOM_TARGET(generate_buffers ALL DEPENDS ${SCHEMA_SOURCEFILES}) | ||
22 | |||
23 | add_executable(${PROJECT_NAME} ${toynadinbuffertest_SRCS}) | ||
24 | qt5_use_modules(${PROJECT_NAME} Core) | ||
25 | install(TARGETS ${PROJECT_NAME} DESTINATION bin) | ||
26 | |||
diff --git a/buffertest/calendar.fbs b/buffertest/calendar.fbs new file mode 100644 index 0000000..203ee43 --- /dev/null +++ b/buffertest/calendar.fbs | |||
@@ -0,0 +1,12 @@ | |||
1 | // example IDL file | ||
2 | |||
3 | namespace Calendar; | ||
4 | |||
5 | table Event { | ||
6 | summary:string; | ||
7 | description:string; | ||
8 | attachment:[byte]; | ||
9 | } | ||
10 | |||
11 | root_type Event; | ||
12 | file_identifier "AKFB"; | ||
diff --git a/buffertest/main.cpp b/buffertest/main.cpp new file mode 100644 index 0000000..5bc2326 --- /dev/null +++ b/buffertest/main.cpp | |||
@@ -0,0 +1,38 @@ | |||
1 | #include "calendar_generated.h" | ||
2 | #include <iostream> | ||
3 | #include <fstream> | ||
4 | |||
5 | using namespace Calendar; | ||
6 | using namespace flatbuffers; | ||
7 | |||
8 | std::string createEvent() | ||
9 | { | ||
10 | FlatBufferBuilder fbb; | ||
11 | { | ||
12 | auto summary = fbb.CreateString("summary"); | ||
13 | const int attachmentSize = 1024 * 1024; // 1MB | ||
14 | int8_t rawData[attachmentSize]; | ||
15 | auto data = fbb.CreateVector(rawData, attachmentSize); | ||
16 | |||
17 | Calendar::EventBuilder eventBuilder(fbb); | ||
18 | eventBuilder.add_summary(summary); | ||
19 | eventBuilder.add_attachment(data); | ||
20 | auto eventLocation = eventBuilder.Finish(); | ||
21 | FinishEventBuffer(fbb, eventLocation); | ||
22 | } | ||
23 | return std::string(reinterpret_cast<const char *>(fbb.GetBufferPointer()), fbb.GetSize()); | ||
24 | } | ||
25 | |||
26 | void readEvent(const std::string &data) | ||
27 | { | ||
28 | auto readEvent = GetEvent(data.c_str()); | ||
29 | std::cout << readEvent->summary()->c_str() << std::endl; | ||
30 | } | ||
31 | |||
32 | int main(int argc, char **argv) | ||
33 | { | ||
34 | std::ofstream myfile; | ||
35 | myfile.open ("buffer.fb"); | ||
36 | myfile << createEvent(); | ||
37 | myfile.close(); | ||
38 | } | ||