### Install Protocol Buffer Library and Headers Source: https://github.com/yandex/yaff/blob/main/include/yaff/proto/CMakeLists.txt Installs the 'yaff_proto' library and its associated header files. The library is installed to the library directory, and headers are installed to the include directory under 'yaff/proto'. ```cmake install(TARGETS yaff_proto EXPORT YaFFTargets ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ) install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/options.proto" "${YAFF_PROTO_GENERATED_DIR}/yaff/proto/options.pb.h" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/yaff/proto ) ``` -------------------------------- ### Install Generated Version Header Source: https://github.com/yandex/yaff/blob/main/include/CMakeLists.txt Installs the generated version header file to the installation directory. This makes the version information accessible. ```cmake install(FILES "${CMAKE_CURRENT_BINARY_DIR}/generated/yaff/version.h" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/yaff ) ``` -------------------------------- ### Start and Finish Fixed Message Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/support.md Use these methods to build a fixed-layout message. You start by calling `StartFixedMessage` with the message's MetaType and finish by calling `FinishFixedMessage` to get the message's offset. ```cpp void StartFixedMessage() Offset FinishFixedMessage() ``` -------------------------------- ### Install Protoc Plugin Executable Source: https://github.com/yandex/yaff/blob/main/src/protoc-plugin/CMakeLists.txt Installs the yaff_protoc_plugin executable to the runtime directory and exports targets. ```cmake install(TARGETS yaff_protoc_plugin EXPORT YaFFTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) ``` -------------------------------- ### Setup Directories for YAFF Benchmarks Source: https://github.com/yandex/yaff/blob/main/benchmarks/access/CMakeLists.txt Creates and configures directories for generated files during the YAFF benchmark build process. ```cmake set(YAFF_BENCH_ACCESS_GENERATED_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated) file(MAKE_DIRECTORY ${YAFF_BENCH_ACCESS_GENERATED_DIR}) file(MAKE_DIRECTORY ${YAFF_BENCH_ACCESS_GENERATED_DIR}/protoyaff) ``` -------------------------------- ### Install Dependencies with Conan Source: https://github.com/yandex/yaff/blob/main/docs/integration.md Install all declared dependencies using `conan install`. This command generates a `CMakeUserPresets.json` file that automatically configures your toolchain and dependency paths for CMake. ```bash conan install . -s build_type=Release ``` -------------------------------- ### Install Dependencies with Conan Source: https://github.com/yandex/yaff/blob/main/docs/benchmarks/overview.md Install YaFF benchmarks and their dependencies, including google/benchmark and FlatBuffers, using Conan. This command ensures a Release build and enables benchmark building. ```bash conan install . --build=missing -s build_type=Release -o build_benchmarks=True cmake --preset conan-release -DYAFF_BUILD_BENCHMARKS=ON cmake --build --preset conan-release ``` -------------------------------- ### Install Yaff Header Files Source: https://github.com/yandex/yaff/blob/main/include/CMakeLists.txt Installs all header files from the project's source directory to the installation's include directory. This ensures headers are available after installation. ```cmake install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} FILES_MATCHING PATTERN "*.h" ) ``` -------------------------------- ### Setup Test Generated Directory Source: https://github.com/yandex/yaff/blob/main/tests/CMakeLists.txt Ensures the directory for generated test files exists. ```cmake set(YAFF_TEST_GENERATED_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated) file(MAKE_DIRECTORY ${YAFF_TEST_GENERATED_DIR}) ``` -------------------------------- ### CMakeLists.txt for YAFF Trading Example Source: https://github.com/yandex/yaff/blob/main/examples/trading/CMakeLists.txt Configures the build for the yaff_example_trading executable. It generates C++ code from proto and YAFF definitions, sets include directories, and links YAFF libraries. ```cmake set(_generated_dir ${CMAKE_CURRENT_BINARY_DIR}/generated) file(MAKE_DIRECTORY ${_generated_dir}) add_executable(yaff_example_trading main.cpp proto/order.proto ) protobuf_generate( TARGET yaff_example_trading LANGUAGE cpp IMPORT_DIRS ${CMAKE_CURRENT_SOURCE_DIR} PROTOC_OUT_DIR ${_generated_dir} ) yaff_generate( TARGET yaff_example_trading IMPORT_DIRS ${CMAKE_CURRENT_SOURCE_DIR} OUT_DIR ${_generated_dir} ) target_include_directories(yaff_example_trading PRIVATE ${_generated_dir}) target_link_libraries(yaff_example_trading PRIVATE yaff::core yaff::proto ) ``` -------------------------------- ### Configure Include Directories for Protocol Buffer Library Source: https://github.com/yandex/yaff/blob/main/include/yaff/proto/CMakeLists.txt Adds necessary include directories to the 'yaff_proto' target for both build and installation contexts. This includes directories for build-time imports and generated files, as well as the installation path. ```cmake target_include_directories(yaff_proto PUBLIC $ $ $ ) ``` -------------------------------- ### Build and Install YaFF via CMake Source: https://github.com/yandex/yaff/blob/main/docs/integration.md Clone the YaFF repository, build it using CMake, and install it to a specified prefix. This makes headers, libraries, and binaries available for your project. ```bash git clone https://github.com/yandex/yaff.git cd yaff cmake -S . -B build # Release build type is used by default cmake --build build cmake --install build --prefix /usr/local ``` -------------------------------- ### Install Yaff Core Target Source: https://github.com/yandex/yaff/blob/main/include/CMakeLists.txt Installs the yaff_core target and exports it for use by other projects. This makes the library available for linking. ```cmake install(TARGETS yaff_core EXPORT YaFFTargets ) ``` -------------------------------- ### Start and Finish Flat Message Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/support.md Use these methods to build a flat-layout message. The `implicit` flag controls whether fields with default values are omitted, and `sized` enables writing field-size metadata for dynamic alternatives. ```cpp void StartFlatMessage(bool implicit = false, bool sized = false) Offset FinishFlatMessage() ``` -------------------------------- ### Start and Finish Sparse Message Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/support.md Use these methods to build a sparse-layout message. Similar to flat messages, the `implicit` flag determines if default fields are omitted. ```cpp void StartSparseMessage(bool implicit = false) Offset FinishSparseMessage() ``` -------------------------------- ### Consumer CMakeLists.txt for YaFF Source: https://github.com/yandex/yaff/blob/main/docs/integration.md Configure your project's `CMakeLists.txt` to find the YaFF package and link against its core and proto libraries. This setup ensures that YaFF's generated code and headers are correctly handled. ```cmake cmake_minimum_required(VERSION 3.20) project(my_app LANGUAGES CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) find_package(YaFF CONFIG REQUIRED) # After find_package: # - yaff_generate() is available (injected via cmake_build_modules) # - yaff::protoc_plugin is defined as an imported executable # - YAFF_PROTO_IMPORT_DIR points to the installed include directory, # so "import yaff/proto/options.proto" works in your .proto files # without any extra -I flags. # ... define your targets, call protobuf_generate() and yaff_generate() ... target_link_libraries(my_app PRIVATE yaff::core yaff::proto ) ``` -------------------------------- ### Basic YaFF Schema Definition Source: https://github.com/yandex/yaff/blob/main/docs/schema/overview.md A standard Protobuf schema file demonstrating YaFF's supported features and the `layout` option. This serves as a foundational example for defining messages, enums, and fields. ```proto syntax = "proto3"; package example; import "yaff/proto/options.proto"; enum Status { STATUS_UNKNOWN = 0; STATUS_ACTIVE = 1; STATUS_BANNED = 2; } message GeoPoint { option (yaff.proto.message) = {layout: LAYOUT_FIXED}; float lat = 1; float lon = 2; } message Profile { message Preferences { optional string locale = 1; optional bool subscribed = 2; // 'theme' (field 3) was removed. reserved 3; reserved "theme"; } uint64 id = 1; optional string name = 2; optional Status status = 3; optional bool verified = 4; optional double rating = 5; optional bytes avatar = 6; repeated string tags = 7; repeated GeoPoint locations = 8; map attributes = 9; optional Preferences preferences = 10; oneof contact { string email = 11; string phone = 12; } optional int32 score = 13 [deprecated = true]; } ``` -------------------------------- ### Configure CMake Build with Custom Prefix Source: https://github.com/yandex/yaff/blob/main/docs/integration.md When building your project, specify the installation prefix of YaFF using CMAKE_PREFIX_PATH so that CMake can locate the YaFFConfig.cmake file. ```bash cmake -S . -B build -DCMAKE_PREFIX_PATH=/usr/local cmake --build build ``` -------------------------------- ### Integrate YaFF into a CMake Project Source: https://github.com/yandex/yaff/blob/main/docs/integration.md Configure your CMake project to find the installed YaFF package and link against its core and proto libraries. Ensure CMAKE_PREFIX_PATH is set if YaFF is installed to a non-standard location. ```cmake cmake_minimum_required(VERSION 3.20) project(my_app LANGUAGES CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) find_package(YaFF CONFIG REQUIRED) # yaff_generate(), YAFF_PROTO_IMPORT_DIR, and all yaff::* targets are now available. # ... define your targets, call protobuf_generate() and yaff_generate() ... target_link_libraries(my_app PRIVATE yaff::core yaff::proto ) ``` -------------------------------- ### CMake Setup for YaFF Code Generation Source: https://github.com/yandex/yaff/blob/main/docs/quick_start.md Integrate YaFF code generation into your CMake build. Ensure Protobuf and YaFF are found, add proto files to your executable target, and call the generation functions. Link against YaFF libraries. ```cmake find_package(Protobuf CONFIG REQUIRED) find_package(YaFF CONFIG REQUIRED) add_executable(my_app main.cpp proto/feed.proto) protobuf_generate( TARGET my_app LANGUAGE cpp IMPORT_DIRS ${CMAKE_CURRENT_SOURCE_DIR} PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR} ) yaff_generate( TARGET my_app IMPORT_DIRS ${CMAKE_CURRENT_SOURCE_DIR} OUT_DIR ${CMAKE_CURRENT_BINARY_DIR} ) target_include_directories(my_app PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) target_link_libraries(my_app PRIVATE yaff::core yaff::proto) ``` -------------------------------- ### Get Forced Dynamic Alternative Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/support.md Returns the currently configured representation for dynamic messages. ```cpp MessageLayout GetForcedDynamicAlternative() const ``` -------------------------------- ### Set Export Name for Protocol Buffer Library Source: https://github.com/yandex/yaff/blob/main/include/yaff/proto/CMakeLists.txt Configures the export name for the 'yaff_proto' library, which is used when the library is installed and exported. ```cmake set_target_properties(yaff_proto PROPERTIES EXPORT_NAME proto) ``` -------------------------------- ### Generated Serializer Function Signature Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/generated.md Example signature of a compiler-generated Serialize function for a message. It takes a Serializer reference and fields directly, with optional fields defaulting to unset. ```cpp ::yaff::InternalOffset SerializeAuthor( ::yaff::Serializer& ys, uint64_t a_id = 0, ::yaff::InternalOffset<::yaff::String> a_name = 0, std::optional a_verified = std::nullopt); ``` -------------------------------- ### Add Executable and Proto Files Source: https://github.com/yandex/yaff/blob/main/examples/doc_views/CMakeLists.txt Defines the main executable and includes the proto file for compilation. ```cmake add_executable(yaff_example_doc_views main.cpp proto/document.proto ) ``` -------------------------------- ### Configure Protocol Buffer Generation Source: https://github.com/yandex/yaff/blob/main/include/yaff/proto/CMakeLists.txt Sets up variables for the proto options file and the directory where generated files will be placed. Ensures the generated directory exists. ```cmake set(YAFF_PROTO_OPTIONS ${CMAKE_CURRENT_SOURCE_DIR}/options.proto) set(YAFF_PROTO_GENERATED_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated) file(MAKE_DIRECTORY ${YAFF_PROTO_GENERATED_DIR}) ``` -------------------------------- ### Configure Generated Directory Source: https://github.com/yandex/yaff/blob/main/examples/doc_views/CMakeLists.txt Sets up a directory for generated files and ensures it exists. ```cmake set(_generated_dir ${CMAKE_CURRENT_BINARY_DIR}/generated) file(MAKE_DIRECTORY ${_generated_dir}) ``` -------------------------------- ### Configure and Build with CMake Presets Source: https://github.com/yandex/yaff/blob/main/docs/integration.md Use CMake presets to configure and build your project. The `conan-release` preset, generated by Conan, automatically applies the correct toolchain and build settings. ```bash cmake --preset conan-release cmake --build --preset conan-release ``` -------------------------------- ### Instantiate Space Benchmark Source: https://github.com/yandex/yaff/blob/main/benchmarks/space/CMakeLists.txt Calls the macro to add a specific space benchmark executable using the provided source file. ```cmake yaff_add_space_benchmark(flat.cpp) ``` -------------------------------- ### Include Directories for Protobuf Library Source: https://github.com/yandex/yaff/blob/main/benchmarks/access/CMakeLists.txt Configures public include directories for the protobuf library used in YAFF access benchmarks. ```cmake target_include_directories(yaff_bench_access_protoyaff PUBLIC ${YAFF_BENCH_ACCESS_GENERATED_DIR}) ``` -------------------------------- ### Repeated Numeric Field Accessors Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/generated.md Generated accessors for repeated numeric fields. Provides methods to get the size, access elements by index, and obtain a read-only view of the elements. ```proto repeated int32 foo = 1; ``` -------------------------------- ### Configure Include Directories Source: https://github.com/yandex/yaff/blob/main/tests/CMakeLists.txt Adds the generated directory to the include path for the proto library. ```cmake target_include_directories(yaff_test_protoyaff PUBLIC ${YAFF_TEST_GENERATED_DIR}) ``` -------------------------------- ### Create and Alias Static Library for Protocol Buffers Source: https://github.com/yandex/yaff/blob/main/include/yaff/proto/CMakeLists.txt Defines a static library 'yaff_proto' using the generated C++ files and creates an ALIAS target 'yaff::proto' for easier referencing. ```cmake add_library(yaff_proto STATIC ${YAFF_PROTO_GENERATED_FILES}) add_library(yaff::proto ALIAS yaff_proto) ``` -------------------------------- ### Find and Include Dependencies Source: https://github.com/yandex/yaff/blob/main/tests/CMakeLists.txt Locates and includes necessary build system components like GTest and custom Yaff modules. ```cmake find_package(GTest CONFIG REQUIRED) include(${PROJECT_SOURCE_DIR}/cmake/YaFFGenerate.cmake) ``` -------------------------------- ### Define Protocol Buffer Library Source: https://github.com/yandex/yaff/blob/main/tests/CMakeLists.txt Creates a static library for protocol buffer definitions. ```cmake add_library(yaff_test_protoyaff STATIC protoyaff/proto_api.proto protoyaff/proto2.proto protoyaff/proto3.proto ) ``` -------------------------------- ### Zero-Copy Read YaFF Message Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/generated.md Reads a serialized YaFF message into a read-only view without parsing or copying. This is the primary method for accessing YaFF data efficiently. The `data` pointer should point to the start of the serialized representation. ```cpp const Foo& view = yaff::ReadMessage(data); ``` -------------------------------- ### Set Compile Options for Protoc Plugin Source: https://github.com/yandex/yaff/blob/main/src/protoc-plugin/CMakeLists.txt Configures compiler warnings based on the operating system (MSVC or others). ```cmake if(MSVC) target_compile_options(yaff_protoc_plugin PRIVATE /W4) else() target_compile_options(yaff_protoc_plugin PRIVATE -Wall -Wextra -Wpedantic) endif() ``` -------------------------------- ### YAFF Feed API CMake Build Configuration Source: https://github.com/yandex/yaff/blob/main/examples/feed_api/CMakeLists.txt Configures the build for the yaff_example_feed_api executable, including generating Protocol Buffers and YAFF code, setting include directories, and linking necessary libraries. ```cmake set(_generated_dir ${CMAKE_CURRENT_BINARY_DIR}/generated) file(MAKE_DIRECTORY ${_generated_dir}) add_executable(yaff_example_feed_api main.cpp proto/feed.proto ) protobuf_generate( TARGET yaff_example_feed_api LANGUAGE cpp IMPORT_DIRS ${CMAKE_CURRENT_SOURCE_DIR} PROTOC_OUT_DIR ${_generated_dir} ) yaff_generate( TARGET yaff_example_feed_api IMPORT_DIRS ${CMAKE_CURRENT_SOURCE_DIR} OUT_DIR ${_generated_dir} ) target_include_directories(yaff_example_feed_api PRIVATE ${_generated_dir}) target_link_libraries(yaff_example_feed_api PRIVATE yaff::core yaff::proto ) ``` -------------------------------- ### Configure YaFF Schema Slice Layouts Source: https://github.com/yandex/yaff/blob/main/docs/schema/overview.md Apply advanced configuration options like `layout` or `reserved_fields` with an optional `slice_name` parameter to fine-tune layout strategies for specific slices. Options are resolved slice-first, with tagged options winning over untagged fallbacks. ```proto message BannerProfile { // This configuration only applies to the "banner_heavy" slice option (yaff.proto.message) = { slice_name: "banner_heavy", layout: LAYOUT_FLAT}; } ``` -------------------------------- ### Configure and Build with CMake Directly Source: https://github.com/yandex/yaff/blob/main/docs/benchmarks/overview.md Configure and build YaFF benchmarks using CMake directly when benchmark and FlatBuffers dependencies are provided manually. This command enables benchmark building for a Release configuration. ```bash cmake -B build -DCMAKE_BUILD_TYPE=Release -DYAFF_BUILD_BENCHMARKS=ON cmake --build build ``` -------------------------------- ### YaFF Message Visitor Implementation Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/support.md Implement the AbstractVisitor interface to define callbacks for message traversal. Use Visitor::VisitMessage to drive the traversal. ```cpp class MyVisitor : public ::yaff::reflect::AbstractVisitor { void OnMessageStart() override; void OnMessageEnd() override; void OnArrayStart() override; void OnArrayEnd() override; void OnField(const char* name) override; void OnElement(size_t i) override; void OnString(std::string_view v) override; void OnInt32(int32_t v) override; // ... OnBool / OnUint32 / OnInt64 / OnUint64 / OnFloat / OnDouble / OnEnum }; MyVisitor sink; // messagePtr points to the message inside a YaFF representation ::yaff::reflect::Visitor(&sink).VisitMessage(messagePtr, Foo::Descriptor()); ``` -------------------------------- ### Protobuf Package to C++ Namespace Mapping Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/generated.md Demonstrates how a Protobuf package declaration maps to a nested C++ namespace in the generated code. The root namespace 'protoyaff' can be customized. ```proto package foo.bar; ``` ```cpp namespace protoyaff::foo::bar { ... } ``` -------------------------------- ### Direct Serialization with YaFF Serializer Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/generated.md Demonstrates the lower-level YaFF API for direct message serialization without an intermediate Protobuf message. This path offers maximum performance but requires careful handling of nested objects and offsets. ```cpp yaff::Serializer ys; const auto name = ys.SerializeString("Alice"); const auto author = SerializeAuthor(ys, /* id */ 42, name, /* verified */ true); ys.Finish(author); const auto buffer = ys.Release(); // buffer.Data() / buffer.Size() ``` -------------------------------- ### Serialize and Read YaFF Message Source: https://github.com/yandex/yaff/blob/main/README.md Demonstrates how to serialize a Protobuf message into a zero-copy YaFF representation and then read fields from it with zero parsing overhead. This is the core usage pattern for YaFF. ```cpp // Turn a Protobuf message into a zero-copy YaFF representation... const auto buffer = yaff::Serialize(proto); // ...then access fields instantly with zero parsing overhead const auto& response = yaff::ReadMessage(buffer.Data()); std::string_view authorName = response.items(0).author().name(); ``` -------------------------------- ### Generate Protocol Buffer Code Source: https://github.com/yandex/yaff/blob/main/tests/CMakeLists.txt Uses protobuf_generate to create C++ source files from .proto definitions. ```cmake protobuf_generate( TARGET yaff_test_protoyaff LANGUAGE cpp IMPORT_DIRS ${CMAKE_CURRENT_SOURCE_DIR} ${YAFF_PROTO_IMPORT_DIR} PROTOC_OUT_DIR ${YAFF_TEST_GENERATED_DIR} ) ``` -------------------------------- ### Build YaFF into Local Conan Cache Source: https://github.com/yandex/yaff/blob/main/docs/integration.md Clone the YaFF repository and create it locally within your Conan cache. This makes it available for other projects on the same machine. ```bash git clone https://github.com/yandex/yaff.git cd yaff conan create . --build=missing -s build_type=Release ``` -------------------------------- ### Find Benchmark Package Source: https://github.com/yandex/yaff/blob/main/benchmarks/CMakeLists.txt Locates the benchmark package configuration. This is required for benchmarking functionalities within the project. ```cmake find_package(benchmark CONFIG REQUIRED) ``` -------------------------------- ### Add Yaff Proto Subdirectory Source: https://github.com/yandex/yaff/blob/main/include/CMakeLists.txt Includes the yaff/proto subdirectory, which likely contains its own CMakeLists.txt for building protobuf-related code. ```cmake add_subdirectory(yaff/proto) ``` -------------------------------- ### Define Executable for Protoc Plugin Source: https://github.com/yandex/yaff/blob/main/src/protoc-plugin/CMakeLists.txt Defines the main executable for the protoc plugin and creates an alias for it. ```cmake add_executable(yaff_protoc_plugin main.cpp protoc_plugin.cpp ) add_executable(yaff::protoc_plugin ALIAS yaff_protoc_plugin) ``` -------------------------------- ### Write YaFF Buffer to Binary File Source: https://github.com/yandex/yaff/blob/main/docs/quick_start.md Write the generated YaFF buffer to a binary file. This demonstrates how to persist the serialized data. ```cpp std::ofstream{"out.bin", std::ios::binary}.write( reinterpret_cast(buffer.Data()), buffer.Size()); ``` -------------------------------- ### Link Libraries Source: https://github.com/yandex/yaff/blob/main/examples/doc_views/CMakeLists.txt Links the necessary YAFF core and proto libraries to the target executable. ```cmake target_link_libraries(yaff_example_doc_views PRIVATE yaff::core yaff::proto ) ``` -------------------------------- ### Link Libraries for Protobuf Target Source: https://github.com/yandex/yaff/blob/main/benchmarks/space/CMakeLists.txt Links necessary libraries for the protobuf-generated code. ```cmake target_link_libraries(yaff_bench_space_protoyaff PUBLIC yaff::core yaff::proto) ``` -------------------------------- ### Link Libraries for Proto Library Source: https://github.com/yandex/yaff/blob/main/tests/CMakeLists.txt Links the proto library against core Yaff libraries. ```cmake target_link_libraries(yaff_test_protoyaff PUBLIC yaff::core yaff::proto) ``` -------------------------------- ### Add Protobuf Library for YAFF Access Source: https://github.com/yandex/yaff/blob/main/benchmarks/access/CMakeLists.txt Defines a static library for protobuf definitions used in YAFF access benchmarks. ```cmake add_library(yaff_bench_access_protoyaff STATIC protoyaff/flat.proto protoyaff/hierarchical.proto ) ``` -------------------------------- ### Add Flat Access Benchmark Source: https://github.com/yandex/yaff/blob/main/benchmarks/access/CMakeLists.txt Calls the yaff_add_access_benchmark function to create an executable for the flat.cpp benchmark. ```cmake yaff_add_access_benchmark(flat.cpp) ``` -------------------------------- ### Generate C++ Protocol Buffer Files Source: https://github.com/yandex/yaff/blob/main/include/yaff/proto/CMakeLists.txt Uses the protobuf_generate CMake function to compile .proto files into C++ source and header files. Specifies the output language, input proto files, import directories, and the output directory for generated files. ```cmake protobuf_generate( OUT_VAR YAFF_PROTO_GENERATED_FILES LANGUAGE cpp PROTOS ${YAFF_PROTO_OPTIONS} IMPORT_DIRS ${YAFF_PROTO_IMPORT_DIR} PROTOC_OUT_DIR ${YAFF_PROTO_GENERATED_DIR} ) ``` -------------------------------- ### Set Compiler Options for Yaff Compilation Source: https://github.com/yandex/yaff/blob/main/src/compilation/CMakeLists.txt Sets compiler warnings and options based on the operating system's compiler. For MSVC, it sets /W4. For others, it sets -Wall -Wextra -Wpedantic. ```cmake if(MSVC) target_compile_options(yaff_compilation PRIVATE /W4) else() target_compile_options(yaff_compilation PRIVATE -Wall -Wextra -Wpedantic) endif() ``` -------------------------------- ### Generate C++ Code from Protobuf Source: https://github.com/yandex/yaff/blob/main/examples/doc_views/CMakeLists.txt Uses the protobuf_generate command to generate C++ code from a proto file. Specify import directories and the output directory for generated files. ```cmake protobuf_generate( TARGET yaff_example_doc_views LANGUAGE cpp IMPORT_DIRS ${CMAKE_CURRENT_SOURCE_DIR} ${YAFF_PROTO_IMPORT_DIR} PROTOC_OUT_DIR ${_generated_dir} ) ``` -------------------------------- ### Generate C++ Code from Protobuf Source: https://github.com/yandex/yaff/blob/main/benchmarks/access/CMakeLists.txt Uses protobuf_generate to create C++ source files from .proto definitions for the YAFF access benchmark library. ```cmake protobuf_generate( TARGET yaff_bench_access_protoyaff LANGUAGE cpp IMPORT_DIRS ${CMAKE_CURRENT_SOURCE_DIR} ${YAFF_PROTO_IMPORT_DIR} PROTOC_OUT_DIR ${YAFF_BENCH_ACCESS_GENERATED_DIR} ) ``` -------------------------------- ### Link Protocol Buffer Library with Dependencies Source: https://github.com/yandex/yaff/blob/main/include/yaff/proto/CMakeLists.txt Links the 'yaff_proto' library against the 'protobuf::libprotobuf' target, making protobuf symbols available to the generated code. ```cmake target_link_libraries(yaff_proto PUBLIC protobuf::libprotobuf) ``` -------------------------------- ### Add Include Directories Source: https://github.com/yandex/yaff/blob/main/examples/doc_views/CMakeLists.txt Adds the generated directory to the include path for the target executable. ```cmake target_include_directories(yaff_example_doc_views PRIVATE ${_generated_dir}) ``` -------------------------------- ### Declare YaFF Dependency in conanfile.txt Source: https://github.com/yandex/yaff/blob/main/docs/integration.md A simpler alternative to `conanfile.py` for declaring YaFF and its dependencies, suitable when build logic is not needed within the conanfile itself. ```ini [requires] protobuf/ yaff/ [tool_requires] protobuf/ [generators] CMakeDeps CMakeToolchain [layout] cmake_layout ``` -------------------------------- ### Find Flatbuffers Package Source: https://github.com/yandex/yaff/blob/main/benchmarks/CMakeLists.txt Locates the flatbuffers package configuration. This is necessary for projects utilizing FlatBuffers for data serialization. ```cmake find_package(flatbuffers CONFIG REQUIRED) ``` -------------------------------- ### Include Generated Protobuf Directory Source: https://github.com/yandex/yaff/blob/main/benchmarks/space/CMakeLists.txt Adds the generated protobuf directory to the include path for the target. ```cmake target_include_directories(yaff_bench_space_protoyaff PUBLIC ${YAFF_BENCH_SPACE_GENERATED_DIR}) ``` -------------------------------- ### Configure Version Header Source: https://github.com/yandex/yaff/blob/main/include/CMakeLists.txt Configures a version header file from a template. This is used to generate version information that can be included in the project. ```cmake configure_file( yaff/version.h.in "${CMAKE_CURRENT_BINARY_DIR}/generated/yaff/version.h" @ONLY ) ``` -------------------------------- ### Set Include Directories for Yaff Core Source: https://github.com/yandex/yaff/blob/main/include/CMakeLists.txt Configures the include directories for the yaff_core interface library. This makes headers available during compilation. ```cmake target_include_directories(yaff_core INTERFACE $ $ $ ) ``` -------------------------------- ### Generate Flatbuffers Headers Source: https://github.com/yandex/yaff/blob/main/benchmarks/access/CMakeLists.txt Generates C++ headers from FlatBuffers schemas, specifying include prefixes and schema files. ```cmake flatbuffers_generate_headers( TARGET yaff_bench_access_flatbuffers INCLUDE_PREFIX flatbuffers SCHEMAS ${CMAKE_CURRENT_SOURCE_DIR}/flatbuffers/flat.fbs ${CMAKE_CURRENT_SOURCE_DIR}/flatbuffers/hierarchical.fbs ) ``` -------------------------------- ### Generate Protobuf Code Source: https://github.com/yandex/yaff/blob/main/benchmarks/space/CMakeLists.txt Generates C++ code from protobuf definitions. Ensure IMPORT_DIRS are correctly set for schema resolution. ```cmake set(YAFF_BENCH_SPACE_GENERATED_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated) file(MAKE_DIRECTORY ${YAFF_BENCH_SPACE_GENERATED_DIR}) file(MAKE_DIRECTORY ${YAFF_BENCH_SPACE_GENERATED_DIR}/protoyaff) add_library(yaff_bench_space_protoyaff STATIC protoyaff/flat.proto ) protobuf_generate( TARGET yaff_bench_space_protoyaff LANGUAGE cpp IMPORT_DIRS ${CMAKE_CURRENT_SOURCE_DIR} ${YAFF_PROTO_IMPORT_DIR} PROTOC_OUT_DIR ${YAFF_BENCH_SPACE_GENERATED_DIR} ) ``` -------------------------------- ### Link Flatbuffers Library Source: https://github.com/yandex/yaff/blob/main/benchmarks/access/CMakeLists.txt Links the generated Flatbuffers code against the Flatbuffers library. ```cmake target_link_libraries(yaff_bench_access_flatbuffers INTERFACE flatbuffers::flatbuffers) ``` -------------------------------- ### Generate Flatbuffers Headers Source: https://github.com/yandex/yaff/blob/main/benchmarks/space/CMakeLists.txt Generates C++ headers from FlatBuffers schemas. The INCLUDE_PREFIX is used for organizing generated headers. ```cmake flatbuffers_generate_headers( TARGET yaff_bench_space_flatbuffers INCLUDE_PREFIX flatbuffers SCHEMAS ${CMAKE_CURRENT_SOURCE_DIR}/flatbuffers/flat.fbs ) ``` -------------------------------- ### Serializer Methods Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/support.md Methods for serializing data and building messages using the YaFF support library. ```APIDOC ## Serializer Methods ### Serializing Data - `InternalOffset SerializeString(std::string_view str)`: Serializes a `string` or `bytes` value and returns an offset to it. Deduplicated. - `InternalOffset> SerializeArray(...)`: Serializes a repeated field. Overloads accept a `std::vector`, a pointer and length, or a length and a generator `gen(i)` returning element `i`. Scalar arrays are deduplicated; offset arrays are not. ### Building Messages - `void StartFixedMessage()` / `Offset FinishFixedMessage()`: Starts and finishes a fixed-layout message. `M` is the message's `MetaType`. - `void StartFlatMessage(bool implicit = false, bool sized = false)` / `Offset FinishFlatMessage()`: Starts and finishes a flat-layout message. `implicit` marks a message that uses implicit presence only; `sized` writes the field-size metadata needed when flat runs as a dynamic alternative. - `void StartSparseMessage(bool implicit = false)` / `Offset FinishSparseMessage()`: Starts and finishes a sparse-layout message. ### Adding Fields - `void AddField(FieldId id, T value, T def)`: Adds a scalar field, where `def` is the field's default. - `void AddField(FieldId id, InternalOffset offset)`: Adds a reference field (string, message, or array). *Note: `AddField` must be called in descending id order.* ### Finishing Serialization - `void Finish(InternalOffset root)`: Marks `root` as the top-level message of the buffer. Call it once, after the top-level message is built. - `DetachedSegment Release()`: Returns the finished, self-owned buffer and resets the serializer. The buffer exposes `Data()` and `Size()`. - `const std::byte* Data() const` / `size_t Size() const`: Access the finished bytes in place, without detaching them. ``` -------------------------------- ### Link Libraries for Protoc Plugin Source: https://github.com/yandex/yaff/blob/main/src/protoc-plugin/CMakeLists.txt Links necessary libraries to the yaff_protoc_plugin target, including compilation and protobuf libraries. ```cmake target_link_libraries(yaff_protoc_plugin PRIVATE yaff::compilation protobuf::libprotoc ) ``` -------------------------------- ### Add Space Benchmark Executable Source: https://github.com/yandex/yaff/blob/main/benchmarks/space/CMakeLists.txt Defines a function to add a new space benchmark executable. It links necessary libraries and adds dependencies for generated code. ```cmake function(yaff_add_space_benchmark _src) get_filename_component(_name "${_src}" NAME_WE) set(_target "yaff_bench_space_${_name}") add_executable(${_target} ${_src}) target_link_libraries(${_target} PRIVATE yaff_bench_space_protoyaff yaff_bench_space_flatbuffers benchmark::benchmark_main ) add_dependencies(${_target} GENERATE_yaff_bench_space_flatbuffers) endfunction() ``` -------------------------------- ### Serialize Protobuf Message to YaFF Buffer Source: https://github.com/yandex/yaff/blob/main/docs/quick_start.md Convert an existing Protobuf message into a YaFF buffer. Include the generated Protobuf and YaFF headers. This is useful for sending data over networks or writing to files. ```cpp #include "feed.pb.h" #include "feed.yaff.h" // In practice the message may be loaded from a database or received from a service. feed::FeedResponse proto = LoadFeedResponse(); // Build the YaFF buffer from the Protobuf representation. const auto buffer = yaff::Serialize(proto); ``` -------------------------------- ### Set Yaff Compilation Include Directories Source: https://github.com/yandex/yaff/blob/main/src/compilation/CMakeLists.txt Configures the public include directories for the yaff_compilation library. ```cmake target_include_directories(yaff_compilation PUBLIC $ ) ``` -------------------------------- ### Serialize Protobuf to YaFF Buffer Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/generated.md Converts an existing Protobuf message into a YaFF representation using `yaff::Serialize`. The result is a buffer containing the serialized bytes. ```cpp const auto buffer = yaff::Serialize(proto); // buffer.Data() and buffer.Size() give the serialized bytes ``` -------------------------------- ### Define a YaFF Schema Source: https://github.com/yandex/yaff/blob/main/docs/quick_start.md Define your data model using Protocol Buffers syntax. This schema will be used by YaFF to generate code. ```proto syntax = "proto3"; package feed; enum ContentType { CONTENT_TYPE_UNKNOWN = 0; CONTENT_TYPE_ARTICLE = 1; CONTENT_TYPE_VIDEO = 2; } message Author { uint64 id = 1; optional string name = 3; // 'verified' (field 2) was removed. reserved 2; reserved "verified"; } message FeedItem { uint64 item_id = 1; optional ContentType content_type = 2; optional string title = 3; optional Author author = 4; repeated string tags = 5; } message FeedResponse { uint64 request_id = 1; repeated FeedItem items = 2; } ``` -------------------------------- ### Link Flatbuffers Library Source: https://github.com/yandex/yaff/blob/main/benchmarks/space/CMakeLists.txt Links the FlatBuffers library to the target. ```cmake target_link_libraries(yaff_bench_space_flatbuffers INTERFACE flatbuffers::flatbuffers) ``` -------------------------------- ### Link Libraries for Protobuf Library Source: https://github.com/yandex/yaff/blob/main/benchmarks/access/CMakeLists.txt Links the protobuf library against core YAFF and protobuf libraries. ```cmake target_link_libraries(yaff_bench_access_protoyaff PUBLIC yaff::core yaff::proto) ``` -------------------------------- ### Deserialize YaFF to Protobuf Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/generated.md Turns a serialized YaFF message back into a Protobuf message using `yaff::ParseMessage` for a new message, or `view.ParseTo(proto)` to fill an existing one. Note that unknown fields are not preserved during deserialization into a Protobuf object. ```cpp auto proto = yaff::ParseMessage(data); // a fresh Protobuf message view.ParseTo(proto); // or fill an existing one ``` -------------------------------- ### Finish Root Message Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/support.md Marks the specified offset as the top-level message of the buffer. This should be called once after the entire buffer has been constructed. ```cpp void Finish(InternalOffset root) ``` -------------------------------- ### Serialization Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/generated.md Functions for converting Protobuf messages to YaFF representations and vice versa. ```APIDOC ## Serializing Messages ### `yaff::Serialize(proto_message)` **Description**: Converts an existing Protobuf message into a YaFF representation (buffer). **Usage**: ```cpp const auto buffer = yaff::Serialize(proto); // buffer.Data() and buffer.Size() provide access to the serialized bytes. ``` ### Lower-level Interface YaFF also provides a lower-level, FlatBuffers-style interface for writing fields directly into the buffer when maximum performance is required and a Protobuf object is not needed. ``` -------------------------------- ### Include Custom Generation Script Source: https://github.com/yandex/yaff/blob/main/benchmarks/CMakeLists.txt Includes a custom CMake script for generating code or other build artifacts. Ensure the path to YaFFGenerate.cmake is correct relative to the project source directory. ```cmake include(${PROJECT_SOURCE_DIR}/cmake/YaFFGenerate.cmake) ``` -------------------------------- ### Pinning Message Layout to LAYOUT_FLAT Source: https://github.com/yandex/yaff/blob/main/docs/schema/evolution.md Use the `layout` option within the `(yaff.proto.message)` extension to pin a message to a specific static layout, such as `LAYOUT_FLAT`. This is a breaking change if the message is already in use or if it's being set to `LAYOUT_FIXED`. ```proto message Reading { option (yaff.proto.message) = {layout: LAYOUT_FLAT}; uint64 ts = 1; double value = 2; } ``` -------------------------------- ### Add Hierarchical Access Benchmark Source: https://github.com/yandex/yaff/blob/main/benchmarks/access/CMakeLists.txt Calls the yaff_add_access_benchmark function to create an executable for the hierarchical.cpp benchmark. ```cmake yaff_add_access_benchmark(hierarchical.cpp) ``` -------------------------------- ### Set Target Properties for Protoc Plugin Source: https://github.com/yandex/yaff/blob/main/src/protoc-plugin/CMakeLists.txt Sets output and export names for the yaff_protoc_plugin target. ```cmake set_target_properties(yaff_protoc_plugin PROPERTIES OUTPUT_NAME protoc-gen-yaff EXPORT_NAME protoc_plugin ) ``` -------------------------------- ### Define Unit Test Function Source: https://github.com/yandex/yaff/blob/main/tests/CMakeLists.txt A CMake function to simplify adding unit tests using Google Test. ```cmake function(yaff_add_unit_test _src) get_filename_component(_name "${_src}" NAME_WE) set(_target "yaff_${_name}_test") add_executable(${_target} ${_src}) target_link_libraries(${_target} PRIVATE yaff_test_protoyaff GTest::gtest_main ) add_test(NAME ${_target} COMMAND ${_target}) endfunction() ``` -------------------------------- ### Declare YaFF Dependency in conanfile.py Source: https://github.com/yandex/yaff/blob/main/docs/integration.md Specify YaFF and its dependencies (like protobuf) in the `requirements` method of your `conanfile.py`. Ensure build tools like protobuf are also declared in `build_requirements`. ```python from conan import ConanFile from conan.tools.cmake import CMake, CMakeToolchain, CMakeDeps, cmake_layout class MyAppConan(ConanFile): name = "my_app" version = "1.0.0" settings = "os", "compiler", "build_type", "arch" def requirements(self): self.requires("protobuf/") self.requires("yaff/") def build_requirements(self): self.tool_requires("protobuf/") def layout(self): cmake_layout(self) def generate(self): CMakeToolchain(self).generate() CMakeDeps(self).generate() def build(self): cmake = CMake(self) cmake.configure() cmake.build() ``` -------------------------------- ### yaff_generate() Function Signature and Parameters Source: https://github.com/yandex/yaff/blob/main/docs/integration.md This snippet shows the signature and all available parameters for the `yaff_generate()` CMake function. It is used to configure the generation of C++ code from .proto files. ```cmake yaff_generate( [TARGET ] # CMake target whose .proto SOURCES are processed [PROTOS ...] # Explicit list of .proto files (alternative to TARGET) [OUT_DIR ] # Output directory (default: CMAKE_CURRENT_BINARY_DIR) [OUT_VAR ] # Variable to receive the list of generated files [TAG ] # Suffix appended to generated file names [NAMESPACE ] # C++ namespace injected into generated code [IMPORT_DIRS ...] # Additional proto import search paths ) ``` -------------------------------- ### Generate YAFF Document Views Source: https://github.com/yandex/yaff/blob/main/examples/doc_views/CMakeLists.txt Generates C++ code for specific document view tags using the yaff_generate command. Each view is placed in its own namespace. ```cmake yaff_generate( TARGET yaff_example_doc_views TAG light_view NAMESPACE light_view IMPORT_DIRS ${CMAKE_CURRENT_SOURCE_DIR} OUT_DIR ${_generated_dir} ) yaff_generate( TARGET yaff_example_doc_views TAG full_view NAMESPACE full_view IMPORT_DIRS ${CMAKE_CURRENT_SOURCE_DIR} OUT_DIR ${_generated_dir} ) ``` -------------------------------- ### Zero-Copy Reading Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/generated.md Reading YaFF data directly through its view without converting back to Protobuf. ```APIDOC ## Zero-Copy Reading ### `yaff::ReadMessage(data)` **Description**: Reads a serialized message directly into a view without parsing or copying. The view overlays the provided bytes in place. **Usage**: ```cpp const Foo& view = yaff::ReadMessage(data); ``` - `data`: A pointer to the start of the serialized representation. - **Behavior**: Returns a view that overlays the bytes. A null `data` pointer yields `Foo::Default()`. - **Security**: Performs no validation; reading malformed or untrusted buffers is undefined behavior. ``` -------------------------------- ### Protobuf Any Message Definition Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/generated.md Defines an ErrorStatus message that includes a google.protobuf.Any field for flexible data storage. YaFF treats Any fields as opaque blobs. ```proto import "google/protobuf/any.proto"; message ErrorStatus { string message = 1; google.protobuf.Any details = 2; } ``` -------------------------------- ### Nested Message Declaration Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/generated.md Illustrates how nested messages are flattened into the enclosing namespace with an underscore-joined name in YaFF. ```proto message Foo { message Bar { ... } } ``` -------------------------------- ### Access Serialized Data In-Place Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/support.md Provides access to the finished serialized bytes without detaching the buffer from the serializer. Useful for inspecting the data before releasing it. ```cpp const std::byte* Data() const size_t Size() const ``` -------------------------------- ### Control YaFF Message Layout Statically Source: https://github.com/yandex/yaff/blob/main/docs/schema/overview.md Lock a message to a specific layout using the `layout` option inside `(yaff.proto.message)` for predictable performance and reduced runtime overhead. Transitioning back to dynamic layout is conditionally safe. ```proto message GeoPoint { option (yaff.proto.message) = {layout: LAYOUT_FIXED}; float lat = 1; float lon = 2; } ``` -------------------------------- ### Define Yaff Compilation Library Source: https://github.com/yandex/yaff/blob/main/src/compilation/CMakeLists.txt Defines the source files for the yaff_compilation static library and creates an alias for it. ```cmake set(YAFF_COMPILATION_SOURCES compiler.cpp cpp_gen.cpp error.cpp ir.cpp ir_compat.cpp ir_processor.cpp ir_reflect.cpp proto_front.cpp util.cpp ) add_library(yaff_compilation STATIC ${YAFF_COMPILATION_SOURCES}) add_library(yaff::compilation ALIAS yaff_compilation) ``` -------------------------------- ### Function to Add YAFF Access Benchmark Source: https://github.com/yandex/yaff/blob/main/benchmarks/access/CMakeLists.txt Defines a CMake function to create benchmark executables, linking them with necessary protobuf and flatbuffers libraries. ```cmake function(yaff_add_access_benchmark _src) get_filename_component(_name "${_src}" NAME_WE) set(_target "yaff_bench_access_${_name}") add_executable(${_target} ${_src}) target_link_libraries(${_target} PRIVATE yaff_bench_access_protoyaff yaff_bench_access_flatbuffers benchmark::benchmark_main ) add_dependencies(${_target} GENERATE_yaff_bench_access_flatbuffers) endfunction() ``` -------------------------------- ### Define YaFF Schema Slices Source: https://github.com/yandex/yaff/blob/main/docs/schema/overview.md Annotate messages and fields with `(yaff.proto.message)` and `(yaff.proto.field)` options to define schema slices, enabling multiple independent runtime representations from a single proto file for lightweight buffers. ```proto message Embedding { option (yaff.proto.message) = {slice_name: "banner_light"}; option (yaff.proto.message) = {slice_name: "banner_heavy"}; // Shared across both slices using its original Protobuf field number optional uint64 vector_id = 1 [ (yaff.proto.field) = {slice_name: "banner_light"}, (yaff.proto.field) = {slice_name: "banner_heavy"} ]; optional uint64 model_version = 2; // Included only in the heavy slice, with an overridden YaFF identifier optional uint64 computed_time = 3 [ (yaff.proto.field) = {slice_name: "banner_heavy", id: 2} ]; } ``` -------------------------------- ### Nested Types Naming Convention Source: https://github.com/yandex/yaff/blob/main/docs/reference/cpp/generated.md Explains how nested messages and enums are flattened into the C++ namespace. ```APIDOC ## Nested Types Nested messages and enums are flattened into the enclosing namespace using an underscore-joined name. ### Example **Proto Definition**: ```proto message Foo { message Bar { enum Baz { VALUE = 0; } } } ``` **Generated C++ Types**: ```cpp struct Foo_Bar final { ... }; enum Foo_Bar_Baz { FOO_BAR_BAZ_VALUE = 0 }; ``` Deeper nesting follows the same rule, joining every level with underscores. ```