Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
This is the initial public release of the Bitcoin Mining Simulator
  • Loading branch information
hkalodner committed Jul 20, 2016
0 parents commit e85ea75
Show file tree
Hide file tree
Showing 78 changed files with 15,923 additions and 0 deletions.
718 changes: 718 additions & 0 deletions BlockSim.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
23 changes: 23 additions & 0 deletions BlockSim/arithmetic_type/License.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:

The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
65 changes: 65 additions & 0 deletions BlockSim/arithmetic_type/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
## Arithmetic types

### Provides

`Arithmetic<T>` where `T` models the `Arithmetic` concept is a `T` wrapper that:
- disables _any_ implicit conversions between `T` and other types, and
- can be made opaque by passing it a unique tag as second template parameter
`Arithmetic<T, unique_tag>`.

### Why is this useful?

It let's you easily specify strongly typed interfaces when Arithmetic types are
involved!

### Example 1: disabling implicit conversions

```c++
int a{2};
long b{3};
b = a; // works: the implicit conversion is safe

Arithmetic<int> a{2};
Arithmetic<long> b{3};
b = a; // fails: implicit assignment requires implicit conversion
b = Arithmetic<long>{a}; // works: explicit construction
b = static_cast<Arithmetic<long>>(a); // works: explicit conversion
```
### Example 2: opaque type-defs
```c++
struct Tag1 {};
struct Tag2 {};
using Type1 = Arithmetic<int, Tag1>;
using Type2 = Arithmetic<int, Tag2>;
Type1 a{2};
Type2 b{3};
a = b; // fails: Type1 != Type2 even tho both wrap an int
b = Type2{a}; // works: explicit construction
b = static_cast<Type2>(a); // works: explicit conversion
```

