- /*
- * MIT License
- *
- * Copyright (c) 2025 Roland Mainz <roland.mainz@nrubsig.org>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * 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 AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
- #if __cplusplus >= 202001L
- #define STARSHIP_OP 1
- #endif
- #define BUILD_TEST_MAIN 1
- #include <cstdint>
- #include <limits>
- #include <type_traits>
- #include <exception>
- #ifdef BUILD_TEST_MAIN
- #include <iostream>
- #endif
- #ifdef STARSHIP_OP
- #include <compare>
- #endif
- template <typename T>
- class rov_int_template
- {
- protected:
- T value_;
- public:
- rov_int_template() : value_(0) { }
- explicit rov_int_template(T value) : value_(value) { }
- operator T() const { return value_; }
- rov_int_template operator+(const rov_int_template& other) const
- {
- return rov_int_template(value_ + other.value_);
- }
- rov_int_template operator-(const rov_int_template& other) const
- {
- return rov_int_template(value_ - other.value_);
- }
- rov_int_template operator*(const rov_int_template& other) const
- {
- return rov_int_template(value_ * other.value_);
- }
- rov_int_template operator/(const rov_int_template& other) const
- {
- if (other.value_ == 0) {
- throw std::exception("Division by zero");
- }
- return rov_int_template(value_ / other.value_);
- }
- rov_int_template operator%(const rov_int_template& other) const
- {
- if (other.value_ == 0) {
- throw std::exception("Modulo by zero");
- }
- return rov_int_template(value_ % other.value_);
- }
- rov_int_template& operator+=(const rov_int_template& other)
- {
- value_ += other.value_;
- return *this;
- }
- rov_int_template& operator-=(const rov_int_template& other)
- {
- value_ -= other.value_;
- return *this;
- }
- rov_int_template& operator*=(const rov_int_template& other)
- {
- value_ *= other.value_;
- return *this;
- }
- rov_int_template& operator/=(const rov_int_template& other)
- {
- if (other.value_ == 0) {
- throw std::exception("Division by zero");
- }
- value_ /= other.value_;
- return *this;
- }
- rov_int_template& operator%=(const rov_int_template& other)
- {
- if (other.value_ == 0) {
- throw std::exception("Modulo by zero");
- }
- value_ %= other.value_;
- return *this;
- }
- rov_int_template operator&(const rov_int_template& other) const
- {
- return rov_int_template(value_ & other.value_);
- }
- rov_int_template operator|(const rov_int_template& other) const
- {
- return rov_int_template(value_ | other.value_);
- }
- rov_int_template operator^(const rov_int_template& other) const
- {
- return rov_int_template(value_ ^ other.value_);
- }
- rov_int_template operator~() const
- {
- return rov_int_template(~value_);
- }
- rov_int_template operator<<(int shift) const
- {
- return rov_int_template(value_ << shift);
- }
- rov_int_template operator>>(int shift) const
- {
- return rov_int_template(value_ >> shift);
- }
- rov_int_template& operator&=(const rov_int_template& other)
- {
- value_ &= other.value_;
- return *this;
- }
- rov_int_template& operator|=(const rov_int_template& other)
- {
- value_ |= other.value_;
- return *this;
- }
- rov_int_template& operator^=(const rov_int_template& other)
- {
- value_ ^= other.value_;
- return *this;
- }
- rov_int_template& operator<<=(int shift)
- {
- value_ <<= shift;
- return *this;
- }
- rov_int_template& operator>>=(int shift)
- {
- value_ >>= shift;
- return *this;
- }
- bool operator==(const rov_int_template& other) const
- {
- return value_ == other.value_;
- }
- bool operator!=(const rov_int_template& other) const
- {
- return value_ != other.value_;
- }
- bool operator<(const rov_int_template& other) const
- {
- return value_ < other.value_;
- }
- bool operator<=(const rov_int_template& other) const
- {
- return value_ <= other.value_;
- }
- bool operator>(const rov_int_template& other) const
- {
- return value_ > other.value_;
- }
- bool operator>=(const rov_int_template& other) const
- {
- return value_ >= other.value_;
- }
- #ifdef STARSHIP_OP
- auto operator<=>(const rov_int_template& other) const noexcept -> std::strong_ordering
- {
- return value_ <=> other.value_;
- }
- #endif /* STARSHIP_OP */
- rov_int_template& operator++()
- {
- ++value_;
- return *this;
- }
- rov_int_template operator++(int)
- {
- rov_int_template temp(*this);
- ++value_;
- return temp;
- }
- rov_int_template& operator--()
- {
- --value_;
- return *this;
- }
- rov_int_template operator--(int)
- {
- rov_int_template temp(*this);
- --value_;
- return temp;
- }
- static constexpr T min()
- {
- return std::numeric_limits<T>::min();
- }
- static constexpr T max()
- {
- return std::numeric_limits<T>::max();
- }
- };
- #ifdef BUILD_TEST_MAIN
- int main(int ac, char *av[])
- {
- rov_int_template<char> byte_int(21);
- rov_int_template<short> short_int(300);
- rov_int_template<int> int_value(4000);
- rov_int_template<long> long_value(650000);
- rov_int_template<long long> long_long_value(87000000);
- std::cout << "# compiled with C++ support __cplusplus=" << long(__cplusplus) << std::endl;
- #ifdef STARSHIP_OP
- std::cout << "# we support the starship operator" << std::endl;
- #endif /* STARSHIP_OP */
- std::cout << byte_int+short_int+int_value+long_value+long_long_value << std::endl;
- return EXIT_SUCCESS;
- }
- #endif /* BUILD_TEST_MAIN */
prototype rov_int_template<> 20250127-001
Posted by Anonymous on Mon 27th Jan 2025 15:16
raw | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.