PLCnext API Documentation 23.0.2.9
BasicFormatter.hxx
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
7#include "cppformat/format.h"
8#include <memory>
9
10#ifndef ARP_INSIDE_ARP_H
11 #error Never include 'BasicFormatter.hxx' directly, just include 'Arp.h'
12#endif
13
14namespace Arp
15{
16
17// forwards
18template<typename C, typename A> class BasicString;
19
25template<typename C, typename A = std::allocator<C>>
27{
28public: // usings
29 using CharType = C;
31 using ExceptionHandler = std::function<void(const char*, const char*)>;
32
33public:
35 template<typename... Args>
36 static StringType FormatCommon(ExceptionHandler exceptionHandler, const StringType& format, const Args& ... args);
37
39 template<typename... Args>
40 static StringType FormatCommon(ExceptionHandler exceptionHandler, const CharType* format, const Args& ... args);
41
43 template<typename... Args>
44 static StringType FormatPrintf(ExceptionHandler exceptionHandler, const StringType& format, const Args& ... args);
45
47 template<typename... Args>
48 static StringType FormatPrintf(ExceptionHandler exceptionHandler, const CharType* format, const Args& ... args);
49};
50
52// inline methods of class BasicFormatter
53template<typename C, typename A>
54template<typename... Args>
55inline typename BasicFormatter<C, A>::StringType BasicFormatter<C, A>::FormatCommon(ExceptionHandler exceptionHandler, const StringType& format, const Args& ... args)
56{
57 return BasicFormatter::FormatCommon(exceptionHandler, format.CStr(), args...);
58}
59
60template<typename C, typename A>
61template<typename... Args>
62inline typename BasicFormatter<C, A>::StringType BasicFormatter<C, A>::FormatCommon(ExceptionHandler exceptionHandler, const CharType* format, const Args& ... args)
63{
64 try
65 {
66 return fmt::format(format, args...);
67 }
68 catch (std::exception& e)
69 {
70 exceptionHandler(format, e.what());
71 return StringType();
72 }
73}
74
75template<typename C, typename A>
76template<typename... Args>
77inline typename BasicFormatter<C, A>::StringType BasicFormatter<C, A>::FormatPrintf(ExceptionHandler exceptionHandler, const StringType& format, const Args& ... args)
78{
79 return BasicFormatter<C, A>::FormatPrintf(exceptionHandler, format.CStr(), args...);
80}
81
82template<typename C, typename A>
83template<typename... Args>
84inline typename BasicFormatter<C, A>::StringType BasicFormatter<C, A>::FormatPrintf(ExceptionHandler exceptionHandler, const CharType* format, const Args& ... args)
85{
86 try
87 {
88 return fmt::sprintf(format, args...);
89 }
90 catch (std::exception& e)
91 {
92 exceptionHandler(format, e.what());
93 return StringType();
94 }
95}
96
97} // end of namespace Arp
This class encapsulates formatting operations which are based on the open source library libfmt forme...
Definition: BasicFormatter.hxx:27
static StringType FormatPrintf(ExceptionHandler exceptionHandler, const StringType &format, const Args &... args)
Uses Ansi-C printf syntax for the placeholders in the format string like 's'.
Definition: BasicFormatter.hxx:77
C CharType
The char type provided as template argument.
Definition: BasicFormatter.hxx:29
static StringType FormatCommon(ExceptionHandler exceptionHandler, const StringType &format, const Args &... args)
Uses common CLR syntax (.Net) for the placeholders in the format string like '{0}'.
Definition: BasicFormatter.hxx:55
std::function< void(const char *, const char *)> ExceptionHandler
The exception handler signature.
Definition: BasicFormatter.hxx:31
Definition: BasicString.hxx:33
const CharType * CStr() const
Gets the character data of this string.
Definition: BasicString.hxx:1508
Root namespace for the PLCnext API