Qore Programming Language 1.19.1
Loading...
Searching...
No Matches
inline_printf.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 inline_printf.h
4
5 Qore Programming Language
6
7 Copyright (C) 2007 - 2023 Qore Technologies, sro
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the "Software"),
11 to deal in the Software without restriction, including without limitation
12 the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26
27 Note that the Qore library is released under a choice of three open-source
28 licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29 information.
30*/
31
32#ifndef INC_UTILS_STRING_INLINE_PRINTF_H_PV20061219_
33#define INC_UTILS_STRING_INLINE_PRINTF_H_PV20061219_
34
35#include <qore/common.h>
36
37#include <cassert>
38#include <cstdarg>
39#include <string>
40
41// helper
42extern DLLEXPORT std::string inline_printf_helper_format_string(const char* fmt, va_list arg);
43
44//------------------------------------------------------------------------------
45// Allow to construct std::string with printf() like operation in place.
46// Short name used for easier typing and no visual space overhead.
47// For the same reason it is placed into ths global namespace.
48//
49// Typical use:
50// string s = S("x = %d", x);
51//
52class S {
53private:
54 S& operator=(const S&); // not implemented
55 S(const S&); // not implemented
56
57 std::string result;
58
59public:
60 S(const std::string& fmt, ...) {
61 assert(!fmt.empty());
62 va_list arg;
63 va_start(arg, fmt);
64 result = inline_printf_helper_format_string(fmt.c_str(), arg);
65 va_end(arg);
66 }
67
68 S(const char* fmt, ...) {
69 assert(fmt && fmt[0]);
70 va_list arg;
71 va_start(arg, fmt);
72 result = inline_printf_helper_format_string(fmt, arg);
73 va_end(arg);
74 }
75
76 S(const char* fmt, va_list arg) {
77 assert(fmt && fmt[0]);
78 result = inline_printf_helper_format_string(fmt, arg);
79 }
80
81 S(const std::string& fmt, va_list arg) {
82 assert(!fmt.empty());
83 result = inline_printf_helper_format_string(fmt.c_str(), arg);
84 }
85
86 operator std::string() const { return result; }
87 operator const char*() const { return result.c_str(); }
88};
89
90#endif