Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

## SofaBuffers C++ library

[![CI](https://github.com/sofa-buffers/corelib-cpp/actions/workflows/build-gcc-x86_64.yaml/badge.svg?branch=main)](https://github.com/sofa-buffers/corelib-cpp/actions/workflows/build-gcc-x86_64.yaml)
[![Coverage](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/sofa-buffers/corelib-cpp/badges/coverage-cpp.json)](https://github.com/sofa-buffers/corelib-cpp/actions/workflows/coverage.yaml)
[![Docs](https://img.shields.io/badge/docs-online-blue)](https://sofa-buffers.github.io/corelib-cpp/)

Expand Down Expand Up @@ -44,7 +45,9 @@ C backend.
Non-native targets are built and run under [QEMU](https://www.qemu.org/)
user-mode emulation in CI, reproducible locally without the real hardware. Both
x86_64 legs build and run the full suite twice — in `Debug` **and** in `Release`
(`-O3 -DNDEBUG`) — so a defect only the optimiser exposes cannot ship green.
(`-O3 -DNDEBUG`) — so a defect only the optimiser exposes cannot ship green. The
**CI** badge at the top of this section tracks the first row; the table is the
whole matrix.

| Target | Status |
| - | - |
Expand Down Expand Up @@ -312,6 +315,15 @@ option is a validation *policy*, never a wire-format switch, so peers with
different settings interoperate on all valid data; conformance and the shared
vectors run with it **ON**.

`SOFAB_STRICT_UTF8` is also the **only** compile-time knob this library has: the
header always compiles the full wire format, with no `#ifdef` gating of wire
types and nothing else to configure. (The conformance harness recognises the
family's `SOFAB_DISABLE_*` names only so it can skip the matching vectors when
validating a feature-reduced profile; the defines do **not** change this
library.) For a strictly minimal binary, use the C corelib
[`corelib-c-cpp`](https://github.com/sofa-buffers/corelib-c-cpp), which does gate
wire features.

### Code generator

The usual way to drive the library is through **generated object code**: a schema
Expand Down Expand Up @@ -648,21 +660,6 @@ These three types are deliberately identical, in name and behaviour, to
`corelib-c-cpp`'s, so generated code for a bounded field is the same whichever C++
corelib it targets.

## Feature flags

**Almost none.** This header-only C++20 port always compiles the full wire
format — there is no `#ifdef` gating of wire types and nothing to configure. (The
conformance harness recognises the family's `SOFAB_DISABLE_*` names only so it can
skip the matching vectors when validating a feature-reduced profile; the defines
do **not** change this library.) For a strictly minimal binary, use the C corelib
[`corelib-c-cpp`](https://github.com/sofa-buffers/corelib-c-cpp).

The one compile-time knob is **`SOFAB_STRICT_UTF8`** (default `1`): the strict
UTF-8 validation policy documented under
[Strict UTF-8 validation](#strict-utf-8-validation-sofab_strict_utf8-default-on).
Define it to `0` for a non-strict build that stores `string` payloads verbatim
(raw, never lossy). It gates a validation policy only, never the wire format.

## Build & test

```sh
Expand All @@ -680,7 +677,7 @@ Run it in both configurations — `-DCMAKE_BUILD_TYPE=Debug` and
(strict aliasing, UB the optimiser acts on, an uninitialised field) is invisible
to a Debug-only run.

Four suites run under CTest:
Five suites run under CTest:

- **`test_roundtrip`** — encode/decode/nested/chunked/skip checks plus the
three-valued decode outcome (§7: COMPLETE / INCOMPLETE / INVALID), malformed-input
Expand Down Expand Up @@ -728,6 +725,14 @@ Four suites run under CTest:
strategy matrix sets `fail-fast: false`. `${{ matrix.build_type }}` is resolved
against the workflow's own `build_type:` axis. Skipped in a source tree
without `.github/workflows/`.
- **`test_readme_structure`** — the other document-level check: CORELIB_PLAN §9
fixes this README's section list, their order and the badge block for the whole
corelib family, and nothing in the library can notice that shape drifting. It
reads `README.md` and requires the §9.1 header block, a §9.2 badge block
carrying CI, coverage and Docs in that order, exactly the §9 top-level sections
with no invented ones, no API-documentation section (§9.4), and the §9.8
two-corelib comparison as a subsection of
[Benchmarks](#benchmarks). Skipped in a tree without a README.

### Coverage and API docs

Expand Down Expand Up @@ -785,7 +790,7 @@ build/bench/bench encode_typical # one operation, then exit (Callgrind mode)

`ctest -R test_bench_tools` guards that sharing.

## Choosing between the two C++ corelibs
### Choosing between the two C++ corelibs

SofaBuffers ships **two** C++ implementations of the same wire format, tuned for
opposite ends of the spectrum:
Expand All @@ -810,7 +815,7 @@ porting between them is mostly mechanical.
| Feature gating | Always full format (no `#ifdef`) | `SOFAB_DISABLE_*` compile out unused wire features |
| Target | Desktop / server | Bare metal / embedded C and C++ |

### Instruction counts (Callgrind)
#### Instruction counts (Callgrind)

Machine-independent instruction counts from the shared benchmark tooling, this
library against the C corelib and its C++ wrapper on identical workloads (lower
Expand Down
12 changes: 12 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ add_test(NAME test_ci_workflows
${PROJECT_SOURCE_DIR}/.github/workflows)
set_tests_properties(test_ci_workflows PROPERTIES SKIP_RETURN_CODE 77)

# CORELIB_PLAN §9 fixes the README's section list, their order, and the §9.2
# badge block for the whole corelib family — the shared shape is what makes one
# port's README navigable to a reader who knows another. That is a property of
# the document, so no C++ test can notice it drifting: this check reads
# README.md and fails on an invented or reordered top-level section, a missing
# CI/coverage/Docs badge, or the §9.8 two-corelib comparison escaping from
# under `## Benchmarks`.
add_test(NAME test_readme_structure
COMMAND bash ${CMAKE_CURRENT_LIST_DIR}/test_readme_structure.sh
${PROJECT_SOURCE_DIR}/README.md)
set_tests_properties(test_readme_structure PROPERTIES SKIP_RETURN_CODE 77)

if(SOFAB_ENABLE_COVERAGE)
target_link_libraries(test_roundtrip PRIVATE sofab_coverage)
target_link_libraries(test_vectors PRIVATE sofab_coverage)
Expand Down
152 changes: 152 additions & 0 deletions test/test_readme_structure.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#!/usr/bin/env bash
#
# SofaBuffers pure-C++20 — guards README.md against CORELIB_PLAN §9.
#
# §9 fixes the README's shape for the whole corelib family: "Do not change the
# section ordering and do not invent new top-level sections; that shared shape
# is the point." A reader who knows one port's README navigates any other by
# looking in the same place. Nothing inside the library can notice when that
# shape drifts — a top-level section that exists in no other port, or a missing
# badge, is invisible to every C++ test — so the check has to read the document.
#
# What it enforces:
#
# 1. §9.1 the centered header block: logo, `# SofaBuffers`, tagline, org link.
# 2. §9.2 the badge block that opens the library section carries a CI badge, a
# coverage badge and a Docs badge, in that order, before any prose.
# 3. §9 the `## ` sections are exactly the prescribed list, in order:
# `## SofaBuffers <Language> library`, `## Why this design`, `## Usage`,
# `## Memory handling`, `## Build & test`, `## Benchmarks` — no extras.
# 4. §9.4 no API-documentation section at any heading level; the Docs badge is
# the only pointer to the generated reference.
# 5. §9.8 C++ is a language with two corelibs, so the comparison between them
# is a *subsection* of `## Benchmarks` — its final one — not a section of
# its own after it.
#
# Usage: test_readme_structure.sh <path-to-README.md>
#
# SPDX-License-Identifier: MIT
set -uo pipefail

README="${1:?usage: test_readme_structure.sh <path-to-README.md>}"

# CTest maps 77 to "skipped" (SKIP_RETURN_CODE): a consumer tree that vendored
# only the header has no README to check.
if [ ! -f "$README" ]; then
echo "SKIP: no README at $README"
exit 77
fi

fails=0
fail() { echo "FAIL: $*" >&2; fails=$((fails + 1)); }
ok() { echo " ok: $*"; }

# Headings are read from a copy with fenced code blocks blanked out: a shell
# comment inside a ```sh block is not a section, and the Build & test snippets
# are full of them.
PROSE="$(mktemp)"
trap 'rm -f "$PROSE"' EXIT
awk '/^```/ { fence = !fence; print ""; next } fence { print "" ; next } { print }' \
"$README" > "$PROSE"

# ---------------------------------------------------------------- §9.1 header

grep -q '<p align="center"><img src="assets/sofabuffers_logo.png"' "$README" \
&& ok "§9.1 centered logo" || fail "§9.1 centered logo block missing"
grep -qx '# SofaBuffers' "$README" \
&& ok "§9.1 title" || fail "§9.1 '# SofaBuffers' title missing"
grep -qF '<b>Structured Objects For Anyone</b><br>' "$README" \
&& ok "§9.1 tagline" || fail "§9.1 tagline missing"
grep -q 'https://github.com/sofa-buffers' "$README" \
&& ok "§9.1 organization link" || fail "§9.1 link back to the organization missing"

# ------------------------------------------------------- §9 top-level sections

# The section list §9 prescribes, in order. The first is the only one whose
# wording varies per port (`## SofaBuffers <Language> library`).
expected="## SofaBuffers C++ library
## Why this design
## Usage
## Memory handling
## Build & test
## Benchmarks"

actual="$(grep -E '^## ' "$PROSE")"

if [ "$actual" = "$expected" ]; then
ok "§9 top-level sections are the prescribed list, in order"
else
fail "§9 top-level sections differ from the prescribed list (no invented sections, no reordering)"
diff <(printf '%s\n' "$expected") <(printf '%s\n' "$actual") \
| sed 's/^/ /' >&2
fi

# --------------------------------------------------------- §9.2 badge block

# Everything between the library heading and the first blank line after it: §9.2
# puts the badges first in the section, ahead of the GitHub link and the summary.
badges="$(awk '
/^## SofaBuffers .* library$/ { inside = 1; next }
inside && /^[[:space:]]*$/ { if (seen) exit; next }
inside { seen = 1; print }
' "$PROSE")"

if [ -z "$badges" ]; then
fail "§9.2 the library section opens with no badge block"
else
# Badge alt texts, in document order: "[![CI](…)](…)" -> "CI".
order="$(printf '%s\n' "$badges" | grep -oE '^\[!\[[^]]*\]' | sed 's/^\[!\[//; s/\]$//')"
for want in CI Coverage Docs; do
printf '%s\n' "$order" | grep -qi "^${want}\$" \
&& ok "§9.2 badge block carries a $want badge" \
|| fail "§9.2 badge block carries no $want badge (it has:$(printf ' %s' $order))"
done
# …and in the order §9.2 lists them: CI, coverage, Docs.
ranked="$(printf '%s\n' "$order" | grep -iE '^(CI|Coverage|Docs)$' | tr 'A-Z' 'a-z')"
if [ "$ranked" = "$(printf 'ci\ncoverage\ndocs')" ]; then
ok "§9.2 badges are in the CI / coverage / Docs order"
else
fail "§9.2 badges out of order: expected CI, coverage, Docs — got$(printf ' %s' $ranked)"
fi
fi

# ------------------------------------------------ §9.4 no API-doc section

if grep -qiE '^#+ +(source documentation|api reference|api documentation|api docs)[[:space:]]*$' "$PROSE"; then
fail "§9.4 an API-documentation section exists; the Docs badge is the only pointer"
else
ok "§9.4 no API-documentation section"
fi

# ------------------------------------- §9.8 two-corelib comparison placement

# C++ ships two corelibs for two use cases, so §9.8 requires the comparison as a
# final *subsection* of `## Benchmarks`.
compare_heading="$(grep -nE '^#+ +Choosing between the two' "$PROSE" | head -1)"
if [ -z "$compare_heading" ]; then
fail "§9.8 no subsection comparing the two C++ corelibs"
else
level="${compare_heading#*:}"
level="${level%% *}"
if [ "$level" = "###" ]; then
ok "§9.8 the two-corelib comparison is a subsection"
else
fail "§9.8 the two-corelib comparison is a '$level' heading; §9.8 makes it a '###' subsection of ## Benchmarks"
fi
# …and it must sit under Benchmarks, which is therefore the last section.
owner="$(awk -F: -v n="${compare_heading%%:*}" '
NR < n && /^## / { last = $0 }
END { print last }
' "$PROSE")"
if [ "$owner" = "## Benchmarks" ]; then
ok "§9.8 the comparison sits under ## Benchmarks"
else
fail "§9.8 the comparison sits under '$owner', not under ## Benchmarks"
fi
fi

if [ "$fails" -ne 0 ]; then
echo "test_readme_structure: $fails failure(s)" >&2
exit 1
fi
echo "test_readme_structure: all checks passed"