Added conversion from optional<variant<...>>

This commit is contained in:
Philipp Möller 2011-06-22 15:27:48 +00:00
parent ba7f258db8
commit b4c1504975
2 changed files with 18 additions and 4 deletions

View File

@ -34,6 +34,7 @@
#include <typeinfo>
#include <boost/variant.hpp>
#include <boost/optional.hpp>
namespace CGAL {
@ -111,9 +112,14 @@ class Object
Object(const T& t) {
// we cannot invoke another ctor from here, so we have to behave
// like the copy ctor of our base
CGAL::Object tmp = boost::apply_visitor(Object_from_variant(), t);
this->ptr = tmp.ptr;
(this->ptr)->add_reference();
if(t) {
CGAL::Object tmp = boost::apply_visitor(Object_from_variant(), *t);
this->ptr = tmp.ptr;
(this->ptr)->add_reference();
} else {
typedef Wrapper<Empty> Wrap;
ptr = new Wrap(Empty());
}
}
template <class T>

View File

@ -3,11 +3,15 @@
#include <CGAL/Object.h>
#include <CGAL/assertions.h>
#include <boost/variant.hpp>
#include <boost/optional.hpp>
void test_object() {
int i = 0;
double j = 0.0;
boost::variant<int, char, double> v = 23;
boost::optional< boost::variant<int, char, double> > v(23);
CGAL::Object o = v;
CGAL_assertion(!o.empty());
CGAL_assertion(CGAL::assign(i, o));
CGAL_assertion(i == 23);
//reassign the variant and assign it again
@ -16,6 +20,10 @@ void test_object() {
CGAL_assertion(!CGAL::assign(i, o));
CGAL_assertion(CGAL::assign(j, o));
CGAL_assertion(j == 2.0);
//empty optional
boost::optional< boost::variant<int, char, double> > v2;
CGAL::Object o2 = v2;
CGAL_assertion(o2.empty());
}
int main() {