Skip to content

Commit 3ee3c41

Browse files
committed
adding more elaborate example
1 parent 508fa70 commit 3ee3c41

File tree

5 files changed

+197
-7
lines changed

5 files changed

+197
-7
lines changed

cppcon2025/software/CMakeLists.txt

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,53 @@
1-
21
cmake_minimum_required(VERSION 3.16)
32
project(cppcon2025)
43

5-
set(CMAKE_CXX_STANDARD 23)
4+
add_compile_options(-freflection -fexpansion-statements
5+
$<$<COMPILE_LANGUAGE:CXX>:-stdlib=libc++>
6+
$<$<COMPILE_LANGUAGE:CXX>:-std=c++26>)
7+
add_definitions(-DSIMDJSON_STATIC_REFLECTION=1)
68

79
add_executable(player_demo main.cpp)
810

911

1012
include(cmake/CPM.cmake)
1113
CPMAddPackage("gh:nlohmann/[email protected]")
12-
CPMAddPackage("gh:fmtlib/fmt#11.2.0")
14+
CPMAddPackage(
15+
NAME fmt
16+
GITHUB_REPOSITORY fmtlib/fmt
17+
GIT_TAG 18e160eb4c2e9f5c750f5eb11ab13be44aa8de78
18+
)
19+
20+
CPMAddPackage(
21+
NAME simdjson
22+
GITHUB_REPOSITORY simdjson/simdjson
23+
GIT_TAG 015daad6a95a4f67c08ed5980d24b57be221c38f
24+
OPTIONS "SIMDJSON_STATIC_REFLECTION=ON"
25+
)
26+
27+
CPMAddPackage(
28+
NAME cURL
29+
VERSION 8.15.0
30+
GIT_TAG tags/curl-8_15_0
31+
GITHUB_REPOSITORY curl/curl
32+
OPTIONS
33+
"BUILD_CURL_EXE OFF"
34+
"CURL_USE_OPENSSL ON"
35+
"CURL_ENABLE_SSL ON"
36+
"CURL_DISABLE_INSTALL ON"
37+
"CURL_DISABLE_DIGEST_AUTH ON"
38+
"CURL_DISABLE_KERBEROS_AUTH ON"
39+
"CURL_DISABLE_NEGOTIATE_AUTH ON"
40+
"CURL_USE_LIBPSL OFF"
41+
)
1342

1443
target_link_libraries(player_demo PRIVATE nlohmann_json::nlohmann_json)
44+
target_link_libraries(player_demo PRIVATE simdjson)
1545

1646

1747
target_link_libraries(player_demo PRIVATE fmt::fmt)
48+
add_executable(webservice webservice.cpp)
49+
#@target_compile_options(webservice PRIVATE -freflection -fexpansion-statements -stdlib=libc++ -std=c++26)
50+
target_link_libraries(webservice PRIVATE libcurl)
51+
target_link_libraries(webservice PRIVATE fmt::fmt)
52+
target_link_libraries(webservice PRIVATE simdjson::simdjson)
53+
#target_compile_definitions(webservice PRIVATE SIMDJSON_STATIC_REFLECTION=1)

