Skip to content
Open
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
23 changes: 23 additions & 0 deletions CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ option(CLIPPER2_HI_PRECISION "Caution: enabling this will compromise performance
option(CLIPPER2_UTILS "Build utilities" ON)
option(CLIPPER2_EXAMPLES "Build examples" ON)
option(CLIPPER2_TESTS "Build tests" ON)
option(CLIPPER2_NO_IOSTREAM "Disable iostream-using header overloads" OFF)
option(USE_EXTERNAL_GTEST "Use system-wide installed GoogleTest" OFF)
option(USE_EXTERNAL_GBENCHMARK "Use the googlebenchmark" OFF)
option(BUILD_SHARED_LIBS "Build shared libs" OFF)
Expand Down Expand Up @@ -69,6 +70,7 @@ if (NOT (CLIPPER2_USINGZ STREQUAL "ONLY"))
Clipper2 PUBLIC
CLIPPER2_MAX_DECIMAL_PRECISION=${CLIPPER2_MAX_DECIMAL_PRECISION}
$<$<BOOL:${CLIPPER2_HI_PRECISION}>:CLIPPER2_HI_PRECISION>
$<$<BOOL:${CLIPPER2_NO_IOSTREAM}>:CLIPPER2_NO_IOSTREAM>
)

target_include_directories(
Expand Down Expand Up @@ -102,6 +104,7 @@ if (NOT (CLIPPER2_USINGZ STREQUAL "OFF"))
USINGZ
CLIPPER2_MAX_DECIMAL_PRECISION=${CLIPPER2_MAX_DECIMAL_PRECISION}
$<$<BOOL:${CLIPPER2_HI_PRECISION}>:CLIPPER2_HI_PRECISION>
$<$<BOOL:${CLIPPER2_NO_IOSTREAM}>:CLIPPER2_NO_IOSTREAM>
)
target_include_directories(
Clipper2Z PUBLIC
Expand Down Expand Up @@ -130,6 +133,26 @@ set_target_properties(${CLIPPER2_LIBS} PROPERTIES FOLDER Libraries
PUBLIC_HEADER "${CLIPPER2_INC}"
)

# Compiles the public headers with CLIPPER2_NO_IOSTREAM defined against
# each shipped library variant. Fails if any iostream usage creeps in
# outside the macro guards. Always-on (independent of CLIPPER2_NO_IOSTREAM
# itself); the targets just inherit each library's other PUBLIC
# definitions (USINGZ, CLIPPER2_HI_PRECISION, ...) via target_link_libraries.
foreach(_lib IN LISTS CLIPPER2_LIBS)
set(_check ${_lib}_no_iostream_check)
add_library(${_check} OBJECT Tests/no_iostream_check.cpp)
target_link_libraries(${_check} PRIVATE ${_lib})
if(NOT CLIPPER2_NO_IOSTREAM)
target_compile_definitions(${_check} PRIVATE CLIPPER2_NO_IOSTREAM)
endif()
if(CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
target_compile_options(${_check} PRIVATE /W4 /WX)
elseif(CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "GNU")
target_compile_options(${_check} PRIVATE -Wall -Wextra -Wpedantic -Werror)
endif()
set_target_properties(${_check} PROPERTIES FOLDER Libraries)
endforeach()

if(CLIPPER2_UTILS OR CLIPPER2_TESTS OR CLIPPER2_EXAMPLES)
set(CLIPPER2_UTILS_INC
Utils/clipper.svg.h
Expand Down
10 changes: 10 additions & 0 deletions CPP/Clipper2Lib/include/clipper2/clipper.core.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#include <cstdint>
#include <vector>
#include <string>
#ifndef CLIPPER2_NO_IOSTREAM
#include <iostream>
#endif
#include <algorithm>
#include <numeric>
#include <cmath>
Expand Down Expand Up @@ -166,11 +168,13 @@ namespace Clipper2Lib

void SetZ(const z_type z_value) { z = z_value; }

#ifndef CLIPPER2_NO_IOSTREAM
friend std::ostream& operator<<(std::ostream& os, const Point& point)
{
os << point.x << "," << point.y << "," << point.z;
return os;
}
#endif

#else

Expand Down Expand Up @@ -203,11 +207,13 @@ namespace Clipper2Lib
return Point(x * scale, y * scale);
}

#ifndef CLIPPER2_NO_IOSTREAM
friend std::ostream& operator<<(std::ostream& os, const Point& point)
{
os << point.x << "," << point.y;
return os;
}
#endif
#endif

friend bool operator==(const Point& a, const Point& b)
Expand Down Expand Up @@ -396,10 +402,12 @@ namespace Clipper2Lib
return result;
}

#ifndef CLIPPER2_NO_IOSTREAM
friend std::ostream& operator<<(std::ostream& os, const Rect<T>& rect) {
os << "(" << rect.left << "," << rect.top << "," << rect.right << "," << rect.bottom << ") ";
return os;
}
#endif
};

template <typename T1, typename T2>
Expand Down Expand Up @@ -498,6 +506,7 @@ namespace Clipper2Lib
return Rect<T>(xmin, ymin, xmax, ymax);
}

#ifndef CLIPPER2_NO_IOSTREAM
template <typename T>
std::ostream& operator << (std::ostream& outstream, const Path<T>& path)
{
Expand All @@ -518,6 +527,7 @@ namespace Clipper2Lib
outstream << p;
return outstream;
}
#endif


template <typename T1, typename T2>
Expand Down
4 changes: 4 additions & 0 deletions CPP/Clipper2Lib/include/clipper2/clipper.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ namespace Clipper2Lib {
return true;
}

#ifndef CLIPPER2_NO_IOSTREAM
static void OutlinePolyPath(std::ostream& os,
size_t idx, bool isHole, size_t count, const std::string& preamble)
{
Expand Down Expand Up @@ -338,6 +339,7 @@ namespace Clipper2Lib {
if (pp.Child(i)->Count())
details::OutlinePolyPathD(os, *pp.Child(i), i, preamble + " ");
}
#endif

template<typename T, typename U>
inline constexpr void MakePathGeneric(const T an_array,
Expand Down Expand Up @@ -377,6 +379,7 @@ namespace Clipper2Lib {

} // end details namespace

#ifndef CLIPPER2_NO_IOSTREAM
inline std::ostream& operator<< (std::ostream& os, const PolyTree64& pp)
{
std::string plural = (pp.Count() == 1) ? " polygon." : " polygons.";
Expand All @@ -399,6 +402,7 @@ namespace Clipper2Lib {
if (!pp.Level()) os << std::endl;
return os;
}
#endif

inline Paths64 PolyTreeToPaths64(const PolyTree64& polytree)
{
Expand Down
16 changes: 16 additions & 0 deletions CPP/Tests/no_iostream_check.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*******************************************************************************
* Author : Angus Johnson *
* Date : 3 May 2026 *
* Website : https://www.angusj.com *
* Copyright : Angus Johnson 2010-2026 *
* Purpose : Build-time check that the public headers compile cleanly with *
* CLIPPER2_NO_IOSTREAM defined. *
* License : https://www.boost.org/LICENSE_1_0.txt *
*******************************************************************************/

#include "clipper2/clipper.h"

namespace {
Clipper2Lib::Paths64 _p64;
Clipper2Lib::Rect64 _r64;
} // namespace