音视频_2_LibJingle代码总结

论坛 期权论坛 脚本     
已经匿名di用户   2022-7-2 21:58   1857   0

1. 利用宏 DISALLOW_EVIL_CONSTRUCTORS(ClassName)禁止类对象拷贝和赋值操作

路径\talk\base\constructormagic.h文件定义了如下若干宏:

#define DISALLOW_ASSIGN(TypeName) \
  void operator=(const TypeName&)

// A macro to disallow the evil copy constructor and operator= functions
// This should be used in the private: declarations for a class
#define DISALLOW_COPY_AND_ASSIGN(TypeName)    \
  TypeName(const TypeName&);                    \
  DISALLOW_ASSIGN(TypeName)

// Alternative, less-accurate legacy name.
#define DISALLOW_EVIL_CONSTRUCTORS(TypeName) \
  DISALLOW_COPY_AND_ASSIGN(TypeName)

// A macro to disallow all the implicit constructors, namely the
// default constructor, copy constructor and operator= functions.
//
// This should be used in the private: declarations for a class
// that wants to prevent anyone from instantiating it. This is
// especially useful for classes containing only static methods.
#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
  TypeName();                                    \
  DISALLOW_EVIL_CONSTRUCTORS(TypeName)
在类的private片断内, DISALLOW_EVIL_CONSTRUCTORS和DISALLOW_COPY_AND_ASSIGN预处理阶段最终转变为:
private:
        // 私有拷贝构造函数
        TypeName(const TypeName&);
       // 私有赋值操作运算符
        void operator=(const TypeName&)
而宏 DISALLOW_IMPLICIT_CONSTRUCTORS更进一步,将空构造函数也设为私有,禁止从外部初始化该类的对象 :

private:
        // 私有空构造函数,禁止从外部通过此方法创建对象
        TypeName();
        // 私有拷贝构造函数
        TypeName(const TypeName&);
       // 私有赋值操作运算符
        void operator=(const TypeName&)



2. 去除字符串string前后空白字符函数trim

base\stringutils.h定义了若干字符串操作函数,如字符串匹配、字符串比较、去除前后空白字符等,这次看一下去除前后空白字符的函数。
首先定义要识别的空白字符:可以看到需要去掉的字符包括, 空格符、回车换行符、回车符和制表符四种。
static const char kWhitespace[] = " \n\r\t";

然后是具体函数实现:
std::string string_trim(const std::string &s)
{
    std::string::size_type first = s.find_first_not_of(kWhitespace);
    std::string::size_type last = s.find_last_not_of(kWhitespace);
    if ( first == std::string::npos || last == std::string::npos )
    {
        return std::string("");
    }
    return s.substr(first, last - first + 1);
}
可以看到主要利用std::string::find_first_not_of 和 find_last_not_of 这两个函数,分别找到第一个和最后一个不是函数参数中任何一个字符的首位置。
简单判断找到位置的有效性,然后返回子字符串。


3. etd


 

4. etd


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP