diff --git a/Lesson_9/SmartPointer/Car.cpp b/Lesson_9/SmartPointer/Car.cpp new file mode 100644 index 0000000..e28d8e4 --- /dev/null +++ b/Lesson_9/SmartPointer/Car.cpp @@ -0,0 +1,15 @@ + +#include "stdafx.h" +#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\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..697908a --- /dev/null +++ b/Lesson_9/SmartPointer/Car.h @@ -0,0 +1,14 @@ +#pragma once + + +class Car +{ +public: + Car(const std::string& color); + ~Car(); + void Drive(); + void GetCarColor(); +private: + std::string color_; +}; + diff --git a/Lesson_9/SmartPointer/CarFactory.cpp b/Lesson_9/SmartPointer/CarFactory.cpp new file mode 100644 index 0000000..61bcfcb --- /dev/null +++ b/Lesson_9/SmartPointer/CarFactory.cpp @@ -0,0 +1,9 @@ + +#include "stdafx.h" +#include "CarFactory.h" +#include "Car.h" + +std::unique_ptr CarFactory::BuildCar(const std::string& color) +{ + return std::unique_ptr(new Car(color)); +} \ No newline at end of file diff --git a/Lesson_9/SmartPointer/CarFactory.h b/Lesson_9/SmartPointer/CarFactory.h new file mode 100644 index 0000000..f303c58 --- /dev/null +++ b/Lesson_9/SmartPointer/CarFactory.h @@ -0,0 +1,10 @@ +#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..c26a2c8 --- /dev/null +++ b/Lesson_9/SmartPointer/Driver.cpp @@ -0,0 +1,106 @@ + +#include "stdafx.h" +#include "Driver.h" +#include "DriverManager.h" +#include "Car.h" +#include "CarFactory.h" +#include "autoschool.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); + std::cout << "I bought a new car\n"; + myManager_->GetPtrDriverWithCar(this); +} + +void Driver::cleverGo() +{ + //int whatToDo = rand() % 3 + 1;//dont work with threads + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution<> distrib(1, 2); + int random_number = distrib(gen); + + if (car_ != nullptr) //if driver has car - drive + { + std::cout << name_ << " have a car"; + car_->Drive(); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + } + else + { + std::cout << name_ << "'ll go on foot.... BUT...."; + switch (random_number) + { + case 1: //buy a new car + { + this->BuyCar(this->SetCarColor()); + std::cout << name_ << " have a car"; + car_->Drive(); + break; + } + case 2://try to buy used car + { + try + { + Driver* p_driverWithCar = myManager_->GivePtrDriverWithCar(); + this->BuyUsedCar(p_driverWithCar); + //this->BuyUsedCar(myManager_->GivePtrDriverWithCar()); //same as the two lines above + } + catch (const std::runtime_error& e) + { + std::cout << e.what(); + } + break; + } + + } + } +} +void Driver::BuyUsedCar(Driver* d) +{ + car_ = d->SellCar(); + std::cout << "I bought a used car\n"; + myManager_->GetPtrDriverWithCar(this); +} +std::unique_ptr Driver::SellCar() +{ + return std::unique_ptr (car_.release()); +} +void Driver::rememberMyManager(DriverManager* myM) +{ + this->myManager_ = myM; +} +DriverManager* Driver::sayNameMyManager() +{ + return this->myManager_; +} +void Driver::rememberMySchool(autoschool* myS) +{ + this->mySchool_ = myS; +} +autoschool* Driver::sayNameMySchool() +{ + return this->mySchool_; +} +Driver* Driver::giveDriverToBuyCar() +{ + return myManager_->GivePtrDriverWithCar(); +} +std::string Driver::SetCarColor() +{ + if (name_ == "Sergey") + return "green"; + else if (name_ == "Ivan") + return "blue"; + else if (name_ == "Sasha") + return "black"; + else if (name_ == "Masha") + return "red"; + else return "silver"; +} \ No newline at end of file diff --git a/Lesson_9/SmartPointer/Driver.h b/Lesson_9/SmartPointer/Driver.h new file mode 100644 index 0000000..42f1f93 --- /dev/null +++ b/Lesson_9/SmartPointer/Driver.h @@ -0,0 +1,32 @@ +#pragma once + +#include "CarFactory.h" +#include "Car.h" +class DriverManager; +class autoschool; + +class Driver +{ +public: + Driver(const std::string& name, std::shared_ptr factory); + void BuyCar(const std::string& color); + void BuyUsedCar(Driver* d); + std::unique_ptr SellCar(); + void Go(); + void cleverGo(); + std::vector canIbuyUsedCar(); + void rememberMyManager(DriverManager* myM); + DriverManager* sayNameMyManager(); + void rememberMySchool(autoschool* myS); + autoschool* sayNameMySchool(); + Driver* giveDriverToBuyCar(); + std::string SetCarColor(); + int intRand(const int& min, const int& max); +private: + std::unique_ptr car_; + std::shared_ptr factory_; + std::string name_; + DriverManager* myManager_; + autoschool* mySchool_; +}; + diff --git a/Lesson_9/SmartPointer/DriverManager.cpp b/Lesson_9/SmartPointer/DriverManager.cpp new file mode 100644 index 0000000..790bc1a --- /dev/null +++ b/Lesson_9/SmartPointer/DriverManager.cpp @@ -0,0 +1,66 @@ + +#include "stdafx.h" +#include "DriverManager.h" +#include "CarFactory.h" +#include "Driver.h" +#include "Car.h" +#include "autoschool.h" +int DriverManager::NumberOfManagers = 0; + +DriverManager::DriverManager(const std::string& nameManager, std::shared_ptr& factory) + : factory_(factory) + , nameManager_(nameManager) +{ +} +void DriverManager::GetOneDriver(std::unique_ptr CurrentDriver) +{ + DriversOwnedByManagers_.push_back(std::move(CurrentDriver)); +} +void DriverManager::ThreaFunctionManager(int DriverNumber) +{ + + int i = 4; + while (i) + { + DriversOwnedByManagers_[DriverNumber]->cleverGo(); + std::this_thread::sleep_for(std::chrono::milliseconds(4000)); + i--; + } +} +void DriverManager::startThread() +{ + std::vector threads; + for (unsigned currentDriverNumber = 0; currentDriverNumber < DriversOwnedByManagers_.size(); ++currentDriverNumber) + { + threads.push_back(std::thread(&DriverManager::ThreaFunctionManager, this, currentDriverNumber)); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + } + for (auto& t : threads) + { + t.join(); + } +} +void DriverManager::setFieldManagerToOnedDrivers(int k) +{ + DriversOwnedByManagers_[k]->rememberMyManager(this); +} +std::mutex mtx; +void DriverManager::GetPtrDriverWithCar(Driver* NewDriverWhitCar) +{ + std::lock_guard locked(mtx); + PtrDriversWithCar_.push_back(NewDriverWhitCar); +} +Driver* DriverManager::GivePtrDriverWithCar() +{ + std::lock_guard locked(mtx); + if (PtrDriversWithCar_.empty()) + { + throw std::runtime_error ("...tried to buy a used car but they aren’t\n"); + } + Driver* p_DriverWithCar = PtrDriversWithCar_.back(); + PtrDriversWithCar_.pop_back(); + return p_DriverWithCar; +} + + + diff --git a/Lesson_9/SmartPointer/DriverManager.h b/Lesson_9/SmartPointer/DriverManager.h new file mode 100644 index 0000000..444136e --- /dev/null +++ b/Lesson_9/SmartPointer/DriverManager.h @@ -0,0 +1,22 @@ +#pragma once +#include "Driver.h" +class autoschool; + +class DriverManager +{ +public: + DriverManager(const std::string& nameManager, std::shared_ptr& factory); + void GetOneDriver(std::unique_ptr CurrentDriver); + void startThread(); + void ThreaFunctionManager(int DriverNumber); + void setFieldManagerToOnedDrivers(int k); + void GetPtrDriverWithCar(Driver* NewDriverWhitCar); + Driver* GivePtrDriverWithCar(); + +private: + std::string nameManager_; + std::shared_ptr factory_; + std::vector> DriversOwnedByManagers_; + static int NumberOfManagers;//TODO подумать что с этим решить + std::vector PtrDriversWithCar_; +}; diff --git a/Lesson_9/SmartPointer/SmartPointer.cpp b/Lesson_9/SmartPointer/SmartPointer.cpp index 3e3666f..4092574 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 "Driver.h" +#include "CarFactory.h" +#include "Car.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 factory(new CarFactory()); + autoschool mySchool(5, 2, "Ivan",factory); + std::thread thp(&autoschool::threadfuct, &mySchool); + thp.join(); return 0; } \ No newline at end of file diff --git a/Lesson_9/SmartPointer/SmartPointer.vcxproj b/Lesson_9/SmartPointer/SmartPointer.vcxproj index 6150fc1..8477a7f 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,21 @@ + + + + + + + + + + + + + diff --git a/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters b/Lesson_9/SmartPointer/SmartPointer.vcxproj.filters index 46986af..74f6a23 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/autoschool.cpp b/Lesson_9/SmartPointer/autoschool.cpp new file mode 100644 index 0000000..0632424 --- /dev/null +++ b/Lesson_9/SmartPointer/autoschool.cpp @@ -0,0 +1,38 @@ +#pragma once +#include "stdafx.h" +#include "autoschool.h" +#include "Driver.h" +#include "CarFactory.h" +#include "Car.h" +#include "DriverManager.h" + +autoschool::autoschool(int numbersOfDrivers, int numberOfDriversInOwnedByOneManager, std::string name, std::shared_ptr& factory) + :factory_(factory), name_(name), numbersOfDrivers_(numbersOfDrivers), Manager_("stepan", factory_) +{} + const void autoschool::getName(const int& i) + { + if (i == 0) + name_ = "Sergey"; + else if (i == 1) + name_ = "Ivan"; + else if (i == 2) + name_ = "Sasha"; + else if (i == 4) + name_ = "Masha"; + else name_ = "Pasha"; + } + void autoschool::threadfuct() + { + for (int i = 0; i < numbersOfDrivers_; i++) //1) create Drivers: + { + getName(i); + DriversInAutoschool_.push_back(std::make_unique(name_, factory_)); + DriversInAutoschool_[i]->rememberMySchool(this);//TODO зачем ему знать школу?! скорее это не требуется + } + for (int i = 0; i < numbersOfDrivers_; i++) //2)Give all Drivers to one Manager: + { + Manager_.GetOneDriver(std::move(std::unique_ptr(DriversInAutoschool_[i].release()))); + Manager_.setFieldManagerToOnedDrivers(i); + } + Manager_.startThread();//3) run manager method + } \ No newline at end of file diff --git a/Lesson_9/SmartPointer/autoschool.h b/Lesson_9/SmartPointer/autoschool.h new file mode 100644 index 0000000..3aadb51 --- /dev/null +++ b/Lesson_9/SmartPointer/autoschool.h @@ -0,0 +1,17 @@ +#pragma once +#include "DriverManager.h" +#include "Driver.h" + +class autoschool +{ +public: + autoschool(int numbersOfDrivers, int numberOfDriversInOwnedByOneManager, std::string name, std::shared_ptr& factory); + void threadfuct(); + const void getName(const int& i); +private: + std::string name_; + int numbersOfDrivers_; + std::shared_ptr factory_; + std::vector> DriversInAutoschool_; + DriverManager Manager_; +}; \ 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..9476eff --- /dev/null +++ b/Lesson_9/SmartPointer/stdafx.h @@ -0,0 +1,13 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +//#include +//#include + 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