cppcon2025/software/Dockerfile

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Stage 1. Check out LLVM source code and run the build.
2+
FROM debian:12 AS builder
3+
# First, Update the apt's source list and include the sources of the packages.
4+
RUN grep deb /etc/apt/sources.list | \
5+
sed 's/^deb/deb-src /g' >> /etc/apt/sources.list
6+
# Install compiler, python and subversion.
7+
RUN apt-get update && \
8+
apt-get install -y --no-install-recommends ca-certificates gnupg \
9+
build-essential cmake make python3 zlib1g wget subversion unzip ninja-build git linux-perf && \
10+
rm -rf /var/lib/apt/lists/*
11+
ARG CLANG_COMMIT=d77eff1cbd78fd065668acf93b1f5f400d39134d
12+
RUN git clone --depth=1 https://github.com/bloomberg/clang-p2996.git /tmp/clang-source && \
13+
cd /tmp/clang-source && \
14+
git fetch origin $CLANG_COMMIT --depth=1 && \
15+
git checkout $CLANG_COMMIT
16+
RUN cmake -S /tmp/clang-source/llvm -B /tmp/clang-source/build-llvm -DCMAKE_BUILD_TYPE=Release \
17+
-DLLVM_ENABLE_ASSERTIONS=ON \
18+
-DLLVM_UNREACHABLE_OPTIMIZE=ON \
19+
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" \
20+
-DCLANG_DEFAULT_CXX_STDLIB=libc++ \
21+
-DLLVM_ENABLE_PROJECTS=clang -G Ninja
22+
RUN cmake --build /tmp/clang-source/build-llvm -j
23+
RUN cmake --install /tmp/clang-source/build-llvm --prefix /tmp/clang-install
24+
25+
26+
# Stage 2. Produce a minimal release image with build results.
27+
FROM debian:12
28+
ARG USER_NAME
29+
ARG USER_ID
30+
ARG GROUP_ID
31+
LABEL maintainer "LLVM Developers"
32+
# Install packages for minimal useful image.
33+
RUN apt-get update && \
34+
apt-get install -y --no-install-recommends build-essential ca-certificates rust-all libcurl4-openssl-dev cmake make wget python3 python3-dev sudo curl ninja-build vim git binutils linux-perf && \
35+
rm -rf /var/lib/apt/lists/*
36+
# Copy build results of stage 1 to /usr/local.
37+
COPY --from=builder /tmp/clang-install/ /usr/local/
38+
RUN P=`/usr/local/bin/clang++ -v 2>&1 | grep Target | cut -d' ' -f2-`; echo /usr/local/lib/$P > /etc/ld.so.conf.d/$P.conf
39+
RUN ldconfig
40+
RUN addgroup --gid $GROUP_ID user; exit 0
41+
RUN adduser --disabled-password --gecos '' --uid $USER_ID --gid $GROUP_ID $USER_NAME; exit 0
42+
RUN echo "$USER_NAME:$USER_NAME" | chpasswd && adduser $USER_NAME sudo
43+
RUN echo '----->'
44+
RUN echo 'root:Docker!' | chpasswd
45+
ENV TERM xterm-256color
46+
USER $USER_NAME

cppcon2025/software/README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,35 @@ struct Player {
1212
};
1313
```
1414
15+
## Use docker
16+
17+
```sh
18+
./run_docker.sh
19+
```
20+
21+
## Install OpenSLL
22+
23+
Your password is your username.
24+
25+
```
26+
sudo apt update
27+
sudo apt-get install libssl-dev -y
28+
```
29+
1530
## Compilation
1631

32+
While in the `software` directory.
33+
1734
```sh
18-
cd software
19-
cmake -B build
35+
36+
CXX=clang++ CC=clang cmake -B build
2037
cmake --build build
2138
```
2239

2340
## Exécution
2441

2542
```sh
2643
./build/player_demo
44+
./build/webservice
2745
```
2846

29-
## Dépendance
30-
La bibliothèque nlohmann/json est récupérée automatiquement via CMake FetchContent.

cppcon2025/software/run_docker.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
if [ -z "$1" ]
4+
then
5+
echo "The run-docker-station script takes a command as a argument. E.g., try ./run-docker-station 'ls' "
6+
exit 1
7+
fi
8+
9+
set -o noglob
10+
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
11+
12+
COMMAND=$@
13+
14+
tuser=$(echo $USER | tr -dc 'a-z')
15+
16+
container_name=${CONTAINER_NAME:-"debian12-clang-p2996-programming_station-for-$tuser-simdjson"}
17+
18+
command -v docker >/dev/null 2>&1 || { echo >&2 "Please install docker. E.g., go to https://www.docker.com/products/docker-desktop Type 'docker' to diagnose the problem."; exit 1; }
19+
20+
docker info >/dev/null 2>&1 || { echo >&2 "Docker server is not running? type 'docker info'."; exit 1; }
21+
22+
SSHPARAM=""
23+
[ -e "${SSH_AUTH_SOCK}" ] && SSHPARAM="--volume /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock --env SSH_AUTH_SOCK=/run/host-services/ssh-auth.sock"
24+
[ -e "${HOME}/.ssh" ] && SSHPARAM="--volume ${HOME}/.ssh:/home/$tuser/.ssh:ro"
25+
26+
27+
docker image inspect $container_name >/dev/null 2>&1 || ( echo "instantiating the container" ; docker build --no-cache -t $container_name -f $SCRIPTPATH/Dockerfile --build-arg USER_NAME="$tuser" --build-arg USER_ID=$(id -u) --build-arg GROUP_ID=$(id -g) . )
28+
29+
if [ -t 0 ]; then DOCKER_ARGS=-it; fi
30+
docker run --rm $DOCKER_ARGS -h $container_name $SSHPARAM -v $(pwd):$(pwd):Z --privileged -w $(pwd) $container_name sh -c "$COMMAND"

cppcon2025/software/webservice.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <string>
2+
#include <vector>
3+
#include <curl/curl.h>
4+
#include <cstdlib>
5+
#include <fmt/core.h>
6+
#include <fmt/format.h>
7+
#include <simdjson.h>
8+
9+
#if !SIMDJSON_STATIC_REFLECTION
10+
#error "You need to enable static reflection for this to work"
11+
#endif
12+
13+
14+
struct weather_data {
15+
std::vector<std::string> time;
16+
std::vector<float> temperature_2m;
17+
std::vector<float> relative_humidity_2m;
18+
std::vector<float> winddirection_10m;
19+
std::vector<float> precipitation;
20+
std::vector<float> windspeed_10m;
21+
};
22+
23+
std::string grab_weather_data(const std::string& latitude, const std::string& longitude) {
24+
std::string url = fmt::format("https://api.open-meteo.com/v1/forecast?latitude={}&longitude={}&hourly=temperature_2m,relative_humidity_2m,winddirection_10m,precipitation,windspeed_10m", latitude, longitude);
25+
CURL *curl = curl_easy_init();
26+
if (!curl) {
27+
throw std::runtime_error("Could not initialize cURL");
28+
}
29+
std::string response_data;
30+
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
31+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, +[](char *ptr, size_t size, size_t nmemb, void *userdata) -> size_t {
32+
auto *str = static_cast<std::string*>(userdata);
33+
str->append(ptr, size * nmemb);
34+
return size * nmemb;
35+
});
36+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_data);
37+
CURLcode res = curl_easy_perform(curl);
38+
if (res != CURLE_OK) {
39+
curl_easy_cleanup(curl);
40+
throw std::runtime_error("Request failed cURL: " + std::string(curl_easy_strerror(res)));
41+
}
42+
curl_easy_cleanup(curl);
43+
return response_data;
44+
}
45+
46+
int main() {
47+
std::string weather_data_str = grab_weather_data("45.5017", "-73.5673");
48+
simdjson::ondemand::parser parser;
49+
simdjson::ondemand::document doc = parser.iterate(simdjson::pad(weather_data_str));
50+
weather_data wd = doc["hourly"].get<weather_data>();
51+
// Assuming all vectors have the same length
52+
for (size_t i = 0; i < wd.time.size(); ++i) {
53+
fmt::print("Time: {}, Temperature: {:.1f}°C, Humidity: {:.1f}%, Wind Direction: {:.1f}°, Precipitation: {:.1f}mm, Wind Speed: {:.1f}km/h\n",
54+
wd.time[i],
55+
wd.temperature_2m[i],
56+
wd.relative_humidity_2m[i],
57+
wd.winddirection_10m[i],
58+
wd.precipitation[i],
59+
wd.windspeed_10m[i]);
60+
}
61+
return EXIT_SUCCESS;
62+
}

0 commit comments

Comments
 (0)