diff --git a/Lesson_9/SmartPointer/AutoSchool.cpp b/Lesson_9/SmartPointer/AutoSchool.cpp new file mode 100644 index 0000000..6be1144 --- /dev/null +++ b/Lesson_9/SmartPointer/AutoSchool.cpp @@ -0,0 +1,42 @@ +#include "stdafx.h" +#include "AutoSchool.h" + +void StartSchool(AutoSchool* school) +{ + while (true) + { + Sleep(5000); + 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); + } +} + +AutoSchool::AutoSchool(std::shared_ptr manager, std::shared_ptr factory) : + factory_(factory), + manager_(manager) +{ + mutex_ = std::make_shared(); + thread_ = std::make_unique(StartSchool, this); +} + +std::shared_ptr AutoSchool::GetFactory() +{ + return factory_; +} + +std::shared_ptr AutoSchool::GetManager() +{ + return manager_; +} + +std::shared_ptr AutoSchool::GetMutex() +{ + return mutex_; +} + +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..34dbe6e --- /dev/null +++ b/Lesson_9/SmartPointer/AutoSchool.h @@ -0,0 +1,19 @@ +#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(); + std::shared_ptr GetMutex(); + uint32_t IncrProd(); +private: + std::shared_ptr manager_; + std::shared_ptr factory_; + std::unique_ptr thread_; + std::shared_ptr mutex_; + uint32_t produced_; +}; + diff --git a/Lesson_9/SmartPointer/Car.cpp b/Lesson_9/SmartPointer/Car.cpp new file mode 100644 index 0000000..2e6880a --- /dev/null +++ b/Lesson_9/SmartPointer/Car.cpp @@ -0,0 +1,25 @@ +#include "stdafx.h" +#include "Car.h" + +Car::Car(const std::string& color) + : color_(color) +{ + status_ = "new"; + //std::cout << status_ << " " << color_ << " car has been created\n"; +} + +Car::~Car() +{ + std::cout << color_ << " car has been destroied\n"; +} + +void Car::BecomeBY() +{ + std::string tmp = status_; + status_ = "BY"; +} + +void Car::Drive() +{ + 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 new file mode 100644 index 0000000..48b541e --- /dev/null +++ b/Lesson_9/SmartPointer/Car.h @@ -0,0 +1,15 @@ +#pragma once + +class Car +{ +public: + Car(const std::string& color); + ~Car(); + + void BecomeBY(); + void Drive(); + +private: + std::string color_; + std::string status_; +}; diff --git a/Lesson_9/SmartPointer/CarFactory.cpp b/Lesson_9/SmartPointer/CarFactory.cpp new file mode 100644 index 0000000..ef10158 --- /dev/null +++ b/Lesson_9/SmartPointer/CarFactory.cpp @@ -0,0 +1,7 @@ +#include "stdafx.h" +#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..1137f3d --- /dev/null +++ b/Lesson_9/SmartPointer/Driver.cpp @@ -0,0 +1,100 @@ +#include "stdafx.h" +#include "Driver.h" +#include "DriverManager.h" + +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) +{ + std::unique_ptr carToSale_ = nullptr; + std::unique_ptr car_ = nullptr; +} + +std::string Driver::GetName() +{ + return name_; +} + +void Driver::BuyCar(const std::string& color) +{ + if (car_ != nullptr) + { + carToSale_ = std::move(car_); + } + car_ = factory_->BuildCar(color); +} + +WhatCar* Driver::GetWhatCar() +{ + return &whatCar_; +} + +std::unique_ptr Driver::SellCar() +{ + return std::unique_ptr(carToSale_.release()); +} + +void Driver::BuyUsedCar(std::shared_ptr driver) +{ + if (driver != NULL) + { + car_ = std::move(driver->SellCar()); + car_->BecomeBY(); + } +} + +void Driver::Start() +{ + uint32_t timeToByBY = 5000; + uint32_t timeToByNew = 10000; + 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 = manager_->WhoIsReadyToSale(); + if (driver != NULL) + { + this->BuyUsedCar(driver); + whatCar_ = WhatCar::BY; + } + } + else if ((curTimeWork) > timeToByNew && whatCar_ != WhatCar::New) + { + 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 new file mode 100644 index 0000000..4bf76d7 --- /dev/null +++ b/Lesson_9/SmartPointer/Driver.h @@ -0,0 +1,33 @@ +#pragma once +#include "CarFactory.h" + +class DriverManager; + +enum class WhatCar +{ + Error = 0, + NoCar = 1, + BY = 2, + New = 3 +}; + +class Driver +{ +public: + 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(); + std::unique_ptr SellCar(); + void BuyUsedCar(std::shared_ptr driver); + void Start(); +private: + 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_; +}; \ 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..429620e --- /dev/null +++ b/Lesson_9/SmartPointer/DriverManager.cpp @@ -0,0 +1,20 @@ +#include "stdafx.h" +#include "DriverManager.h" + +void DriverManager::AddDriver(std::shared_ptr driver, std::shared_ptr thread) +{ + drivers_.push_back(driver); + driversthreads_.push_back(thread); +} + +std::shared_ptr DriverManager::WhoIsReadyToSale() +{ + for (auto& driver : drivers_) + { + if (driver->SellCar() != nullptr) + { + return driver; + } + } + return nullptr; +} diff --git a/Lesson_9/SmartPointer/DriverManager.h b/Lesson_9/SmartPointer/DriverManager.h new file mode 100644 index 0000000..0ab47e1 --- /dev/null +++ b/Lesson_9/SmartPointer/DriverManager.h @@ -0,0 +1,13 @@ +#pragma once +#include "Driver.h" + +class DriverManager +{ +public: + void AddDriver(std::shared_ptr driver, std::shared_ptr thread); + std::shared_ptr WhoIsReadyToSale(); +private: + std::vector> drivers_; + std::vector> driversthreads_; +}; + diff --git a/Lesson_9/SmartPointer/SmartPointer.cpp b/Lesson_9/SmartPointer/SmartPointer.cpp index 3e3666f..69a92ff 100644 --- a/Lesson_9/SmartPointer/SmartPointer.cpp +++ b/Lesson_9/SmartPointer/SmartPointer.cpp @@ -1,93 +1,16 @@ -#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 "stdafx.h" +#include "Car.h" +#include "Driver.h" +#include "CarFactory.h" +#include "DriverManager.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 6150fc1..bdb3ae8 100644 --- a/Lesson_9/SmartPointer/SmartPointer.vcxproj +++ b/Lesson_9/SmartPointer/SmartPointer.vcxproj @@ -1,4 +1,4 @@ - + @@ -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,21 @@ + + + + + + + + + + + + + diff --git a/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters b/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters index 46986af..38641e0 100644 --- a/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters +++ b/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters @@ -18,5 +18,40 @@ Source Files + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/Lesson_9/SmartPointer/stdafx.h b/Lesson_9/SmartPointer/stdafx.h new file mode 100644 index 0000000..0939476 --- /dev/null +++ b/Lesson_9/SmartPointer/stdafx.h @@ -0,0 +1,10 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include \ 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. Подумать какие проблемы могут возникнуть после продажи машины и как их решить.