Skip to content

Commit b4aa707

Browse files
committed
split serialization code into header/source file
1 parent f7f320f commit b4aa707

4 files changed

Lines changed: 134 additions & 31 deletions

File tree

CMakeLists.txt

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,39 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
77
set(CMAKE_CXX_EXTENSIONS OFF)
88

99
# Standard CMake toggle:
10-
# -DBUILD_SHARED_LIBS=ON -> SHARED
11-
# -DBUILD_SHARED_LIBS=OFF -> STATIC
10+
# -DBUILD_SHARED_LIBS=ON -> SHARED
11+
# -DBUILD_SHARED_LIBS=OFF -> STATIC
1212
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
1313

1414
# Custom CMake toggle:
15-
# -DCORELIB_USE_STD_FILESYSTEM=ON -> (Default) use <filesystem>
16-
# -DCORELIB_USE_STD_FILESYSTEM=OFF -> use <boost/filesystem.hpp>
15+
# -DCORELIB_USE_STD_FILESYSTEM=ON -> (Default) use <filesystem>
16+
# -DCORELIB_USE_STD_FILESYSTEM=OFF -> use <boost/filesystem.hpp>
1717
option(CORELIB_USE_STD_FILESYSTEM "Use <filesystem> instead of <boost/filesystem.hpp>" ON)
1818

1919
# Custom CMake toggle:
20-
# -DCORELIB_USE_FLATBUFFERS=ON -> support Google flatbuffers
21-
# -DCORELIB_USE_FLATBUFFERS=OFF -> do not support Google flatbuffers
20+
# -DCORELIB_USE_FLATBUFFERS=ON -> support Google flatbuffers
21+
# -DCORELIB_USE_FLATBUFFERS=OFF -> do not support Google flatbuffers
2222
option(CORELIB_USE_FLATBUFFERS "Support Google flatbuffers." OFF)
2323

2424
# Custom CMake toggle:
25-
# -DCORELIB_SOCKET_DEBUG=ON -> Extra debug to log.
26-
# -DCORELIB_SOCKET_DEBUG=OFF -> (Default) No extra debug to log/
25+
# -DCORELIB_SOCKET_DEBUG=ON -> Extra debug to log.
26+
# -DCORELIB_SOCKET_DEBUG=OFF -> (Default) No extra debug to log/
2727
option(CORELIB_SOCKET_DEBUG "Use extra debug output from Asio classes" OFF)
2828

2929
# Local env vars for this CMakelists file, must be set before calling CMake configure
3030
# and as these are cached will be rememebr until the CMake cache is cleared.
3131
#
3232
# Path that is parent to the /boost include directory.
3333
set(CORELIB_BOOST_ROOT $ENV{CORELIB_BOOST_ROOT} CACHE PATH "Boost root")
34+
3435
# Path to the correct Boost lib folder for your compiler and platform.
3536
set(CORELIB_BOOST_LIB $ENV{CORELIB_BOOST_LIB} CACHE PATH "Boost libs")
37+
3638
# Name modifier for boost libs.
37-
set (CORELIB_BOOST_LIB_NAME_STUB $ENV{CORELIB_BOOST_LIB_NAME_STUB} CACHE PATH "Boost lib name stub")
39+
set(CORELIB_BOOST_LIB_NAME_STUB $ENV{CORELIB_BOOST_LIB_NAME_STUB} CACHE PATH "Boost lib name stub")
40+
3841
# Name modifier for boost libs in debug mode.
39-
set (CORELIB_BOOST_LIB_NAME_STUB_D $ENV{CORELIB_BOOST_LIB_NAME_STUB_D} CACHE PATH "Boost lib name stub (debug)")
42+
set(CORELIB_BOOST_LIB_NAME_STUB_D $ENV{CORELIB_BOOST_LIB_NAME_STUB_D} CACHE PATH "Boost lib name stub (debug)")
4043

