Linux c/c++编程一些方法

17 篇文章 0 订阅
1.定义函数指针
typedef void (*TimeMethodPtr) (void*);

2.定义类的函数模板,必须写在头文件中
class CAppTools {
public:
    virtual ~CAppTools();
    template <typename T2> void clearList(std::list<T2*>& vList) {
        for (typename std::list<T2*>::iterator iter = vList.begin(); iter != vList.end();) {
            delete(*iter);
            iter = vList.erase(iter);
        }
    };
};


3.定义不定长度函数

void CAppTools::writeParamLog(const char* format, ...)
{
    va_list paramList;
    va_start(paramList, format);
    char msg[1024];
    vsprintf(msg, format, paramList);
    va_end(paramList);
    writeLog(msg);
}


4.多父类调用

class IChildManager {
public:
    virtual int start(void*) = 0;
    virtual void stop() = 0;
}

class ISendSipInfo {
public:
    virtual int send(void*) = 0;
}

class CSipManager: public IChildManager, public ISendSipInfo {  // 实现类
public:
    virtual int start(void*);
    virtual void stop();
    virtual int send(void*);
}

class CAppManager {
private:
   IChildManager* m_SipM;
public:
   static CAppManager& getInstance() {static CAppManager m_Manager; return m_Manager}
   CAppManager() { m_SipM = new CSipManager();}
   IChildManager* getManager() {return m_SipM;}
   ISendSipInfo* getSendSipInfo() {return (ISendSipInfo*) m_SipM};
}

//  ...

CAppManager::getInstance().getSendSipInfo().send(NULL);  // 出错,将先调用IChildManager的start接口,才调用send方法

// 修改CAppManager如下
ISendSipInfo* getSendSipInfo() {return (CSipManager*) m_SipM};

4.创建单实例

#ifdef	__cplusplus
extern "C"
{
#endif

#define OneInstance(ClassName) static ClassName& getInstance() { \
	static ClassName instance; \
	return instance; \
	}
	
#ifdef	__cplusplus
}
#endif

调用可以在类中定义
public:
    OneInstance(CAppTools)

5.定义接口

#ifndef INFDEFINE_H
#define	INFDEFINE_H

//#ifdef	__cplusplus
//extern "C" {
//#endif

#define Interface class

#define implements public
    
#define extends public 
    
#define InterfaceDestructor(name) public: \
	virtual ~name() {}

#define DeclareInterface(name) Interface name { \
		InterfaceDestructor(name)

#define DeclareExtInterface(name, baseName) Interface name: extends baseName { \
	InterfaceDestructor(name)

#define EndInterface };
    
#define DeclareMultiExtInterfaceBegin(name) Interface name:  

#define DeclareMultiExtInterfaceEnd(name) { \
        InterfaceDestructor(name)
    
#define InfMethod(name) virtual name = 0
#define ImplInfMethod(name) virtual name


//#ifdef	__cplusplus
//}
//#endif

#endif	/* INFDEFINE_H */
定义接口

DeclareInterface(IFactory)
    InfMethod(IRunApp* getRunApp());
EndInterface

定义实现类

class CFactoryImpl : implements IFactory
{
public:
    CFactoryImpl();
    virtual ~CFactoryImpl();
    ImplInfMethod(IRunApp* getRunApp());
private:

};

定义多扩展接口实现

DeclareMultiExtInterfaceBegin(ISendSipInfo) extends IRegisterServer, extends ISendHeartPackage, extends ILogoutServer
    DeclareMultiExtInterfaceEnd(ISendSipInfo)

EndInterface

定义单扩展接口实现

DeclareExtInterface(ITimer, IRunInfo)
    InfMethod(void setInterval(int mSec));
    InfMethod(void registerTimeMethod(TimeMethodPtr tmMethod, void* param));
EndInterface    


定义单实例定义

#define OneInstance(ClassName) static ClassName& getInstance() { \
	static ClassName instance; \
	return instance; \
	}


6.定义模板类

#ifndef SERVICERETINFO_H
#define	SERVICERETINFO_H

#include "RetInfo.h"
template<class T> 
class CServiceRetInfo: public CRetInfo, public T
{
public:
   
private:

};

#endif	/* SERVICERETINFO_H */

#ifndef SERVICELISTRETINFO_H
#define	SERVICELISTRETINFO_H
#include <vector>
#include "RetInfo.h"

template<class T>
class CServiceListRetInfo: public  CRetInfo
{
public:
    std::vector<T>* getList() {return &m_List;}
private:
    std::vector<T> m_List;    
};

