Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor (for qtils) #20

Merged
merged 5 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ env:

jobs:
MacOS:
runs-on: macos-latest
runs-on: macos-13
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: ${{ env.CACHE_PATHS }}
key: ${{ github.job }}-${{ env.CACHE_VERSION }}
- name: build
env:
DEVELOPER_DIR: /Applications/Xcode_13.3.app/Contents/Developer
run: ./scripts/build.sh

Linux:
Expand All @@ -53,7 +51,7 @@ jobs:
clang-tidy:
name: "Linux: clang-tidy"
runs-on: ubuntu-latest
container: soramitsu/kagome-dev:2-minideb
container: qdrvm/kagome-dev@sha256:14d5aa92c971073e82ba9bbac9b615701e99c71f64e58bdd45e5b3dbc09944bd
steps:
- uses: actions/checkout@v2
with:
Expand Down
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

cmake_minimum_required(VERSION 3.12)

if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.27")
cmake_policy(SET CMP0144 NEW)
endif()

include(${CMAKE_CURRENT_LIST_DIR}/cmake/HunterGate.cmake)

HunterGate(
Expand All @@ -30,7 +34,7 @@ add_subdirectory(src)

if (BUILD_TESTS)
enable_testing()
add_subdirectory(test ${CMAKE_BINARY_DIR}/tests_bin)
add_subdirectory(test ${CMAKE_BINARY_DIR}/test_bin)
endif ()

###############################################################################
Expand All @@ -39,7 +43,7 @@ endif ()

include(GNUInstallDirs)

install(TARGETS scale scale_encode_append EXPORT scaleConfig
install(TARGETS scale EXPORT scaleConfig
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
Expand Down
4 changes: 2 additions & 2 deletions cmake/HunterGate.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# This is a gate file to Hunter package manager.
# Include this file using `include` command and add package you need, example:
#
# cmake_minimum_required(VERSION 3.2)
# cmake_minimum_required(VERSION 3.5)
#
# include("cmake/HunterGate.cmake")
# HunterGate(
Expand Down Expand Up @@ -257,7 +257,7 @@ function(hunter_gate_download dir)
file(
WRITE
"${cmakelists}"
"cmake_minimum_required(VERSION 3.2)\n"
"cmake_minimum_required(VERSION 3.5)\n"
"project(HunterDownload LANGUAGES NONE)\n"
"include(ExternalProject)\n"
"ExternalProject_Add(\n"
Expand Down
118 changes: 0 additions & 118 deletions include/scale/detail/variant.hpp

This file was deleted.

3 changes: 2 additions & 1 deletion include/scale/encode_append.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

#pragma once

#include <scale/scale.hpp>
#include <scale/outcome/outcome.hpp>
#include <scale/types.hpp>

namespace scale {

Expand Down
43 changes: 7 additions & 36 deletions include/scale/outcome/outcome-register.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,51 +70,22 @@ namespace scale::__outcome_detail {
/// MUST BE EXECUTED A FILE LEVEL (no namespace) in HPP
// ns - fully qualified enum namespace. Example: libp2p::common
// Enum - enum name. Example: EncodeError
#define OUTCOME_HPP_DECLARE_ERROR_2(ns, Enum) \
namespace ns { \
__OUTCOME_DECLARE_MAKE_ERROR_CODE(Enum) \
} \
\
template <> \
#define OUTCOME_HPP_DECLARE_ERROR(ns, Enum) \
namespace ns { \
__OUTCOME_DECLARE_MAKE_ERROR_CODE(Enum) \
} \
\
template <> \
struct std::is_error_code_enum<ns::Enum> : std::true_type {};

/// MUST BE EXECUTED A FILE LEVEL (global namespace) in HPP
// Enum - enum name. Example: EncodeError
#define OUTCOME_HPP_DECLARE_ERROR_1(Enum) \
__OUTCOME_DECLARE_MAKE_ERROR_CODE(Enum) \
template <> \
struct std::is_error_code_enum<Enum> : std::true_type {};

/// MUST BE EXECUTED AT FILE LEVEL(no namespace) IN CPP
// ns - fully qualified enum namespace. Example: libp2p::common
// Enum - enum name. Example: EncodeError
// Name - variable name. Example: e
#define OUTCOME_CPP_DEFINE_CATEGORY_3(ns, Enum, Name) \
#define OUTCOME_CPP_DEFINE_CATEGORY(ns, Enum, Name) \
namespace ns { \
__OUTCOME_DEFINE_MAKE_ERROR_CODE(Enum) \
}; \
template <> \
std::string scale::__outcome_detail::Category<ns::Enum>::toString( \
ns::Enum Name)

/// MUST BE EXECUTED AT FILE LEVEL(global namespace) IN CPP
// Enum - enum name. Example: EncodeError
// Name - variable name. Example: e
#define OUTCOME_CPP_DEFINE_CATEGORY_2(Enum, Name) \
__OUTCOME_DEFINE_MAKE_ERROR_CODE(Enum) \
template <> \
std::string scale::__outcome_detail::Category<Enum>::toString(Enum Name)

// kind of "macro overloading"
#define __GET_MACRO_3(_1, _2, _3, NAME, ...) NAME
#define __GET_MACRO_2(_1, _2, NAME, ...) NAME

/// with 3 args: OUTCOME_CPP_DEFINE_CATEGORY_3
/// with 2 args: OUTCOME_CPP_DEFINE_CATEGORY_2
#define OUTCOME_CPP_DEFINE_CATEGORY(...) \
OUTCOME_CPP_DEFINE_CATEGORY_3(__VA_ARGS__)

/// with 2 args: OUTCOME_CPP_DEFINE_CATEGORY_2
/// with 1 arg : OUTCOME_CPP_DEFINE_CATEGORY_1
#define OUTCOME_HPP_DECLARE_ERROR(...) \
OUTCOME_HPP_DECLARE_ERROR_2(__VA_ARGS__)
21 changes: 2 additions & 19 deletions include/scale/outcome/outcome_throw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,11 @@

#pragma once

#include <boost/system/system_error.hpp>
#include <boost/throw_exception.hpp>
#include <system_error>

namespace scale {
/**
* @brief throws outcome::result error as boost exception
* @tparam T enum error type, only outcome::result enums are allowed
* @param t error value
*/
template <typename T, typename = std::enable_if_t<std::is_enum_v<T>>>
[[noreturn]] void raise(T t) {
std::error_code ec = make_error_code(t);
[[noreturn]] inline void raise(const std::error_code &ec) {
boost::throw_exception(std::system_error(ec));
}

/**
* @brief throws outcome::result error made of error as boost exception
* @tparam T outcome error type
* @param t outcome error value
*/
template <typename T, typename = std::enable_if_t<!std::is_enum_v<T>>>
[[noreturn]] void raise(const T &t) {
boost::throw_exception(std::system_error(t.value(), t.category()));
}
} // namespace scale
43 changes: 29 additions & 14 deletions include/scale/scale.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

#pragma once

#include <vector>

#include <boost/system/system_error.hpp>
#include <boost/throw_exception.hpp>

Expand Down Expand Up @@ -35,6 +33,20 @@
SCALE_EMPTY_DECODER(TargetType)

namespace scale {
template <typename F>
outcome::result<std::invoke_result_t<F>> outcomeCatch(F &&f) {
try {
if constexpr (std::is_void_v<std::invoke_result_t<F>>) {
f();
return outcome::success();
} else {
return outcome::success(f());
}
} catch (std::system_error &e) {
turuslan marked this conversation as resolved.
Show resolved Hide resolved
return outcome::failure(e.code());
}
}

/**
* @brief convenience function for encoding primitives data to stream
* @tparam Args primitive types to be encoded
Expand All @@ -44,13 +56,13 @@ namespace scale {
template <typename... Args>
outcome::result<std::vector<uint8_t>> encode(Args &&...args) {
ScaleEncoderStream s{};
try {
(s << ... << std::forward<Args>(args));
} catch (std::system_error &e) {
return outcome::failure(e.code());
}
OUTCOME_TRY(encode(s, std::forward<Args>(args)...));
return s.to_vector();
}
template <typename... Args>
outcome::result<void> encode(ScaleEncoderStream &s, Args &&...args) {
return outcomeCatch([&] { (s << ... << std::forward<Args>(args)); });
}

/**
* @brief convenience function for decoding primitives data from stream
Expand All @@ -60,14 +72,17 @@ namespace scale {
*/
template <class T>
outcome::result<T> decode(ConstSpanOfBytes data) {
T t{};
ScaleDecoderStream s(data);
try {
s >> t;
} catch (std::system_error &e) {
return outcome::failure(e.code());
}

return decode<T>(s);
}
template <typename T>
outcome::result<T> decode(ScaleDecoderStream &s) {
T t{};
OUTCOME_TRY(decode<T>(s, t));
return outcome::success(std::move(t));
}
template <typename T>
outcome::result<void> decode(ScaleDecoderStream &s, T &t) {
return outcomeCatch([&] { s >> t; });
}
} // namespace scale
5 changes: 2 additions & 3 deletions include/scale/scale_decoder_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@

#pragma once

#include <array>
#include <deque>
#include <iterator>
#include <list>
#include <memory>
#include <optional>
#include <utility>

#include <boost/variant.hpp>

#include <scale/bitvec.hpp>
#include <scale/detail/fixed_width_integer.hpp>
#include <scale/scale_error.hpp>
#include <scale/types.hpp>
#include <type_traits>

Expand Down
Loading
Loading