對于支持C99標準的編譯器來說,可以#include <cstdint>
來使用 int32_t, 但是目前不少的編譯器都做不到這點
所以,自己寫了個IntType.hpp
1 #ifndef INTTYPE_HPP_
2 #define INTTYPE_HPP_
3
4 #include "Platform.hpp"
5
6 namespace Charlie
7 {
8
9 typedef long long int64_t;
10 typedef int int32_t;
11 typedef short int16_t;
12 typedef char int8_t;
13
14 typedef unsigned long long uint64_t;
15 typedef unsigned int uint32_t;
16 typedef unsigned short uint16_t;
17 typedef unsigned char uint8_t;
18
19 }
20
21 #endif // INTTYPE_HPP_
22
2 #define INTTYPE_HPP_
3
4 #include "Platform.hpp"
5
6 namespace Charlie
7 {
8
9 typedef long long int64_t;
10 typedef int int32_t;
11 typedef short int16_t;
12 typedef char int8_t;
13
14 typedef unsigned long long uint64_t;
15 typedef unsigned int uint32_t;
16 typedef unsigned short uint16_t;
17 typedef unsigned char uint8_t;
18
19 }
20
21 #endif // INTTYPE_HPP_
22
然后今天大部分時間都消耗在了Timer的跨平臺處理上
這里采用了Bridge Design Pattern
即pImpl方法,實現了Linux 和 Windows 兩個平臺相關的高精度計時器
接口設計得很簡單,只能得到計時器定義以來經過的時間(單位是毫秒 //雖然精度都高于毫秒)
具體請見一下幾個文件了
http://code.google.com/p/charlib/source/browse/trunk/Charlib/includes/Timer.hpp
http://code.google.com/p/charlib/source/browse/trunk/Charlib/includes/TimerImpl.hpp
http://code.google.com/p/charlib/source/browse/trunk/Charlib/includes/LinuxTimerImpl.hpp
http://code.google.com/p/charlib/source/browse/trunk/Charlib/includes/Win32TimerImpl.hpp
以及對應的srcs下的.cpp文件
基本上是這樣
Timer {
protected:
TimerImpl* pImpl;
}
class LinuxTimerImpl : public TimerImpl {}
class Win32TimerImpl : public TimerImpl {}
還定義了一個用于多平臺多編譯器的平臺辨識頭文件 - Platform.hpp
目前SVN上也有VC 05的工程 和 Code::Blocks 的工程文件
代碼在Windows下 MINGW 和 MSVC 8 兩個編譯器上編譯通過
在Ubuntu下使用Code::Blocks + GCC 編譯器上通過
// fibonacci數列的模板元編程版本因為在Ubuntu下GCC編譯未通過,所以注釋掉了,但是在MSVC 8和Code::Blocks的MINGW下編譯通過
SVN地址: http://charlib.googlecode.com/svn/trunk/
其他雜項:
HugeInt added 預計在有空的時候會完成,用于高精度整形的運算 = =~!