TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec_print.hpp
Go to the documentation of this file.
1// Time-stamp: <Last changed 2026-02-20 16:00:43 by magnolia>
2/*----------------------------------------------------------------------
3------------------------------------------------------------------------
4Copyright (c) 2020-2026 The Emacs Cat (https://github.com/olddeuteronomy/tec).
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17------------------------------------------------------------------------
18----------------------------------------------------------------------*/
27#pragma once
28
29#include <iostream>
30#include <ostream>
31#include <sstream>
32#include <string>
33
34#include "tec/tec_def.hpp" // IWYU pragma: keep
35
36
37namespace tec {
38
45
53template <typename T>
54void print(std::ostream* out, const T& arg) {
55 *out << arg;
56}
57
69template <typename T, typename... Targs>
70void print(std::ostream* out, const char* fmt, const T& value, Targs&&... Args) {
71 for (; *fmt != '\0'; fmt++) {
72 if (*fmt == '{' && *(fmt + 1) == '}') {
73 *out << value;
74 if (*(fmt + 1) != '\0') ++fmt;
75 print<>(out, fmt + 1, Args...); // recursive call
76 return;
77 }
78 *out << *fmt;
79 }
80}
81
89template <typename T>
90void println(std::ostream* out, const T& arg) {
91 *out << arg << std::endl;
92}
93
105template <typename T, typename... Targs>
106void println(std::ostream* out, const char* fmt, const T& value, Targs&&... Args) {
107 print<>(out, fmt, value, Args...);
108 *out << std::endl;
109}
110
117template <typename T>
118void print(const T& arg) {
119 print<>(&std::cout, arg);
120}
121
132template <typename T, typename... Targs>
133void print(const char* fmt, const T& value, Targs&&... Args) {
134 print<>(&std::cout, fmt, value, Args...);
135}
136
143template <typename T>
144void println(const T& arg) {
145 println<>(&std::cout, arg);
146}
147
158template <typename T, typename... Targs>
159void println(const char* fmt, const T& value, Targs&&... Args) {
160 println<>(&std::cout, fmt, value, Args...);
161}
162
170template <typename T>
171std::string format(const T& arg) {
172 std::ostringstream buf;
173 print<>(&buf, arg);
174 return buf.str();
175}
176
188template <typename T, typename... Targs>
189std::string format(const char* fmt, const T& value, Targs&&... Args) {
190 std::ostringstream buf;
191 print<>(&buf, fmt, value, Args...);
192 return buf.str();
193}
194
196
197} // namespace tec
Common definitions and utilities for the tec namespace.
std::string format(const T &arg)
Formats a single argument into a string.
Definition tec_print.hpp:171
void print(std::ostream *out, const T &arg)
Outputs a single argument to the specified stream.
Definition tec_print.hpp:54
void println(std::ostream *out, const T &arg)
Outputs a single argument to the specified stream with a newline.
Definition tec_print.hpp:90