(59) 孙子类 _Atomic_integral_facade《T》的泛化版本 的源代码如下:
// 至此,整数原子性的支持:加减法、*(-1)取反、a++、++a、a--、--a 以及按位的 与and、或or、异或xor 运算;
// 以及复合运算符 : +=、-=、&=、|=、^= 。
template <class _Ty> // provides operator overloads and other support for atomic integral specializations
struct _Atomic_integral_facade : _Atomic_integral<_Ty> // 为整数提供了原子操作,facade 外表,表面,外观
{
using _Base = _Atomic_integral<_Ty>; // 本孙子模板类再次扩充了其爷、其爹模板类的成员函数,
using difference_type = _Ty; // 以更好的支持整型数据的加减运算与逻辑运算。
using _Base::_Base;
using _Base::fetch_add;
_Ty fetch_add(const _Ty _Operand) volatile noexcept // 返回值是原子量中的旧值
{
return const_cast<_Atomic_integral_facade*>(this)->_Base::fetch_add(_Operand);
}
_Ty fetch_add(const _Ty _Operand, const memory_order _Order) volatile noexcept // 指定内存序
{
return const_cast<_Atomic_integral_facade*>(this)->_Base::fetch_add(_Operand, _Order);
}
//******定义取反运算*******
static _Ty _Negate(const _Ty _Value) noexcept // 本函数是静态函数,经测试:返回值是形参值乘以 -1
{ // 对 make_unsigned_t<_Ty> 的注释见上面
return static_cast<_Ty>(0U - static_cast<make_unsigned_t<_Ty>>(_Value));
}
//*****定义减法运算********
_Ty fetch_sub(const _Ty _Operand) noexcept { return fetch_add( _Negate(_Operand) ); } // a - b = a + ( -b )
_Ty fetch_sub(const _Ty _Operand) volatile noexcept { return fetch_add( _Negate(_Operand) ); }
_Ty fetch_sub(const _Ty _Operand, const memory_order _Order) noexcept // 函数重载
{
return fetch_add(_Negate(_Operand), _Order); // 依然是返回原子量中的旧值
}
_Ty fetch_sub(const _Ty _Operand, const memory_order _Order) volatile noexcept
{
return fetch_add(_Negate(_Operand), _Order);
}
//*****定义自增运算符******
using _Base::operator++; // 说明对 atomic 的 ++a 与 a++ 都是原子性的
_Ty operator ++ (int) volatile noexcept // a++ 输出原子量中的原值
{
return const_cast<_Atomic_integral_facade*>(this)->_Base::operator++(0);
}
_Ty operator ++ () volatile noexcept // ++a 输出原子量中的新值
{
return const_cast<_Atomic_integral_facade*>(this)->_Base::operator++();
}
//*****定义自减运算符*****
using _Base::operator--;
_Ty operator -- (int) volatile noexcept // a-- 输出原子量中的原值
{
return const_cast<_Atomic_integral_facade*>(this)->_Base::operator--(0);
}
_Ty operator -- () volatile noexcept // --a 输出原子量中的新值
{
return const_cast<_Atomic_integral_facade*>(this)->_Base::operator--();
}
//****扩充复合加减运算符****
_Ty operator+=(const _Ty _Operand) noexcept // 旧值 + 增量后的结果作为返回值
{
return static_cast<_Ty>( this->_Base::fetch_add(_Operand) + _Operand );
}
_Ty operator+=(const _Ty _Operand) volatile noexcept // fetch_add(..) 返回的是原子量中的旧值
{
return static_cast<_Ty>( // 先给 this 去掉了 volatile 修饰符
const_cast<_Atomic_integral_facade*>(this)->_Base::fetch_add(_Operand) + _Operand );
}
_Ty operator-=(const _Ty _Operand) noexcept // 旧值 - 形参后的结果作为返回值
{
return static_cast<_Ty>( fetch_sub(_Operand) - _Operand );
}
_Ty operator-=(const _Ty _Operand) volatile noexcept
{
return static_cast<_Ty>( // // fetch_sub(..) 返回的是原子量中的旧值
const_cast<_Atomic_integral_facade*>(this)->fetch_sub(_Operand) - _Operand);
}
//*****与或异或逻辑运算符*****
using _Base::fetch_and;
_Ty fetch_and(const _Ty _Operand) volatile noexcept // 与,返回原子量中的旧值
{
return const_cast<_Atomic_integral_facade*>(this)->_Base::fetch_and(_Operand);
}
_Ty fetch_and(const _Ty _Operand, const memory_order _Order) volatile noexcept
{
return const_cast<_Atomic_integral_facade*>(this)->_Base::fetch_and(_Operand, _Order);
}
using _Base::fetch_or;
_Ty fetch_or(const _Ty _Operand) volatile noexcept // 或,返回原子量中的旧值
{
return const_cast<_Atomic_integral_facade*>(this)->_Base::fetch_or(_Operand);
}
_Ty fetch_or(const _Ty _Operand, const memory_order _Order) volatile noexcept
{
return const_cast<_Atomic_integral_facade*>(this)->_Base::fetch_or(_Operand, _Order);
}
using _Base::fetch_xor;
_Ty fetch_xor(const _Ty _Operand) volatile noexcept // 异或,返回原子量中的旧值
{
return const_cast<_Atomic_integral_facade*>(this)->_Base::fetch_xor(_Operand);
}
_Ty fetch_xor(const _Ty _Operand, const memory_order _Order) volatile noexcept
{
return const_cast<_Atomic_integral_facade*>(this)->_Base::fetch_xor(_Operand, _Order);
}
//****定义复合逻辑运算符****
_Ty operator&=(const _Ty _Operand) noexcept // 返回与运算的最终结果
{
return static_cast<_Ty>( this->_Base::fetch_and(_Operand) & _Operand );
}
_Ty operator&=(const _Ty _Operand) volatile noexcept // 函数重载
{
return static_cast<_Ty>(
const_cast<_Atomic_integral_facade*>(this)->_Base::fetch_and(_Operand) & _Operand);
}
_Ty operator|=(const _Ty _Operand) noexcept // 返回或运算的最终结果
{
return static_cast<_Ty>(this->_Base::fetch_or(_Operand) | _Operand);
}
_Ty operator|=(const _Ty _Operand) volatile noexcept // 函数重载
{
return static_cast<_Ty>(
const_cast<_Atomic_integral_facade*>(this)->_Base::fetch_or(_Operand) | _Operand);
}
_Ty operator^=(const _Ty _Operand) noexcept // 返回 异或 运算的最终结果
{
return static_cast<_Ty>(this->_Base::fetch_xor(_Operand) ^ _Operand);
}
_Ty operator^=(const _Ty _Operand) volatile noexcept // 函数重载
{
return static_cast<_Ty>(
const_cast<_Atomic_integral_facade*>(this)->_Base::fetch_xor(_Operand) ^ _Operand);
}
};
(60) 对上面的泛化模板类 _Atomic_integral_facade《T》 的 成员函数的测试结果如下:

(61)
谢谢

被折叠的 条评论
为什么被折叠?



