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