4144
# Check required paths are configured and exist on disk.
4245
foreach(v CORELIB_BOOST_ROOT CORELIB_BOOST_LIB)
@@ -75,6 +78,7 @@ add_library(CoreLibrary
7578
Source/DebugLog/DebugLogSingleton.cpp
7679
Source/CsvGrid/CsvGridCell.cpp
7780
Source/CsvGrid/CsvGridCellDouble.cpp
81+
Source/Serialization/SerializeToVector.cpp
7882
Source/Asio/AsioDefines.cpp
7983
Source/Asio/IoContextThreadGroup.cpp
8084
Source/Asio/MessageUtils.cpp
@@ -143,14 +147,15 @@ target_include_directories(CoreLibrary PUBLIC
143147
)
144148

145149
# --- Generator-expression helpers ---
146-
set(_CORELIB_WIN "$<PLATFORM_ID:Windows>")
150+
set(_CORELIB_WIN "$<PLATFORM_ID:Windows>")
147151
set(_CORELIB_LINUX "$<PLATFORM_ID:Linux>")
148-
set(_CORELIB_DBG "$<CONFIG:Debug>")
149-
set(_CORELIB_REL "$<IN_LIST:$<CONFIG>,Release;RelWithDebInfo;MinSizeRel>")
152+
set(_CORELIB_DBG "$<CONFIG:Debug>")
153+
set(_CORELIB_REL "$<IN_LIST:$<CONFIG>,Release;RelWithDebInfo;MinSizeRel>")
150154
set(_CORELIB_NEED_BOOST_FS "$<NOT:$<BOOL:${CORELIB_USE_STD_FILESYSTEM}>>")
151155
set(_CORELIB_NEED_FLATBUFFERS "$<BOOL:${CORELIB_USE_FLATBUFFERS}>")
152156

153157
target_link_libraries(CoreLibrary PUBLIC
158+
154159
# Linux
155160
$<${_CORELIB_LINUX}:${CORELIB_BOOST_LIB}/libboost_system.a>
156161
$<${_CORELIB_LINUX}:${CORELIB_BOOST_LIB}/libboost_thread.a>
@@ -162,6 +167,7 @@ target_link_libraries(CoreLibrary PUBLIC
162167

163168
# Windows
164169
$<$<PLATFORM_ID:Windows>:iphlpapi>
170+
165171
# Windows Release-ish
166172
$<$<AND:${_CORELIB_WIN},${_CORELIB_REL}>:${CORELIB_BOOST_LIB}/libboost_thread-${CORELIB_BOOST_LIB_NAME_STUB}.lib>
167173
$<$<AND:${_CORELIB_WIN},${_CORELIB_REL}>:${CORELIB_BOOST_LIB}/libboost_locale-${CORELIB_BOOST_LIB_NAME_STUB}.lib>

Include/Serialization/SerializeToVector.h

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -441,15 +441,7 @@ char_vector_t ToCharVectorFlatBuf(const T& object, PackFunc packFunc)
441441
*
442442
* auto buf = ToCharVectorFlatBuf(builder);
443443
*/
444-
char_vector_t ToCharVectorFlatBuf(flatbuffers::FlatBufferBuilder builder)
445-
{
446-
const auto* p = builder.GetBufferPointer(); // const uint8_t*
447-
const auto n = builder.GetSize(); // size_t
448-
449-
char_vector_t out(n);
450-
std::memcpy(out.data(), p, n);
451-
return out;
452-
}
444+
CORE_LIBRARY_DLL_SHARED_API char_vector_t ToCharVectorFlatBuf(flatbuffers::FlatBufferBuilder builder);
453445

454446
/*!
455447
* \brief Function to serialize object via flatbuffers
@@ -513,15 +505,7 @@ void ToCharVectorFlatBuf(const T& object, char_vector_t& out, PackFunc packFunc)
513505
* std::vector<char> out;
514506
* ToCharVectorFlatBuf(builder, out);
515507
*/
516-
void ToCharVectorFlatBuf(flatbuffers::FlatBufferBuilder builder, char_vector_t& out)
517-
{
518-
const auto* p = builder.GetBufferPointer(); // const uint8_t*
519-
const auto n = builder.GetSize(); // size_t
520-
521-
out.resize(n);
522-
std::memcpy(out.data(), p, n);
523-
}
524-
508+
CORE_LIBRARY_DLL_SHARED_API void ToCharVectorFlatBuf(flatbuffers::FlatBufferBuilder builder, char_vector_t& out);
525509

526510
/*!
527511
* \brief Deserialize a char vector into a corresponding object.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// This file is part of CoreLibrary containing useful reusable utility
2+
// classes.
3+
//
4+
// Copyright (C) 2014 to present, Duncan Crutchley
5+
// Contact <15799155+dac1976@users.noreply.github.com>
6+
//
7+
// This program is free software: you can redistribute it and/or modify
8+
// it under the terms of the GNU Lesser General Public License as published
9+
// by the Free Software Foundation, either version 3 of the License, or
10+
// (at your option) any later version.
11+
//
12+
// This program is distributed in the hope that it will be useful,
13+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
// GNU General Public License and GNU Lesser General Public License
16+
// for more details.
17+
//
18+
// You should have received a copy of the GNU General Public License
19+
// and GNU Lesser General Public License along with this program. If
20+
// not, see <http://www.gnu.org/licenses/>.
21+
22+
/*!
23+
* \file SerializeToVector.cpp
24+
* \brief File containing utilities to serialize objects to byte vectors.
25+
*/
26+
#include "Serialization/SerializeToVector.h"
27+
28+
/*! \brief The core_lib namespace. */
29+
namespace core_lib
30+
{
31+
/*! \brief The serialize namespace. */
32+
namespace serialize
33+
{
34+
35+
#if defined(USE_FLATBUFFERS)
36+
/*!
37+
* \brief Function to serialize object via flatbuffers
38+
* \param[in] builder - A falttbuffer builder already initialised with the message fields.
39+
* \return Char vector containing serialized object
40+
*
41+
* This overload creates new memory but has best performance regarding creating a message view object
42+
* but the usage is a little more clumsy and less encapsulated.
43+
*
44+
* Example of how to create objectOffset:
45+
*
46+
* flatbuffers::FlatBufferBuilder builder;
47+
*
48+
* auto name_off = builder.CreateString("I am a test message");
49+
* uint64_t counter = 666;
50+
* auto values_off = builder.CreateVector(std::vector<double>(100, 666.666));
51+
*
52+
* auto objectOffset = core_lib_test_fb::CreateTestMessage(
53+
* builder,
54+
* name_off,
55+
* counter,
56+
* values_off
57+
* );
58+
*
59+
* builder.Finish(objectOffset);
60+
*
61+
* auto buf = ToCharVectorFlatBuf(builder);
62+
*/
63+
char_vector_t ToCharVectorFlatBuf(flatbuffers::FlatBufferBuilder builder)
64+
{
65+
const auto* p = builder.GetBufferPointer(); // const uint8_t*
66+
const auto n = builder.GetSize(); // size_t
67+
68+
char_vector_t out(n);
69+
std::memcpy(out.data(), p, n);
70+
return out;
71+
}
72+
73+
/*!
74+
* \brief Function to serialize object via flatbuffers
75+
* \param[in] builder - A falttbuffer builder already initialised with the message fields.
76+
* \param[out] out - Char vector to be filled with serialized object
77+
*
78+
* This overload uses the passed in memory (unless it needs resizing) but has best
79+
* performance regarding creating a message view object but the usage is a little more
80+
* clumsy and less encapsulated.
81+
*
82+
* Example of how to create objectOffset:
83+
*
84+
* flatbuffers::FlatBufferBuilder builder;
85+
*
86+
* auto name_off = builder.CreateString("I am a test message");
87+
* uint64_t counter = 666;
88+
* auto values_off = builder.CreateVector(std::vector<double>(100, 666.666));
89+
*
90+
* auto objectOffset = core_lib_test_fb::CreateTestMessage(
91+
* builder,
92+
* name_off,
93+
* counter,
94+
* values_off
95+
* );
96+
*
97+
* builder.Finish(objectOffset);
98+
* std::vector<char> out;
99+
* ToCharVectorFlatBuf(builder, out);
100+
*/
101+
void ToCharVectorFlatBuf(flatbuffers::FlatBufferBuilder builder, char_vector_t& out)
102+
{
103+
const auto* p = builder.GetBufferPointer(); // const uint8_t*
104+
const auto n = builder.GetSize(); // size_t
105+
106+
out.resize(n);
107+
std::memcpy(out.data(), p, n);
108+
}
109+
#endif
110+
111+
} // namespace serialize
112+
} // namespace core_lib

UnitTests/GoogleTests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ add_executable(
9494
../../Source/DebugLog/DebugLogSingleton.cpp
9595
../../Source/CsvGrid/CsvGridCell.cpp
9696
../../Source/CsvGrid/CsvGridCellDouble.cpp
97+
../../Source/Serialization/SerializeToVector.cpp
9798
../../Source/Asio/AsioDefines.cpp
9899
../../Source/Asio/IoContextThreadGroup.cpp
99100
../../Source/Asio/MessageUtils.cpp

0 commit comments

Comments
 (0)