C++ 编码规范

编程风格整理

以下只针对自定义,三方库保持原样

类名:大驼峰 PascalCase(首字母大写,无下划线)

文件名:全小写 + 下划线(与类名完全对应)

文件夹:全小写

缩写:

复制代码
Calibration → Calib
Parameter → Param
Initialize → Init
Configuration → Config
Manager → Mgr(可选)
Utility → Util (工具)
Calculate → Calc
Process → Proc
Communication → Comm (通信)

1.License声明 每个文件都需要

复制代码
/*****************************************************************************
*                                                                            *
*  OpenNI 1.x Alpha                                                          *
*  Copyright (C) 2012 PrimeSense Ltd.                                        *
*                                                                            *
*  This file is part of OpenNI.                                              *
*                                                                            *
*  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.                                            *
*                                                                            *
*****************************************************************************/

2.头文件

2.1 条件编译

复制代码
#ifndef __XN_OPEN_NI_H__
#define __XN_OPEN_NI_H__
#endif // __XN_OPEN_NI_H__

上面是传统写法

国内项目、Qt 开发、Windows/Linux 开发:全部统一用: #pragma once(现代写法,推荐)

2.2 #Include

复制代码
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include "XnTypes.h"

2.3 #define

复制代码
//---------------------------------------------------------------------------
// Defines
//---------------------------------------------------------------------------
#define XN_MASK_OPEN_NI "OpenNI"

2.4 struct

复制代码
//---------------------------------------------------------------------------
// Types
//---------------------------------------------------------------------------
#pragma pack(push, 1)
/**
 * The base of the building block of the data types - XnBaseNode
 */
typedef struct XnBaseNode
{
	/** the next XnNode */
	XnBaseNode* m_pNext;
	/** the previous XnNode */
	XnBaseNode* m_pPrevious;
	/** the value of the XnNode */
	XnValue m_Data;
} XnBaseNode;

#pragma pack(pop)

2.5 enum

复制代码
//---------------------------------------------------------------------------
// Types
//---------------------------------------------------------------------------
typedef enum
{
	/** Control is idle **/
	DEVICE_CONTROL_CLEAR,
	/** Control request was received **/
	DEVICE_CONTROL_REQUEST_RECEIVED,
	/** Control request was read by device, no reply yet **/
	DEVICE_CONTROL_REQUEST_READ,
	/** Control reply received, waiting for host in-request **/
	DEVICE_CONTROL_REPLY_READY,
} DeviceControlState;

//---------------------------------------------------------------------------
// Structures & Enums
//---------------------------------------------------------------------------

// Network socket type
/** The network socket type. */ 
typedef enum {
	/** UDP socket. */ 
	XN_OS_UDP_SOCKET = 0,
	/** TCP socket. */ 
	XN_OS_TCP_SOCKET
} XnOSSocketType;

2.6 全局变量

复制代码
//---------------------------------------------------------------------------
// Global Variables
//---------------------------------------------------------------------------
static XnMemBlockDataLinkedList g_allocatedMemory = {NULL, NULL};
static XN_CRITICAL_SECTION_HANDLE g_hCS;
static XnDumpFile* g_dump = NULL;

2.7 重定义内部使用类型

复制代码
//---------------------------------------------------------------------------
// Basic Types
//---------------------------------------------------------------------------
/** Boolean TRUE/FALSE type. */ 
typedef	BOOL				XnBool;

/** Signed character for strings. */ 
typedef	char				XnChar;
/** Unsigned character for strings. */ 
typedef	unsigned char		XnUChar;

/** Signed wide character for strings. */ 
typedef	wchar_t				XnWChar;

/** 8-bit signed integer. */ 
typedef	signed char			XnInt8;
/** 8-bit unsigned integer. */ 
typedef	unsigned char		XnUInt8;

/** 16-bit signed integer. */ 
typedef	short				XnInt16;
/** 16-bit unsigned integer. */ 
typedef	unsigned short		XnUInt16;

/** 32-bit signed integer. */ 
typedef	int					XnInt32;
/** 32-bit unsigned integer. */ 
typedef	unsigned int		XnUInt32;

/** 64-bit signed integer. */ 
typedef	__int64				XnInt64;
/** 64-bit unsigned integer. */ 
typedef	unsigned __int64	XnUInt64;

/** natural signed integer. */ 
typedef	int					XnInt;
/** natural unsigned integer. */ 
typedef	unsigned int		XnUInt;

/** Float (32bit) */ 
typedef	float				XnFloat;
/** Double (64bit) */ 
typedef	double				XnDouble;

2.8 API

复制代码
//---------------------------------------------------------------------------
// API
//---------------------------------------------------------------------------

/**
 * Converts a Xiron Status enumerator into a meaningful error string.
 *
 * @param	Status	[in]	The input Xiron Status to be converted to a string.
 *
 * @return A string representation of the Xiron status.
 */
XN_C_API const XnChar* XN_C_DECL xnGetStatusString(const XnStatus Status);


