PLCnext API Documentation  20.6.0.30321
BasicFormatter.hxx
1 //
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 
14 namespace Arp
15 {
16 
17 // forwards
18 template<typename C, typename A> class BasicString;
19 
20 template<typename C, typename A = std::allocator<C>>
22 {
23 public: // usings
24  using CharType = C;
26  using ExceptionHandler = std::function<void(const char*, const char*)>;
27 
28 public:
29  template<typename... Args>
30  static StringType FormatCommon(ExceptionHandler exceptionHandler, const StringType& format, const Args& ... args);
31 
32  template<typename... Args>
33  static StringType FormatCommon(ExceptionHandler exceptionHandler, const CharType* format, const Args& ... args);
34 
35  template<typename... Args>
36  static StringType FormatPrintf(ExceptionHandler exceptionHandler, const StringType& format, const Args& ... args);
37 
38  template<typename... Args>
39  static StringType FormatPrintf(ExceptionHandler exceptionHandler, const CharType* format, const Args& ... args);
40 };
41 
43 // inline methods of class BasicFormatter
45 template<typename C, typename A>
46 template<typename... Args>
47 inline typename BasicFormatter<C, A>::StringType BasicFormatter<C, A>::FormatCommon(ExceptionHandler exceptionHandler, const StringType& format, const Args& ... args)
48 {
49  return BasicFormatter::FormatCommon(exceptionHandler, format.CStr(), args...);
50 }
51 
53 template<typename C, typename A>
54 template<typename... Args>
55 inline typename BasicFormatter<C, A>::StringType BasicFormatter<C, A>::FormatCommon(ExceptionHandler exceptionHandler, const CharType* format, const Args& ... args)
56 {
57  try
58  {
59  return fmt::format(format, args...);
60  }
61  catch (std::exception& e)
62  {
63  exceptionHandler(format, e.what());
64  return StringType();
65  }
66 }
67 
69 template<typename C, typename A>
70 template<typename... Args>
71 inline typename BasicFormatter<C, A>::StringType BasicFormatter<C, A>::FormatPrintf(ExceptionHandler exceptionHandler, const StringType& format, const Args& ... args)
72 {
73  return BasicFormatter<C, A>::FormatPrintf(exceptionHandler, format.CStr(), args...);
74 }
75 
77 template<typename C, typename A>
78 template<typename... Args>
79 inline typename BasicFormatter<C, A>::StringType BasicFormatter<C, A>::FormatPrintf(ExceptionHandler exceptionHandler, const CharType* format, const Args& ... args)
80 {
81  try
82  {
83  return fmt::sprintf(format, args...);
84  }
85  catch (std::exception& e)
86  {
87  exceptionHandler(format, e.what());
88  return StringType();
89  }
90 }
91 
92 } // end of namespace Arp
Definition: BasicFormatter.hxx:21
Definition: BasicFormatter.hxx:18
static StringType FormatCommon(ExceptionHandler exceptionHandler, const StringType &format, const Args &... args)
Uses common CLR syntax (.Net) for the placeholders in the format string like &#39;{0}&#39;.
Definition: BasicFormatter.hxx:47
Root namespace for the PLCnext API
static StringType FormatPrintf(ExceptionHandler exceptionHandler, const StringType &format, const Args &... args)
Uses Ansi-C printf syntax for the placeholders in the format string like &#39;s&#39;.
Definition: BasicFormatter.hxx:71