strong_typedef.hpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #ifndef BOOST_SERIALIZATION_STRONG_TYPEDEF_HPP
  2. #define BOOST_SERIALIZATION_STRONG_TYPEDEF_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // strong_typedef.hpp:
  9. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  10. // (C) Copyright 2016 Ashish Sadanandan
  11. // Use, modification and distribution is subject to the Boost Software
  12. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. // See http://www.boost.org/libs/serialization for updates, documentation, and revision history.
  15. // macro used to implement a strong typedef. strong typedef
  16. // guarentees that two types are distinguised even though the
  17. // share the same underlying implementation. typedef does not create
  18. // a new type. BOOST_STRONG_TYPEDEF(T, D) creates a new type named D
  19. // that operates as a type T.
  20. #include <boost/config.hpp>
  21. #include <boost/operators.hpp>
  22. #include <boost/type_traits/has_nothrow_assign.hpp>
  23. #include <boost/type_traits/has_nothrow_constructor.hpp>
  24. #include <boost/type_traits/has_nothrow_copy.hpp>
  25. #define BOOST_STRONG_TYPEDEF(T, D) \
  26. struct D \
  27. : boost::totally_ordered1< D \
  28. , boost::totally_ordered2< D, T \
  29. > > \
  30. { \
  31. T t; \
  32. explicit D(const T& t_) BOOST_NOEXCEPT_IF(boost::has_nothrow_copy_constructor<T>::value) : t(t_) {} \
  33. D() BOOST_NOEXCEPT_IF(boost::has_nothrow_default_constructor<T>::value) : t() {} \
  34. D(const D & t_) BOOST_NOEXCEPT_IF(boost::has_nothrow_copy_constructor<T>::value) : t(t_.t) {} \
  35. D& operator=(const D& rhs) BOOST_NOEXCEPT_IF(boost::has_nothrow_assign<T>::value) {t = rhs.t; return *this;} \
  36. D& operator=(const T& rhs) BOOST_NOEXCEPT_IF(boost::has_nothrow_assign<T>::value) {t = rhs; return *this;} \
  37. operator const T&() const {return t;} \
  38. operator T&() {return t;} \
  39. bool operator==(const D& rhs) const {return t == rhs.t;} \
  40. bool operator<(const D& rhs) const {return t < rhs.t;} \
  41. };
  42. #endif // BOOST_SERIALIZATION_STRONG_TYPEDEF_HPP