PLCnext API Documentation 25.0.2.69
BasicFormatter.hxx
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
7#include "Arp/Base/Core/Detail/BasicString.hxx"
8#include "Arp/Base/Core/Detail/fmt_cast.hxx"
9#include <fmt/format.h>
10#include <fmt/ostream.h>
11#include <fmt/printf.h>
12#include <functional>
13
14#ifndef ARP_INSIDE_ARP_BASE_CORE_ARP_H
15 #error Never include 'BasicFormatter.hxx' directly, just include 'Arp.hpp'
16#endif
17
18namespace Arp { namespace Base { namespace Core
19{
20
21
22// forwards
23template<typename C, typename A> class BasicString;
24
30template<typename C, typename A = std::allocator<C>>
32{
33public: // usings
34 using CharType = C;
36 using ExceptionHandler = void(*)(const char*, const char*);
37
38public:
40 template<typename... Args>
41 static StringType FormatCommon(ExceptionHandler exceptionHandler, const StringType& format, Args&& ... args);
42
44 template<typename... Args>
45 static StringType FormatCommon(ExceptionHandler exceptionHandler, const CharType* format, Args&& ... args);
46
48 template<typename... Args>
49 static StringType FormatPrintf(ExceptionHandler exceptionHandler, const StringType& format, Args&& ... args);
50
52 template<typename... Args>
53 static StringType FormatPrintf(ExceptionHandler exceptionHandler, const CharType* format, Args&& ... args);
54};
55
57// inline methods of class BasicFormatter
58template<typename C, typename A>
59template<typename... Args>
60inline typename BasicFormatter<C, A>::StringType BasicFormatter<C, A>::FormatCommon(ExceptionHandler exceptionHandler, const StringType& format, Args&& ... args)
61{
62 return BasicFormatter::FormatCommon(exceptionHandler, format.CStr(), std::forward<Args>(args)...);
63}
64
65template<typename C, typename A>
66template<typename... Args>
67inline typename BasicFormatter<C, A>::StringType BasicFormatter<C, A>::FormatCommon(ExceptionHandler exceptionHandler, const CharType* format, Args&& ... args)
68{
69 try
70 {
71 return fmt::format(fmt::runtime(format), fmt_cast(std::forward<Args>(args))...);
72 }
73 catch (std::exception& e)
74 {
75 exceptionHandler(format, e.what());
76 return StringType();
77 }
78}
79
80template<typename C, typename A>
81template<typename... Args>
82inline typename BasicFormatter<C, A>::StringType BasicFormatter<C, A>::FormatPrintf(ExceptionHandler exceptionHandler, const StringType& format, Args&& ... args)
83{
84 return BasicFormatter<C, A>::FormatPrintf(exceptionHandler, format.CStr(), std::forward<Args>(args)...);
85}
86
87template<typename C, typename A>
88template<typename... Args>
89inline typename BasicFormatter<C, A>::StringType BasicFormatter<C, A>::FormatPrintf(ExceptionHandler exceptionHandler, const CharType* format, Args&& ... args)
90{
91 try
92 {
93 return fmt::sprintf(format, fmt_cast(std::forward<Args>(args))...);
94 }
95 catch (std::exception& e)
96 {
97 exceptionHandler(format, e.what());
98 return StringType();
99 }
100}
101
102}}} // end of namespace Arp::Base::Core
103
106namespace fmt
107{
108}
This class encapsulates formatting operations which are based on the open source library libfmt forme...
Definition: BasicFormatter.hxx:32
static StringType FormatPrintf(ExceptionHandler exceptionHandler, const StringType &format, Args &&... args)
Uses Ansi-C printf syntax for the placeholders in the format string like 's'.
Definition: BasicFormatter.hxx:82
static StringType FormatCommon(ExceptionHandler exceptionHandler, const StringType &format, Args &&... args)
Uses common CLR syntax (.Net) for the placeholders in the format string like '{0}'.
Definition: BasicFormatter.hxx:60
C CharType
The char type provided as template argument.
Definition: BasicFormatter.hxx:34
void(*)(const char *, const char *) ExceptionHandler
The exception handler signature.
Definition: BasicFormatter.hxx:36
This template class implements the extensions of class String related to std::string,...
Definition: BasicString.hxx:34
Root namespace for the PLCnext API
Namespace of fmtlib.
Definition: BasicFormatter.hxx:107