Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions cpl/inc/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,57 @@
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <ostream>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

CPL_BEGIN

// ostream overloads for debugging output
template <class First, class Second>
std::ostream& operator<<(std::ostream& os, const std::pair<First, Second>& p) {
return os << '(' << p.first << ", " << p.second << ')';
}

template <typename Cont,
class Valty = typename std::enable_if<!std::is_same<Cont, std::string>::value, typename Cont::value_type>::type>
std::ostream& operator<<(std::ostream& os, const Cont& container) {
os << '{';
auto it = container.begin();
auto end = container.end();

if (it != end) {
os << *it;
++it;
}

for (; it != end; ++it) {
os << ", " << *it;
}

return os << '}';
}

// macros for debugging output
#if DBG_MODE || CPL
template <class... Args>
void dbg_out() {
std::cerr << std::endl;
}

template <class Head, class... Tail>
void dbg_out(Head&& head, Tail&&... tail) {
std::cerr << ' ' << head;
dbg_out(std::forward<Tail>(tail)...);
}

#define test(...) std::cerr << "[" << #__VA_ARGS__ << "]:", dbg_out(__VA_ARGS__)
#else // ^^^ DBG_MODE || CPL ^^^ / vvv !DBG_MODE && !CPL
#define test(...)
#endif // DBG_MODE || CPL

template <typename... Ts>
struct _Is_container_helper {};

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

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

template <class FwdIter>
const void output_container(FwdIter first, FwdIter last, const bool& space = true, const bool& new_line = true) {
for (; first != last; ++first) {
std::cout << *first;

if (space && first != last - 1) {
std::cout << ' ';
}
}

std::cout << (new_line ? '\n' : ' ');
}

template <typename Cont>
const void output_container(const Cont& container, const bool& space = true, const bool& new_line = true) {
#if CPL
CPL_IS_CONTAINER(Cont);
#endif // CPL
output_container(container.begin(), container.end(), space, new_line);
}

template <class FwdIter>
const void output_reverse_container(
FwdIter first, FwdIter last, const bool& space = true, const bool& new_line = true) {
for (; first != last; ++first) {
std::cout << *first;

if (space && first != last - 1) {
std::cout << ' ';
}
}

std::cout << (new_line ? '\n' : ' ');
}

template <typename Cont>
const void output_reverse_container(const Cont& container, const bool& space = true, const bool& new_line = true) {
#if CPL
CPL_IS_CONTAINER(Cont);
#endif // CPL
output_reverse_container(container.rbegin(), container.rend(), space, new_line);
}

template <class... Args>
auto alternating_insertion(Args&&... args) {
// 3-arg -> Treat arguments as containers, e.g. (input1, input2, output)
Expand Down
108 changes: 0 additions & 108 deletions cpl/inc/stdoutput.h

This file was deleted.

68 changes: 68 additions & 0 deletions tests/cpl/container_debug_output/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) Brandon Pacewic
// SPDX-License-Identifier: MIT

#include <cassert>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#include "container.h"

int main() {
using namespace std;
using namespace cpl;
{
vector<int> v{1, 2, 3};
stringstream ss;
ss << v;
assert(ss.str() == "{1, 2, 3}");
}
{
list<string> l{"foo", "bar"};
stringstream ss;
ss << l;
assert(ss.str() == "{foo, bar}");
}
{
set<char> s{'a', 'b'};
stringstream ss;
ss << s;
assert(ss.str() == "{a, b}");
}
{
pair<int, string> p{42, "answer"};
stringstream ss;
ss << p;
assert(ss.str() == "(42, answer)");
}
{
map<string, int> m{{"x", 1}, {"y", 2}};
stringstream ss;
ss << m;
// Note: map is ordered by key, so this will be {(x, 1), (y, 2)}
assert(ss.str() == "{(x, 1), (y, 2)}");
}
#if DBG_MODE || CPL
{
stringstream errbuf;
auto old = cerr.rdbuf(errbuf.rdbuf());

int a = 10;
string b = "hi";
test(a, b);

cerr.rdbuf(old);

auto out = errbuf.str();
assert(out.find("[a, b]:") != string::npos);
assert(out.find("10") != string::npos);
assert(out.find("hi") != string::npos);
}
#endif
return 0;
}
8 changes: 0 additions & 8 deletions tests/cpl/include_stdoutput_header/test.cpp

This file was deleted.