Skip to content

Commit

Permalink
Rework a few things
Browse files Browse the repository at this point in the history
  • Loading branch information
wopss committed Sep 20, 2024
1 parent 8a36bc7 commit 8e08396
Show file tree
Hide file tree
Showing 16 changed files with 321 additions and 65 deletions.
8 changes: 5 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,16 @@ set(
RENHOOK_HEADER_FILES
include/renhook/renhook.hpp

include/renhook/config.hpp
include/renhook/exceptions.hpp
include/renhook/errc.hpp
include/renhook/error_code.hpp

include/renhook/detail/core.hpp
include/renhook/detail/error_code.hpp
)

set(
RENHOOK_SRC_FILES
src/exceptions.cpp
src/error_code.cpp
)

get_property(RENHOOK_USE_FOLDERS GLOBAL PROPERTY USE_FOLDERS)
Expand Down
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Docs

2 changes: 2 additions & 0 deletions docs/development/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Development Docs

27 changes: 27 additions & 0 deletions docs/development/coding_guidelines.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Coding Guidelines

This document contains the coding guidelines of this library. Any contributor
should follow this guidelines.

## C++ version

This library is mainly targeting C++14, i.e., newer C++ standards can be used
with macros providing a fallback to the targeted version.

> :warning: **GOOD**
> ```cpp
> #if RENHOOK_HAS_CPP17_ATTRIBUTE(nodiscard)
> #define RENHOOK_NODISCARD [[nodiscard]]
> #else
> #define RENHOOK_NODISCARD
> #endif
> ```
All functions marked as const and noexcept by default, remove them if necessary.
All class marked as final by default, remove them if necessary.
All functions must be constexpr by default, remove if the function will not do constexpr stuff.
51 changes: 51 additions & 0 deletions docs/development/testing_guidelines.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Testing Guidelines

This document contains the testing guidelines of this library. Any contributor
should follow this guidelines.

## Foreword

