Skip to content

Commit 00b21b3

Browse files
feat(cont): refactor container components (#126)
1 parent f1bace6 commit 00b21b3

File tree

4 files changed

+157
-116
lines changed

4 files changed

+157
-116
lines changed

cpl/inc/container.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,57 @@
1111
#include <algorithm>
1212
#include <cstdint>
1313
#include <iostream>
14+
#include <ostream>
15+
#include <string>
1416
#include <type_traits>
17+
#include <utility>
1518
#include <vector>
1619

1720
CPL_BEGIN
1821

22+
// ostream overloads for debugging output
23+
template <class First, class Second>
24+
std::ostream& operator<<(std::ostream& os, const std::pair<First, Second>& p) {
25+
return os << '(' << p.first << ", " << p.second << ')';
26+
}
27+
28+
template <typename Cont,
29+
class Valty = typename std::enable_if<!std::is_same<Cont, std::string>::value, typename Cont::value_type>::type>
30+
std::ostream& operator<<(std::ostream& os, const Cont& container) {
31+
os << '{';
32+
auto it = container.begin();
33+
auto end = container.end();
34+
35+
if (it != end) {
36+
os << *it;
37+
++it;
38+
}
39+
40+
for (; it != end; ++it) {
41+
os << ", " << *it;
42+
}
43+
44+
return os << '}';
45+
}
46+
47+
// macros for debugging output
48+
#if DBG_MODE || CPL
49+
template <class... Args>
50+
void dbg_out() {
51+
std::cerr << std::endl;
52+
}
53+
54+
template <class Head, class... Tail>
55+
void dbg_out(Head&& head, Tail&&... tail) {
56+
std::cerr << ' ' << head;
57+
dbg_out(std::forward<Tail>(tail)...);
58+
}
59+
60+
#define test(...) std::cerr << "[" << #__VA_ARGS__ << "]:", dbg_out(__VA_ARGS__)
61+
#else // ^^^ DBG_MODE || CPL ^^^ / vvv !DBG_MODE && !CPL
62+
#define test(...)
63+
#endif // DBG_MODE || CPL
64+
1965
template <typename... Ts>
2066
struct _Is_container_helper {};
2167

@@ -33,6 +79,49 @@ constexpr bool is_container = _Is_container<T>::value;
3379

3480
#define CPL_IS_CONTAINER(T) static_assert(is_container<T>, "Templated parameter is not a valid container.")
3581

82+
template <class FwdIter>
83+
const void output_container(FwdIter first, FwdIter last, const bool& space = true, const bool& new_line = true) {
84+
for (; first != last; ++first) {
85+
std::cout << *first;
86+
87+
if (space && first != last - 1) {
88+
std::cout << ' ';
89+
}
90+
}
91+
92+
std::cout << (new_line ? '\n' : ' ');
93+
}
94+
95+
template <typename Cont>
96+
const void output_container(const Cont& container, const bool& space = true, const bool& new_line = true) {
97+
#if CPL
98+
CPL_IS_CONTAINER(Cont);
99+
#endif // CPL
100+
output_container(container.begin(), container.end(), space, new_line);
101+
}
102+
103+
template <class FwdIter>
104+
const void output_reverse_container(
105+
FwdIter first, FwdIter last, const bool& space = true, const bool& new_line = true) {
106+
for (; first != last; ++first) {
107+
std::cout << *first;
108+
109+
if (space && first != last - 1) {
110+
std::cout << ' ';
111+
}
112+
}
113+
114+
std::cout << (new_line ? '\n' : ' ');
115+
}
116+
117+
template <typename Cont>
118+
const void output_reverse_container(const Cont& container, const bool& space = true, const bool& new_line = true) {
119+
#if CPL
120+
CPL_IS_CONTAINER(Cont);
121+
#endif // CPL
122+
output_reverse_container(container.rbegin(), container.rend(), space, new_line);
123+
}
124+
36125
template <class... Args>
37126
auto alternating_insertion(Args&&... args) {
38127
// 3-arg -> Treat arguments as containers, e.g. (input1, input2, output)

cpl/inc/stdoutput.h

Lines changed: 0 additions & 108 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) Brandon Pacewic
2+
// SPDX-License-Identifier: MIT
3+
4+
#include <cassert>
5+
#include <iostream>
6+
#include <list>
7+
#include <map>
8+
#include <set>
9+
#include <sstream>
10+
#include <string>
11+
#include <utility>
12+
#include <vector>
13+
14+
#include "container.h"
15+
16+
int main() {
17+
using namespace std;
18+
using namespace cpl;
19+
{
20+
vector<int> v{1, 2, 3};
21+
stringstream ss;
22+
ss << v;
23+
assert(ss.str() == "{1, 2, 3}");
24+
}
25+
{
26+
list<string> l{"foo", "bar"};
27+
stringstream ss;
28+
ss << l;
29+
assert(ss.str() == "{foo, bar}");
30+
}
31+
{
32+
set<char> s{'a', 'b'};
33+
stringstream ss;
34+
ss << s;
35+
assert(ss.str() == "{a, b}");
36+
}
37+
{
38+
pair<int, string> p{42, "answer"};
39+
stringstream ss;
40+
ss << p;
41+
assert(ss.str() == "(42, answer)");
42+
}
43+
{
44+
map<string, int> m{{"x", 1}, {"y", 2}};
45+
stringstream ss;
46+
ss << m;
47+
// Note: map is ordered by key, so this will be {(x, 1), (y, 2)}
48+
assert(ss.str() == "{(x, 1), (y, 2)}");
49+
}
50+
#if DBG_MODE || CPL
51+
{
52+
stringstream errbuf;
53+
auto old = cerr.rdbuf(errbuf.rdbuf());
54+
55+
int a = 10;
56+
string b = "hi";
57+
test(a, b);
58+
59+
cerr.rdbuf(old);
60+
61+
auto out = errbuf.str();
62+
assert(out.find("[a, b]:") != string::npos);
63+
assert(out.find("10") != string::npos);
64+
assert(out.find("hi") != string::npos);
65+
}
66+
#endif
67+
return 0;
68+
}

tests/cpl/include_stdoutput_header/test.cpp

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)