Skip to content

Commit

Permalink
Merge pull request #43 from GeniusVentures/test-exploring
Browse files Browse the repository at this point in the history
Test exploring
  • Loading branch information
henriqueaklein authored May 8, 2024
2 parents 2af9d1e + cef9ed8 commit a6722b2
Show file tree
Hide file tree
Showing 59 changed files with 370 additions and 510 deletions.
17 changes: 12 additions & 5 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ Checks: >
-cppcoreguidelines-pro-type-reinterpret-cast,
-cppcoreguidelines-special-member-functions,
google-*,
-google-build-using-namespace,
-google-explicit-constructor,
-google-readability-namespace-comments,
-google-readability-todo,
google-build-explicit-make-pair,
google-build-namespaces,
google-default-arguments,
google-global-names-in-headers,
google-readability-avoid-underscore-in-googletest-name,
google-readability-casting,
google-runtime-int,
google-runtime-operator,
google-upgrade-googletest-case,
hicpp-multiway-paths-covered,
Expand Down Expand Up @@ -61,3 +65,6 @@ CheckOptions:
- { key: readability-identifier-naming.TypeDefCase, value: CamelCase }

- { key: readability-identifier-naming.ClassMemberPrefix, value: m_ }

- { key: modernize-use-std-print.ReplacementPrintFunction, value: "fmt::print"}
- { key: modernize-use-std-print.ReplacementPrintlnFunction, value: "fmt::println"}
4 changes: 3 additions & 1 deletion .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
# git config blame.ignoreRevsFile .git-blame-ignore-revs

# Reformats
cd55bda30ece6f3af73bdbaea84e5cda93f8973a
cd55bda30ece6f3af73bdbaea84e5cda93f8973a
968c9f34ae30975867ca18dea49d6c4671bdbb78
5f7734854e56dda50ee22270f11b8afb45ff16f9
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ Testing

# Autogenerated Tooling
.build.Debug
cmake-build-*/
cmake-build-*/

# Testing residuals
CRDT.Datastore.TEST.unit/
CRDT.Datastore.TEST/
2 changes: 2 additions & 0 deletions src/base/blob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ namespace sgns::base {
{
std::string out_str;
char temp_buf[3];

for ( auto it = this->begin(); it != this->end(); ++it )
{
snprintf( temp_buf, sizeof( temp_buf ), "%02x", *it );
out_str.append( temp_buf, sizeof( temp_buf ) - 1 );
}

return out_str;
}

Expand Down
108 changes: 51 additions & 57 deletions src/base/visitor.hpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
#ifndef SUPERGENIUS_VISITOR_HPP
#define SUPERGENIUS_VISITOR_HPP

#include <type_traits> // for std::decay
#include <utility> // for std::forward
#include <type_traits>
#include <utility>

#include <boost/variant/apply_visitor.hpp> // for boost::apply_visitor
#include <boost/variant/apply_visitor.hpp>

namespace sgns {
namespace sgns
{
template <typename... Lambdas> struct lambda_visitor;

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();

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... )
{
}
};

// NOLINTNEXTLINE(google-explicit-constructor)
lambda_visitor(Lambda lambda, Lambdas... lambdas)
: Lambda(lambda), lambda_visitor<Lambdas...>(lambdas...) {}
};
template <typename Lambda> struct lambda_visitor<Lambda> : public Lambda
{
using Lambda::operator();

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

// NOLINTNEXTLINE(google-explicit-constructor)
lambda_visitor(Lambda lambda) : Lambda(lambda) {}
};

/**
/**
* @brief Convenient in-place compile-time visitor creation, from a set of
* lambdas
*
* @code
* @code{.cpp}
* make_visitor([](int a){ return 1; },
* [](std::string b) { return 2; });
* @nocode
Expand All @@ -48,50 +48,44 @@ namespace sgns {
* }
* @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)...);
}
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
* @code{.cpp}
* 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 decltype(auto) visit_in_place(TVariant &&variant,
TVisitors &&... visitors) {
return boost::apply_visitor(
make_visitor(std::forward<TVisitors>(visitors)...),
std::forward<TVariant>(variant));
}
template <typename TVariant, typename... TVisitors>
constexpr decltype( auto ) visit_in_place( TVariant &&variant, TVisitors &&...visitors )
{
return boost::apply_visitor( make_visitor( std::forward<TVisitors>( visitors )... ),
std::forward<TVariant>( variant ) );
}

/// apply Matcher to optional T
template <typename T, typename Matcher>
constexpr decltype(auto) match(T &&t, Matcher &&m) {
return std::forward<T>(t) ? std::forward<Matcher>(m)(*std::forward<T>(t))
: std::forward<Matcher>(m)();
}
/// apply Matcher to optional T
template <typename T, typename Matcher> constexpr decltype( auto ) match( T &&t, Matcher &&m )
{
return std::forward<T>( t ) ? std::forward<Matcher>( m )( *std::forward<T>( t ) )
: std::forward<Matcher>( m )();
}

/// construct visitor from Fs and apply it to optional T
template <typename T, typename... Fs>
constexpr decltype(auto) match_in_place(T &&t, Fs &&... fs) {
return match(std::forward<T>(t), make_visitor(std::forward<Fs>(fs)...));
}
} // namespace sgns
/// construct visitor from Fs and apply it to optional T
template <typename T, typename... Fs> constexpr decltype( auto ) match_in_place( T &&t, Fs &&...fs )
{
return match( std::forward<T>( t ), make_visitor( std::forward<Fs>( fs )... ) );
}
}

#endif // SUPERGENIUS_VISITOR_HPP
#endif
2 changes: 0 additions & 2 deletions src/blockchain/block_header_repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ namespace sgns::blockchain
class BlockHeaderRepository : public IComponent
{
public:
virtual ~BlockHeaderRepository() = default;

/**
* @param hash - a blake2_256 hash of an SCALE encoded block header
* @return the number of the block with the provided hash in case one is in
Expand Down
24 changes: 9 additions & 15 deletions src/blockchain/impl/block_header_repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@

#include "base/visitor.hpp"

namespace sgns::blockchain {

namespace sgns::blockchain
{
outcome::result<primitives::BlockNumber> BlockHeaderRepository::getNumberById( const primitives::BlockId &id ) const
{
return visit_in_place(
id, []( const primitives::BlockNumber &n ) { return n; },
[this]( const base::Hash256 &hash ) { return getNumberByHash( hash ); } );
}

/**
* @param id of a block which hash is returned
* @return block hash or a none optional if the corresponding block header
* is not in storage or a storage error
*/
outcome::result<base::Hash256> BlockHeaderRepository::getHashById( const primitives::BlockId &id ) const
{
return visit_in_place(
id, [this]( const primitives::BlockNumber &n ) { return getHashByNumber( n ); },
[]( const base::Hash256 &hash ) { return hash; } );
}

} // namespace sgns::blockchain
outcome::result<base::Hash256> BlockHeaderRepository::getHashById( const primitives::BlockId &id ) const
{
return visit_in_place(
id, [this]( const primitives::BlockNumber &n ) { return getHashByNumber( n ); },
[]( const base::Hash256 &hash ) { return hash; } );
}
}
Loading

0 comments on commit a6722b2

Please sign in to comment.