|
|
发表于 2009-5-20 10:21:00
|
显示全部楼层
Re:我写的网络模块,大家来看看,不用也来讨论讨论……
我明白你的意思。我的意思是这样的:
1.接口:站在使用者的角度,我需要知道使用netgate,需要那些头文件。你的实现改变后我的程序是否受影响。
你看你的文件你的文件内容,有类继承还有头文件依赖,这是不太好的风格。
换句话说,你不因该站在你的角度来回答这个问题说“我有头文件”,而是应该站在使用者的角度,我需要那些头文件,这些是否必要,能不能更简单。库的版本变化是否要导致我重新编译等等。这是接口方面的问题。
文件stdio.h内容
#pragma once
#include "Channel.h"
class Stdio : public Channel
{
public:
Stdio();
~Stdio();
public:
int fdIn(void) const;
int fdOut(void) const;
private:
Stdio& operator = ( const Stdio& other );
};
文件channel.h内容
#pragma once
#include "EventHandler.h"
#include "Buffer.h"
#include "RestrictCopyCtor.h"
class Channel : public EventHandler, public RestrictCopyCtor< Channel >
{
public:
Channel( EventHandler::Type type =EventHandler::T_CHANNEL );
Channel( Buffer::ReadFun readFun, Buffer::WriteFun writeFun, EventHandler::Type type =EventHandler::T_CHANNEL );
virtual ~Channel();
public:
size_t sizeRecved(void) const;
size_t sizeToSend(void) const;
bool needFlush(void) const;
void discard( size_t len );
Channel& operator << ( Channel& channel );
template< typename T >
Channel& operator << ( const T& t )
{
bufOut.readFrom( &t, sizeof( T ) );
return *this;
}
Channel& operator << ( const std::string& t );
template< typename T >
Channel& operator << ( const std::vector< T >& t )
{
bufOut.readFrom( &t[0], t.size() * sizeof( T ) );
return *this;
}
Channel& operator << ( Channel& (*fun)( Channel& ) );
template< typename T >
Channel& operator >> ( T& t )
{
if( bufIn.size() >= sizeof( T ) ){
bufIn.writeTo( &t, sizeof( T ) );
}
return *this;
}
Channel& operator >> ( std::string& t );
template< typename T >
Channel& operator >> ( std::vector< T >& t )
{
size_t num= bufIn.size() / sizeof( T );
if( t.capacity() != 0 ){
num= min( num, t.capacity() );
}
t.resize( num );
bufIn.writeTo( &t[0], t.size() * sizeof( T ) );
return *this;
}
|
|