mirror of
https://git.suyu.dev/suyu/ext-boost.git
synced 2026-01-06 22:48:20 +01:00
ext-boost: Add safe-numerics.
This commit is contained in:
parent
1a1e0fc797
commit
caf8e19fb1
27 changed files with 7339 additions and 0 deletions
14
boost/safe_numerics/concept/CMakeLists.txt
Normal file
14
boost/safe_numerics/concept/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
####################
|
||||
# add include headers to IDE
|
||||
|
||||
set(USE_FOLDERS TRUE)
|
||||
|
||||
file(GLOB include_files
|
||||
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/*.hpp"
|
||||
)
|
||||
add_custom_target(concepts SOURCES ${include_files})
|
||||
set_target_properties(concepts PROPERTIES FOLDER "safe_numerics")
|
||||
|
||||
# end headers in IDE
|
||||
####################
|
||||
29
boost/safe_numerics/concept/exception_policy.hpp
Normal file
29
boost/safe_numerics/concept/exception_policy.hpp
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#ifndef BOOST_NUMERIC_CONCEPT_EXCEPTION_POLICY_HPP
|
||||
#define BOOST_NUMERIC_CONCEPT_EXCEPTION_POLICY_HPP
|
||||
|
||||
// Copyright (c) 2015 Robert Ramey
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
namespace boost {
|
||||
namespace safe_numerics {
|
||||
|
||||
template<class EP>
|
||||
struct ExceptionPolicy {
|
||||
const char * message;
|
||||
/*
|
||||
BOOST_CONCEPT_USAGE(ExceptionPolicy){
|
||||
EP::on_arithmetic_error(e, message);
|
||||
EP::on_undefined_behavior(e, message)
|
||||
EP::on_implementation_defined_behavior(e, message)
|
||||
EP::on_uninitialized_value(e, message)
|
||||
}
|
||||
*/
|
||||
};
|
||||
|
||||
} // safe_numerics
|
||||
} // boost
|
||||
|
||||
#endif // BOOST_NUMERIC_CONCEPT_EXCEPTION_POLICY_HPP
|
||||
27
boost/safe_numerics/concept/integer.hpp
Normal file
27
boost/safe_numerics/concept/integer.hpp
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#ifndef BOOST_NUMERIC_CONCEPT_INTEGER_HPP
|
||||
#define BOOST_NUMERIC_CONCEPT_INTEGER_HPP
|
||||
|
||||
// Copyright (c) 2012 Robert Ramey
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include "numeric.hpp"
|
||||
|
||||
namespace boost {
|
||||
namespace safe_numerics {
|
||||
|
||||
template <class T>
|
||||
class Integer : public Numeric<T> {
|
||||
// integer types must have the corresponding numeric trait.
|
||||
static_assert(
|
||||
std::numeric_limits<T>::is_integer,
|
||||
"Fails to fulfill requirements for an integer type"
|
||||
);
|
||||
};
|
||||
|
||||
} // safe_numerics
|
||||
} // boost
|
||||
|
||||
#endif // BOOST_NUMERIC_CONCEPT_INTEGER_HPP
|
||||
29
boost/safe_numerics/concept/numeric.hpp
Normal file
29
boost/safe_numerics/concept/numeric.hpp
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#ifndef BOOST_NUMERIC_CONCEPT_NUMERIC_HPP
|
||||
#define BOOST_NUMERIC_CONCEPT_NUMERIC_HPP
|
||||
|
||||
// Copyright (c) 2012 Robert Ramey
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <limits>
|
||||
|
||||
namespace boost {
|
||||
namespace safe_numerics {
|
||||
|
||||
template<class T>
|
||||
struct Numeric {
|
||||
// if your program traps here, you need to create a
|
||||
// std::numeric_limits class for your type T. see
|
||||
// see C++ standard 18.3.2.2
|
||||
static_assert(
|
||||
std::numeric_limits<T>::is_specialized,
|
||||
"std::numeric_limits<T> has not been specialized for this type"
|
||||
);
|
||||
};
|
||||
|
||||
} // safe_numerics
|
||||
} // boost
|
||||
|
||||
#endif // BOOST_NUMERIC_CONCEPT_NUMERIC_HPP
|
||||
33
boost/safe_numerics/concept/promotion_policy.hpp
Normal file
33
boost/safe_numerics/concept/promotion_policy.hpp
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#ifndef BOOST_NUMERIC_CONCEPT_PROMOTION_POLICY_HPP
|
||||
#define BOOST_NUMERIC_CONCEPT_PROMOTION_POLICY_HPP
|
||||
|
||||
// Copyright (c) 2015 Robert Ramey
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
namespace boost {
|
||||
namespace safe_numerics {
|
||||
|
||||
template<class PP>
|
||||
struct PromotionPolicy {
|
||||
using T = int;
|
||||
using U = int;
|
||||
using a_type = typename PP::template addition_result<T, U>;
|
||||
using s_type = typename PP::template subtraction_result<T, U>;
|
||||
using m_type = typename PP::template multiplication_result<T, U>;
|
||||
using d_type = typename PP::template division_result<T, U>;
|
||||
using mod_type = typename PP::template modulus_result<T, U>;
|
||||
using ls_type = typename PP::template left_shift_result<T, U>;
|
||||
using rs_type = typename PP::template right_shift_result<T, U>;
|
||||
using cc_type = typename PP::template comparison_result<T, U>;
|
||||
using baw_type = typename PP::template bitwise_and_result<T, U>;
|
||||
using bow_type = typename PP::template bitwise_or_result<T, U>;
|
||||
using bxw_type = typename PP::template bitwise_xor_result<T, U>;
|
||||
};
|
||||
|
||||
} // safe_numerics
|
||||
} // boost
|
||||
|
||||
#endif // BOOST_NUMERIC_CONCEPT_EXCEPTION_POLICY_HPP
|
||||
34
boost/safe_numerics/concept/safe_numeric.hpp
Normal file
34
boost/safe_numerics/concept/safe_numeric.hpp
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#ifndef BOOST_NUMERIC_CONCEPT_SAFE_NUMERIC_HPP
|
||||
#define BOOST_NUMERIC_CONCEPT_SAFE_NUMERIC_HPP
|
||||
|
||||
// Copyright (c) 2015 Robert Ramey
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <limits>
|
||||
#include <typetraits>
|
||||
#include <boost/concept/usage.hpp>
|
||||
#include "concept/numeric.hpp"
|
||||
|
||||
namespace boost {
|
||||
namespace safe_numerics {
|
||||
|
||||
template<class T>
|
||||
struct SafeNumeric : public Numeric<T> {
|
||||
static_assert(
|
||||
is_safe<T>::value,
|
||||
"std::numeric_limits<T> has not been specialized for this type"
|
||||
);
|
||||
BOOST_CONCEPT_USAGE(SafeNumeric){
|
||||
using t1 = get_exception_policy<T>;
|
||||
using t2 = get_promotion_policy<T>;
|
||||
using t3 = base_type<T>;
|
||||
}
|
||||
};
|
||||
|
||||
} // safe_numerics
|
||||
} // boost
|
||||
|
||||
#endif // BOOST_NUMERIC_CONCEPT_SAFE_NUMERIC_HPP
|
||||
Loading…
Add table
Add a link
Reference in a new issue