fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdint>
  4. #include <string>
  5. #include <future>
  6.  
  7. //#pragma once
  8.  
  9. #include <chrono>
  10.  
  11. class StopWatch {
  12. std::chrono::high_resolution_clock::time_point S;
  13. std::chrono::high_resolution_clock::time_point E;
  14. public:
  15.  
  16. typedef std::chrono::milliseconds TimeType;
  17.  
  18. StopWatch() { Start(); };
  19.  
  20. bool Start() {
  21. S = E = std::chrono::high_resolution_clock::now();
  22. return true;
  23. }
  24. bool ReStart() {
  25. return Start();
  26. }
  27. bool Stop() {
  28. E = std::chrono::high_resolution_clock::now();
  29. return true;
  30. }
  31.  
  32. bool Reset() {
  33. S = E = std::chrono::high_resolution_clock::now();
  34. return true;
  35. }
  36. template<class T=TimeType>
  37. T Ellipse() {
  38. return std::chrono::duration_cast<T>(std::chrono::high_resolution_clock::now() -S);
  39. }
  40. template<class T=TimeType>
  41. T Result() {
  42. return std::chrono::duration_cast<T>(E - S);
  43. }
  44.  
  45. };
  46. //#include "StopWatch_II.h"
  47.  
  48.  
  49. bool KillProcess() {
  50. //do something....
  51. return false;
  52. }
  53.  
  54. std::string GetCommand() {
  55. std::string R;
  56.  
  57. std::getline(std::cin, R);
  58.  
  59. return R;
  60. }
  61.  
  62.  
  63.  
  64. int main() {
  65.  
  66. StopWatch SW;
  67. std::int64_t L = 4900;
  68. std::future<std::string> F = std::async(std::launch::async,GetCommand);
  69.  
  70. {
  71. std::ifstream ifs("WatchDog.cfg");
  72. if (ifs.is_open()) {
  73. ifs >> L;
  74. }
  75. }
  76.  
  77. std::cout << "Start WatchDog." << std::endl;
  78. std::int64_t C = 0;
  79.  
  80. while (SW.Ellipse<std::chrono::milliseconds>().count() <= L) {
  81. std::string R;
  82.  
  83. if (F.wait_for(std::chrono::milliseconds(500)) == std::future_status::ready) {
  84. R = F.get();
  85. F = std::async(std::launch::async,GetCommand);
  86. }
  87.  
  88. if (R == "" && SW.Ellipse<std::chrono::seconds>().count() > C) {
  89. C = SW.Ellipse<std::chrono::seconds>().count()+5;
  90. std::cout << "クゥン クゥン・・・" << std::endl;
  91. }
  92.  
  93. if (R == "Cry") {
  94. std::cout << "わん!" << std::endl;
  95. }
  96. if (R == "Right") {
  97. std::cout << "わん!" << std::endl;
  98. }
  99. if (R == "Left") {
  100. std::cout << "わん!" << std::endl;
  101. }
  102. if (R == "Reset") {
  103. SW.Reset();
  104. std::cout << "ワン!" << std::endl;
  105. }
  106. if (R == "Drop"|| R=="Exit"||R=="Quit") {
  107. std::cout << "辞めさせてもらうわ!!" << std::endl;
  108. break;
  109. }
  110.  
  111. if (R == "Now") {
  112. std::cout << "ばう" << std::endl;
  113. KillProcess();
  114. std::cout << "ワン!!" << std::endl;
  115. }
  116. }
  117.  
  118. std::cout << "Dog go to Sleep..."<<std::endl;
  119.  
  120. return 0;
  121.  
  122. }
Success #stdin #stdout 0s 82816KB
stdin
Right
Left
Cry
Quit
stdout
Start WatchDog.
わん!
わん!
わん!
辞めさせてもらうわ!!
Dog go to Sleep...