[Muduo网络库源码分析] (11) base/Types.h_基本类型声明
来源:互联网 发布:什么是源码 编辑:程序博客网 时间:2024/06/12 00:37
基本类型声明
功能:为网络库提供基本的类型
知识点:
- stirng 的三种实现方式:
用途:
可用与类型转换和string的高效使用
代码及分析:
Types.h
#ifndef MUDUO_BASE_TYPES_H#define MUDUO_BASE_TYPES_H#include <stdint.h>#ifdef MUDUO_STD_STRING#include <string>#else // !MUDUO_STD_STRING#include <ext/vstring.h>#include <ext/vstring_fwd.h>#endif#ifndef NDEBUG#include <assert.h>#endif////// The most common stuffs.///namespace muduo{#ifdef MUDUO_STD_STRING//无特殊处理的C++实现方式using std::string;#else // !MUDUO_STD_STRING//利用string的短字符优化实现方式typedef __gnu_cxx::__sso_string string;#endif// Taken from google-protobuf stubs/common.h//// Protocol Buffers - Google's data interchange format// Copyright 2008 Google Inc. All rights reserved.// http://code.google.com/p/protobuf///// Redistribution and use in source and binary forms, with or without// modification, are permitted provided that the following conditions are// met://// * Redistributions of source code must retain the above copyright// notice, this list of conditions and the following disclaimer.// * Redistributions in binary form must reproduce the above// copyright notice, this list of conditions and the following disclaimer// in the documentation and/or other materials provided with the// distribution.// * Neither the name of Google Inc. nor the names of its// contributors may be used to endorse or promote products derived from// this software without specific prior written permission.//// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.// Author: kenton@google.com (Kenton Varda) and others//// Contains basic types and utilities used by the rest of the library.//// Use implicit_cast as a safe version of static_cast or const_cast// for upcasting in the type hierarchy (i.e. casting a pointer to Foo// to a pointer to SuperclassOfFoo or casting a pointer to Foo to// a const pointer to Foo).// When you use implicit_cast, the compiler checks that the cast is safe.// Such explicit implicit_casts are necessary in surprisingly many// situations where C++ demands an exact type match instead of an// argument type convertable to a target type.//// The From type can be inferred, so the preferred syntax for using// implicit_cast is the same as for static_cast etc.://// implicit_cast<ToType>(expr)//// implicit_cast would have been part of the C++ standard library,// but the proposal was submitted too late. It will probably make// its way into the language in the future.//类型强转,implicit_cast只能执行up-cast,派生类->基类//这里模拟了boost库里面的implicit_casttemplate<typename To, typename From>inline To implicit_cast(From const &f){ return f;}// When you upcast (that is, cast a pointer from type Foo to type// SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts// always succeed. When you downcast (that is, cast a pointer from// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because// how do you know the pointer is really of type SubclassOfFoo? It// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,// when you downcast, you should use this macro. In debug mode, we// use dynamic_cast<> to double-check the downcast is legal (we die// if it's not). In normal mode, we do the efficient static_cast<>// instead. Thus, it's important to test in debug mode to make sure// the cast is legal!// This is the only place in the code we should use dynamic_cast<>.// In particular, you SHOULDN'T be using dynamic_cast<> in order to// do RTTI (eg code like this:// if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);// if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);// You should design the code some other way not to need this.//进行down_cast 基类->派生类转换,并检查转换是否安全template<typename To, typename From> // use like this: down_cast<T*>(foo);inline To down_cast(From* f) // so we only accept pointers{ // Ensures that To is a sub-type of From *. This test is here only // for compile-time type checking, and has no overhead in an // optimized build at run-time, as it will be optimized away // completely. if (false) { implicit_cast<From*, To>(0); }#if !defined(NDEBUG) && !defined(GOOGLE_PROTOBUF_NO_RTTI) //dynamic_cast检查该类型转换是否安全 assert(f == NULL || dynamic_cast<To>(f) != NULL); // RTTI: debug mode only!#endif return static_cast<To>(f);}}#endif
阅读全文
0 0
- [Muduo网络库源码分析] (11) base/Types.h_基本类型声明
- [Muduo网络库源码分析] (8) base/StringPiece.h_字符串参数传递类型
- [Muduo网络库源码分析] (1) base/Atomic.h_原子操作与原子整数
- [Muduo网络库源码分析] (6) base/Mutex.h_互斥锁操作
- [Muduo网络库源码分析] (10) base/ThreadPoll_cc_h_线程池
- [Muduo网络库源码分析] (2) base/Condition.cc_h_条件变量操作
- [Muduo网络库源码分析] (3) base/CountDownLatch.cc_h_“倒计时门闩”同步
- [Muduo网络库源码分析] (4) base/Exception_cc_h_带 stack trace 的异常基类
- [Muduo网络库源码分析] (5) base/FileUtil.cc_h_文件操作
- [Muduo网络库源码分析] (7) base/Singleton.h_SINGLETON对象创建型模式
- [Muduo网络库源码分析] (9) base/Thread.cc_h_CurrentThread_h线程对象
- muduo网络库源码分析-定时器
- muduo库源码分析
- muduo库源码学习(base)Atomic
- muduo库源码学习(base)AsyncLogging
- muduo库源码学习(base)Condition
- muduo库源码学习(base)CountDownLatch
- muduo库源码学习(base)Date
- PCIe学习笔记(31)--- PL-Logical (Gen 1 and Gen 2) --- (3)
- leetcode --100. Same Tree
- Linux下使用docker搭建STF(踩坑成功)
- hover改变子标签/同级标签样式
- 测试技能图谱
- [Muduo网络库源码分析] (11) base/Types.h_基本类型声明
- 8月1日做题跌坑记录---详解来自维基百科
- SVN安装配置
- Docker初次安装运行出错:Cannot connect to the Docker daemon at ***/docker.sock. Is the docker daemon running?
- SQList数据库
- 进程
- 普元EOS命名sql查询案例
- input样式class="form-control"
- IntelliJ Idea 常用快捷键列表