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; }
};
template<>
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))
;
}
};
template<>
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:
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;
Example
#include "convert.hpp"
#include "threadPool.hpp"
#include <atomic>
#include <chrono>
#include <iostream>
int
main() {
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));
std::cout << value * ratio << " " << sum << std::endl;
return 0;
}