https://www.boost.org/sgi/stl/character_traits.html
char_traits<char>
char_traits<wchar_t>
traits翻译为特征、特性类,一般是指某种类型的特性类应该提供的一组接口、类型定义。
web页面描述了一些接口要求。感觉没有什么特别的。直接看代码吧
char_traits.h文件中,以下两个头文件不在sgi stl范围内,属于c标准库的范围
在vs中创建一个空工程,#include “…/stl/char_traits.h” 后,就可以在vs中跳转过去,看到的是windows的实现。
#include <string.h>
#include <wchar.h>
以上两个头文件定义了strcpy_s、_memccpy之类的基础C函数,说明STL中的一些函数间接使用了C函数
__char_traits_base 定义了char类型的比较、赋值、拷贝、查找、求长度等基础函数
template <class _CharT, class _IntT> class __char_traits_base {
public:
typedef _CharT char_type;
typedef _IntT int_type;
#ifdef __STL_USE_NEW_IOSTREAMS //这个没用上,暂时可以不管
typedef streamoff off_type;
typedef streampos pos_type;
typedef mbstate_t state_type;
#endif /* __STL_USE_NEW_IOSTREAMS */
static void assign(char_type& __c1, const char_type& __c2) { __c1 = __c2; }
static bool eq(const _CharT& __c1, const _CharT& __c2) { return __c1 == __c2; }
static bool lt(const _CharT& __c1, const _CharT& __c2) { return __c1 < __c2; }
static int compare(const _CharT* __s1, const _CharT* __s2, size_t __n) {
for (size_t __i = 0; __i < __n; ++__i)
if (!eq(__s1[__i], __s2[__i]))
return __s1[__i] < __s2[__i] ? -1 : 1;
return 0;
}
static size_t length(const _CharT* __s) {
const _CharT __nullchar = _CharT();//默认构造函数必须置0(概念上可以不为0,定义无歧义空值即可)
size_t __i;
for (__i = 0; !eq(__s[__i], __nullchar); ++__i) {}
return __i;
}
static const _CharT* find(const _CharT* __s, size_t __n, const _CharT& __c) {//此处传值或许更好,编译器优化后可能无差别
for ( ; __n > 0 ; ++__s, --__n)
if (eq(*__s, __c))
return __s;
return 0;
}
static _CharT* move(_CharT* __s1, const _CharT* __s2, size_t __n) {
memmove(__s1, __s2, __n * sizeof(_CharT));//__n 是__s1 __s2的size
return __s1;
}
static _CharT* copy(_CharT* __s1, const _CharT* __s2, size_t __n) {
memcpy(__s1, __s2, __n * sizeof(_CharT));
return __s1;
}
static _CharT* assign(_CharT* __s, size_t __n, _CharT __c) {
for (size_t __i = 0; __i < __n; ++__i)
__s[__i] = __c;
return __s;
}
static int_type not_eof(const int_type& __c) {
return !eq_int_type(__c, eof()) ? __c : 0;
}
static char_type to_char_type(const int_type& __c) {
return static_cast<char_type>(__c);
}
static int_type to_int_type(const char_type& __c) {
return static_cast<int_type>(__c);
}
static bool eq_int_type(const int_type& __c1, const int_type& __c2) {
return __c1 == __c2;
}
static int_type eof() {
return static_cast<int_type>(-1);//-1为无效值,大文件实际应该为long long -1
}
};
POD表示"Plain Old Data"(简单老数据),衍生概念是指可简单拷贝内存复制对象的结构体,含有指针的就不适合简单拷贝。
char_traits不适用与非POD类型。
template <class _CharT> class char_traits //特化版本,与后面的char_traits<char>特化不一样,此处实际还没确定类型
: public __char_traits_base<_CharT, _CharT>
{};
__STL_TEMPLATE_NULL class char_traits<char> //char的特化版本,重载了几个函数,典型的是调用C函数的几个
: public __char_traits_base<char, int>{
public:
static char_type to_char_type(const int_type& __c) {
return static_cast<char_type>(static_cast<unsigned char>(__c));
}
static int_type to_int_type(const char_type& __c) {
return static_cast<unsigned char>(__c);
}
static int compare(const char* __s1, const char* __s2, size_t __n)
{ return memcmp(__s1, __s2, __n); }
static size_t length(const char* __s) { return strlen(__s); }
static void assign(char& __c1, const char& __c2) { __c1 = __c2; }
static char* assign(char* __s, size_t __n, char __c)
{ memset(__s, __c, __n); return __s; }
};
wchar_t版本特化,wint_t实际是unsigned short
__STL_TEMPLATE_NULL class char_traits<wchar_t>
: public __char_traits_base<wchar_t, wint_t>{};
#include “…/sgi_stl/stl/string”
basic_string在string中定义,web说明中没有很复杂,基本就是template <class _CharT, class _Traits, class _Alloc>三个的组合
实际代码如下
#pragma set woff 1174 //这种用法比较少见,据说是ms vs才支持的关闭告警语法
#pragma set woff 1375
// A helper class to use a char_traits as a function object.将char_traits用作函数对象
template <class _Traits>
struct _Not_within_traits //是否在内部找得到
: public unary_function<typename _Traits::char_type, bool>{
typedef const typename _Traits::char_type* _Pointer;
const _Pointer _M_first;
const _Pointer _M_last;
_Not_within_traits(_Pointer __f, _Pointer __l)
: _M_first(__f), _M_last(__l) {}
bool operator()(const typename _Traits::char_type& __x) const {
//找到了就在string内有__x,否则_Not_within_traits
return find_if(_M_first, _M_last, bind1st(_Eq_traits<_Traits>(), __x)) == _M_last;
}//bind1st存储了op和1st参数,变成了一个一元仿函数对象,再去操作序列中的元素。
};
template <class _InputIter, class _Predicate>
inline _InputIter find_if(_InputIter __first, _InputIter __last,
_Predicate __pred,
input_iterator_tag){
while (__first != __last && !__pred(*__first))//遇到相等就停,不等于就循环下去
++__first;
return __first;
}
template <class _Traits>
struct _Eq_traits: public binary_function<typename _Traits::char_type,typename _Traits::char_type,bool>{
bool operator()(const typename _Traits::char_type& __x,const typename _Traits::char_type& __y) const
{ return _Traits::eq(__x, __y); }//实际就是判断是否相等。封装是为了编译期检查?
};
template <class _Arg1, class _Arg2, class _Result>
struct binary_function { //二元函数
typedef _Arg1 first_argument_type;
typedef _Arg2 second_argument_type;
typedef _Result result_type;
};
template <class _Operation, class _Tp>//_Operation == _Eq_traits<_Traits>()
inline binder1st<_Operation> //返回值是一个函数对象,编译后可能是函数指针,或者内联
bind1st(const _Operation& __fn, const _Tp& __x) {
typedef typename _Operation::first_argument_type _Arg1_type;
return binder1st<_Operation>(__fn, _Arg1_type(__x));//说明__x是__fn的第一个参数
}
template <class _Operation>
class binder1st : public unary_function<typename _Operation::second_argument_type,
typename _Operation::result_type> {
protected:
_Operation op;//操作的存储,使得当前类替代了被绑定的操作(操作的递归替换)
typename _Operation::first_argument_type value;//降元的本质就是将部分存到仿函数内部。
public:
binder1st(const _Operation& __x,
const typename _Operation::first_argument_type& __y)
: op(__x), value(__y) {}//bind1st(_Eq_traits<_Traits>(), __x)把第一个参数绑定,实际是存储起来
typename _Operation::result_type
operator()(const typename _Operation::second_argument_type& __x) const {//与第二个参数进行操作(eg:判断是否相等)
return op(value, __x);
}
};
前面这段代码比较难看懂的是以下这句
return find_if(_M_first, _M_last, bind1st(_Eq_traits<_Traits>(), __x)) == _M_last;
//bind1st存储了op和1st参数,变成了一个一元仿函数对象,再去操作序列中的元素。
//find_if返回值是序列的指针,指针指向的对象满足bind1st绑定后的一元仿函数(返回值true)
find_if这种函数只接受一元仿函数,bind1st实现了操作降元(二元操作降到一元操作),类似降元都可以模仿着写,本质就是将第一个元存到仿函数内部。
// General base class.
template <class _Tp, class _Alloc, bool _S_instanceless>//instanceless _Alloc是否无实例
class _String_alloc_base { //提供内存管理
public:
typedef typename _Alloc_traits<_Tp, _Alloc>::allocator_type allocator_type;//new delete仿函数类型定义
allocator_type get_allocator() const { return _M_data_allocator; }//获得内部的new delete仿函数对象
_String_alloc_base(const allocator_type& __a)
: _M_data_allocator(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0) {}
protected:
_Tp* _M_allocate(size_t __n){ return _M_data_allocator.allocate(__n); }//new
void _M_deallocate(_Tp* __p, size_t __n) {
if (__p)_M_data_allocator.deallocate(__p, __n); //delete
}
protected:
allocator_type _M_data_allocator; //仿函数对象(默认false,无实例的false,就是此处有_Alloc示例)此处占内存
_Tp* _M_start;//起始指针
_Tp* _M_finish;//已用范围的尾部指针
_Tp* _M_end_of_storage; //标识最大容量指针
};
_String_alloc_base 的特化版本
// Specialization for instanceless allocators.
template <class _Tp, class _Alloc>
class _String_alloc_base<_Tp,_Alloc,true> {
public:
typedef typename _Alloc_traits<_Tp, _Alloc>::allocator_type allocator_type;//此处无实例化,就是定义allocator_type
allocator_type get_allocator() const { return allocator_type(); }
_String_alloc_base(const allocator_type&) : _M_start(0), _M_finish(0), _M_end_of_storage(0) {}
protected:
typedef typename _Alloc_traits<_Tp, _Alloc>::_Alloc_type _Alloc_type;
_Tp* _M_allocate(size_t __n) { return _Alloc_type::allocate(__n); }
void _M_deallocate(_Tp* __p, size_t __n) { _Alloc_type::deallocate(__p, __n); }
protected:
_Tp* _M_start;
_Tp* _M_finish;
_Tp* _M_end_of_storage;
};
template <class _Tp, class _Alloc>
class _String_base
: public _String_alloc_base<_Tp, _Alloc,
_Alloc_traits<_Tp, _Alloc>::_S_instanceless>{
protected:
typedef _String_alloc_base<_Tp, _Alloc,_Alloc_traits<_Tp, _Alloc>::_S_instanceless> _Base;
typedef typename _Base::allocator_type allocator_type;
void _M_allocate_block(size_t __n) {
if (__n <= max_size()) {
_M_start = _M_allocate(__n);
_M_finish = _M_start;
_M_end_of_storage = _M_start + __n;
}
else
_M_throw_length_error();
}
void _M_deallocate_block() { _M_deallocate(_M_start, _M_end_of_storage - _M_start); }
size_t max_size() const { return (size_t(-1) / sizeof(_Tp)) - 1; }
_String_base(const allocator_type& __a) : _Base(__a) { }
_String_base(const allocator_type& __a, size_t __n) : _Base(__a)
{ _M_allocate_block(__n); }
~_String_base() { _M_deallocate_block(); }
void _M_throw_length_error() const;//两种异常之一。类外定义,防止内联?
void _M_throw_out_of_range() const;//两种异常之一
};
// Helper functions for exception handling.
template <class _Tp, class _Alloc>
void _String_base<_Tp,_Alloc>::_M_throw_length_error() const {
__STL_THROW(length_error("basic_string"));//两种异常之一
}
template <class _Tp, class _Alloc>
void _String_base<_Tp, _Alloc>::_M_throw_out_of_range() const {
__STL_THROW(out_of_range("basic_string"));//两种异常之一
}
未完待续,相关头文件如下
#include "../sgi_stl/stl/stl_config.h"
#include "../sgi_stl/stl/stl_alloc.h"
#include "../sgi_stl/stl/stl_string_fwd.h"
#include <ctype.h>
#include "../sgi_stl/stl/stl_function.h"
#include <stddef.h>
#include "../sgi_stl/stl/functional"
#include "../sgi_stl/stl/stl_ctraits_fns.h"
#include <exception>
#include "../sgi_stl/stl/stl_exception.h"
#include "../sgi_stl/stl/stdexcept"
#include "../sgi_stl/stl/concept_checks.h"
#include "../sgi_stl/stl/stl_iterator_base.h"
#include "../sgi_stl/stl/stl_iterator.h"
#include "../sgi_stl/stl/stl_relops.h"
#include "../sgi_stl/stl/stl_pair.h"
#include "../sgi_stl/stl/type_traits.h"
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <stddef.h>
#include <new.h>
#include <iosfwd>
#include "../sgi_stl/stl/stl_algobase.h"
#include <new.h>
#include "../sgi_stl/stl/stl_construct.h"
#include "../sgi_stl/stl/stl_tempbuf.h"
#include "../sgi_stl/stl/stl_uninitialized.h"
#include "../sgi_stl/stl/stl_raw_storage_iter.h"
#include "../sgi_stl/stl/memory"
#include "../sgi_stl/stl/stl_heap.h"
#include "../sgi_stl/stl/stl_algo.h"
#include "../sgi_stl/stl/algorithm"
#include <iosfwd>
#include "../sgi_stl/stl/char_traits.h"
#include "../sgi_stl/stl/string"