代码风格
贡献代码时,除了遵循 C++核心指南 之外,还请遵循与MongoDB/ Mongo相同的设计指南和风格指南。下面列出了新增内容和例外情况。对于此处未明确涵盖的任何内容,默认使用 Google C++样式指南。使用我们的配置文件mongo-cxx-driver/.clang-format 运行clang-format,将有助于确保您的代码符合上述标准。
提交消息
如果拉取请求处理 JIRA 票证,则对于单次提交 PR,请在主题行前加上票证 ID 作为前缀。 (对于多次提交 PR,我们将稍后在压缩或合并时添加 ID。)
CXX-883 Add commit message conventions to CONTRIBUTING.md
主题行大写,不要使用尾随句点。 主题长度最多为70字符。 使用主动语态! 想象一下这样的序言:
If applied, this commit will... [your subject line]
请参阅 Chris Beams 的《如何写入git提交消息》,了解更多值得遵循的良好指南。
生命周期方法
带默认值或参数的“用户”构造函数
声明或删除复制构造函数
移动构造函数的声明或删除
复制赋值操作符的声明或删除
移动赋值操作符的声明或删除
声明-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 关键字(为了清晰起见)
关系操作符
倾向于使用免费函数