From eff4bcc2a7a72bbb679a5e3b947f18f57a455d3f Mon Sep 17 00:00:00 2001 From: Boris Chernov Date: Mon, 8 Jun 2020 12:36:33 +0300 Subject: [PATCH 1/7] 4 step(separate classes into separate files into one class .h and .cpp.) --- Lesson_9/SmartPointer/Car.cpp | 17 +++++ Lesson_9/SmartPointer/Car.h | 16 +++++ Lesson_9/SmartPointer/CarFactory.cpp | 6 ++ Lesson_9/SmartPointer/CarFactory.h | 8 +++ Lesson_9/SmartPointer/Driver.cpp | 25 +++++++ Lesson_9/SmartPointer/Driver.h | 20 ++++++ Lesson_9/SmartPointer/SmartPointer.cpp | 68 +------------------ Lesson_9/SmartPointer/SmartPointer.vcxproj | 18 +++-- .../SmartPointer/SmartPointer.vcxproj.filters | 20 ++++++ Lesson_9/StdThread/StdThread.vcxproj | 10 +-- Lesson_9/Task.txt | 7 +- 11 files changed, 136 insertions(+), 79 deletions(-) create mode 100644 Lesson_9/SmartPointer/Car.cpp create mode 100644 Lesson_9/SmartPointer/Car.h create mode 100644 Lesson_9/SmartPointer/CarFactory.cpp create mode 100644 Lesson_9/SmartPointer/CarFactory.h create mode 100644 Lesson_9/SmartPointer/Driver.cpp create mode 100644 Lesson_9/SmartPointer/Driver.h diff --git a/Lesson_9/SmartPointer/Car.cpp b/Lesson_9/SmartPointer/Car.cpp new file mode 100644 index 0000000..dedf3f5 --- /dev/null +++ b/Lesson_9/SmartPointer/Car.cpp @@ -0,0 +1,17 @@ +#include "Car.h" + +Car::Car(const std::string& color) + : color_(color) +{ + std::cout << color_ << " car has been created\n"; +} + +Car::~Car() +{ + std::cout << color_ << " car has been destroied\n"; +} + +void Car::Drive() +{ + std::cout << color_ << " car in move\n"; +} \ No newline at end of file diff --git a/Lesson_9/SmartPointer/Car.h b/Lesson_9/SmartPointer/Car.h new file mode 100644 index 0000000..db1ee23 --- /dev/null +++ b/Lesson_9/SmartPointer/Car.h @@ -0,0 +1,16 @@ +#pragma once +#include +#include +#include + +class Car +{ +public: + Car(const std::string& color); + ~Car(); + + void Drive(); + +private: + std::string color_; +}; diff --git a/Lesson_9/SmartPointer/CarFactory.cpp b/Lesson_9/SmartPointer/CarFactory.cpp new file mode 100644 index 0000000..736a6cd --- /dev/null +++ b/Lesson_9/SmartPointer/CarFactory.cpp @@ -0,0 +1,6 @@ +#include "CarFactory.h" + +std::unique_ptr CarFactory::BuildCar(const std::string& color) +{ + return std::unique_ptr(new Car(color)); +} diff --git a/Lesson_9/SmartPointer/CarFactory.h b/Lesson_9/SmartPointer/CarFactory.h new file mode 100644 index 0000000..bf6a71a --- /dev/null +++ b/Lesson_9/SmartPointer/CarFactory.h @@ -0,0 +1,8 @@ +#pragma once +#include "Car.h" + +class CarFactory +{ +public: + std::unique_ptr BuildCar(const std::string& color); +}; diff --git a/Lesson_9/SmartPointer/Driver.cpp b/Lesson_9/SmartPointer/Driver.cpp new file mode 100644 index 0000000..93f183d --- /dev/null +++ b/Lesson_9/SmartPointer/Driver.cpp @@ -0,0 +1,25 @@ +#include "Driver.h" + +Driver::Driver(const std::string& name, std::shared_ptr factory) + : factory_(factory) + , name_(name) +{ +} + +void Driver::BuyCar(const std::string& color) +{ + car_ = factory_->BuildCar(color); +} + +void Driver::Go() +{ + if (car_ != nullptr) + { + std::cout << name_ << " I have a car "; + car_->Drive(); + } + else + { + std::cout << name_ << ": I'll go on foot\n"; + } +} diff --git a/Lesson_9/SmartPointer/Driver.h b/Lesson_9/SmartPointer/Driver.h new file mode 100644 index 0000000..41d7c9c --- /dev/null +++ b/Lesson_9/SmartPointer/Driver.h @@ -0,0 +1,20 @@ +#pragma once +#include "CarFactory.h" + +class Driver +{ +public: + Driver(const std::string& name, std::shared_ptr factory); + + void BuyCar(const std::string& color); + + // SellCar + // BuyUsedCar + + void Go(); + +private: + std::unique_ptr car_; + std::shared_ptr factory_; + std::string name_; +}; \ No newline at end of file diff --git a/Lesson_9/SmartPointer/SmartPointer.cpp b/Lesson_9/SmartPointer/SmartPointer.cpp index 3e3666f..ff4de25 100644 --- a/Lesson_9/SmartPointer/SmartPointer.cpp +++ b/Lesson_9/SmartPointer/SmartPointer.cpp @@ -1,70 +1,4 @@ -#include -#include -#include - -class Car -{ -public: - Car(const std::string& color) - : color_(color) - { - std::cout << color_ << " car has been created\n"; - } - ~Car() { std::cout << color_ << " car has been destroied\n"; } - - void Drive() - { - std::cout << color_ << " car in move\n"; - } - -private: - std::string color_; -}; - -class CarFactory -{ -public: - std::unique_ptr BuildCar(const std::string& color) - { - return std::unique_ptr(new Car(color)); - } -}; - -class Driver -{ -public: - Driver(const std::string& name, std::shared_ptr factory) - : factory_(factory) - , name_(name) - { - } - - void BuyCar(const std::string& color) - { - car_ = factory_->BuildCar(color); - } - - // SellCar - // BuyUsedCar - - void Go() - { - if (car_ != nullptr) - { - std::cout << name_ << " I have a car "; - car_->Drive(); - } - else - { - std::cout << name_ << ": I'll go on foot\n"; - } - } - -private: - std::unique_ptr car_; - std::shared_ptr factory_; - std::string name_; -}; +#include "Driver.h" int main() { diff --git a/Lesson_9/SmartPointer/SmartPointer.vcxproj b/Lesson_9/SmartPointer/SmartPointer.vcxproj index 6150fc1..03511b6 100644 --- a/Lesson_9/SmartPointer/SmartPointer.vcxproj +++ b/Lesson_9/SmartPointer/SmartPointer.vcxproj @@ -23,32 +23,32 @@ {F6CAC44C-D7A8-48BA-B457-F0FD6A929F1C} Win32Proj SmartPointer - 10.0.17763.0 + 10.0 Application true - v141 + v142 Unicode Application false - v141 + v142 true Unicode Application true - v141 + v142 Unicode Application false - v141 + v142 true Unicode @@ -151,8 +151,16 @@ + + + + + + + + diff --git a/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters b/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters index 46986af..32d5cc4 100644 --- a/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters +++ b/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters @@ -18,5 +18,25 @@ Source Files + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/Lesson_9/StdThread/StdThread.vcxproj b/Lesson_9/StdThread/StdThread.vcxproj index d2e65cc..0375706 100644 --- a/Lesson_9/StdThread/StdThread.vcxproj +++ b/Lesson_9/StdThread/StdThread.vcxproj @@ -23,32 +23,32 @@ {52F68E52-72B0-44B0-84D7-CBE3CAB4537B} Win32Proj StdThread - 10.0.17763.0 + 10.0 Application true - v141 + v142 Unicode Application false - v141 + v142 true Unicode Application true - v141 + v142 Unicode Application false - v141 + v142 true Unicode diff --git a/Lesson_9/Task.txt b/Lesson_9/Task.txt index 5de0df2..e21ceb9 100644 --- a/Lesson_9/Task.txt +++ b/Lesson_9/Task.txt @@ -4,6 +4,9 @@ 4. разнести классы по отдельным файлам на один класс .h и .cpp. 5. реализовать SellCar BuyUsedCar. 6. реализовать автошколу, которая в отдельном потоке будет производить водителей. -7. реализовать класс DriverManager. автошкола произведенного водителя отдает DriverManager-у. DriverManager для каждого нового водителя создает поток, в котором вызывается функция Go -8. Изменить реализацию функции Driver::Go так, чтобы после ее вызова, Driver периодически вызывал car_->Drive(). Если машины нет - Driver покупает БУ машину, еще через некоторое время покупает новую машину. У кого взять БУ машину может подсказать DriverManager. +7. реализовать класс DriverManager. автошкола произведенного водителя отдает DriverManager-у. + DriverManager для каждого нового водителя создает поток, в котором вызывается функция Go +8. Изменить реализацию функции Driver::Go так, чтобы после ее вызова, Driver периодически вызывал car_->Drive(). + Если машины нет - Driver покупает БУ машину, еще через некоторое время покупает новую машину. + У кого взять БУ машину может подсказать DriverManager. 9. Подумать какие проблемы могут возникнуть после продажи машины и как их решить. From 32c705d324abcae3a2891f542477e6efb0688a13 Mon Sep 17 00:00:00 2001 From: Boris Chernov Date: Tue, 9 Jun 2020 14:26:22 +0300 Subject: [PATCH 2/7] DriverManager, AutoSchool --- Lesson_9/SmartPointer/AutoSchool.cpp | 35 +++++++++++++++ Lesson_9/SmartPointer/AutoSchool.h | 17 +++++++ Lesson_9/SmartPointer/Car.h | 3 ++ Lesson_9/SmartPointer/Driver.cpp | 45 +++++++++++++++---- Lesson_9/SmartPointer/Driver.h | 7 ++- Lesson_9/SmartPointer/DriverManager.cpp | 43 ++++++++++++++++++ Lesson_9/SmartPointer/DriverManager.h | 17 +++++++ Lesson_9/SmartPointer/SmartPointer.cpp | 24 ++-------- Lesson_9/SmartPointer/SmartPointer.vcxproj | 6 ++- .../SmartPointer/SmartPointer.vcxproj.filters | 12 +++++ 10 files changed, 179 insertions(+), 30 deletions(-) create mode 100644 Lesson_9/SmartPointer/AutoSchool.cpp create mode 100644 Lesson_9/SmartPointer/AutoSchool.h create mode 100644 Lesson_9/SmartPointer/DriverManager.cpp create mode 100644 Lesson_9/SmartPointer/DriverManager.h diff --git a/Lesson_9/SmartPointer/AutoSchool.cpp b/Lesson_9/SmartPointer/AutoSchool.cpp new file mode 100644 index 0000000..f08761f --- /dev/null +++ b/Lesson_9/SmartPointer/AutoSchool.cpp @@ -0,0 +1,35 @@ +#include "AutoSchool.h" + +void StartSchool(AutoSchool* school) +{ + while (true) + { + Sleep(3000); + Driver* driver = new Driver("Bob #" + std::to_string(school->IncrProd()), school->GetFactory()); + DriverManager* manager = *school->GetManager(); + manager->AddDriver(driver); + } +} + +AutoSchool::AutoSchool(std::shared_ptr manager, std::shared_ptr factory) : + factory_(factory) +{ + DriverManager* tmp = &*manager; + manager_ = std::make_shared(tmp); + thread_ = std::make_unique(new std::thread(StartSchool, this)); +} + +std::shared_ptr AutoSchool::GetFactory() +{ + return factory_; +} + +std::shared_ptr AutoSchool::GetManager() +{ + return manager_; +} + +uint32_t AutoSchool::IncrProd() +{ + return ++produced; +} diff --git a/Lesson_9/SmartPointer/AutoSchool.h b/Lesson_9/SmartPointer/AutoSchool.h new file mode 100644 index 0000000..4c9165a --- /dev/null +++ b/Lesson_9/SmartPointer/AutoSchool.h @@ -0,0 +1,17 @@ +#pragma once +#include "DriverManager.h" + +class AutoSchool +{ +public: + AutoSchool(std::shared_ptr manager, std::shared_ptr factory); + std::shared_ptr GetFactory(); + std::shared_ptr GetManager(); + uint32_t IncrProd(); +private: + std::shared_ptr manager_; + std::shared_ptr factory_; + std::unique_ptr thread_; + uint32_t produced; +}; + diff --git a/Lesson_9/SmartPointer/Car.h b/Lesson_9/SmartPointer/Car.h index db1ee23..4134955 100644 --- a/Lesson_9/SmartPointer/Car.h +++ b/Lesson_9/SmartPointer/Car.h @@ -2,6 +2,9 @@ #include #include #include +#include +#include +#include class Car { diff --git a/Lesson_9/SmartPointer/Driver.cpp b/Lesson_9/SmartPointer/Driver.cpp index 93f183d..fb9f3f0 100644 --- a/Lesson_9/SmartPointer/Driver.cpp +++ b/Lesson_9/SmartPointer/Driver.cpp @@ -1,9 +1,42 @@ #include "Driver.h" +void Go(Driver* driver) +{ + while (true) + { + if (driver->GetCar() != nullptr) + { + std::cout << driver->GetName() << " I have a car "; + driver->GetCar()->Drive(); + } + else + { + std::cout << driver->GetName() << ": I'll go on foot\n"; + } + Sleep(1000); + } +} + Driver::Driver(const std::string& name, std::shared_ptr factory) : factory_(factory) , name_(name) { + work_ = false; +} + +Car* Driver::GetCar() +{ + return &*(car_); +} + +std::string Driver::GetName() +{ + return name_; +} + +std::thread* Driver::GetThread() +{ + return *thread_; } void Driver::BuyCar(const std::string& color) @@ -11,15 +44,11 @@ void Driver::BuyCar(const std::string& color) car_ = factory_->BuildCar(color); } -void Driver::Go() +void Driver::Start() { - if (car_ != nullptr) - { - std::cout << name_ << " I have a car "; - car_->Drive(); - } - else + if (!work_) { - std::cout << name_ << ": I'll go on foot\n"; + thread_ = std::make_unique(new std::thread(Go, this)); + work_ = true; } } diff --git a/Lesson_9/SmartPointer/Driver.h b/Lesson_9/SmartPointer/Driver.h index 41d7c9c..0bd82fd 100644 --- a/Lesson_9/SmartPointer/Driver.h +++ b/Lesson_9/SmartPointer/Driver.h @@ -6,15 +6,20 @@ class Driver public: Driver(const std::string& name, std::shared_ptr factory); + Car* GetCar(); + std::string GetName(); + std::thread* GetThread(); void BuyCar(const std::string& color); // SellCar // BuyUsedCar - void Go(); + void Start(); private: std::unique_ptr car_; std::shared_ptr factory_; std::string name_; + std::unique_ptr thread_; + bool work_; }; \ No newline at end of file diff --git a/Lesson_9/SmartPointer/DriverManager.cpp b/Lesson_9/SmartPointer/DriverManager.cpp new file mode 100644 index 0000000..7ec1ccf --- /dev/null +++ b/Lesson_9/SmartPointer/DriverManager.cpp @@ -0,0 +1,43 @@ +#include "DriverManager.h" + +void RunDriver(DriverManager* manager) +{ + while (true) + { + WaitForSingleObject(manager->GetEvent(), INFINITE); + manager->StartDriver(); + } +} + +DriverManager::DriverManager() +{ + g_event_ = CreateEvent( + NULL, + FALSE, + FALSE, + NULL + ); + thread_ = std::make_unique(new std::thread(RunDriver, this)); +} + +void DriverManager::AddDriver(Driver* driver) +{ + drivers_.push_back(driver); + SetEvent(g_event_); +} + +void DriverManager::StartDriver() +{ + drivers_.back()->Start(); +} + +std::thread* DriverManager::GetThread() +{ + return *thread_; +} + +HANDLE DriverManager::GetEvent() +{ + return g_event_; +} + diff --git a/Lesson_9/SmartPointer/DriverManager.h b/Lesson_9/SmartPointer/DriverManager.h new file mode 100644 index 0000000..a2d9ceb --- /dev/null +++ b/Lesson_9/SmartPointer/DriverManager.h @@ -0,0 +1,17 @@ +#pragma once +#include "Driver.h" + +class DriverManager +{ +public: + DriverManager(); + void AddDriver(Driver* driver); + void StartDriver(); + std::thread* GetThread(); + HANDLE GetEvent(); +private: + HANDLE g_event_; + std::vector drivers_; + std::unique_ptr thread_; +}; + diff --git a/Lesson_9/SmartPointer/SmartPointer.cpp b/Lesson_9/SmartPointer/SmartPointer.cpp index ff4de25..85cb6ff 100644 --- a/Lesson_9/SmartPointer/SmartPointer.cpp +++ b/Lesson_9/SmartPointer/SmartPointer.cpp @@ -1,27 +1,11 @@ -#include "Driver.h" +#include "AutoSchool.h" int main() { std::shared_ptr factory(new CarFactory()); - Driver driver1("Bob", factory); - driver1.Go(); - - driver1.BuyCar("red"); - driver1.Go(); - - driver1.BuyCar("blue"); - driver1.Go(); - - - Driver driver2("Sam", factory); - driver2.Go(); - - driver2.BuyCar("red"); - driver2.Go(); - - driver2.BuyCar("blue"); - driver2.Go(); - + std::shared_ptr manager(new DriverManager()); + std::unique_ptr school(new AutoSchool(manager, factory)); + Sleep(1000000); return 0; } \ No newline at end of file diff --git a/Lesson_9/SmartPointer/SmartPointer.vcxproj b/Lesson_9/SmartPointer/SmartPointer.vcxproj index 03511b6..655e936 100644 --- a/Lesson_9/SmartPointer/SmartPointer.vcxproj +++ b/Lesson_9/SmartPointer/SmartPointer.vcxproj @@ -1,4 +1,4 @@ - + @@ -151,15 +151,19 @@ + + + + diff --git a/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters b/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters index 32d5cc4..6ec974b 100644 --- a/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters +++ b/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters @@ -27,6 +27,12 @@ Source Files + + Source Files + + + Source Files + @@ -38,5 +44,11 @@ Header Files + + Header Files + + + Header Files + \ No newline at end of file From 27914870e1b34c795a9966eb4c964a8ed19a0680 Mon Sep 17 00:00:00 2001 From: Boris Chernov Date: Tue, 9 Jun 2020 19:07:04 +0300 Subject: [PATCH 3/7] update that dont work --- Lesson_9/SmartPointer/AutoSchool.cpp | 2 +- Lesson_9/SmartPointer/Car.h | 1 + Lesson_9/SmartPointer/Driver.cpp | 10 ++++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Lesson_9/SmartPointer/AutoSchool.cpp b/Lesson_9/SmartPointer/AutoSchool.cpp index f08761f..7ddce10 100644 --- a/Lesson_9/SmartPointer/AutoSchool.cpp +++ b/Lesson_9/SmartPointer/AutoSchool.cpp @@ -4,7 +4,7 @@ void StartSchool(AutoSchool* school) { while (true) { - Sleep(3000); + Sleep(5000); Driver* driver = new Driver("Bob #" + std::to_string(school->IncrProd()), school->GetFactory()); DriverManager* manager = *school->GetManager(); manager->AddDriver(driver); diff --git a/Lesson_9/SmartPointer/Car.h b/Lesson_9/SmartPointer/Car.h index 4134955..3555787 100644 --- a/Lesson_9/SmartPointer/Car.h +++ b/Lesson_9/SmartPointer/Car.h @@ -5,6 +5,7 @@ #include #include #include +#include class Car { diff --git a/Lesson_9/SmartPointer/Driver.cpp b/Lesson_9/SmartPointer/Driver.cpp index fb9f3f0..e68b67e 100644 --- a/Lesson_9/SmartPointer/Driver.cpp +++ b/Lesson_9/SmartPointer/Driver.cpp @@ -4,6 +4,7 @@ void Go(Driver* driver) { while (true) { + unsigned int start = clock(); if (driver->GetCar() != nullptr) { std::cout << driver->GetName() << " I have a car "; @@ -14,6 +15,15 @@ void Go(Driver* driver) std::cout << driver->GetName() << ": I'll go on foot\n"; } Sleep(1000); + unsigned int end = clock(); + if ((end - start) > 10000) + { + driver->BuyCar("(b/y)red"); + } + else if ((end - start) > 15000) + { + driver->BuyCar("green"); + } } } From 8e419d6b5c05b30a72231936fa65a706947e47c9 Mon Sep 17 00:00:00 2001 From: Boris Chernov Date: Tue, 9 Jun 2020 19:58:19 +0300 Subject: [PATCH 4/7] fix problems --- Lesson_9/SmartPointer/AutoSchool.cpp | 14 ++++++-------- Lesson_9/SmartPointer/AutoSchool.h | 6 +++--- Lesson_9/SmartPointer/Driver.cpp | 23 +++++++++++++---------- Lesson_9/SmartPointer/Driver.h | 17 +++++++++++++---- Lesson_9/SmartPointer/DriverManager.cpp | 9 ++------- Lesson_9/SmartPointer/DriverManager.h | 7 +++---- 6 files changed, 40 insertions(+), 36 deletions(-) diff --git a/Lesson_9/SmartPointer/AutoSchool.cpp b/Lesson_9/SmartPointer/AutoSchool.cpp index 7ddce10..aeaba7c 100644 --- a/Lesson_9/SmartPointer/AutoSchool.cpp +++ b/Lesson_9/SmartPointer/AutoSchool.cpp @@ -5,18 +5,16 @@ void StartSchool(AutoSchool* school) while (true) { Sleep(5000); - Driver* driver = new Driver("Bob #" + std::to_string(school->IncrProd()), school->GetFactory()); - DriverManager* manager = *school->GetManager(); - manager->AddDriver(driver); + std::shared_ptr driver = std::make_shared("Bob #" + std::to_string(school->IncrProd()), school->GetFactory()); + school->GetManager()->AddDriver(driver); } } AutoSchool::AutoSchool(std::shared_ptr manager, std::shared_ptr factory) : - factory_(factory) + factory_(factory), + manager_(manager) { - DriverManager* tmp = &*manager; - manager_ = std::make_shared(tmp); - thread_ = std::make_unique(new std::thread(StartSchool, this)); + thread_ = std::make_unique(StartSchool, this); } std::shared_ptr AutoSchool::GetFactory() @@ -24,7 +22,7 @@ std::shared_ptr AutoSchool::GetFactory() return factory_; } -std::shared_ptr AutoSchool::GetManager() +std::shared_ptr AutoSchool::GetManager() { return manager_; } diff --git a/Lesson_9/SmartPointer/AutoSchool.h b/Lesson_9/SmartPointer/AutoSchool.h index 4c9165a..9823a52 100644 --- a/Lesson_9/SmartPointer/AutoSchool.h +++ b/Lesson_9/SmartPointer/AutoSchool.h @@ -6,12 +6,12 @@ class AutoSchool public: AutoSchool(std::shared_ptr manager, std::shared_ptr factory); std::shared_ptr GetFactory(); - std::shared_ptr GetManager(); + std::shared_ptr GetManager(); uint32_t IncrProd(); private: - std::shared_ptr manager_; + std::shared_ptr manager_; std::shared_ptr factory_; - std::unique_ptr thread_; + std::unique_ptr thread_; uint32_t produced; }; diff --git a/Lesson_9/SmartPointer/Driver.cpp b/Lesson_9/SmartPointer/Driver.cpp index e68b67e..e7ee53e 100644 --- a/Lesson_9/SmartPointer/Driver.cpp +++ b/Lesson_9/SmartPointer/Driver.cpp @@ -2,9 +2,9 @@ void Go(Driver* driver) { + unsigned int start = clock(); while (true) { - unsigned int start = clock(); if (driver->GetCar() != nullptr) { std::cout << driver->GetName() << " I have a car "; @@ -16,13 +16,15 @@ void Go(Driver* driver) } Sleep(1000); unsigned int end = clock(); - if ((end - start) > 10000) + if ((end - start) > 5000 && *driver->GetWhatCar() == WhatCar::NoCar) { driver->BuyCar("(b/y)red"); + *driver->GetWhatCar() = WhatCar::BY; } - else if ((end - start) > 15000) + else if ((end - start) > 10000 && *driver->GetWhatCar() == WhatCar::BY) { driver->BuyCar("green"); + *driver->GetWhatCar() = WhatCar::New; } } } @@ -31,12 +33,13 @@ Driver::Driver(const std::string& name, std::shared_ptr factory) : factory_(factory) , name_(name) { + whatCar_ = WhatCar::NoCar; work_ = false; } -Car* Driver::GetCar() +std::shared_ptr Driver::GetCar() { - return &*(car_); + return car_; } std::string Driver::GetName() @@ -44,21 +47,21 @@ std::string Driver::GetName() return name_; } -std::thread* Driver::GetThread() +void Driver::BuyCar(const std::string& color) { - return *thread_; + car_ = factory_->BuildCar(color); } -void Driver::BuyCar(const std::string& color) +WhatCar* Driver::GetWhatCar() { - car_ = factory_->BuildCar(color); + return &whatCar_; } void Driver::Start() { if (!work_) { - thread_ = std::make_unique(new std::thread(Go, this)); + thread_ = std::make_unique(Go, this); work_ = true; } } diff --git a/Lesson_9/SmartPointer/Driver.h b/Lesson_9/SmartPointer/Driver.h index 0bd82fd..7acc0f6 100644 --- a/Lesson_9/SmartPointer/Driver.h +++ b/Lesson_9/SmartPointer/Driver.h @@ -1,15 +1,23 @@ #pragma once #include "CarFactory.h" +enum class WhatCar +{ + Error = 0, + NoCar = 1, + BY = 2, + New = 3 +}; + class Driver { public: Driver(const std::string& name, std::shared_ptr factory); - Car* GetCar(); + std::shared_ptr GetCar(); std::string GetName(); - std::thread* GetThread(); void BuyCar(const std::string& color); + WhatCar* GetWhatCar(); // SellCar // BuyUsedCar @@ -17,9 +25,10 @@ class Driver void Start(); private: - std::unique_ptr car_; + std::shared_ptr car_; std::shared_ptr factory_; std::string name_; - std::unique_ptr thread_; + std::unique_ptr thread_; + WhatCar whatCar_; bool work_; }; \ No newline at end of file diff --git a/Lesson_9/SmartPointer/DriverManager.cpp b/Lesson_9/SmartPointer/DriverManager.cpp index 7ec1ccf..49f02a1 100644 --- a/Lesson_9/SmartPointer/DriverManager.cpp +++ b/Lesson_9/SmartPointer/DriverManager.cpp @@ -17,10 +17,10 @@ DriverManager::DriverManager() FALSE, NULL ); - thread_ = std::make_unique(new std::thread(RunDriver, this)); + thread_ = std::make_unique(RunDriver, this); } -void DriverManager::AddDriver(Driver* driver) +void DriverManager::AddDriver(std::shared_ptr driver) { drivers_.push_back(driver); SetEvent(g_event_); @@ -31,11 +31,6 @@ void DriverManager::StartDriver() drivers_.back()->Start(); } -std::thread* DriverManager::GetThread() -{ - return *thread_; -} - HANDLE DriverManager::GetEvent() { return g_event_; diff --git a/Lesson_9/SmartPointer/DriverManager.h b/Lesson_9/SmartPointer/DriverManager.h index a2d9ceb..aec3579 100644 --- a/Lesson_9/SmartPointer/DriverManager.h +++ b/Lesson_9/SmartPointer/DriverManager.h @@ -5,13 +5,12 @@ class DriverManager { public: DriverManager(); - void AddDriver(Driver* driver); + void AddDriver(std::shared_ptr driver); void StartDriver(); - std::thread* GetThread(); HANDLE GetEvent(); private: HANDLE g_event_; - std::vector drivers_; - std::unique_ptr thread_; + std::vector> drivers_; + std::unique_ptr thread_; }; From b3336ca0fcecfa79e40a2a5b511a2e4788527b7a Mon Sep 17 00:00:00 2001 From: Boris Chernov Date: Tue, 9 Jun 2020 21:29:49 +0300 Subject: [PATCH 5/7] commit --- Lesson_9/SmartPointer/AutoSchool.cpp | 6 +- Lesson_9/SmartPointer/Car.h | 8 +- Lesson_9/SmartPointer/Driver.cpp | 89 ++++++++++++------- Lesson_9/SmartPointer/Driver.h | 17 ++-- Lesson_9/SmartPointer/DriverManager.cpp | 41 +++------ Lesson_9/SmartPointer/DriverManager.h | 9 +- Lesson_9/SmartPointer/SmartPointer.cpp | 5 ++ Lesson_9/SmartPointer/SmartPointer.vcxproj | 1 + .../SmartPointer/SmartPointer.vcxproj.filters | 11 ++- Lesson_9/SmartPointer/stdafx.h | 9 ++ 10 files changed, 106 insertions(+), 90 deletions(-) create mode 100644 Lesson_9/SmartPointer/stdafx.h diff --git a/Lesson_9/SmartPointer/AutoSchool.cpp b/Lesson_9/SmartPointer/AutoSchool.cpp index aeaba7c..dd22b6d 100644 --- a/Lesson_9/SmartPointer/AutoSchool.cpp +++ b/Lesson_9/SmartPointer/AutoSchool.cpp @@ -6,7 +6,11 @@ void StartSchool(AutoSchool* school) { Sleep(5000); std::shared_ptr driver = std::make_shared("Bob #" + std::to_string(school->IncrProd()), school->GetFactory()); - school->GetManager()->AddDriver(driver); + if (!driver->GetWorkStatus()) + { + std::shared_ptr thread = std::make_shared(&Driver::Start, driver); + school->GetManager()->AddDriver(driver, thread); + } } } diff --git a/Lesson_9/SmartPointer/Car.h b/Lesson_9/SmartPointer/Car.h index 3555787..cb88b70 100644 --- a/Lesson_9/SmartPointer/Car.h +++ b/Lesson_9/SmartPointer/Car.h @@ -1,11 +1,5 @@ #pragma once -#include -#include -#include -#include -#include -#include -#include +#include "stdafx.h" class Car { diff --git a/Lesson_9/SmartPointer/Driver.cpp b/Lesson_9/SmartPointer/Driver.cpp index e7ee53e..287ea8c 100644 --- a/Lesson_9/SmartPointer/Driver.cpp +++ b/Lesson_9/SmartPointer/Driver.cpp @@ -2,31 +2,7 @@ void Go(Driver* driver) { - unsigned int start = clock(); - while (true) - { - if (driver->GetCar() != nullptr) - { - std::cout << driver->GetName() << " I have a car "; - driver->GetCar()->Drive(); - } - else - { - std::cout << driver->GetName() << ": I'll go on foot\n"; - } - Sleep(1000); - unsigned int end = clock(); - if ((end - start) > 5000 && *driver->GetWhatCar() == WhatCar::NoCar) - { - driver->BuyCar("(b/y)red"); - *driver->GetWhatCar() = WhatCar::BY; - } - else if ((end - start) > 10000 && *driver->GetWhatCar() == WhatCar::BY) - { - driver->BuyCar("green"); - *driver->GetWhatCar() = WhatCar::New; - } - } + } Driver::Driver(const std::string& name, std::shared_ptr factory) @@ -35,11 +11,7 @@ Driver::Driver(const std::string& name, std::shared_ptr factory) { whatCar_ = WhatCar::NoCar; work_ = false; -} - -std::shared_ptr Driver::GetCar() -{ - return car_; + readyToSale_ = false; } std::string Driver::GetName() @@ -57,11 +29,62 @@ WhatCar* Driver::GetWhatCar() return &whatCar_; } +bool Driver::GetWorkStatus() +{ + return work_; +} + +bool Driver::IfReady() +{ + return readyToSale_; +} + +void Driver::ReadyToSell() +{ + carToSale_ = std::move(car_); + +} + +std::unique_ptr Driver::SellCar() +{ + return std::unique_ptr(carToSale_.release()); + BuyCar("green"); +} + +void Driver::BuyUsedCar(std::shared_ptr driver) +{ + if (driver != nullptr) + { + car_ = std::move(driver->SellCar()); + } +} + void Driver::Start() { - if (!work_) + work_ = true; + unsigned int start = clock(); + while (true) { - thread_ = std::make_unique(Go, this); - work_ = true; + unsigned int end = clock(); + if ((end - start) > 5000 && whatCar_ == WhatCar::NoCar) + { + this->BuyCar("(b/y)red"); + whatCar_ = WhatCar::BY; + } + else if ((end - start) > 10000 && whatCar_ == WhatCar::BY) + { + this->BuyCar("green"); + whatCar_ = WhatCar::New; + } + if (car_ != nullptr) + { + std::cout << name_ << " I have a car "; + car_->Drive(); + } + else + { + std::cout << name_ << ": I'll go on foot\n"; + } + Sleep(1000); } } diff --git a/Lesson_9/SmartPointer/Driver.h b/Lesson_9/SmartPointer/Driver.h index 7acc0f6..ef32586 100644 --- a/Lesson_9/SmartPointer/Driver.h +++ b/Lesson_9/SmartPointer/Driver.h @@ -13,22 +13,21 @@ class Driver { public: Driver(const std::string& name, std::shared_ptr factory); - - std::shared_ptr GetCar(); std::string GetName(); void BuyCar(const std::string& color); WhatCar* GetWhatCar(); - - // SellCar - // BuyUsedCar - + bool GetWorkStatus(); + bool IfReady(); + void ReadyToSell(); + std::unique_ptr SellCar(); + void BuyUsedCar(std::shared_ptr driver); void Start(); - private: - std::shared_ptr car_; + std::unique_ptr car_; + std::unique_ptr carToSale_; std::shared_ptr factory_; std::string name_; - std::unique_ptr thread_; WhatCar whatCar_; bool work_; + bool readyToSale_; }; \ No newline at end of file diff --git a/Lesson_9/SmartPointer/DriverManager.cpp b/Lesson_9/SmartPointer/DriverManager.cpp index 49f02a1..a8265d1 100644 --- a/Lesson_9/SmartPointer/DriverManager.cpp +++ b/Lesson_9/SmartPointer/DriverManager.cpp @@ -1,38 +1,19 @@ #include "DriverManager.h" -void RunDriver(DriverManager* manager) -{ - while (true) - { - WaitForSingleObject(manager->GetEvent(), INFINITE); - manager->StartDriver(); - } -} - -DriverManager::DriverManager() -{ - g_event_ = CreateEvent( - NULL, - FALSE, - FALSE, - NULL - ); - thread_ = std::make_unique(RunDriver, this); -} - -void DriverManager::AddDriver(std::shared_ptr driver) +void DriverManager::AddDriver(std::shared_ptr driver, std::shared_ptr thread) { drivers_.push_back(driver); - SetEvent(g_event_); -} - -void DriverManager::StartDriver() -{ - drivers_.back()->Start(); + driversthreads_.push_back(thread); } -HANDLE DriverManager::GetEvent() +std::shared_ptr DriverManager::WhoIsReadyToSale() { - return g_event_; + for (auto& driver : drivers_) + { + if (driver->IfReady()) + { + return driver; + } + } + return nullptr; } - diff --git a/Lesson_9/SmartPointer/DriverManager.h b/Lesson_9/SmartPointer/DriverManager.h index aec3579..0ab47e1 100644 --- a/Lesson_9/SmartPointer/DriverManager.h +++ b/Lesson_9/SmartPointer/DriverManager.h @@ -4,13 +4,10 @@ class DriverManager { public: - DriverManager(); - void AddDriver(std::shared_ptr driver); - void StartDriver(); - HANDLE GetEvent(); + void AddDriver(std::shared_ptr driver, std::shared_ptr thread); + std::shared_ptr WhoIsReadyToSale(); private: - HANDLE g_event_; std::vector> drivers_; - std::unique_ptr thread_; + std::vector> driversthreads_; }; diff --git a/Lesson_9/SmartPointer/SmartPointer.cpp b/Lesson_9/SmartPointer/SmartPointer.cpp index 85cb6ff..69a92ff 100644 --- a/Lesson_9/SmartPointer/SmartPointer.cpp +++ b/Lesson_9/SmartPointer/SmartPointer.cpp @@ -1,3 +1,8 @@ +#include "stdafx.h" +#include "Car.h" +#include "Driver.h" +#include "CarFactory.h" +#include "DriverManager.h" #include "AutoSchool.h" int main() diff --git a/Lesson_9/SmartPointer/SmartPointer.vcxproj b/Lesson_9/SmartPointer/SmartPointer.vcxproj index 655e936..bdb3ae8 100644 --- a/Lesson_9/SmartPointer/SmartPointer.vcxproj +++ b/Lesson_9/SmartPointer/SmartPointer.vcxproj @@ -164,6 +164,7 @@ + diff --git a/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters b/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters index 6ec974b..38641e0 100644 --- a/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters +++ b/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters @@ -27,10 +27,10 @@ Source Files - + Source Files - + Source Files @@ -44,10 +44,13 @@ Header Files - + Header Files - + + Header Files + + Header Files diff --git a/Lesson_9/SmartPointer/stdafx.h b/Lesson_9/SmartPointer/stdafx.h new file mode 100644 index 0000000..d45f486 --- /dev/null +++ b/Lesson_9/SmartPointer/stdafx.h @@ -0,0 +1,9 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include \ No newline at end of file From f23d20bac5dcd67f3da02f40e2304bf44cea4544 Mon Sep 17 00:00:00 2001 From: Boris Chernov Date: Thu, 11 Jun 2020 01:07:53 +0300 Subject: [PATCH 6/7] working on homework --- Lesson_9/SmartPointer/AutoSchool.cpp | 1 + Lesson_9/SmartPointer/Car.cpp | 1 + Lesson_9/SmartPointer/Car.h | 1 - Lesson_9/SmartPointer/CarFactory.cpp | 1 + Lesson_9/SmartPointer/Driver.cpp | 47 ++++++++++++------------- Lesson_9/SmartPointer/Driver.h | 3 -- Lesson_9/SmartPointer/DriverManager.cpp | 3 +- 7 files changed, 27 insertions(+), 30 deletions(-) diff --git a/Lesson_9/SmartPointer/AutoSchool.cpp b/Lesson_9/SmartPointer/AutoSchool.cpp index dd22b6d..1563bb9 100644 --- a/Lesson_9/SmartPointer/AutoSchool.cpp +++ b/Lesson_9/SmartPointer/AutoSchool.cpp @@ -1,3 +1,4 @@ +#include "stdafx.h" #include "AutoSchool.h" void StartSchool(AutoSchool* school) diff --git a/Lesson_9/SmartPointer/Car.cpp b/Lesson_9/SmartPointer/Car.cpp index dedf3f5..f2fa861 100644 --- a/Lesson_9/SmartPointer/Car.cpp +++ b/Lesson_9/SmartPointer/Car.cpp @@ -1,3 +1,4 @@ +#include "stdafx.h" #include "Car.h" Car::Car(const std::string& color) diff --git a/Lesson_9/SmartPointer/Car.h b/Lesson_9/SmartPointer/Car.h index cb88b70..ac83e5a 100644 --- a/Lesson_9/SmartPointer/Car.h +++ b/Lesson_9/SmartPointer/Car.h @@ -1,5 +1,4 @@ #pragma once -#include "stdafx.h" class Car { diff --git a/Lesson_9/SmartPointer/CarFactory.cpp b/Lesson_9/SmartPointer/CarFactory.cpp index 736a6cd..ef10158 100644 --- a/Lesson_9/SmartPointer/CarFactory.cpp +++ b/Lesson_9/SmartPointer/CarFactory.cpp @@ -1,3 +1,4 @@ +#include "stdafx.h" #include "CarFactory.h" std::unique_ptr CarFactory::BuildCar(const std::string& color) diff --git a/Lesson_9/SmartPointer/Driver.cpp b/Lesson_9/SmartPointer/Driver.cpp index 287ea8c..a712796 100644 --- a/Lesson_9/SmartPointer/Driver.cpp +++ b/Lesson_9/SmartPointer/Driver.cpp @@ -1,17 +1,15 @@ +#include "stdafx.h" #include "Driver.h" -void Go(Driver* driver) -{ - -} - Driver::Driver(const std::string& name, std::shared_ptr factory) : factory_(factory) , name_(name) { whatCar_ = WhatCar::NoCar; work_ = false; - readyToSale_ = false; + std::unique_ptr carToSale_ = nullptr; + std::unique_ptr car_ = nullptr; + Car* tmp = &*car_; } std::string Driver::GetName() @@ -21,6 +19,10 @@ std::string Driver::GetName() void Driver::BuyCar(const std::string& color) { + if (car_ != nullptr) + { + carToSale_ = std::move(car_); + } car_ = factory_->BuildCar(color); } @@ -34,26 +36,14 @@ bool Driver::GetWorkStatus() return work_; } -bool Driver::IfReady() -{ - return readyToSale_; -} - -void Driver::ReadyToSell() -{ - carToSale_ = std::move(car_); - -} - std::unique_ptr Driver::SellCar() { return std::unique_ptr(carToSale_.release()); - BuyCar("green"); } void Driver::BuyUsedCar(std::shared_ptr driver) { - if (driver != nullptr) + if (driver != NULL) { car_ = std::move(driver->SellCar()); } @@ -61,17 +51,24 @@ void Driver::BuyUsedCar(std::shared_ptr driver) void Driver::Start() { + uint32_t timeToByBY = 5000; + uint32_t timeToByNew = 10000; work_ = true; - unsigned int start = clock(); + uint32_t start = clock(); while (true) { - unsigned int end = clock(); - if ((end - start) > 5000 && whatCar_ == WhatCar::NoCar) + uint32_t end = clock(); + uint32_t curTimeWork = end - start; + if ((curTimeWork) > timeToByBY && (curTimeWork) < timeToByNew && whatCar_ == WhatCar::NoCar) { - this->BuyCar("(b/y)red"); - whatCar_ = WhatCar::BY; + std::shared_ptr driver; + if (driver != NULL) + { + this->BuyUsedCar(driver); + whatCar_ = WhatCar::BY; + } } - else if ((end - start) > 10000 && whatCar_ == WhatCar::BY) + else if ((curTimeWork) > timeToByNew && whatCar_ != WhatCar::New) { this->BuyCar("green"); whatCar_ = WhatCar::New; diff --git a/Lesson_9/SmartPointer/Driver.h b/Lesson_9/SmartPointer/Driver.h index ef32586..e1c16a4 100644 --- a/Lesson_9/SmartPointer/Driver.h +++ b/Lesson_9/SmartPointer/Driver.h @@ -17,8 +17,6 @@ class Driver void BuyCar(const std::string& color); WhatCar* GetWhatCar(); bool GetWorkStatus(); - bool IfReady(); - void ReadyToSell(); std::unique_ptr SellCar(); void BuyUsedCar(std::shared_ptr driver); void Start(); @@ -29,5 +27,4 @@ class Driver std::string name_; WhatCar whatCar_; bool work_; - bool readyToSale_; }; \ No newline at end of file diff --git a/Lesson_9/SmartPointer/DriverManager.cpp b/Lesson_9/SmartPointer/DriverManager.cpp index a8265d1..429620e 100644 --- a/Lesson_9/SmartPointer/DriverManager.cpp +++ b/Lesson_9/SmartPointer/DriverManager.cpp @@ -1,3 +1,4 @@ +#include "stdafx.h" #include "DriverManager.h" void DriverManager::AddDriver(std::shared_ptr driver, std::shared_ptr thread) @@ -10,7 +11,7 @@ std::shared_ptr DriverManager::WhoIsReadyToSale() { for (auto& driver : drivers_) { - if (driver->IfReady()) + if (driver->SellCar() != nullptr) { return driver; } From becfbf2979ad0575533a0c08e6ed551aecd4e795 Mon Sep 17 00:00:00 2001 From: Boris Chernov Date: Sat, 13 Jun 2020 01:15:13 +0300 Subject: [PATCH 7/7] Almost done --- Lesson_9/SmartPointer/AutoSchool.cpp | 18 ++++++++------ Lesson_9/SmartPointer/AutoSchool.h | 4 +++- Lesson_9/SmartPointer/Car.cpp | 11 +++++++-- Lesson_9/SmartPointer/Car.h | 2 ++ Lesson_9/SmartPointer/Driver.cpp | 35 +++++++++++++++++++--------- Lesson_9/SmartPointer/Driver.h | 9 ++++--- Lesson_9/SmartPointer/stdafx.h | 3 ++- 7 files changed, 57 insertions(+), 25 deletions(-) diff --git a/Lesson_9/SmartPointer/AutoSchool.cpp b/Lesson_9/SmartPointer/AutoSchool.cpp index 1563bb9..6be1144 100644 --- a/Lesson_9/SmartPointer/AutoSchool.cpp +++ b/Lesson_9/SmartPointer/AutoSchool.cpp @@ -6,12 +6,10 @@ void StartSchool(AutoSchool* school) while (true) { Sleep(5000); - std::shared_ptr driver = std::make_shared("Bob #" + std::to_string(school->IncrProd()), school->GetFactory()); - if (!driver->GetWorkStatus()) - { - std::shared_ptr thread = std::make_shared(&Driver::Start, driver); - school->GetManager()->AddDriver(driver, thread); - } + uint32_t tmp = school->IncrProd(); + std::shared_ptr driver = std::make_shared("Bob #" + std::to_string(tmp), school->GetFactory(), school->GetManager(), tmp - 1, school->GetMutex()); + std::shared_ptr thread = std::make_shared(&Driver::Start, driver); + school->GetManager()->AddDriver(driver, thread); } } @@ -19,6 +17,7 @@ AutoSchool::AutoSchool(std::shared_ptr manager, std::shared_ptr(); thread_ = std::make_unique(StartSchool, this); } @@ -32,7 +31,12 @@ std::shared_ptr AutoSchool::GetManager() return manager_; } +std::shared_ptr AutoSchool::GetMutex() +{ + return mutex_; +} + uint32_t AutoSchool::IncrProd() { - return ++produced; + return ++produced_; } diff --git a/Lesson_9/SmartPointer/AutoSchool.h b/Lesson_9/SmartPointer/AutoSchool.h index 9823a52..34dbe6e 100644 --- a/Lesson_9/SmartPointer/AutoSchool.h +++ b/Lesson_9/SmartPointer/AutoSchool.h @@ -7,11 +7,13 @@ class AutoSchool AutoSchool(std::shared_ptr manager, std::shared_ptr factory); std::shared_ptr GetFactory(); std::shared_ptr GetManager(); + std::shared_ptr GetMutex(); uint32_t IncrProd(); private: std::shared_ptr manager_; std::shared_ptr factory_; std::unique_ptr thread_; - uint32_t produced; + std::shared_ptr mutex_; + uint32_t produced_; }; diff --git a/Lesson_9/SmartPointer/Car.cpp b/Lesson_9/SmartPointer/Car.cpp index f2fa861..2e6880a 100644 --- a/Lesson_9/SmartPointer/Car.cpp +++ b/Lesson_9/SmartPointer/Car.cpp @@ -4,7 +4,8 @@ Car::Car(const std::string& color) : color_(color) { - std::cout << color_ << " car has been created\n"; + status_ = "new"; + //std::cout << status_ << " " << color_ << " car has been created\n"; } Car::~Car() @@ -12,7 +13,13 @@ Car::~Car() std::cout << color_ << " car has been destroied\n"; } +void Car::BecomeBY() +{ + std::string tmp = status_; + status_ = "BY"; +} + void Car::Drive() { - std::cout << color_ << " car in move\n"; + std::cout << status_ << " " << color_ << " car in move\n"; } \ No newline at end of file diff --git a/Lesson_9/SmartPointer/Car.h b/Lesson_9/SmartPointer/Car.h index ac83e5a..48b541e 100644 --- a/Lesson_9/SmartPointer/Car.h +++ b/Lesson_9/SmartPointer/Car.h @@ -6,8 +6,10 @@ class Car Car(const std::string& color); ~Car(); + void BecomeBY(); void Drive(); private: std::string color_; + std::string status_; }; diff --git a/Lesson_9/SmartPointer/Driver.cpp b/Lesson_9/SmartPointer/Driver.cpp index a712796..1137f3d 100644 --- a/Lesson_9/SmartPointer/Driver.cpp +++ b/Lesson_9/SmartPointer/Driver.cpp @@ -1,15 +1,17 @@ #include "stdafx.h" #include "Driver.h" +#include "DriverManager.h" -Driver::Driver(const std::string& name, std::shared_ptr factory) +Driver::Driver(const std::string& name, std::shared_ptr factory, std::shared_ptr manager, uint32_t id, std::shared_ptr mut) : factory_(factory) , name_(name) + , manager_(manager) + , id_(id) + , whatCar_(WhatCar::NoCar) + , mutex_(mut) { - whatCar_ = WhatCar::NoCar; - work_ = false; std::unique_ptr carToSale_ = nullptr; std::unique_ptr car_ = nullptr; - Car* tmp = &*car_; } std::string Driver::GetName() @@ -31,11 +33,6 @@ WhatCar* Driver::GetWhatCar() return &whatCar_; } -bool Driver::GetWorkStatus() -{ - return work_; -} - std::unique_ptr Driver::SellCar() { return std::unique_ptr(carToSale_.release()); @@ -46,6 +43,7 @@ void Driver::BuyUsedCar(std::shared_ptr driver) if (driver != NULL) { car_ = std::move(driver->SellCar()); + car_->BecomeBY(); } } @@ -53,15 +51,18 @@ void Driver::Start() { uint32_t timeToByBY = 5000; uint32_t timeToByNew = 10000; - work_ = true; + uint32_t timeToByNew2 = 15000; uint32_t start = clock(); + COORD coordinates; + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + coordinates.X = 0; while (true) { uint32_t end = clock(); uint32_t curTimeWork = end - start; if ((curTimeWork) > timeToByBY && (curTimeWork) < timeToByNew && whatCar_ == WhatCar::NoCar) { - std::shared_ptr driver; + std::shared_ptr driver = manager_->WhoIsReadyToSale(); if (driver != NULL) { this->BuyUsedCar(driver); @@ -73,14 +74,26 @@ void Driver::Start() this->BuyCar("green"); whatCar_ = WhatCar::New; } + else if ((curTimeWork) > timeToByNew2 && whatCar_ == WhatCar::New) + { + this->BuyCar("green"); + } if (car_ != nullptr) { + mutex_->lock(); + coordinates.Y = id_; + SetConsoleCursorPosition(hConsole, coordinates); std::cout << name_ << " I have a car "; car_->Drive(); + mutex_->unlock(); } else { + mutex_->lock(); + coordinates.Y = id_; + SetConsoleCursorPosition(hConsole, coordinates); std::cout << name_ << ": I'll go on foot\n"; + mutex_->unlock(); } Sleep(1000); } diff --git a/Lesson_9/SmartPointer/Driver.h b/Lesson_9/SmartPointer/Driver.h index e1c16a4..4bf76d7 100644 --- a/Lesson_9/SmartPointer/Driver.h +++ b/Lesson_9/SmartPointer/Driver.h @@ -1,6 +1,8 @@ #pragma once #include "CarFactory.h" +class DriverManager; + enum class WhatCar { Error = 0, @@ -12,11 +14,10 @@ enum class WhatCar class Driver { public: - Driver(const std::string& name, std::shared_ptr factory); + Driver(const std::string& name, std::shared_ptr factory, std::shared_ptr manager, uint32_t id, std::shared_ptr mut); std::string GetName(); void BuyCar(const std::string& color); WhatCar* GetWhatCar(); - bool GetWorkStatus(); std::unique_ptr SellCar(); void BuyUsedCar(std::shared_ptr driver); void Start(); @@ -24,7 +25,9 @@ class Driver std::unique_ptr car_; std::unique_ptr carToSale_; std::shared_ptr factory_; + std::shared_ptr manager_; + std::shared_ptr mutex_; std::string name_; + uint32_t id_; WhatCar whatCar_; - bool work_; }; \ No newline at end of file diff --git a/Lesson_9/SmartPointer/stdafx.h b/Lesson_9/SmartPointer/stdafx.h index d45f486..0939476 100644 --- a/Lesson_9/SmartPointer/stdafx.h +++ b/Lesson_9/SmartPointer/stdafx.h @@ -6,4 +6,5 @@ #include #include #include -#include \ No newline at end of file +#include +#include \ No newline at end of file