Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed May 22, 2024
1 parent e773a82 commit 766910b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/struct_pb/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
if("${yaLanTingLibs_SOURCE_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
# If this is a subproject in ylt
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/output/examples)
else()
# else find installed yalantinglibs
cmake_minimum_required(VERSION 3.15)
project(struct_pb_examples)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# if you have install ylt
find_package(yalantinglibs REQUIRED)
link_libraries(yalantinglibs::yalantinglibs)
# else
# include_directories(include)
# include_directories(include/ylt/thirdparty)
endif()

add_executable(struct_pb_examples
main.cpp
)
27 changes: 27 additions & 0 deletions src/struct_pb/examples/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <cassert>
#include <iostream>
#include <ylt/struct_pb.hpp>

struct my_struct : struct_pb::pb_base {
int x;
bool y;
iguana::fixed64_t z;
};
REFLECTION(my_struct, x, y, z);

struct nest : struct_pb::pb_base {
std::string name;
my_struct value;
int var;
};
REFLECTION(nest, name, value, var);

int main() {
nest v{0, "Hi", {0, 1, false, 3}, 5}, v2{};
std::string s;
struct_pb::to_pb(v, s);
struct_pb::from_pb(v2, s);
assert(v.var == v2.var);
assert(v.value.y == v2.value.y);
assert(v.value.z == v2.value.z);
}

0 comments on commit 766910b

Please sign in to comment.