ä¸è¨ã®è¨äºã§ãShift_JISï¼CP932ï¼ã®ã³ã¡ã³ãã«ã¾ã¤ãããã©ãã«ãç´¹ä»ãã¾ããããã¾ãæãããããã¦ãC/C++ã§ã¾ããããã¾ãããã³ã¡ã³ãäºä»¶ã»ã©ã®æå¤æ§ã¯ãªãã«ãããäºæ ãç¥ããªãã¨å¯¾å¦ãã«ãããã©ãã«ãªã®ã§é¡æ«ãè¨ãã¦ããã¾ãã
ãã®è¨äºã®å 容ã¯ãåãå®éã«ä½é¨ããç¶æ³ãã®ãã®ã§ã¯ããã¾ãããããããããä¾é¡ãä»ç«ã¦ãã®ã§ãã¹ãã¼ãªã¼ã追ãããã¦ã¿ã¦ãã ããã
å 容ï¼
- ä¾é¡ï¼æå»ä»ãã®ã¡ãã»ã¼ã¸ãã¥ã¼
- å®è£ ãå¤ãã¦ã¿ãã¨
- ãªãã§ããããªãã¨ã«
- 誰ãæªãã®ã
ä¾é¡ï¼æå»ä»ãã®ã¡ãã»ã¼ã¸ãã¥ã¼
ä¾é¡ã¨ãã¦ã次ã®ãããªæ©è½ãæã¤ã¯ã©ã¹ãèãã¾ãã
- æååã¡ãã»ã¼ã¸ããæå»ï¼ã¿ã¤ã ã¹ã¿ã³ãï¼ãæ·»ãã¦ãã¥ã¼ã§ç®¡çããã
ã¯ã©ã¹ã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ãã次ã®ãããªãããã¼ãã¡ã¤ã«ã«è¨è¿°ãã¾ãã
// msgq.h #ifndef MSGQ_H #define MSGQ_H #include <queue> #include <string> class MessageQueue { public: void PostMessage(const std::string& msg);// æå»ãä»å ãã const std::string TakeMessage(void); bool IsEmpty(void) const; private: std::queue<std::string> queue_; }; #endif
å®è£ ãã¡ã¤ã«ã¯æ¬¡ã®ããã§ãã
// msgq.cpp Version 1 #include <time.h> // time(), localtime() #include <stdio.h> // sprintf() #include "msgq.h" void MessageQueue::PostMessage(const std::string& msg) { time_t now; time(&now); struct tm *ptm = localtime(&now); char timeStamp[30]; // å¤ã sprintf(timeStamp, "[%4d-%02d-%02d %02d:%02d:%02d] ", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec ); queue_.push(std::string(timeStamp) + msg); } const std::string MessageQueue::TakeMessage(void) { const std::string msg = queue_.front(); queue_.pop(); return msg; } bool MessageQueue::IsEmpty(void) const { return queue_.empty(); }
MessageQueueã¯ã©ã¹ããã¹ãããããã®ããã°ã©ã ãæ¸ãã¾ãã
// test-msgq.cpp #include <iostream> #include "msgq.h" int main() { MessageQueue msgq; msgq.PostMessage("hello"); msgq.PostMessage("good morning"); msgq.PostMessage("good afternoon"); msgq.PostMessage("good evening"); msgq.PostMessage("good night"); while (!msgq.IsEmpty()) { std::cout << msgq.TakeMessage() << std::endl; } return 0; }
ãã®ãã¹ãããã°ã©ã ã§ã¯ãã¡ãã»ã¼ã¸ã«ä»å ãããæå»ãã»ã¨ãã©åãã«ãªã£ã¦ãã¾ãã¾ãããããã¯ããã¨ãã¾ãããã
ã³ã³ãã¤ã«ï¼å®è¡ãã¦ã¿ã¾ãã
$ g++ msgq.cpp test-msgq.cpp $ ./a.exe [2016-05-16 09:14:42] hello [2016-05-16 09:14:42] good morning [2016-05-16 09:14:42] good afternoon [2016-05-16 09:14:42] good evening [2016-05-16 09:14:42] good night
使ã£ã¦ããã³ã³ãã¤ã©ã¯MinGWã®gccã§ãããä»åã®è©±ã§ã¯ãã³ã³ãã¤ã©ããªãã§ãããã¯å½±é¿ãã¾ããã
å®è£ ãå¤ãã¦ã¿ãã¨
ä¾é¡ã®ããã°ã©ã ã¯Windowsä¸ã§åããã¨ãã¾ããããã¯ããã«æã£ã¦ããã¨ããã¾ã¼ããWindowsç¹æã®è©±ããã¨è¨ãã¨ãããã§ããªãã¦ããã¨ã®æ¬è³ªã¯ãOSäºã ã¨ããããã¯C/C++ã®ä»æãã®åé¡ã§ãã
ã§ãWindowsä¸ãªã®ã§ãtime.hã®é¢æ°ã使ã代ããã«ãWindows APIã®GetLocalTime()é¢æ°ã使ããã¨ã«ãã¾ããGetLocalTime()ã¯æ¬¡ã®ç¹ã§time.hã®é¢æ°ãã便å©ã§ãã
- time()ã¨localtime()ã®2ã¤ã使ãå¿ è¦ããªããGetLocalTime()ã ãã§æ¸ãã
- time_tåã¨struct tmåï¼ã¸ã®ãã¤ã³ã¿ã¼ï¼ã®2ã¤ã使ãå¿ è¦ããªããSYSTEMTIMEåã ãã§æ¸ãã
- å¹´æã«å¯¾ãã¦ã+1900ãã+1ãã®ãããªè£æ£ãããå¿ è¦ããªãã
GetLocalTime()çã®MessageQueueã¯ã©ã¹ãVersion 2ã¨ãã¾ãã
// msgq.cpp Version 2 #include <Windows.h> // GetLocalTime() #include <stdio.h> // sprintf() #include "msgq.h" void MessageQueue::PostMessage(const std::string& msg) { SYSTEMTIME now; GetLocalTime(&now); char timeStamp[30]; // å¤ã sprintf(timeStamp, "[%4d-%02d-%02d %02d:%02d:%02d] ", now.wYear, now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond ); queue_.push(std::string(timeStamp) + msg); } const std::string MessageQueue::TakeMessage(void) { const std::string msg = queue_.front(); queue_.pop(); return msg; } bool MessageQueue::IsEmpty(void) const { return queue_.empty(); }
ãã¦ãã³ã³ãã¤ã«ï¼å®è¡ã
$ g++ msgq.cpp test-msgq.cpp C:\Users\hiyama\AppData\Local\Temp\ccIGR1jP.o:test-msgq.cpp:(.text+0x55): undefined reference to `MessageQueue::PostMessage(std::string const&)' C:\Users\hiyama\AppData\Local\Temp\ccIGR1jP.o:test-msgq.cpp:(.text+0xa2): undefined reference to `MessageQueue::PostMessage(std::string const&)' C:\Users\hiyama\AppData\Local\Temp\ccIGR1jP.o:test-msgq.cpp:(.text+0xef): undefined reference to `MessageQueue::PostMessage(std::string const&)' C:\Users\hiyama\AppData\Local\Temp\ccIGR1jP.o:test-msgq.cpp:(.text+0x13c): undefined reference to `MessageQueue::PostMessage(std::string const&)' C:\Users\hiyama\AppData\Local\Temp\ccIGR1jP.o:test-msgq.cpp:(.text+0x189): undefined reference to `MessageQueue::PostMessage(std::string const&)' collect2: ld returned 1 exit status
ãããã¼ï¼ ã¨ã©ã¼ã§ããã¨ã©ã¼ã®å 容ã¯ãtest-msgq.cppããåç §ããã¦ãã`MessageQueue::PostMessage(std::string const&)'é¢æ°ãè¦ã¤ãããªããã¨ãããã®ã§ãã
ãªãã§ããããªãã¨ã«
ç¾å¨æå»ã®åå¾ã«ãtime()ã¨localtime()ã使ãããããã¨ãGetLocalTime()ã使ããã¯ãå®è£ è ã®å¤æã§ãã£ã¦ãã©ã£ã¡ãé¸ã¶ãã¯èªç±ã§ããã³ã³ãã¤ã©ã«æå¥ãè¨ãããçåãã¯ãªãã§ããããã«ãããããããGetLocalTime()ã使ç¨ããã¨ãã¡ãªãã§ããããªãã§ï¼
å®ã¯ãï¼ãã°ããâ¦ãç¶ãï¼
â¦
â¦
â¦
â¦
â¦
â¦
â¦
â¦
GetLocalTime()é¢æ°ãç´æ¥ã®åå ã§ã¯ããã¾ãããåé¡ãªã®ã¯ãMessageQueueã¯ã©ã¹ã®ã¡ã½ããï¼ã¡ã³ãã¼é¢æ°ï¼ã§ããPostMessage()ã§ãããPostMessageãã¨ããååãã®ãã®ãååã ããåé¡ãªã®ã§ãã
Windows APIã«PostMessage()ã¨ããé¢æ°ããããããã¨ååããã¶ã£ã¦ããã®ã§ããããããWindows APIã®PostMessage()ã¯å¤§åçé¢æ°ããããã¯ã©ã¹ã®ã¡ã½ãããªã®ã ãããåãååã§ãåé¡ããªãã ãããã¨ãããæãã§ãããã§ãããã¡ãªãã§ããå½±é¿ããã¡ãããã§ãã
Windows APIé¢æ°ã®ååã¯ãWindows.hã®ãªãã§ãã¯ãå®ç¾©ãããååãªãã§ããä»åã®ã±ã¼ã¹ã§ã¯ã次ã®ãã¯ãå®ç¾©ãä½ç¨ãã¾ãã
- #define PostMessage PostMessageA
.cppã½ã¼ã¹ã³ã¼ãå ã§ã¯ããã®ãã¯ãå®ç¾©ãåãã¦ãPostMessage â PostMessageAãã¨ããååã®ç½®æãèµ·ãã¦ããã®ã§ããå®ç¾©ãããååã MessageQueue::PostMessageA ã§ãå¼ã³åºãã«ä½¿ã£ãååã MessageQueue::PostMessage ã§ããããå¼ã³åºãããããããã¾ããã
誰ãæªãã®ã
ãã£ã±ãWindowsãæªããã§ãã -- ã¨ã¯è¨ããªãã§ããWindows.hã«éãããæå³ãã¬ãã¯ãç½®æã«ãã£ã¦ã³ã¼ããå£ããããã¨ã¯ãããã»ã©çããç¡ãã¨æãã¾ããã³ã³ãã¤ã«ã¨ã©ã¼ï¼ãªã³ã¯ã¨ã©ã¼ã«ãªã£ã¦ãããã°ããã§ããããã¾ãã¾æ§æçã«æ£ããã£ããããã¨ç¸å½åããã«ããã§ãããã
C/C++ã§ã¯ãããªããã»ããµãè¨èªå¦çç³»å ¨ä½ã®ãªãã§ãã£ããéè¦ãªå½¹å²ãæ ã£ã¦ãã¾ãããã®ããªããã»ããµã¯ãC/C++ã®æ§æã«é¢ãã¦ä½ãç¥ãã¾ãããååã®ã¹ã³ã¼ãã®ãã¨ãªããåãããªãã®ã§ãéé²ãªããã¹ãç½®æãããããã¾ããç¡ç¥ããã«ééããç¯ããããã®ã§ãã
ããªããã»ããµã£ã¦ãå¦çã®å ¥ãå£æ å½ãªãã§ãããå¦ç対象ã«å¯¾ããç¥èãå ¨ãæããªãã¤ãã«æåã®å¦çãä»»ããã£ã¦ããã¤ã¨æãã¾ããããæ æ¨ççã¾ãæ±äº¬å¨ä½ã§çµç¶æ¹ãè¦ããã¨ããªãåã«ãæ»è³çã®è¦³å 大使ãä»»ãããããªããã§ãï¼ä¾ããæå³ä¸æï¼ ããã§ããï¼ã
æã«æ¯ã¹ãã°ãããªããã»ããµã¸ã®ä¾å度ã¯æ¸ã£ã¦ã¯ããï¼inlineé¢æ°ã¨ãconstã«ããå®æ°ã¨ãï¼ã§ãããããå±ãªã£ãããããªããã»ããµå¦çããã·ã«ã§ããªãã®ããªã¼ãã¨æã£ããã¾ãã