代码风格
When contributing code, in addition to following the C++ Core Guidelines, please follow the same design guidelines and style guidelines as mongodb/mongo. Additions and exceptions are listed below. For anything that isn't explicitly covered here, default to the Google C++ Style Guide. Running clang-format with our configuration file, mongo-cxx-driver/.clang-format, will help ensure your code conforms to the above standards.
提交消息
如果拉取请求处理 JIRA 票证,则对于单次提交 PR,请在主题行前加上票证 ID 作为前缀。 (对于多次提交 PR,我们将稍后在压缩或合并时添加 ID。)
CXX-883 Add commit message conventions to CONTRIBUTING.md
主题行大写,不要使用尾随句点。 主题长度最多为70字符。 使用主动语态! 想象一下这样的序言:
If applied, this commit will... [your subject line]
See Chris Beams' How to write a git commit message for more good guidelines to follow.
生命周期方法
带默认值或参数的“用户”构造函数
声明或删除复制构造函数
移动构造函数的声明或删除
复制赋值操作符的声明或删除
移动赋值操作符的声明或删除
声明-of-dtor
标头
公共标头必须包含“.hpp” 后缀。 私有标头必须有一个“.hh” 后缀。
总体结构:
许可证
包含守卫 (
#pragma once
)标题前奏
系统标头
<vector>
(按字母顺序)驱动程序标头
<path/to/header.hpp>
(按字母顺序排列)开放命名空间 mongocxx
inline namespace v_noabi {
代码
} // namespace v_noabi
关闭命名空间 mongocxx
标题片尾曲
示例:
// Copyright 2018-present MongoDB Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace mongocxx { inline namespace v_noabi { // Declarations // Inline Implementations } // namespace v_noabi } // namespace mongocxx
类声明
指南:
类声明开头和结尾的空行
公共部分在上/私有部分在下
生命周期方法优先(请参阅上面的规则)
私有成员排序
友谊
私有构造函数
私有方法
私有变量
示例:
class foo { public: foo(); foo(foo&& other) noexcept; foo& operator=(foo&& other) noexcept; ~foo(); private: friend baz; class MONGOCXX_PRIVATE impl; std::unique_ptr<impl> _impl; };
内联
在类声明之外定义
在声明和定义中指定 inline 关键字(为了清晰起见)
关系操作符
倾向于使用免费函数