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
11 changes: 11 additions & 0 deletions Stream_support/include/CGAL/IO/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,17 @@ class Input_rep<std::optional<T>>

//! perform the input, calls \c operator\>\> by default.
std::istream& operator()( std::istream& is) const {
// an empty optional is written as "--" (see `Output_rep<std::optional<T>>`)
is >> std::ws;
if(is.peek() == '-') {
is.get();
if(is.peek() == '-') {
is.get();
t = std::nullopt;
return is;
}
is.putback('-');
}
T v;
if(is >> v) t = v;
return is;
Expand Down
23 changes: 23 additions & 0 deletions Stream_support/test/Stream_support/test_ioformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <optional>


template <class NT>
Expand All @@ -49,6 +50,24 @@ void test_io(const NT& x){
}
}

// an empty optional is written as "--" and must be read back as such
template <class NT>
void test_io_optional(const NT& x){
std::optional<NT> a, b(x), c(-x), d;
std::ostringstream os;
os << ::CGAL::IO::oformat(a) << ' ' << ::CGAL::IO::oformat(b) << ' '
<< ::CGAL::IO::oformat(c) << ' ' << ::CGAL::IO::oformat(d);
std::istringstream is(os.str());
std::optional<NT> ta(x), tb, tc, td(x);
is >> ::CGAL::IO::iformat(ta) >> ::CGAL::IO::iformat(tb)
>> ::CGAL::IO::iformat(tc) >> ::CGAL::IO::iformat(td);
assert( is );
assert( a == ta );
assert( b == tb );
assert( c == tc );
assert( d == td );
}

int main() {
static_assert(CGAL::Output_rep<int>::is_specialized == false);
#ifndef CGAL_DISABLE_FROM_CHARS
Expand Down Expand Up @@ -91,6 +110,10 @@ int main() {
test_io<long double>(0);
test_io<long double>(-12);

std::cout << "test_io: std::optional "<< std::endl;
test_io_optional<int>(12);
test_io_optional<double>(1.5);

//TODO: add more
std::cout << "ok" <<std::endl;

Expand Down
Loading