PLCnext API Documentation 25.0.2.69
BasicFormatter.hxx
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
7#include <fmt/format.h>
8#include <fmt/ostream.h>
9#include <fmt/printf.h>
10#include <memory>
11
12#ifndef ARP_USE_ARP_SYSTEM_CORE
13
14// nothing to include here, included by Arp.hpp
15
16#else
17
18#ifndef ARP_INSIDE_ARP_H
19 #error Never include 'BasicFormatter.hxx' directly, just include 'Arp.h'
20#endif
21
22namespace Arp
23{
24
25// forwards
26template<typename C, typename A> class BasicString;
27
33template<typename C, typename A = std::allocator<C>>
34class BasicFormatter
35{
36public: // usings
37 using CharType = C;
38 using StringType = BasicString<C, A>;
39 using ExceptionHandler = std::function<void(const char*, const char*)>;
40
41public:
43 template<typename... Args>
44 static StringType FormatCommon(ExceptionHandler exceptionHandler, const StringType& format, const Args& ... args);
45
47 template<typename... Args>
48 static StringType FormatCommon(ExceptionHandler exceptionHandler, const CharType* format, const Args& ... args);
49
51 template<typename... Args>
52 static StringType FormatPrintf(ExceptionHandler exceptionHandler, const StringType& format, const Args& ... args);
53
55 template<typename... Args>
56 static StringType FormatPrintf(ExceptionHandler exceptionHandler, const CharType* format, const Args& ... args);
57};
58
60// inline methods of class BasicFormatter
61template<typename C, typename A>
62template<typename... Args>
63inline typename BasicFormatter<C, A>::StringType BasicFormatter<C, A>::FormatCommon(ExceptionHandler exceptionHandler, const StringType& format, const Args& ... args)
64{
65 return BasicFormatter::FormatCommon(exceptionHandler, format.CStr(), args...);
66}
67
68template<typename C, typename A>
69template<typename... Args>
70inline typename BasicFormatter<C, A>::StringType BasicFormatter<C, A>::FormatCommon(ExceptionHandler exceptionHandler, const CharType* format, const Args& ... args)
71{
72 try
73 {
74 return fmt::format(fmt::runtime(format), args...);
75 }
76 catch (std::exception& e)
77 {
78 exceptionHandler(format, e.what());
79 return StringType();
80 }
81}
82
83template<typename C, typename A>
84template<typename... Args>
85inline typename BasicFormatter<C, A>::StringType BasicFormatter<C, A>::FormatPrintf(ExceptionHandler exceptionHandler, const StringType& format, const Args& ... args)
86{
87 return BasicFormatter<C, A>::FormatPrintf(exceptionHandler, format.CStr(), args...);
88}
89
90template<typename C, typename A>
91template<typename... Args>
92inline typename BasicFormatter<C, A>::StringType BasicFormatter<C, A>::FormatPrintf(ExceptionHandler exceptionHandler, const CharType* format, const Args& ... args)
93{
94 try
95 {
96 return fmt::sprintf(format, args...);
97 }
98 catch (std::exception& e)
99 {
100 exceptionHandler(format, e.what());
101 return StringType();
102 }
103}
104
105} // end of namespace Arp
106
107#endif // ndef ARP_USE_ARP_SYSTEM_CORE
Root namespace for the PLCnext API