Commit cd837d25 authored by Cédric Traizet's avatar Cédric Traizet
Browse files

REFAC: use boost operators instead of otb::OperatorUtilities (ossimplugins::ossimOperatorUtilities

parent e2a658c5
Pipeline #7939 failed with stages
in 66 minutes and 4 seconds
/*
* Copyright (C) 2005-2020 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef otbOperatorUtilities_h
#define otbOperatorUtilities_h
#include <iostream>
namespace boostAdapter {
// Uses Barton-Nackman trick to help implement operator on classes
// Strongly inspired by boost.operators interface
//TODO: imported from ossim plugins, but can we use boost instead ?
#define DEFINE_OPERATORS(name, compound, op) \
template <typename T> struct name ## 1 \
{ \
friend T operator op(T lhs, T const& rhs) { \
lhs compound rhs; \
return lhs; \
} \
}; \
template <typename T, typename U> struct name ## 2 \
{ \
friend T operator op(T lhs, U const& rhs) { \
lhs compound rhs; \
return lhs; \
} \
friend T operator op(U const& lhs, T rhs) { \
rhs compound lhs; \
return rhs; \
} \
}; \
template <typename T, typename U=void> struct name : name ## 2<T,U> {}; \
template <typename T> struct name<T,void> : name ## 1<T> {};
template <typename U, typename V> struct substractable_asym
{
friend U operator-(V const& lhs, V const& rhs) {
return V::template diff<U,V>(lhs, rhs);
}
};
DEFINE_OPERATORS(addable, +=, +);
DEFINE_OPERATORS(substractable, -=, -);
DEFINE_OPERATORS(multipliable, *=, *);
#undef DEFINE_OPERATORS
template <typename T, typename R> struct dividable {
typedef R scalar_type;
friend T operator/(T lhs, scalar_type const& rhs) {
lhs /= rhs;
return lhs;
}
friend scalar_type operator/(T const& lhs, T const& rhs) {
return ratio_(lhs, rhs);
}
};
template <typename T> struct streamable {
friend std::ostream & operator<<(std::ostream & os, const T & v)
{ return v.display(os); }
friend std::istream & operator>>(std::istream & is, T & v)
{ return v.read(is); }
};
template <typename T> struct less_than_comparable {
friend bool operator>(T const& lhs, T const& rhs) {
return rhs < lhs;
}
friend bool operator>=(T const& lhs, T const& rhs) {
return !(lhs < rhs);
}
friend bool operator<=(T const& lhs, T const& rhs) {
return !(rhs < lhs);
}
};
template <typename T> struct equality_comparable {
friend bool operator!=(T const& lhs, T const& rhs) {
return !(rhs == lhs);
}
};
}// namespace boostAdapter
#endif // otbOperatorUtilities_h
......@@ -28,27 +28,60 @@
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/config.hpp>
#include "otbOperatorUtilities.h"
#include <boost/operators.hpp>
// boost::posix_time::time_duration doesn't have a sufficient precision to
// store things such an azimuth time interval, and yet, this IS a duration.
// Hence this new class injected into boost namespace to emulate a duration
// with precision behind the microsecond.
namespace boost { namespace posix_time {
namespace boost
{
namespace posix_time
{
namespace details
{
// division by a scalar and ratio
template <typename T, typename R> struct dividable
{
typedef R scalar_type;
friend T operator/(T lhs, scalar_type const& rhs)
{
lhs /= rhs;
return lhs;
}
friend scalar_type operator/(T const& lhs, T const& rhs)
{
return ratio_(lhs, rhs);
}
};
template <typename T> struct streamable
{
friend std::ostream & operator<<(std::ostream & os, const T & v)
{
return v.display(os);
}
friend std::istream & operator>>(std::istream & is, T & v)
{
return v.read(is);
}
};
} // namespace details
class precise_duration;
double ratio_(precise_duration const& lhs, precise_duration const& rhs);
class precise_duration
: private boostAdapter::addable<precise_duration>
, private boostAdapter::substractable<precise_duration>
, private boostAdapter::streamable<precise_duration>
, private boostAdapter::multipliable2<precise_duration, double>
, private boostAdapter::dividable<precise_duration, double>
, private boostAdapter::equality_comparable<precise_duration>
, private boostAdapter::less_than_comparable<precise_duration>
, private boostAdapter::addable<ptime, precise_duration>
, private boostAdapter::substractable<ptime, precise_duration>
: private boost::addable<precise_duration>
, private boost::subtractable<precise_duration>
, private details::streamable<precise_duration>
, private boost::multipliable2<precise_duration, double>
, private details::dividable<precise_duration, double>
, private boost::equality_comparable<precise_duration>
, private boost::less_than_comparable<precise_duration>
, private boost::addable<ptime, precise_duration>
, private boost::subtractable<ptime, precise_duration>
{
public:
typedef double scalar_type;
......@@ -155,7 +188,7 @@ TimeType ReadFormattedDate(const std::string & dateStr, const std::string & form
#else //OTB_USE_BOOST_TIME
#include "otbOperatorUtilities.h"
#include <boost/operators.hpp>
#include <cassert>
#include <iomanip>
......@@ -165,6 +198,42 @@ namespace MetaData
{
namespace details
{
template <typename U, typename V> struct substractable_asym
{
friend U operator-(V const& lhs, V const& rhs)
{
return V::template diff<U,V>(lhs, rhs);
}
};
// division by a scalar and ratio
template <typename T, typename R> struct dividable
{
typedef R scalar_type;
friend T operator/(T lhs, scalar_type const& rhs)
{
lhs /= rhs;
return lhs;
}
friend scalar_type operator/(T const& lhs, T const& rhs)
{
return ratio_(lhs, rhs);
}
};
template <typename T> struct streamable
{
friend std::ostream & operator<<(std::ostream & os, const T & v)
{
return v.display(os);
}
friend std::istream & operator>>(std::istream & is, T & v)
{
return v.read(is);
}
};
class DayFrac
{
public:
......@@ -249,13 +318,13 @@ class DayFrac
*/
class Duration
: public details::DayFrac
, private boostAdapter::addable<Duration>
, private boostAdapter::substractable<Duration>
, private boostAdapter::streamable<Duration>
, private boostAdapter::multipliable2<Duration, double>
, private boostAdapter::dividable<Duration, details::DayFrac::scalar_type>
, private boostAdapter::equality_comparable<Duration>
, private boostAdapter::less_than_comparable<Duration>
, private boost::addable<Duration>
, private boost::subtractable<Duration>
, private details::streamable<Duration>
, private boost::multipliable2<Duration, double>
, private details::dividable<Duration, details::DayFrac::scalar_type>
, private boost::equality_comparable<Duration>
, private boost::less_than_comparable<Duration>
{
public:
typedef details::DayFrac::scalar_type scalar_type;
......@@ -293,12 +362,12 @@ class DayFrac
*/
class ModifiedJulianDate
: public details::DayFrac
, private boostAdapter::addable<ModifiedJulianDate, Duration>
, private boostAdapter::substractable<ModifiedJulianDate, Duration>
, private boostAdapter::substractable_asym<Duration, ModifiedJulianDate>
, private boostAdapter::streamable<ModifiedJulianDate>
, private boostAdapter::equality_comparable<ModifiedJulianDate>
, private boostAdapter::less_than_comparable<ModifiedJulianDate>
, private boost::addable<ModifiedJulianDate, Duration>
, private boost::subtractable<ModifiedJulianDate, Duration>
, private details::substractable_asym<Duration, ModifiedJulianDate>
, private details::streamable<ModifiedJulianDate>
, private boost::equality_comparable<ModifiedJulianDate>
, private boost::less_than_comparable<ModifiedJulianDate>
{
public:
typedef details::DayFrac::scalar_type scalar_type;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment