Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Lesson_9/SmartPointer/AutoSchool.cpp
Original file line number Diff line number Diff line change
@@ -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> driver = std::make_shared<Driver>("Bob #" + std::to_string(tmp), school->GetFactory(), school->GetManager(), tmp - 1, school->GetMutex());
std::shared_ptr<std::thread> thread = std::make_shared<std::thread>(&Driver::Start, driver);
school->GetManager()->AddDriver(driver, thread);
}
}

AutoSchool::AutoSchool(std::shared_ptr<DriverManager> manager, std::shared_ptr<CarFactory> factory) :
factory_(factory),
manager_(manager)
{
mutex_ = std::make_shared<std::mutex>();
thread_ = std::make_unique<std::thread>(StartSchool, this);
}

std::shared_ptr<CarFactory> AutoSchool::GetFactory()
{
return factory_;
}

std::shared_ptr<DriverManager> AutoSchool::GetManager()
{
return manager_;
}

std::shared_ptr<std::mutex> AutoSchool::GetMutex()
{
return mutex_;
}

uint32_t AutoSchool::IncrProd()
{
return ++produced_;
}
19 changes: 19 additions & 0 deletions Lesson_9/SmartPointer/AutoSchool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include "DriverManager.h"

class AutoSchool
{
public:
AutoSchool(std::shared_ptr<DriverManager> manager, std::shared_ptr<CarFactory> factory);
std::shared_ptr<CarFactory> GetFactory();
std::shared_ptr<DriverManager> GetManager();
std::shared_ptr<std::mutex> GetMutex();
uint32_t IncrProd();
private:
std::shared_ptr<DriverManager> manager_;
std::shared_ptr<CarFactory> factory_;
std::unique_ptr<std::thread> thread_;
std::shared_ptr<std::mutex> mutex_;
uint32_t produced_;
};

25 changes: 25 additions & 0 deletions Lesson_9/SmartPointer/Car.cpp
Original file line number Diff line number Diff line change
@@ -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";
}
15 changes: 15 additions & 0 deletions Lesson_9/SmartPointer/Car.h
Original file line number Diff line number Diff line change
@@ -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_;
};
7 changes: 7 additions & 0 deletions Lesson_9/SmartPointer/CarFactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "stdafx.h"
#include "CarFactory.h"

std::unique_ptr<Car> CarFactory::BuildCar(const std::string& color)
{
return std::unique_ptr<Car>(new Car(color));
}
8 changes: 8 additions & 0 deletions Lesson_9/SmartPointer/CarFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once
#include "Car.h"

class CarFactory
{
public:
std::unique_ptr<Car> BuildCar(const std::string& color);
};
100 changes: 100 additions & 0 deletions Lesson_9/SmartPointer/Driver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include "stdafx.h"
#include "Driver.h"
#include "DriverManager.h"

Driver::Driver(const std::string& name, std::shared_ptr<CarFactory> factory, std::shared_ptr<class DriverManager> manager, uint32_t id, std::shared_ptr<std::mutex> mut)
: factory_(factory)
, name_(name)
, manager_(manager)
, id_(id)
, whatCar_(WhatCar::NoCar)
, mutex_(mut)
{
std::unique_ptr<Car> carToSale_ = nullptr;
std::unique_ptr<Car> 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<Car> Driver::SellCar()
{
return std::unique_ptr<Car>(carToSale_.release());
}

void Driver::BuyUsedCar(std::shared_ptr<Driver> 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> 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);
}
}
33 changes: 33 additions & 0 deletions Lesson_9/SmartPointer/Driver.h
Original file line number Diff line number Diff line change
@@ -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<CarFactory> factory, std::shared_ptr<DriverManager> manager, uint32_t id, std::shared_ptr<std::mutex> mut);
std::string GetName();
void BuyCar(const std::string& color);
WhatCar* GetWhatCar();
std::unique_ptr<Car> SellCar();
void BuyUsedCar(std::shared_ptr<Driver> driver);
void Start();
private:
std::unique_ptr<Car> car_;
std::unique_ptr<Car> carToSale_;
std::shared_ptr<CarFactory> factory_;
std::shared_ptr<DriverManager> manager_;
std::shared_ptr<std::mutex> mutex_;
std::string name_;
uint32_t id_;
WhatCar whatCar_;
};
20 changes: 20 additions & 0 deletions Lesson_9/SmartPointer/DriverManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "stdafx.h"
#include "DriverManager.h"

void DriverManager::AddDriver(std::shared_ptr<Driver> driver, std::shared_ptr<std::thread> thread)
{
drivers_.push_back(driver);
driversthreads_.push_back(thread);
}

std::shared_ptr<Driver> DriverManager::WhoIsReadyToSale()
{
for (auto& driver : drivers_)
{
if (driver->SellCar() != nullptr)
{
return driver;
}
}
return nullptr;
}
13 changes: 13 additions & 0 deletions Lesson_9/SmartPointer/DriverManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
#include "Driver.h"

class DriverManager
{
public:
void AddDriver(std::shared_ptr<Driver> driver, std::shared_ptr<std::thread> thread);
std::shared_ptr<Driver> WhoIsReadyToSale();
private:
std::vector<std::shared_ptr<Driver>> drivers_;
std::vector<std::shared_ptr<std::thread>> driversthreads_;
};

95 changes: 9 additions & 86 deletions Lesson_9/SmartPointer/SmartPointer.cpp
Original file line number Diff line number Diff line change
@@ -1,93 +1,16 @@
#include <iostream>
#include <string>
#include <memory>

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<Car> BuildCar(const std::string& color)
{
return std::unique_ptr<Car>(new Car(color));
}
};

class Driver
{
public:
Driver(const std::string& name, std::shared_ptr<CarFactory> 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> car_;
std::shared_ptr<CarFactory> 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<CarFactory> 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<DriverManager> manager(new DriverManager());
std::unique_ptr<AutoSchool> school(new AutoSchool(manager, factory));

Sleep(1000000);
return 0;
}
Loading