summaryrefslogtreecommitdiffstats
path: root/buffertest
diff options
context:
space:
mode:
Diffstat (limited to 'buffertest')
-rw-r--r--buffertest/CMakeLists.txt26
-rw-r--r--buffertest/calendar.fbs12
-rw-r--r--buffertest/main.cpp38
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 @@
1project(toynadi_buffertest)
2
3include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
4
5set(toynadinbuffertest_SRCS
6 main.cpp
7)
8
9set(SCHEMAS calendar.fbs)
10set(SCHEMA_SOURCEFILES calendar_generated.h)
11
12add_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
20SET_SOURCE_FILES_PROPERTIES(${SCHEMA_SOURCEFILES} PROPERTIES GENERATED 1)
21ADD_CUSTOM_TARGET(generate_buffers ALL DEPENDS ${SCHEMA_SOURCEFILES})
22
23add_executable(${PROJECT_NAME} ${toynadinbuffertest_SRCS})
24qt5_use_modules(${PROJECT_NAME} Core)
25install(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
3namespace Calendar;
4
5table Event {
6 summary:string;
7 description:string;
8 attachment:[byte];
9}
10
11root_type Event;
12file_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
5using namespace Calendar;
6using namespace flatbuffers;
7
8std::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
26void readEvent(const std::string &data)
27{
28 auto readEvent = GetEvent(data.c_str());
29 std::cout << readEvent->summary()->c_str() << std::endl;
30}
31
32int main(int argc, char **argv)
33{
34 std::ofstream myfile;
35 myfile.open ("buffer.fb");
36 myfile << createEvent();
37 myfile.close();
38}