#endif	/* SERVICELISTRETINFO_H */


最好将模板实现放在.h文件里面,下面一个cpp只是演示如何在cpp实现

#include "ServiceListRetInfo.h"

template<class T> std::vector<T>* CServiceListRetInfo<T>::getList() {
    return &m_List;
}

定义模块类

/* 
 * File:   IServerService.h
 * Author: pc01
 *
 * Created on March 28, 2013, 3:48 PM
 */

#ifndef ISERVERSERVICE_H
#define	ISERVERSERVICE_H

#include "InfDefine.h"

template<class RETT, class LRETT>
DeclareInterface(IServerService) 
    InfMethod(int getInfoById(const char* address, RETT* retInfo));    
    InfMethod(int listAllInfo(const char* address, LRETT* listRetInfo));
EndInterface    

#endif	/* ISERVERSERVICE_H */

定义模板继承类

/* 
 * File:   IIndustryTypeManagementWebService.h
 * Author: pc01
 *
 * Created on March 28, 2013, 3:05 PM
 */

#ifndef IINDUSTRYTYPEMANAGEMENTWEBSERVICE_H
#define	IINDUSTRYTYPEMANAGEMENTWEBSERVICE_H

#include "IServerService.h"
#include "IndustryTypeInfo.h"
#include "ServiceRetInfo.h"
#include "ServiceListRetInfo.h"

