Skip to content

Commit

Permalink
Refactor to trailing return types
Browse files Browse the repository at this point in the history
- Trailing return types everywhere
- Optionally, return type deduction where sensible (simple and short functions)

This is related to introduction of common .clang-format,
see boostorg#596 (comment)
  • Loading branch information
mloskot committed May 6, 2021
1 parent bbdce36 commit a0b95e7
Show file tree
Hide file tree
Showing 60 changed files with 655 additions and 522 deletions.
32 changes: 18 additions & 14 deletions include/boost/gil/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,38 +83,38 @@ struct binary_operation_obj
using result_type = Result;

template <typename V1, typename V2> BOOST_FORCEINLINE
result_type operator()(const std::pair<const V1*,const V2*>& p) const {
auto operator()(const std::pair<const V1*,const V2*>& p) const -> result_type {
return apply(*p.first, *p.second, typename views_are_compatible<V1,V2>::type());
}

template <typename V1, typename V2> BOOST_FORCEINLINE
result_type operator()(const V1& v1, const V2& v2) const {
auto operator()(const V1& v1, const V2& v2) const -> result_type {
return apply(v1, v2, typename views_are_compatible<V1,V2>::type());
}

result_type operator()(const error_t&) const { throw std::bad_cast(); }
auto operator()(const error_t&) const -> result_type { throw std::bad_cast(); }
private:

// dispatch from apply overload to a function with distinct name
template <typename V1, typename V2>
BOOST_FORCEINLINE
result_type apply(V1 const& v1, V2 const& v2, std::false_type) const
auto apply(V1 const& v1, V2 const& v2, std::false_type) const -> result_type
{
return ((const Derived*)this)->apply_incompatible(v1, v2);
}

// dispatch from apply overload to a function with distinct name
template <typename V1, typename V2>
BOOST_FORCEINLINE
result_type apply(V1 const& v1, V2 const& v2, std::true_type) const
auto apply(V1 const& v1, V2 const& v2, std::true_type) const -> result_type
{
return ((const Derived*)this)->apply_compatible(v1, v2);
}

// function with distinct name - it can be overloaded by subclasses
template <typename V1, typename V2>
BOOST_FORCEINLINE
result_type apply_incompatible(V1 const& /*v1*/, V2 const& /*v2*/) const
auto apply_incompatible(V1 const& /*v1*/, V2 const& /*v2*/) const -> result_type
{
throw std::bad_cast();
}
Expand Down Expand Up @@ -149,9 +149,10 @@ auto copy(
/// \ingroup STLOptimizations
/// \brief Copy when both src and dst are interleaved and of the same type can be just memmove
template<typename T, typename CS>
BOOST_FORCEINLINE boost::gil::pixel<T,CS>*
copy(const boost::gil::pixel<T,CS>* first, const boost::gil::pixel<T,CS>* last,
boost::gil::pixel<T,CS>* dst) {
BOOST_FORCEINLINE
auto copy(const boost::gil::pixel<T,CS>* first, const boost::gil::pixel<T,CS>* last,
boost::gil::pixel<T,CS>* dst) -> boost::gil::pixel<T,CS>*
{
return (boost::gil::pixel<T,CS>*)std::copy((unsigned char*)first,(unsigned char*)last, (unsigned char*)dst);
}
} // namespace std
Expand All @@ -168,7 +169,8 @@ namespace std {
/// \ingroup STLOptimizations
/// \brief Copy when both src and dst are planar pointers is copy for each channel
template<typename CS, typename IC1, typename IC2> BOOST_FORCEINLINE
boost::gil::planar_pixel_iterator<IC2,CS> copy(boost::gil::planar_pixel_iterator<IC1,CS> first, boost::gil::planar_pixel_iterator<IC1,CS> last, boost::gil::planar_pixel_iterator<IC2,CS> dst) {
auto copy(boost::gil::planar_pixel_iterator<IC1,CS> first, boost::gil::planar_pixel_iterator<IC1,CS> last, boost::gil::planar_pixel_iterator<IC2,CS> dst) -> boost::gil::planar_pixel_iterator<IC2,CS>
{
boost::gil::gil_function_requires<boost::gil::ChannelsCompatibleConcept<typename std::iterator_traits<IC1>::value_type,typename std::iterator_traits<IC2>::value_type>>();
static_for_each(first,last,dst,boost::gil::detail::copy_fn<IC1,IC2>());
return dst+(last-first);
Expand Down Expand Up @@ -244,7 +246,7 @@ struct copier_n<iterator_from_2d<IL>,iterator_from_2d<OL>> {
};

template <typename SrcIterator, typename DstIterator>
BOOST_FORCEINLINE DstIterator copy_with_2d_iterators(SrcIterator first, SrcIterator last, DstIterator dst) {
BOOST_FORCEINLINE auto copy_with_2d_iterators(SrcIterator first, SrcIterator last, DstIterator dst) -> DstIterator {
using src_x_iterator = typename SrcIterator::x_iterator;
using dst_x_iterator = typename DstIterator::x_iterator;

Expand All @@ -270,9 +272,11 @@ namespace std {
/// \ingroup STLOptimizations
/// \brief std::copy(I1,I1,I2) with I1 and I2 being a iterator_from_2d
template <typename IL, typename OL>
BOOST_FORCEINLINE boost::gil::iterator_from_2d<OL> copy1(boost::gil::iterator_from_2d<IL> first, boost::gil::iterator_from_2d<IL> last, boost::gil::iterator_from_2d<OL> dst) {
BOOST_FORCEINLINE auto copy1(boost::gil::iterator_from_2d<IL> first, boost::gil::iterator_from_2d<IL> last, boost::gil::iterator_from_2d<OL> dst) -> boost::gil::iterator_from_2d<OL>
{
return boost::gil::detail::copy_with_2d_iterators(first,last,dst);
}

} // namespace std

namespace boost { namespace gil {
Expand Down Expand Up @@ -307,13 +311,13 @@ class copy_and_convert_pixels_fn : public binary_operation_obj<copy_and_convert_
copy_and_convert_pixels_fn(CC cc_in) : _cc(cc_in) {}
// when the two color spaces are incompatible, a color conversion is performed
template <typename V1, typename V2> BOOST_FORCEINLINE
result_type apply_incompatible(const V1& src, const V2& dst) const {
auto apply_incompatible(const V1& src, const V2& dst) const -> result_type {
copy_pixels(color_converted_view<typename V2::value_type>(src,_cc),dst);
}

// If the two color spaces are compatible, copy_and_convert is just copy
template <typename V1, typename V2> BOOST_FORCEINLINE
result_type apply_compatible(const V1& src, const V2& dst) const {
auto apply_compatible(const V1& src, const V2& dst) const -> result_type {
copy_pixels(src,dst);
}
};
Expand Down
34 changes: 20 additions & 14 deletions include/boost/gil/bit_aligned_pixel_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,22 @@ struct bit_aligned_pixel_iterator : public iterator_facade<bit_aligned_pixel_ite

/// For some reason operator[] provided by iterator_adaptor returns a custom class that is convertible to reference
/// We require our own reference because it is registered in iterator_traits
reference operator[](difference_type d) const { bit_aligned_pixel_iterator it=*this; it.advance(d); return *it; }
auto operator[](difference_type d) const -> reference { bit_aligned_pixel_iterator it=*this; it.advance(d); return *it; }

reference operator->() const { return **this; }
const bit_range_t& bit_range() const { return _bit_range; }
bit_range_t& bit_range() { return _bit_range; }
auto operator->() const -> reference { return **this; }
auto bit_range() const -> bit_range_t const& { return _bit_range; }
auto bit_range() -> bit_range_t& { return _bit_range; }
private:
bit_range_t _bit_range;
static constexpr int bit_size = NonAlignedPixelReference::bit_size;

friend class boost::iterator_core_access;
reference dereference() const { return NonAlignedPixelReference(_bit_range); }
auto dereference() const -> reference { return NonAlignedPixelReference(_bit_range); }
void increment() { ++_bit_range; }
void decrement() { --_bit_range; }
void advance(difference_type d) { _bit_range.bit_advance(d*bit_size); }

difference_type distance_to(const bit_aligned_pixel_iterator& it) const { return _bit_range.bit_distance_to(it._bit_range) / bit_size; }
auto distance_to(bit_aligned_pixel_iterator const& it) const -> difference_type { return _bit_range.bit_distance_to(it._bit_range) / bit_size; }
bool equal(const bit_aligned_pixel_iterator& it) const { return _bit_range==it._bit_range; }
};

Expand Down Expand Up @@ -122,12 +122,14 @@ struct byte_to_memunit<bit_aligned_pixel_iterator<NonAlignedPixelReference>>
{};

template <typename NonAlignedPixelReference>
inline std::ptrdiff_t memunit_step(const bit_aligned_pixel_iterator<NonAlignedPixelReference>&) {
inline auto memunit_step(const bit_aligned_pixel_iterator<NonAlignedPixelReference>&) -> std::ptrdiff_t
{
return NonAlignedPixelReference::bit_size;
}

template <typename NonAlignedPixelReference>
inline std::ptrdiff_t memunit_distance(const bit_aligned_pixel_iterator<NonAlignedPixelReference>& p1, const bit_aligned_pixel_iterator<NonAlignedPixelReference>& p2) {
inline auto memunit_distance(bit_aligned_pixel_iterator<NonAlignedPixelReference> const& p1, bit_aligned_pixel_iterator<NonAlignedPixelReference> const& p2) -> std::ptrdiff_t
{
return (p2.bit_range().current_byte() - p1.bit_range().current_byte())*8 + p2.bit_range().bit_offset() - p1.bit_range().bit_offset();
}

Expand All @@ -137,14 +139,15 @@ inline void memunit_advance(bit_aligned_pixel_iterator<NonAlignedPixelReference>
}

template <typename NonAlignedPixelReference>
inline bit_aligned_pixel_iterator<NonAlignedPixelReference> memunit_advanced(const bit_aligned_pixel_iterator<NonAlignedPixelReference>& p, std::ptrdiff_t diff) {
inline auto memunit_advanced(bit_aligned_pixel_iterator<NonAlignedPixelReference> const& p, std::ptrdiff_t diff) -> bit_aligned_pixel_iterator<NonAlignedPixelReference> {
bit_aligned_pixel_iterator<NonAlignedPixelReference> ret=p;
memunit_advance(ret, diff);
return ret;
}

template <typename NonAlignedPixelReference> inline
NonAlignedPixelReference memunit_advanced_ref(bit_aligned_pixel_iterator<NonAlignedPixelReference> it, std::ptrdiff_t diff) {
auto memunit_advanced_ref(bit_aligned_pixel_iterator<NonAlignedPixelReference> it, std::ptrdiff_t diff) -> NonAlignedPixelReference
{
return *memunit_advanced(it,diff);
}
/////////////////////////////
Expand Down Expand Up @@ -183,11 +186,14 @@ namespace std {
// It is important to provide an overload of uninitialized_copy for bit_aligned_pixel_iterator. The default STL implementation calls placement new,
// which is not defined for bit_aligned_pixel_iterator.
template <typename NonAlignedPixelReference>
boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> uninitialized_copy(boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> first,
boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> last,
boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> dst) {
auto uninitialized_copy(boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> first,
boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> last,
boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference> dst)
-> boost::gil::bit_aligned_pixel_iterator<NonAlignedPixelReference>
{
return std::copy(first,last,dst);
}

} // namespace std
} // namespace std

#endif
33 changes: 19 additions & 14 deletions include/boost/gil/bit_aligned_pixel_reference.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ class bit_range {
BOOST_ASSERT(bit_offset >= 0 && bit_offset < 8);
}

bit_range(const bit_range& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {}
bit_range(bit_range const& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {}
template <bool M> bit_range(const bit_range<RangeSize,M>& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {}

bit_range& operator=(const bit_range& br) { _current_byte = br._current_byte; _bit_offset=br._bit_offset; return *this; }
bool operator==(const bit_range& br) const { return _current_byte==br._current_byte && _bit_offset==br._bit_offset; }
auto operator=(bit_range const& br) -> bit_range& { _current_byte = br._current_byte; _bit_offset=br._bit_offset; return *this; }
bool operator==(bit_range const& br) const { return _current_byte==br._current_byte && _bit_offset==br._bit_offset; }

bit_range& operator++() {
auto operator++() -> bit_range& {
_current_byte += (_bit_offset+RangeSize) / 8;
_bit_offset = (_bit_offset+RangeSize) % 8;
return *this;
}
bit_range& operator--() { bit_advance(-RangeSize); return *this; }
auto operator--() -> bit_range& { bit_advance(-RangeSize); return *this; }

void bit_advance(difference_type num_bits) {
int new_offset = int(_bit_offset+num_bits);
Expand All @@ -71,11 +71,13 @@ class bit_range {
--_current_byte;
}
}
difference_type bit_distance_to(const bit_range& b) const {

auto bit_distance_to(bit_range const& b) const -> difference_type
{
return (b.current_byte() - current_byte())*8 + b.bit_offset()-bit_offset();
}
byte_t* current_byte() const { return _current_byte; }
int bit_offset() const { return _bit_offset; }
auto current_byte() const -> byte_t* { return _current_byte; }
auto bit_offset() const -> int { return _bit_offset; }
};

/// \defgroup ColorBaseModelNonAlignedPixel bit_aligned_pixel_reference
Expand Down Expand Up @@ -136,8 +138,10 @@ struct bit_aligned_pixel_reference

bit_aligned_pixel_reference(){}
bit_aligned_pixel_reference(data_ptr_t data_ptr, int bit_offset) : _bit_range(data_ptr, bit_offset) {}
explicit bit_aligned_pixel_reference(const bit_range_t& bit_range) : _bit_range(bit_range) {}
template <bool IsMutable2> bit_aligned_pixel_reference(const bit_aligned_pixel_reference<BitField,ChannelBitSizes,Layout,IsMutable2>& p) : _bit_range(p._bit_range) {}
explicit bit_aligned_pixel_reference(bit_range_t const& bit_range) : _bit_range(bit_range) {}

template <bool IsMutable2>
bit_aligned_pixel_reference(bit_aligned_pixel_reference<BitField,ChannelBitSizes,Layout,IsMutable2> const& p) : _bit_range(p._bit_range) {}

// Grayscale references can be constructed from the channel reference
explicit bit_aligned_pixel_reference(typename kth_element_type<bit_aligned_pixel_reference,0>::type const channel0)
Expand Down Expand Up @@ -183,11 +187,12 @@ struct bit_aligned_pixel_reference

auto operator->() const -> bit_aligned_pixel_reference const* { return this; }

bit_range_t const& bit_range() const { return _bit_range; }
auto bit_range() const -> bit_range_t const& { return _bit_range; }

private:
mutable bit_range_t _bit_range;
template <typename B, typename C, typename L, bool M> friend struct bit_aligned_pixel_reference;
template <typename B, typename C, typename L, bool M>
friend struct bit_aligned_pixel_reference;

template <typename Pixel> static void check_compatible() { gil_function_requires<PixelsCompatibleConcept<Pixel,bit_aligned_pixel_reference> >(); }

Expand Down Expand Up @@ -369,7 +374,7 @@ namespace std {
// Having three overloads allows us to swap between different (but compatible) models of PixelConcept

template <typename B, typename C, typename L, typename R> inline
void swap(const boost::gil::bit_aligned_pixel_reference<B,C,L,true> x, R& y) {
void swap(boost::gil::bit_aligned_pixel_reference<B,C,L,true> const x, R& y) {
boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
}

Expand All @@ -381,7 +386,7 @@ void swap(typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_ty


template <typename B, typename C, typename L> inline
void swap(const boost::gil::bit_aligned_pixel_reference<B,C,L,true> x, const boost::gil::bit_aligned_pixel_reference<B,C,L,true> y) {
void swap(boost::gil::bit_aligned_pixel_reference<B,C,L,true> const x, const boost::gil::bit_aligned_pixel_reference<B,C,L,true> y) {
boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
}

Expand Down
Loading

0 comments on commit a0b95e7

Please sign in to comment.