[Catch2](https://github.com/catchorg/Catch2) is used for unit testing the library,
make use of its features, such as:

* Use `TEST_CASE` and `SECTION` macros;
* Use [matchers](https://github.com/catchorg/Catch2/blob/v3.1.1/docs/matchers.md)
(if needed);
* Use [data generators](https://github.com/catchorg/Catch2/blob/v3.1.1/docs/generators.md)
(if needed);

... but:

* Do NOT create another `TEST_CASE` when a `SECTION` would suffice;
* Do NOT use [BDD-style test cases](https://github.com/catchorg/Catch2/blob/v3.1.1/docs/test-cases-and-sections.md#bdd-style-test-cases);

## General

* A test should only test one thing;
* A test should be short;
* Test should be able to run alone or all together in any order;
* Try to not use if / switch / for / while / etc., only use them if necessary;
* Avoid testing private / protected methods;

## Naming

A test should have the following name's structure:

* `<unit under test> should <expected result> when <condition>`, e.g.
`a transaction should throw when a thread is updated using a 'nullptr' handle`;

A section's name should have the following structure:

* `and <parameters>`; **OR**
* `using <parameters>`; **OR**
* `with <parameters>`;

## Tagging

[Catch2](https://github.com/catchorg/Catch2) can associate [tags](https://github.com/catchorg/Catch2/blob/v3.1.1/docs/test-cases-and-sections.md#tags)
with a test case. When creating a test case it must have:

* A tag related to the unit of work being tested, e.g. a test case for a class
named `my_cool_class` should be defined as `TEST_CASE("<NAME>", "[my_cool_class]<OTHER_TAGS>")`;
* A tag related to the method being tested, e.g. a test case for a function named
`my_func_1` in `my_cool_class` should be defined as `TEST_CASE("<NAME>", "[my_cool_class][my_func_1]")`;
6 changes: 0 additions & 6 deletions include/renhook/config.hpp

This file was deleted.

2 changes: 2 additions & 0 deletions include/renhook/detail/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#endif

#define RENHOOK_HAS_CPP17 (RENHOOK_CPLUSPLUS >= 201703L)
//#define RENHOOK_HAS_CPP20 (RENHOOK_CPLUSPLUS >= 202002L)

#define RENHOOK_HAS_CPP17_ATTRIBUTE(attribute) (RENHOOK_HAS_CPP17 && RENHOOK_HAS_CPP_ATTRIBUTE(attribute))

#if RENHOOK_HAS_CPP17_ATTRIBUTE(nodiscard)
Expand Down
13 changes: 13 additions & 0 deletions include/renhook/detail/error_code.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <renhook/error_code.hpp>

namespace renhook
{
namespace detail
{
//error_code make_error_code()
//{
//}
} // namespace detail
} // namespace renhook
13 changes: 13 additions & 0 deletions include/renhook/errc.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <cstdint>

namespace renhook
{
// TODO: Document this.
enum class RENHOOK_NODISCARD errc : std::uint8_t
{
success = 0,
error_name
};
} // namespace renhook
73 changes: 73 additions & 0 deletions include/renhook/error_code.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#pragma once

#include <system_error>
#include <utility>

#include <renhook/detail/core.hpp>
#include <renhook/errc.hpp>

namespace renhook
{
// TODO: https://www.foonathan.net/2016/05/final/
// TODO: Document this.
class RENHOOK_NODISCARD error_code
{
public:
/**
* @brief Constructs an error code with the default value and inner error.
*/
error_code() noexcept;

/**
* @brief Constructs an error code with the specified value and the default inner error.
*
* @param[in] value The value of the error code.
*/
error_code(errc value) noexcept;

/**
* @brief Constructs an error code with the specified value and inner error.
*
* @param[in] value The value of the error code.
* @param[in] inner_error The inner error of the error code.
*/
error_code(errc value, const std::error_code& inner_error) noexcept;

/*error_code(const error_code&) noexcept = default;
error_code(error_code&&) noexcept = default;
error_code& operator=(const error_code&) noexcept = default;
error_code& operator=(error_code&&) noexcept = default;
~error_code() noexcept = default;*/

// error_code& operator=(errc rhs) noexcept;
// error_code& operator=(std::pair<errc, std::error_code> rhs) noexcept;

// void assign(errc value) noexcept;
// void assign(errc value, const std::error_code& inner_error) noexcept;

void clear() noexcept;

RENHOOK_NODISCARD errc value() const noexcept;
RENHOOK_NODISCARD const std::error_code& inner_error() const noexcept; // TODO: This might not be necessary.
RENHOOK_NODISCARD std::string message() const;

RENHOOK_NODISCARD explicit operator bool() const noexcept;

private:
errc m_value;
std::error_code m_inner_error;
};
} // namespace renhook

// bool operator==( const std::error_code& lhs, const std::error_code& rhs ) noexcept;
// bool operator!=( const std::error_code& lhs, const std::error_code& rhs ) noexcept;
// bool operator<( const std::error_code& lhs, const std::error_code& rhs ) noexcept;
// std::strong_ordering operator<=>( const std::error_code& lhs, const std::error_code& rhs ) noexcept;

// template< class CharT, class Traits >
// std::basic_ostream<CharT, Traits>& operator<<(basic_ostream<CharT, Traits>& os, const error_code& ec);
// template<> struct hash<error_code>;

// std::error_code make_error_code( std::errc e ) noexcept;
42 changes: 0 additions & 42 deletions include/renhook/exceptions.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion include/renhook/renhook.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#pragma once

#include <renhook/exceptions.hpp>
#include <renhook/error_code.hpp>
46 changes: 46 additions & 0 deletions src/error_code.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <renhook/error_code.hpp>

renhook::error_code::error_code() noexcept
: error_code(errc::success)
{
}

renhook::error_code::error_code(errc value) noexcept
: error_code(value, {})
{
}

renhook::error_code::error_code(errc value, const std::error_code& inner_error) noexcept
: m_value(value)
, m_inner_error(inner_error)
{
// TODO: Test this.
// TODO: See if const ref is better. https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/cpp-value-categories
}

void renhook::error_code::clear() noexcept
{
m_value = errc::success;
m_inner_error.clear();
}

renhook::errc renhook::error_code::value() const noexcept
{
return m_value;
}

const std::error_code& renhook::error_code::inner_error() const noexcept
{
return m_inner_error;
}

std::string renhook::error_code::message() const
{
return std::string();
}

renhook::error_code::operator bool() const noexcept
{
// TODO: Maybe assert if value = success && !inner_error.
return value() != errc::success || m_inner_error;
}
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
set(
RENHOOK_TESTS_SRC_FILES
exceptions.cpp
error_code.cpp
)

source_group(
Expand Down
Loading

0 comments on commit 8e08396

Please sign in to comment.