Interface IIndustryTypeManagementWebService: extends IServerService< CServiceRetInfo<CIndustryTypeInfo>,  CServiceListRetInfo<CIndustryTypeInfo> > {

EndInterface

#endif	/* IINDUSTRYTYPEMANAGEMENTWEBSERVICE_H */

注意在gcc低版本时  CServiceListRetInfo<CIndustryTypeInfo>>  两个> 之间加个空格

7.结构初始化

typedef struct St_RunServiceInfo {
	bool m_Active; // 激活标志
	bool m_Terminate; //
	pthread_t m_RunThread;
	struct soap m_Soap;
} RunServiceInfo;
RunServiceInfo deviceServiceServiceInfo = { .m_Active = FALSE, .m_Termiante = FALSE };
可以给结构部分数据初始化

8.模板类继承

记得将模板类的实现或模板函数的实现最好都放在头文件里

/*
 * Copyright (c) 2013, TEST .Ltd
 * All rights reserved.
 *
 * FileName:iList.h
 * Description:列表接口
 * Author:TEST
 * 完成日期:Jun 6, 2013
 */

#ifndef ILIST_H_
#define ILIST_H_

#include "infDefine.h"

/**
  *   得到配置对象接口
  *   Author:TEST
  *   完成日期:May 31, 2013
  */
template<typename T>
DeclareInterface(IList)

	/**
	  *   添加对象
	  *   @param:	info 待添加对象
	  *   Author:TEST
	  *   完成日期:Jun 6, 2013
	  */
    InterfaceMethod(void add(T* info));
	/**
	  *   清除指定对象
	  *   @param:	info 待清除对象
	  *   @param:	del 是否删除
	  *   @return:	返回值,清除成功返回RET_SUCCESS
	  *   Author:TEST
	  *   完成日期:Jun 6, 2013
	  */
	InterfaceMethod(int remove(T* info, bool del));
	/**
	  *   得到指定位置的对象
	  *   @param:	index 位置
	  *   @return:	返回指定对象
	  *   Author:TEST
	  *   完成日期:Jun 6, 2013
	  */
	InterfaceMethod(T* get(int index));
	/**
	  *   得到对象所在位置
	  *   @param:	info 对象
	  *   @return:	返回对应位置,没有返回-1
	  *   Author:TEST
	  *   完成日期:Jun 6, 2013
	  */
	InterfaceMethod(int indexOf(T* info));
	/**
	  *   删除指定位置对象
	  *   @param:	info 对象
	  *   @return:	返回对应位置,没有返回-1
	  *   Author:TEST
	  *   完成日期:Jun 6, 2013
	  */
	InterfaceMethod(int del(int index));
	/**
	  *   返回列表总数
	  *   @return:	返回总数,没有返回0
	  *   Author:TEST
	  *   完成日期:Jun 6, 2013
	  */
	InterfaceMethod(int count());
EndInterface


#endif /* ILIST_H_ */

模板类继承

/*
 * Copyright (c) 2013, TEST .Ltd
 * All rights reserved.
 *
 * FileName:containerList.h
 * Description:
 * Author:TEST
 * 完成日期:Jun 6, 2013
 */

#ifndef CONTAINERLIST_H_
#define CONTAINERLIST_H_

#include <list>
#include "../inf/iList.h"
#include "../../utils/appTools.h"
#include "../../utils/appDefine.h"

template<typename T>
class CContainerList: implements IList<T> {
public:
	CContainerList() {
	}
	virtual ~CContainerList() {
		CAppTools::getInstance().clearList(m_List);
	}
public:
	/**
	 *   添加对象
	 *   @param:	info 待添加对象
	 *   Author:TEST
	 *   完成日期:Jun 6, 2013
	 */
	ImplInfMethod(void add(T* info)){
		m_List.push_back(info);
	}
	/**
	 *   清除指定对象
	 *   @param:	info 待清除对象
	 *   @param:	del 是否删除
	 *   @return:	返回值,清除成功返回RET_SUCCESS
	 *   Author:TEST
	 *   完成日期:Jun 6, 2013
	 */
	ImplInfMethod(int remove(T* info, bool del)){
		int result = ERROR_OBJECT_NULL;
		typename std::list<T*>::iterator iter;
		for (iter = m_List.begin(); iter != m_List.end(); iter++) {
			if (info == *iter) {
				if (del) {
					delete (*iter);
				}
				iter = m_List.erase(iter);
				result = RET_SUCCESS;
				break;
			}
		}
		return result;
	}
	/**
	 *   得到指定位置的对象
	 *   @param:	index 位置
	 *   @return:	返回指定对象
	 *   Author:TEST
	 *   完成日期:Jun 6, 2013
	 */
	ImplInfMethod(T* get(int index)){
		if (index < 0)
			return NULL;
		if (index > m_List.size())
			return NULL;
		T* result = NULL;
		int pos = 0;
		for (typename std::list<T*>::iterator iter = m_List.begin(); iter != m_List.end();
				iter++) {
			if (pos == index) {
				result = *iter;
				break;
			}
			pos++;
		}
		return result;
	}
	/**
	 *   得到对象所在位置
	 *   @param:	info 对象
	 *   @return:	返回对应位置,没有返回-1
	 *   Author:TEST
	 *   完成日期:Jun 6, 2013
	 */
	ImplInfMethod(int indexOf(T* info)){
		int result = -1;
		int pos = 0;
		for (typename std::list<T*>::iterator iter = m_List.begin(); iter != m_List.end();
				iter++) {
			if (*iter == info) {
				result = pos;
				break;
			}
			pos++;
		}
		return result;
	}
	/**
	 *   删除指定位置对象
	 *   @param:	info 对象
	 *   @return:	返回对应位置,没有返回-1
	 *   Author:TEST
	 *   完成日期:Jun 6, 2013
	 */
	ImplInfMethod(int del(int index)){
		int result = ERROR_OBJECT_NULL;
		int pos = 0;
		for (typename std::list<T*>::iterator iter = m_List.begin(); iter != m_List.end();
				iter++) {
			if (pos == index) {
				delete (*iter);
				m_List.erase(iter);
				result = RET_SUCCESS;
				break;
			}
			pos++;
		}
		return result;
	}
	/**
	 *   返回列表总数
	 *   @return:	返回总数,没有返回0
	 *   Author:TEST
	 *   完成日期:Jun 6, 2013
	 */
	ImplInfMethod(int count()){
		return m_List.size();
	}
private:
	std::list<T*> m_List;
};

#endif /* CONTAINERLIST_H_ */

8.模板类继承

记得将模板类的实现或模板函数的实现最好都放在头文件里

/*
 * Copyright (c) 2013, TEST .Ltd
 * All rights reserved.
 *
 * FileName:iList.h
 * Description:列表接口
 * Author:TEST
 * 完成日期:Jun 6, 2013
 */

#ifndef ILIST_H_
#define ILIST_H_

#include "infDefine.h"

/**
  *   得到配置对象接口
  *   Author:TEST
  *   完成日期:May 31, 2013
  */
template<typename T>
DeclareInterface(IList)

	/**
	  *   添加对象
	  *   @param:	info 待添加对象
	  *   Author:TEST
	  *   完成日期:Jun 6, 2013
	  */
    InterfaceMethod(void add(T* info));
	/**
	  *   清除指定对象
	  *   @param:	info 待清除对象
	  *   @param:	del 是否删除
	  *   @return:	返回值,清除成功返回RET_SUCCESS
	  *   Author:TEST
	  *   完成日期:Jun 6, 2013
	  */
	InterfaceMethod(int remove(T* info, bool del));
	/**
	  *   得到指定位置的对象
	  *   @param:	index 位置
	  *   @return:	返回指定对象
	  *   Author:TEST
	  *   完成日期:Jun 6, 2013
	  */
	InterfaceMethod(T* get(int index));
	/**
	  *   得到对象所在位置
	  *   @param:	info 对象
	  *   @return:	返回对应位置,没有返回-1
	  *   Author:TEST
	  *   完成日期:Jun 6, 2013
	  */
	InterfaceMethod(int indexOf(T* info));
	/**
	  *   删除指定位置对象
	  *   @param:	info 对象
	  *   @return:	返回对应位置,没有返回-1
	  *   Author:TEST
	  *   完成日期:Jun 6, 2013
	  */
	InterfaceMethod(int del(int index));
	/**
	  *   返回列表总数
	  *   @return:	返回总数,没有返回0
	  *   Author:TEST
	  *   完成日期:Jun 6, 2013
	  */
	InterfaceMethod(int count());
EndInterface


#endif /* ILIST_H_ */

模板类继承

/*
 * Copyright (c) 2013, TEST .Ltd
 * All rights reserved.
 *
 * FileName:containerList.h
 * Description:
 * Author:TEST
 * 完成日期:Jun 6, 2013
 */

#ifndef CONTAINERLIST_H_
#define CONTAINERLIST_H_

#include <list>
#include "../inf/iList.h"
#include "../../utils/appTools.h"
#include "../../utils/appDefine.h"

template<typename T>
class CContainerList: implements IList<T> {
public:
	CContainerList() {
	}
	virtual ~CContainerList() {
		CAppTools::getInstance().clearList(m_List);
	}
public:
	/**
	 *   添加对象
	 *   @param:	info 待添加对象
	 *   Author:TEST
	 *   完成日期:Jun 6, 2013
	 */
	ImplInfMethod(void add(T* info)){
		m_List.push_back(info);
	}
	/**
	 *   清除指定对象
	 *   @param:	info 待清除对象
	 *   @param:	del 是否删除
	 *   @return:	返回值,清除成功返回RET_SUCCESS
	 *   Author:TEST
	 *   完成日期:Jun 6, 2013
	 */
	ImplInfMethod(int remove(T* info, bool del)){
		int result = ERROR_OBJECT_NULL;
		typename std::list<T*>::iterator iter;
		for (iter = m_List.begin(); iter != m_List.end(); iter++) {
			if (info == *iter) {
				if (del) {
					delete (*iter);
				}
				iter = m_List.erase(iter);
				result = RET_SUCCESS;
				break;
			}
		}
		return result;
	}
	/**
	 *   得到指定位置的对象
	 *   @param:	index 位置
	 *   @return:	返回指定对象
	 *   Author:TEST
	 *   完成日期:Jun 6, 2013
	 */
	ImplInfMethod(T* get(int index)){
		if (index < 0)
			return NULL;
		if (index > m_List.size())
			return NULL;
		T* result = NULL;
		int pos = 0;
		for (typename std::list<T*>::iterator iter = m_List.begin(); iter != m_List.end();
				iter++) {
			if (pos == index) {
				result = *iter;
				break;
			}
			pos++;
		}
		return result;
	}
	/**
	 *   得到对象所在位置
	 *   @param:	info 对象
	 *   @return:	返回对应位置,没有返回-1
	 *   Author:TEST
	 *   完成日期:Jun 6, 2013
	 */
	ImplInfMethod(int indexOf(T* info)){
		int result = -1;
		int pos = 0;
		for (typename std::list<T*>::iterator iter = m_List.begin(); iter != m_List.end();
				iter++) {
			if (*iter == info) {
				result = pos;
				break;
			}
			pos++;
		}
		return result;
	}
	/**
	 *   删除指定位置对象
	 *   @param:	info 对象
	 *   @return:	返回对应位置,没有返回-1
	 *   Author:TEST
	 *   完成日期:Jun 6, 2013
	 */
	ImplInfMethod(int del(int index)){
		int result = ERROR_OBJECT_NULL;
		int pos = 0;
		for (typename std::list<T*>::iterator iter = m_List.begin(); iter != m_List.end();
				iter++) {
			if (pos == index) {
				delete (*iter);
				m_List.erase(iter);
				result = RET_SUCCESS;
				break;
			}
			pos++;
		}
		return result;
	}
	/**
	 *   返回列表总数
	 *   @return:	返回总数,没有返回0
	 *   Author:TEST
	 *   完成日期:Jun 6, 2013
	 */
	ImplInfMethod(int count()){
		return m_List.size();
	}
private:
	std::list<T*> m_List;
};

#endif /* CONTAINERLIST_H_ */




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值