2009年5月15日星期五
2009年5月11日星期一
2009年5月9日星期六
一种自动的singleton工具
看下面描述。
基类singleton
template<class T>
class singleton
{
static T* ms_Singleton;
public:
singleton(void)
{
assert(!ms_Singleton);
int offset = (int)(T*)1-(int)(singleton<T>*)(T*)1;
ms_Singleton = (T*)((int)this+offset);
}
~singleton()
{
assert(ms_Singleton);
ms_Singleton = 0;
}
static T& GetSingleton(void)
{
assert(ms_Singleton);
return (*ms_Singleton);
}
static T* GetSingletonPtr(void)
{
return ms_Singleton;
}
};
template<class T>
T* singleton<T>::ms_Singleton = 0;
派生类
class A : public singleton<A>
{
public:
void outputA() {cout << "10";}
};
使用
static A g_A;
int main()
{
A::GetSingleton().outputA();
return 0;
}
整个来说就是一个比较好的模式。
2009年5月8日星期五
xerces-c环境配置以及测试
编译xerces projects目录下的lib工程会生成2个文件dll和lib。
然后添加src目录。
include D:\vs2005\xerces-c-3.0.1\src
library D:\vs2005\xerces-c-3.0.1\Build\Win32\VC9\Release
就是编译生成lib和dll的那个目录
然后在projects-property下链接xerces-c_3.lib
或者#pragma comment(lib,"xerces-c_3.lib")也行
好配置成功了。
下面该做练习了。
我随意在google找了个代码,自己敲上去,有bug,修改了下,
成功解析。
代码还是蛮简单的。
游戏编程精粹里面有讲到数据驱动的,以后数据就存xml。。。
上代码和发图才是王道
sample.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<ApplicationSettings option_a="10" option_b="24">
</ApplicationSettings>
<OtherStuff option_x="500">
</OtherStuff>
</root>
parser.h
#ifndef XML_PARSER_H
#define XML_PARSER_H
#include <xercesc\dom\DOM.hpp>
#include <xercesc\dom\DOMDocument.hpp>
#include <xercesc\dom\DOMDocumentType.hpp>
#include <xercesc\dom\DOMElement.hpp>
#include <xercesc\dom\DOMImplementation.hpp>
#include <xercesc\dom\DOMImplementationLS.hpp>
#include <xercesc\dom\DOMNodeIterator.hpp>
#include <xercesc\dom\DOMNodeList.hpp>
#include <xercesc\dom\DOMText.hpp>
#include <xercesc\parsers\XercesDOMParser.hpp>
#include <xercesc\util\XMLUni.hpp>
#include <string>
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
enum
{
ERROR_ARGS = 1,
ERROR_XERCES_INIT,
ERROR_PARSE,
ERROR_EMPTY_DOCUMENT
};
class GetConfig
{
public:
GetConfig();
~GetConfig();
void readConfigFile(std::string&) throw(std::runtime_error);
char* getOptionA() {return m_optionA;}
char* getOptionB() {return m_optionB;}
private:
xercesc::XercesDOMParser* m_ConfigFileParser;
char* m_optionA;
char* m_optionB;
XMLCh* TAG_root;
XMLCh* TAG_ApplicationSettings;
XMLCh* ATTR_OptionA;
XMLCh* ATTR_OptionB;
};
#include "Parser.h"
using namespace std;
using namespace xercesc;
GetConfig::GetConfig()
{
try
{
XMLPlatformUtils::Initialize();
}
catch(XMLException& e)
{
char* message = XMLString::transcode(e.getMessage());
cerr << "XML toolKit initializtion error:"
<< message << endl;
XMLString::release(&message);
}
TAG_root = XMLString::transcode("root");
TAG_ApplicationSettings = XMLString::transcode("ApplicationSettings");
ATTR_OptionA = XMLString::transcode("option_a");
ATTR_OptionB = XMLString::transcode("option_b");
m_ConfigFileParser = new XercesDOMParser;
}
GetConfig::~GetConfig()
{
try
{
XMLPlatformUtils::Terminate();
}
catch(xercesc::XMLException& e)
{
char* message = XMLString::transcode(e.getMessage());
cerr << "XML toolkit teerdown error" << message << endl;
XMLString::release(&message);
}
}
void GetConfig::readConfigFile(std::string& configfile)
throw(std::runtime_error)
{
struct stat fileStatus;
int iretStat = stat(configfile.c_str(),&fileStatus);
if (iretStat == ENOENT)
throw(std::runtime_error("Path name does not exsit"));else if(iretStat == ENOTDIR)
throw(std::runtime_error("a component of the path is not a directory"));
//else if(iretStat == ELOOP)
// throw(std::runtime_error("Too many symbolic links encountered while traversing the path"));
else if(iretStat == EACCES)
throw(std::runtime_error("Permission denied"));
else if(iretStat == ENAMETOOLONG)
throw(std::runtime_error("File can not be read\n"));
m_ConfigFileParser->setValidationScheme(XercesDOMParser::Val_Never);
m_ConfigFileParser->setDoNamespaces(false);
m_ConfigFileParser->setDoSchema(false);
m_ConfigFileParser->setLoadExternalDTD(false);
try
{
m_ConfigFileParser->parse(configfile.c_str());
DOMDocument* xmlDoc = m_ConfigFileParser->getDocument();
DOMElement* elementroot = xmlDoc->getDocumentElement();
if(! elementroot) throw(std::runtime_error("empty XML document"));
DOMNodeList* children = elementroot->getChildNodes();
const XMLSize_t nodeCount = children->getLength();
for(XMLSize_t xx = 0; xx < nodeCount; xx++)
{
DOMNode* currentNode = children->item(xx);
if (currentNode->getNodeType() &&
currentNode->getNodeType() == DOMNode::ELEMENT_NODE)
{
DOMElement* currentElement = dynamic_cast<xercesc::DOMElement*>(currentNode);
if (XMLString::equals(currentElement->getTagName(),TAG_ApplicationSettings))
{
const XMLCh* xmlch_OptionA = currentElement->getAttribute(ATTR_OptionA);
m_optionA = XMLString::transcode(xmlch_OptionA);
const XMLCh* xmlch_OptionB = currentElement->getAttribute(ATTR_OptionB);
m_optionB = XMLString::transcode(xmlch_OptionB);
break;
}
}
}
}
catch(xercesc::XMLException& e)
{
char* message = xercesc::XMLString::transcode(e.getMessage());
ostringstream errBuf;
errBuf << "Error parseing file" << message << flush;
XMLString::release(&message);
}
}
#include <iostream>
#include "Parser.h"
#include <string>
using namespace std;
int main()
{
string configfilename = "sample.xml";
GetConfig appConfig;
appConfig.readConfigFile(configfilename);
cout << "Application Option B=" << appConfig.getOptionB() << endl;
return 0;
}
2009年5月7日星期四
数据结构作业 练习函数指针
#include <vector>
#include <iostream>
#include <algorithm>
#include <time.h>
using namespace std;
typedef vector<int> ArrayInt;
typedef void (*sort_type)(ArrayInt&);
void insersion_sort(ArrayInt& arr);
void selection_sort(ArrayInt& arr);
void quick_sort(ArrayInt& arr);
int max_key(ArrayInt &arr,int low,int hight);
void recursive_quick_sort(ArrayInt& arr,int low,int hight);
int partition(ArrayInt& arr,int low,int hight);
void my_test_sort(ArrayInt& arr,sort_type p)
{
(*p)(arr);
}
void insersion_sort(ArrayInt& arr)
{
int first_unsorted;
int position;
int current;
for (first_unsorted = 1;first_unsorted < arr.size();first_unsorted++)
{
if (arr[first_unsorted] < arr[first_unsorted-1])
{
position = first_unsorted;
current = arr[first_unsorted];
do
{
arr[position] = arr[position-1];
position--;
}while(position>0 && arr[position-1] >current);
arr[position] = current;
}
}
}
void selection_sort(ArrayInt& arr)
{
for(int position = arr.size()-1;position>0;position--)
{
int max = max_key(arr,0,position);
swap(arr[max],arr[position]);
}
}
void quick_sort(ArrayInt& arr)
{
recursive_quick_sort(arr,0,arr.size()-1);
}
void recursive_quick_sort(ArrayInt& arr,int low,int hight)
{
int pivot_position;
if (low < hight)
{
pivot_position = partition(arr,low,hight);
recursive_quick_sort(arr,low,pivot_position-1);
recursive_quick_sort(arr,pivot_position+1,hight);
}
}
int partition(ArrayInt& arr,int low,int hight)
{
int iRndPivID;
srand(unsigned(time(0)));
iRndPivID = (rand() % (hight - low + 1)) + low;
swap(arr[iRndPivID], arr[hight]);
int iPivValue;
int i;
int iPivPos;
iPivValue = arr[hight];
iPivPos = low - 1;
for (i=low; i<=hight-1; i++)
{
if (arr[i] <= iPivValue)
{
iPivPos++;
swap(arr[iPivPos],arr[i]);
}
}
iPivPos++;
swap(arr[iPivPos],arr[hight]);
return iPivPos;
}
int max_key(ArrayInt &arr,int low,int hight)
{
int largest,current;
largest = low;
for(current = low+1;current<=hight;current++)
{
if (arr[largest] < arr[current])
{
largest = current;
}
}
return largest;
}
int main()
{
ArrayInt arrayInt;
int st;
cout <<"请输入排序算法\n1.插入排序\n2.选择排序\n3.快速排序\n";
cin >> st;
sort_type p = NULL;
switch(st)
{
case 1:
p = insersion_sort;
break;
case 2:
p = selection_sort;
break;
case 3:
p = quick_sort;
break;
default:
p = quick_sort;
}
cout << "请输入整数的个数后,然后输入整数"<<endl;
int n;
cin>>n;
for (int i=0;i<n;i++)
{
int num;
cin>>num;
arrayInt.push_back(num);
}
cout << "排序前的数字为:"<<endl;
for (int i=0;i<n;i++)
{
cout << arrayInt[i] << endl;
}
my_test_sort(arrayInt,p);
cout << "排序后的数字为:"<<endl;
for (int i=0;i<n;i++)
{
cout << arrayInt[i] << endl;
}
return 0;
}
2009年5月6日星期三
容器erase的时候不会释放的指针指向的内存
主要是想用map管理无模式对话框,但不知道map erase的时候会不会释放掉内存。用map测试下。
//这个破博客 居然打不出来尖括号
class A
{
public:
A(int _x,int _y){_x = x; _y =y;}
public:
int x;
int y;
};
typedef map
int main()
{
stringAMap map1;
A* a = new A(100,200);
map1.insert(make_pair("韦宇浩",a));
map1.insert(make_pair("陆一峰",new A(200,300)));
stringAMap::iterator pos;
pos = map1.find("韦宇浩");
if (pos != map1.end())
{
map1.erase(pos);
}
cout <<>x;
return 0;
}
看结果 便知,没有释放掉内存。
诱惑的判定
当你在玩和不玩之间徘徊时,诱惑就出现了,因为你在徘徊,这时,坚定地告诉自己:这是诱惑,拒绝诱惑,得到更多,失去更少。开始study
但是,当你很想休息,而没有徘徊的时候,这样休息就不算诱惑了,人类的先天生理因素决定了休息室明智的选择。。。
所以,判定一件事情是不是诱惑的时候,主要是看有没有徘徊这个词。。。
思考了下,对很多人用此判定都是可行的。
你想踢球,但是却没有不想踢球的念头,那么踢球对你来说不是诱惑,你习惯了。。。
克服诱惑是一个慢慢提高自己的过程
所以,当徘徊的时候,一定要理性。。
这是早上起来突发奇想。
借用云风的话:思绪来的快去得也快,偶尔会在这里停留。
2009年5月5日星期二
2009年5月4日星期一
singleton设计模式
class Singleton1
{
public:
Singleton1& Instance()
{
static Singleton Obj;
return Obj;
private:
Singleton1();
}
}
优雅的解决了问题。
再看一个可扩展的。
#include
#include
class SingletonBase
{
public:
SingletonBase() { cout << "SingletonBase created!" << endl; }
virtual ~SingletonBase() { cout << "SingletonBase destroyed!" << endl; }
virtual void Access() { cout << "SingletonBase accessed!" << endl; }
static SingletonBase* GetObj() { return m_pObj; }
static void SetObj(SingletonBase* pObj) { m_pObj = pObj; }
protected:
static SingletonBase* m_pObj;
};
SingletonBase* SingletonBase::m_pObj;
inline SingletonBase* Base()
{
assert(SingletonBase::GetObj());
return SingletonBase::GetObj();
}
// Create a derived singleton-type class
class SingletonDerived : public SingletonBase
{
public:
SingletonDerived()
{ cout << "SingletonDerived created!" << endl; }
virtual ~SingletonDerived()
{ cout << "SingletonDerived destroyed" << endl; }
virtual void Access()
{ cout << "SingletonDerived accessed!" << endl; }
protected:
};
inline SingletonDerived* Derived()
{
assert(SingletonDerived::GetObj());
return (SingletonDerived*)SingletonDerived::GetObj();
}
// Using the code...
// The complex singleton requires more work to use, but is
// more flexible. It also allows more control over object
// creation, which is sometimes desirable.
SingletonDerived::SetObj(new SingletonDerived);
// Notice that the functionality has been overridden by the new
// class, even though accessing it through the original method.
Base()->Access();
Derived()->Access();
// This variation on a singleton unfortunately requires both
// explicit creation and deletion.
delete SingletonDerived::GetObj();
一个可扩展的singleton。
主要用于需要大量访问的全局对象。