Skip to content

Commit

Permalink
macroify the proxied functions
Browse files Browse the repository at this point in the history
  • Loading branch information
akrieger committed Dec 24, 2023
1 parent f40d35d commit 8216003
Showing 1 changed file with 27 additions and 54 deletions.
81 changes: 27 additions & 54 deletions src/value_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,72 +152,43 @@ struct heap : private std::unique_ptr<T> {
public:

// Various conditionally defined proxy functions for common types like containers.
#pragma push_macro("PROXY")
#define PROXY(func) \
template<typename ...Us> \
auto func( Us&& ...us ) -> decltype( val().func( std::forward<Us>( us )... ) ) { \
return val().func( std::forward<Us>( us )... ); \
}
#pragma push_macro("PROXY_CONST")
#define PROXY_CONST(func) \
template<typename ...Us> \
auto func( Us&& ...us ) const -> decltype( val().func( std::forward<Us>( us )... ) ) { \
return val().func( std::forward<Us>( us )... ); \
}

// Tests & sets
auto empty() const -> decltype( val().empty() ) {
return val().empty();
}

template<typename U>
auto count( U &&u ) const -> decltype( val().count( std::forward<U>( u ) ) ) {
return val().count( std::forward<U>( u ) );
}

auto size() const -> decltype( val().size() ) {
return val().size();
}

auto clear() -> decltype( val().clear() ) {
return val().clear();
}
PROXY_CONST( empty );
PROXY_CONST( count );
PROXY_CONST( size );
PROXY( clear );

// Iterators
auto begin() -> decltype( val().begin() ) {
return val().begin();
}

auto begin() const -> decltype( val().begin() ) {
return val().begin();
}

auto end() -> decltype( val().end() ) {
return val().end();
}

auto end() const -> decltype( val().end() ) {
return val().end();
}
PROXY( begin );
PROXY_CONST( begin );
PROXY( end );
PROXY_CONST( end );

// Accessors
template<typename U>
auto operator[]( U &&u ) -> decltype( val()[std::forward<U>( u )] ) {
return val()[std::forward<U>( u )];
}

template<typename U>
auto find( U &&u ) -> decltype( val().find( std::forward<U>( u ) ) ) {
return val().find( std::forward<U>( u ) );
}

template<typename U>
auto find( U &&u ) const -> decltype( val().find( std::forward<U>( u ) ) ) {
return val().find( std::forward<U>( u ) );
}

template<typename U>
auto erase( U &&u ) -> decltype( val().erase( std::forward<U>( u ) ) ) {
return val().erase( std::forward<U>( u ) );
}
PROXY( find );
PROXY_CONST( find );

template<typename U>
auto insert( U &&u ) -> decltype( val().insert( std::forward<U>( u ) ) ) {
return val().insert( std::forward<U>( u ) );
}

template<typename U>
auto emplace( U &&u ) -> decltype( val().emplace( std::forward<U>( u ) ) ) {
return val().emplace( std::forward<U>( u ) );
}
PROXY( erase );
PROXY( insert );
PROXY( emplace );

// Json* support.
template<typename Stream = JsonOut>
Expand All @@ -229,6 +200,8 @@ struct heap : private std::unique_ptr<T> {
void deserialize( const Value &jsin ) {
jsin.read( val() );
}
#pragma pop_macro("PROXY")
#pragma pop_macro("PROXY_CONST")
};

} // namespace cata
Expand Down

0 comments on commit 8216003

Please sign in to comment.