See the [tests](https://github.com/gnzlbg/arithmetic_type/blob/master/test/all_test.cpp)
for more examples.

### Other facilities
- `to_string` function is provided in
`arithmetic_type/arithmetic_to_string.hpp`
- i/o stream operators are provided in
`arithmetic_type/arithmetic_istream.hpp` and
`arithmetic_type/arithmetic_ostream.hpp`
- when writing generic code one sometimes need to deal with both `Arithmetic<T>`
and `T`. The function `primitive_cast(T t)` does the right thing.

### Dependencies:
- C++11/14 compiler (currently tested with clang 3.5 only)

### License
- Boost Software License Version 1.0

### Comparison with BOOST_STRONG_TYPEDEF

`Arithmetic<T>` supports C++1y `constexpr`, move semantics, and disables
implicit conversions.
19 changes: 19 additions & 0 deletions BlockSim/arithmetic_type/arithmetic_istream.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef ARITHMETIC_UTILITIES_ARITHMETIC_TYPE_ARITHMETIC_ISTREAM_
#define ARITHMETIC_UTILITIES_ARITHMETIC_TYPE_ARITHMETIC_ISTREAM_
////////////////////////////////////////////////////////////////////////////////
#include <istream>
////////////////////////////////////////////////////////////////////////////////

namespace arithmetic {

/// \brief istream operator
template <class C, class CT, class T, class B>
inline auto operator>>(std::basic_istream<C, CT>& i, Arithmetic<T, B>& v)
-> decltype(i >> v()) {
return i >> v();
}

} // namespace arithmetic

////////////////////////////////////////////////////////////////////////////////
#endif // ARITHMETIC_UTILITIES_ARITHMETIC_TYPE_ARITHMETIC_ISTREAM_
19 changes: 19 additions & 0 deletions BlockSim/arithmetic_type/arithmetic_ostream.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef ARITHMETIC_UTILITIES_ARITHMETIC_TYPE_ARITHMETIC_OSTREAM_
#define ARITHMETIC_UTILITIES_ARITHMETIC_TYPE_ARITHMETIC_OSTREAM_
////////////////////////////////////////////////////////////////////////////////
#include <ostream>
////////////////////////////////////////////////////////////////////////////////

namespace arithmetic {

/// \brief ostream operator
template <class C, class CT, class T, class B>
inline auto operator<<(std::basic_ostream<C, CT>& o, const Arithmetic<T, B>& v)
-> decltype(o << v()) {
return o << v();
}

} // namespace arithmetic

////////////////////////////////////////////////////////////////////////////////
#endif // ARITHMETIC_UTILITIES_ARITHMETIC_TYPE_ARITHMETIC_OSTREAM_
18 changes: 18 additions & 0 deletions BlockSim/arithmetic_type/arithmetic_to_string.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef ARITHMETIC_UTILITIES_ARITHMETIC_TYPE_ARITHMETIC_TO_STRING_
#define ARITHMETIC_UTILITIES_ARITHMETIC_TYPE_ARITHMETIC_TO_STRING_
////////////////////////////////////////////////////////////////////////////////
#include <string>
////////////////////////////////////////////////////////////////////////////////

namespace arithmetic {

/// \brief to_string
template <class T, class U>
inline std::string to_string(const Arithmetic<T, U> a) {
return std::to_string(a());
}

} // namespace arithmetic

////////////////////////////////////////////////////////////////////////////////
#endif // ARITHMETIC_UTILITIES_ARITHMETIC_TYPE_ARITHMETIC_TO_STRING_
231 changes: 231 additions & 0 deletions BlockSim/arithmetic_type/arithmetic_type.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
#ifndef ARITHMETIC_UTILITIES_ARITHMETIC_TYPE_ARITHMETIC_TYPE_
#define ARITHMETIC_UTILITIES_ARITHMETIC_TYPE_ARITHMETIC_TYPE_
////////////////////////////////////////////////////////////////////////////////
#include <limits>
#include <type_traits>
#include "returns.hpp"
#include "enable_if.hpp"
////////////////////////////////////////////////////////////////////////////////
namespace arithmetic {
////////////////////////////////////////////////////////////////////////////////

/// \name Index types
///@{
/// \brief Implements an integer type
template <class T, class B = void> struct Arithmetic {
using value_type = T;
using type = T;

/// \name Asignment operators
///@{
constexpr Arithmetic() = default;
constexpr Arithmetic(const Arithmetic& other) = default;
constexpr Arithmetic(Arithmetic&& other) = default;
constexpr Arithmetic& operator=(const Arithmetic& other) = default;
constexpr Arithmetic& operator=(Arithmetic&& other) = default;

constexpr explicit Arithmetic(const T& other) noexcept(
std::is_nothrow_constructible<T>::value)
: value{other} {}
template <class U, class V>
constexpr explicit Arithmetic(const Arithmetic<U, V>& other) noexcept(
std::is_nothrow_constructible<T>::value)
: value(other.value) {}
constexpr Arithmetic& operator=(const T& other) noexcept {
value = other;
return *this;
}
///@}

/// \name Conversion operators
///@{
explicit constexpr operator T() noexcept { return value; }
explicit constexpr operator const T() const noexcept { return value; }

template <class U, class V>
explicit constexpr operator Arithmetic<U, V>() noexcept {
return value;
}

template <class U, class V>
explicit constexpr operator const Arithmetic<U, V>() const noexcept {
return value;
}
///@}

/// \name Compound assignment +=, -=, *=, /=
///@{
constexpr Arithmetic& operator+=(const Arithmetic& other) noexcept {
value += other.value;
return *this;
}
constexpr Arithmetic& operator-=(const Arithmetic& other) noexcept {
value -= other.value;
return *this;
}
constexpr Arithmetic& operator*=(const Arithmetic& other) noexcept {
value *= other.value;
return *this;
}
constexpr Arithmetic& operator/=(const Arithmetic& other) noexcept {
value /= other.value;
return *this;
}
///@}

/// \name Prefix increment operators ++(),--()
///@{
constexpr Arithmetic& operator++() noexcept {
++value;
return *this;
}
constexpr Arithmetic& operator--() noexcept {
--value;
return *this;
}
///@}

/// \name Postfix increment operators ()++,()--
///@{
constexpr Arithmetic operator++(int) noexcept {
Arithmetic tmp(*this);
++(*this);
return tmp;
}
constexpr Arithmetic operator--(int) noexcept {
Arithmetic tmp(*this);
--(*this);
return tmp;
}
///@}

/// \name Access operator
///@{
constexpr T& operator()() & noexcept { return value; }
constexpr T operator()() && noexcept { return value; }
constexpr T operator()() const& noexcept { return value; }
///@}

/// Data (wrapped value):
T value;
};
///@}

/// \brief swap
/// \relates Arithmetic<T, U>
template <class T, class U>
constexpr void swap(Arithmetic<T, U>&& a, Arithmetic<T, U>&& b) noexcept {
using std::swap;
swap(a.value, b.value);
}

/// \name Arithmetic operators +,-,*,/,unary -
/// \relates Arithmetic<T, U>
///@{
template <class T, class U>
constexpr Arithmetic<T, U> operator+(Arithmetic<T, U> a,
const Arithmetic<T, U>& b) noexcept {
return a += b;
}
template <class T, class U>
constexpr Arithmetic<T, U> operator-(Arithmetic<T, U> a,
const Arithmetic<T, U>& b) noexcept {
return a -= b;
}
template <class T, class U>
constexpr Arithmetic<T, U> operator*(Arithmetic<T, U> a,
const Arithmetic<T, U>& b) noexcept {
return a *= b;
}
template <class T, class U>
constexpr Arithmetic<T, U> operator/(Arithmetic<T, U> a,
const Arithmetic<T, U>& b) noexcept {
return a /= b;
}

template <class T, class U>
constexpr Arithmetic<T, U> operator-(Arithmetic<T, U> const& other) noexcept {
return Arithmetic<T, U>{-other.value};
}
///@}

/// \name Comparison operators ==, !=, <, >, <=, >=
/// \relates Arithmetic<T, U>
///@{
template <class T, class U>
constexpr bool operator==(const Arithmetic<T, U>& a,
const Arithmetic<T, U>& b) noexcept {
return a.value == b.value;
}
template <class T, class U>
constexpr bool operator<(const Arithmetic<T, U>& a,
const Arithmetic<T, U>& b) noexcept {
return a.value < b.value;
}
template <class T, class U>
constexpr bool operator<=(const Arithmetic<T, U>& a,
const Arithmetic<T, U>& b) noexcept {
return a < b || a == b;
}
template <class T, class U>
constexpr bool operator!=(const Arithmetic<T, U>& a,
const Arithmetic<T, U>& b) noexcept {
return !(a == b);
}
template <class T, class U>
constexpr bool operator>(const Arithmetic<T, U>& a,
const Arithmetic<T, U>& b) noexcept {
return !(a <= b);
}
template <class T, class U>
constexpr bool operator>=(const Arithmetic<T, U>& a,
const Arithmetic<T, U>& b) noexcept {
return !(a < b);
}
///@}

/// \name Pointer arithmetic
///@{

template<class T, class U, class V,
detail_::EnableIf<std::is_pointer<T>> = detail_::dummy>
constexpr auto operator+(T a, const Arithmetic<U, V>& i)
RETURNS(a + i());

template<class T, class U, class V,
detail_::EnableIf<std::is_array<T>> = detail_::dummy>
constexpr auto operator+(T a, const Arithmetic<U, V>& i)
RETURNS(a + i());

template<class T, class U, class V,
detail_::EnableIf<std::is_pointer<T>> = detail_::dummy>
constexpr auto operator-(T a, const Arithmetic<U, V>& i)
RETURNS(a - i());

template<class T, class U, class V,
detail_::EnableIf<std::is_array<T>> = detail_::dummy>
constexpr auto operator-(T a, const Arithmetic<U, V>& i)
RETURNS(a - i());

///@}

////////////////////////////////////////////////////////////////////////////////
} // namespace arithmetic
////////////////////////////////////////////////////////////////////////////////

/// \name Arithmetic types are numeric types
/// \relates Arithmetic<T, U>
///@{
namespace std {

template <class T, class B>
class numeric_limits<arithmetic::Arithmetic<T, B>> : public numeric_limits<T> {
public:
static constexpr bool is_specialized = true;
};

} // namespace std
///@}

////////////////////////////////////////////////////////////////////////////////
#endif // ARITHMETIC_UTILITIES_ARITHMETIC_TYPE_ARITHMETIC_TYPE_
Loading

0 comments on commit e85ea75

Please sign in to comment.