Skip to content

Commit

Permalink
IR-578 Get rid of boost:hana
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Chernyshov <[email protected]>
  • Loading branch information
Alexey-N-Chernyshov committed Jan 18, 2018
1 parent 32d1689 commit 23aea1e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
5 changes: 3 additions & 2 deletions irohad/consensus/yac/impl/yac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include <utility>

#include "common/inplace_visitor.hpp"
#include "common/visitor.hpp"
#include "consensus/yac/yac.hpp"

namespace iroha {
Expand Down Expand Up @@ -167,7 +167,8 @@ namespace iroha {
},
[&](const RejectMessage &reject) {
log_->warn("reject case");
// TODO 14/08/17 Muratov: work on reject case IR-497
// TODO 14/08/17 Muratov: work on reject case
// IR-497
});
vote_storage_.markAsProcessedState(proposal_hash);
}
Expand Down
2 changes: 1 addition & 1 deletion libs/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
add_library(libs_common
files.cpp
)
visitor.hpp)

target_link_libraries(libs_common
logger
Expand Down
94 changes: 94 additions & 0 deletions libs/common/visitor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* 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 IROHA_VISITOR_HPP
#define IROHA_VISITOR_HPP

#include <boost/variant.hpp>

namespace iroha {

template <typename... Lambdas>
struct lambda_visitor;

template <typename Lambda, typename... Lambdas>
struct lambda_visitor<Lambda, Lambdas...>
: public Lambda, public lambda_visitor<Lambdas...> {
using Lambda::operator();
using lambda_visitor<Lambdas...>::operator();

lambda_visitor(Lambda lambda, Lambdas... lambdas)
: Lambda(lambda), lambda_visitor<Lambdas...>(lambdas...) {}
};

template <typename Lambda>
struct lambda_visitor<Lambda> : public Lambda {
using Lambda::operator();

lambda_visitor(Lambda lambda) : Lambda(lambda) {}
};

/**
* @brief Convenient in-place compile-time visitor creation, from a set of
* lambdas
*
* @code
* make_visitor([](int a){ return 1; },
* [](std::string b) { return 2; });
* @nocode
*
* is essentially the same as
*
* @code
* struct visitor : public boost::static_visitor<int> {
* int operator()(int a) { return 1; }
* int operator()(std::string b) { return 2; }
* }
* @nocode
*
* @param lambdas
* @return visitor
*/
template <class... Fs>
constexpr auto make_visitor(Fs &&... fs) {
using visitor_type = lambda_visitor<std::decay_t<Fs>...>;
return visitor_type(std::forward<Fs>(fs)...);
}

/**
* @brief Inplace visitor for boost::variant.
* @code
* boost::variant<int, std::string> value = "1234";
* ...
* visit_in_place(value,
* [](int v) { std::cout << "(int)" << v; },
* [](std::string v) { std::cout << "(string)" << v;}
* );
* @nocode
*
* @param variant
* @param lambdas
* @param lambdas
*/
template <typename TVariant, typename... TVisitors>
constexpr auto visit_in_place(TVariant &&variant, TVisitors &&... visitors) {
auto visitor = make_visitor(visitors...);
return boost::apply_visitor(visitor, std::forward<TVariant>(variant));
}
}

#endif // IROHA_VISITOR_HPP

0 comments on commit 23aea1e

Please sign in to comment.