Skip to content

Commit

Permalink
[#73] Protobuf serialization example supports nested messages and enums
Browse files Browse the repository at this point in the history
  • Loading branch information
Manu343726 committed Oct 27, 2016
1 parent 89c7a49 commit cf79486
Show file tree
Hide file tree
Showing 10 changed files with 699 additions and 67 deletions.
7 changes: 6 additions & 1 deletion cmake/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ endfunction()
function(add_prebuild_command)
set(options)
set(oneValueArgs NAME TARGET)
set(multiValueArgs)
set(multiValueArgs DEPENDS)
cmake_parse_arguments(PC
"${options}"
"${oneValueArgs}"
Expand All @@ -230,6 +230,11 @@ function(add_prebuild_command)
get_target_dependencies_targets_only(${PC_TARGET} deps)
add_dependencies(${PC_NAME} ${deps})
add_dependencies(${PC_TARGET} ${PC_NAME})

# Build the explicit dependencies first:
if(PC_DEPENDS)
add_dependencies(${PC_NAME} ${PC_DEPENDS})
endif()
endfunction()

# Copies dll dependencies of a target to its runtime
Expand Down
8 changes: 8 additions & 0 deletions examples/reflection/astexamples.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ class ClassWithMultipleMembers
void fo();
};

template<typename T>
class TemplateToBeSpecialized
{};

template<>
class TemplateToBeSpecialized<int>
{};

#include <reflection/examples/reflection/astexamples.hpp>
8 changes: 5 additions & 3 deletions examples/reflection/static/membermatching.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
#include <astexamples.hpp>
#include <siplasplas/utility/meta.hpp>
#include <siplasplas/constexpr/arrayview.hpp>
#include <siplasplas/constexpr/algorithm.hpp>
#include <siplasplas/reflection/static/api.hpp>
#include <siplasplas/utility/fusion.hpp>
#include <iostream>

namespace reflection = cpp::static_reflection;


constexpr cpp::ConstArrayView<char> method = "foobar";
constexpr cpp::constexp::ConstArrayView<char> method = "foobar";

struct Compare
{
template<typename Lhs, typename Rhs>
struct apply
{
using type = cpp::meta::bool_<
cpp::levenshteinDistance(method, Lhs::SourceInfo::spelling()) >=
cpp::levenshteinDistance(method, Rhs::SourceInfo::spelling())
cpp::constexp::levenshteinDistance(method, Lhs::SourceInfo::spelling()) >=
cpp::constexp::levenshteinDistance(method, Rhs::SourceInfo::spelling())
>;
};
};
Expand Down
19 changes: 18 additions & 1 deletion examples/reflection/static/protoserialization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,27 @@ FILES
${CMAKE_CURRENT_BINARY_DIR}/commmodel.pb.h
)

add_siplasplas_example_simple(protoserialization
add_siplasplas_example(protoserialization
SOURCES
protoserialization.cpp
mapping.cpp
DEPENDS
siplasplas-reflection-static
examples-protoserialization-commmodel
)

if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
math(EXPR MAX_CONSTEXPR_STEPS "1024*1024*1024")
math(EXPR MAX_CONSTEXPR_DEPTH "1024*1024*1024")

message(STATUS " Max constexpr steps: ${MAX_CONSTEXPR_STEPS}")
message(STATUS " Max constexpr depth: ${MAX_CONSTEXPR_DEPTH}")

target_compile_options(examples-protoserialization PRIVATE
-fconstexpr-steps=${MAX_CONSTEXPR_STEPS}
-fconstexpr-depth=${MAX_CONSTEXPR_DEPTH}
-fconstexpr-backtrace-limit=0
)
endif()

configure_siplasplas_reflection(examples-protoserialization)
30 changes: 29 additions & 1 deletion examples/reflection/static/protoserialization/commmodel.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,35 @@ message NetworkSettings
{
optional string ipAddress = 1;
optional string gateway = 2;
optional int32 pingInterval = 3;
optional int32 pingIntervalMs = 3;
}

message ServerSettings
{
optional int32 port = 1;
}

message Settings
{
optional NetworkSettings networkSettings = 1;
optional ServerSettings serverSettings = 2;
}

enum Operation
{
Set = 0;
Get = 1;
}

message SettingsOperation
{
optional Operation operation = 1;
optional Settings settings = 2;
}

message MessageWithOperation
{
optional Operation operation = 1;
}

message ChangeNetworkSettings
Expand Down
74 changes: 73 additions & 1 deletion examples/reflection/static/protoserialization/datamodel.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#ifndef SIPLASPLAS_EXAMPLES_REFLECTION_STATIC_PROTOSERIALIZATION_DATAMODEL_HPP
#define SIPLAPSLAS_EXAMPLES_REFLECTION_STATIC_PROTOSERIALIZATION_DATAMODEL_HPP
#define SIPLASPLAS_EXAMPLES_REFLECTION_STATIC_PROTOSERIALIZATION_DATAMODEL_HPP

#include <string>
#include <chrono>
#include <ostream>

class NetworkSettings
{
Expand All @@ -12,4 +13,75 @@ class NetworkSettings
std::chrono::milliseconds pingInterval = std::chrono::milliseconds(0);
};

class ServerSettings
{
public:
int port = 8080;
};

class Settings
{
public:
NetworkSettings netwokSettings;
ServerSettings serverSettings;
};

enum class Operation
{
Set,
Get
};

class SettingsOperation
{
public:
Operation operation;
Settings settings;
};

class MessageWithOperation
{
public:
Operation operation;
};

#include <reflection/examples/reflection/static/protoserialization/datamodel.hpp>

std::ostream& operator<<(std::ostream& os, const NetworkSettings& networkSettings)
{
return os << "NetworkSettings {\n"
<< " ipAddress: " << networkSettings.ipAddress
<< "\n gateway: " << networkSettings.gateway
<< "\n pingInterval: " << networkSettings.pingInterval.count() << " ms"
<< "\n}";
}

std::ostream& operator<<(std::ostream& os, const ServerSettings& serverSettings)
{
return os << "ServerSettings {\n"
<< " port: " << serverSettings.port
<< "\n}";
}

std::ostream& operator<<(std::ostream& os, const Settings& settings)
{
return os << "Settings{\n"
<< " networkSettings: " << settings.netwokSettings
<< "\n serverSettings: " << settings.serverSettings
<< "\n}";
}

std::ostream& operator<<(std::ostream& os, const Operation& operation)
{
return os << "Operation::" << cpp::static_reflection::Enum<Operation>::toString(operation);
}

std::ostream& operator<<(std::ostream& os, const SettingsOperation& settingsOperation)
{
return os << "SettingsOperation{\n"
<< " operation: " << settingsOperation.operation
<< "\n settings: " << settingsOperation.settings
<< "\n}";
}

#endif // SIPLASPLAS_EXAMPLES_REFLECTION_STATIC_PROTOSERIALIZATION_DATAMODEL_HPP
Empty file.
Loading

0 comments on commit cf79486

Please sign in to comment.