/**
 * Allocates a node info object, and sets its details.
 *
 * @param	pDescription	[in]	The description for the new node info object.
 * @param	strCreationInfo	[in]	The creation info for the new node info object.
 * @param	pNeededNodes	[in]	A list of node info's that are needed by the new node info.
 * @param	ppNodeInfo		[out]	A pointer to pointer to the new node info object.
 */
XN_C_API XnStatus XN_C_DECL xnNodeInfoAllocate(const XnProductionNodeDescription* pDescription, const XnChar* strCreationInfo, XnNodeInfoList* pNeededNodes, XnNodeInfo** ppNodeInfo);

2.9变量命名规则

复制代码
类型 m_小写类型名字
type m_tName;
int m_nAge;
bool m_bActiveState;
char* m_pstrName;
int* m_pnAge;
 
type m_tName_;
 
type t_tName;

3.注释

3.1 文件内部模块注释

复制代码
// Shared libraries
XN_C_API XnStatus XN_C_DECL xnOSLoadLibrary(const XnChar* cpFileName, XN_LIB_HANDLE* pLibHandle);
XN_C_API XnStatus XN_C_DECL xnOSFreeLibrary(const XN_LIB_HANDLE LibHandle); 
	
// Time
XN_C_API XnStatus XN_C_DECL xnOSGetEpochTime(XnUInt32* nEpochTime);
XN_C_API XnStatus XN_C_DECL xnOSGetTimeStamp(XnUInt64* nTimeStamp);

XN_C_API XnStatus xnInit(XnContext** ppContext)
{
	XnStatus nRetVal = XN_STATUS_OK;
	
	XN_VALIDATE_OUTPUT_PTR(ppContext);

	// make sure xnOS is initialized
	nRetVal = xnOSInit();
	if (nRetVal != XN_STATUS_OK && nRetVal != XN_STATUS_OS_ALREADY_INIT)
	{
		return (nRetVal);
	}

	// and also log system
	xnLogInitSystem();

	*ppContext = NULL;

	// allocate context
	XnContext* pContext;
	XN_VALIDATE_NEW(pContext, XnContext);
}

3.2 单个函数或者变量注释

复制代码
/**..........*/

/**
 * Allocates a node info object, and sets its details.
 *
 * @param	pDescription	[in]	The description for the new node info object.
 * @param	strCreationInfo	[in]	The creation info for the new node info object.
 * @param	pNeededNodes	[in]	A list of node info's that are needed by the new node info.
 * @param	ppNodeInfo		[out]	A pointer to pointer to the new node info object.
 */
XN_C_API XnStatus XN_C_DECL xnNodeInfoAllocate(const XnProductionNodeDescription* pDescription, const XnChar* strCreationInfo, XnNodeInfoList* pNeededNodes, XnNodeInfo** ppNodeInfo);

4.类 头文件命名

  1. 谷歌 C++ 规范(最主流)

    这是目前工业界使用最广泛的规范之一,规则清晰且易落地:

    类名:采用PascalCase(大驼峰),每个单词首字母大写,无下划线。

    示例:UserInfo、HttpRequest、DataManager

    头文件 / 源文件:全小写,单词之间用下划线_分隔,后缀分别为.h/.cpp(或.cc/.cxx)。

    示例:类UserInfo对应头文件user_info.h、源文件user_info.cpp

  2. Qt / 微软规范

    偏向 Windows/Qt 生态的命名风格:

    类名:同谷歌规范,PascalCase(大驼峰)。

    示例:QString、MainWindow、FileDialog

    头文件 / 源文件:与类名完全一致(大小写相同),后缀.h/.cpp。

    示例:类MainWindow对应MainWindow.h、MainWindow.cpp

  3. Linux 内核 / 开源库规范(全小写 + 下划线)

    偏向 Unix/Linux 生态的极简风格:

    类名:全小写,单词间用下划线分隔(注:Linux 内核本身少用类,但开源库如 Boost 部分模块采用此风格)。

    示例:user_info、http_request

    头文件 / 源文件:与类名一致,全小写 + 下划线,后缀.h/.c(或.cpp)。

    示例:类user_info对应user_info.h、user_info.cpp

5.其他

SDK的C接口中不支持引用,采用指针

相关推荐
用户805533698034 小时前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
BadBadBad__AK16 小时前
线段树维护区间 k 次方和
c++·数学·算法·stl
卷无止境1 天前
Eigen 库如何借助 OpenMP 加速计算
c++·后端
卷无止境1 天前
OpenMPI、MPICH 与 OpenMP:关系、核心概念与架构全解
c++·后端
郝学胜_神的一滴2 天前
CMake 30:循环语法全解|foreach_while双循环精讲、迭代技巧与实战避坑指南
c++·cmake
卷无止境4 天前
C++ 的Eigen 库全解析
c++
卷无止境4 天前
现代 C++特性大盘点:一门脱胎换骨的老语言
c++·后端
郝学胜_神的一滴4 天前
CMake 27:缓存变量的特性、语法、类型与实操全解
c++·cmake
博客18006 天前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴6 天前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake