AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
AnCH utils library documentation

Introduction

AnCH utils is a header only library of general purpose utilities, used by the other AnCH libraries.

Prerequisites

None.

Installation

CMake target anch::utils (header only):

find_package(Anch REQUIRED)
target_link_libraries(my-app PRIVATE anch::utils)

See installation.

Content

Objects description

A type is described once for every format (anch-json, CSV, ...) by specializing Mapping: its registerObject function registers the fields on the mapper of a format. The specialization is declared in the header of the type, just after it, so that every translation unit which uses the type sees it (code generated by anch-oas does so).

struct Pet {
uint64_t id;
std::string name;
std::string nickname() const { return "little " + name; }
};
// One description of Pet for every format
template<>
struct anch::Mapping<Pet> {
static void
registerObject(anch::ObjectMapper<Pet> auto& mapper) {
mapper
.registerField("id", &Pet::id)
.registerField("name", &Pet::name)
.registerField("nickname", std::function<std::string(const Pet&)>(&Pet::nickname)) // written only
;
}
};
// Types of other libraries can be described too
template<>
struct anch::Mapping<std::pair<std::string, int>> {
static void
registerObject(anch::ObjectMapper<std::pair<std::string, int>> auto& mapper) {
mapper
.registerField("key", &std::pair<std::string, int>::first)
.registerField("value", &std::pair<std::string, int>::second)
;
}
};

A format provides a mapper which satisfies ObjectMapper and gets the fields with describe:

// Mapper of a format: here, it lists the fields of a description
template<typename T>
class FieldsPrinter {
public:
template<typename P, typename... C>
FieldsPrinter& registerField(const std::string& name, P T::*, C&&...) {
std::cout << name << " (member)" << std::endl;
return *this;
}
template<typename P, typename... C>
FieldsPrinter& registerField(const std::string& name, std::function<P(const T&)>, C&&...) {
std::cout << name << " (getter)" << std::endl;
return *this;
}
template<typename P, typename... C>
FieldsPrinter& registerField(const std::string& name, std::function<void(T&, const P&)>, C&&...) {
std::cout << name << " (setter)" << std::endl;
return *this;
}
template<typename P, typename... C>
FieldsPrinter& registerField(const std::string& name, std::function<P(const T&)>, std::function<void(T&, const P&)>, C&&...) {
std::cout << name << " (getter and setter)" << std::endl;
return *this;
}
};
FieldsPrinter<Pet> printer;
anch::describe<Pet>(printer); // id (member), name (member), nickname (getter)

Example

#include "convert.hpp"
#include "threadPool.hpp"
#include <atomic>
#include <chrono>
#include <iostream>
int
main() {
// Strict number conversion (std::bad_cast on error ; anch::tryConvert returns a std::expected)
int value = anch::convert<int>("42");
double ratio = anch::convert<double>("0.5");
// Thread pool: tasks are executed by persistent workers
std::atomic<int> sum = 0;
pool.start();
for(int i = 1 ; i <= 10 ; ++i) {
pool.add([&sum](int n) { sum += n; }, i);
}
pool.awaitTermination(std::chrono::seconds(0)); // waits for queued tasks (0: no timeout), then stops
std::cout << value * ratio << " " << sum << std::endl;
return 0;
}