Rolling back use of typedef with smart pointers.

This is to reduce the obfuscation of the code.
This commit is contained in:
Ian Dinwoodie
2019-05-18 10:12:01 -04:00
parent 176fe54642
commit 5a4975691f
7 changed files with 37 additions and 48 deletions

View File

@@ -37,7 +37,6 @@ class Hunter
class WildDog class WildDog
{ {
public: public:
typedef std::shared_ptr<WildDog> ptr_t;
void bark(void) void bark(void)
{ {
std::cout << "*wild dog bark*" << std::endl; std::cout << "*wild dog bark*" << std::endl;
@@ -47,7 +46,7 @@ class WildDog
class WildDogAdapter : public Lion class WildDogAdapter : public Lion
{ {
public: public:
WildDogAdapter(WildDog::ptr_t dog) WildDogAdapter(std::shared_ptr<WildDog> dog)
: dog_(dog) : dog_(dog)
{ {
} }
@@ -58,12 +57,12 @@ class WildDogAdapter : public Lion
} }
private: private:
WildDog::ptr_t dog_; std::shared_ptr<WildDog> dog_;
}; };
int main() int main()
{ {
WildDog::ptr_t wildDog = std::make_shared<WildDog>(); std::shared_ptr<WildDog> wildDog = std::make_shared<WildDog>();
WildDogAdapter wildDogAdapter(wildDog); WildDogAdapter wildDogAdapter(wildDog);
Hunter hunter; Hunter hunter;

View File

@@ -5,7 +5,6 @@
class Theme class Theme
{ {
public: public:
typedef std::shared_ptr<Theme> ptr_t;
virtual std::string getColor(void) = 0; virtual std::string getColor(void) = 0;
}; };
@@ -18,7 +17,7 @@ class WebPage
class About : public WebPage class About : public WebPage
{ {
public: public:
About(Theme::ptr_t theme) About(std::shared_ptr<Theme> theme)
: theme_(theme) : theme_(theme)
{ {
} }
@@ -29,13 +28,13 @@ class About : public WebPage
} }
private: private:
Theme::ptr_t theme_; std::shared_ptr<Theme> theme_;
}; };
class Projects : public WebPage class Projects : public WebPage
{ {
public: public:
Projects(Theme::ptr_t theme) Projects(std::shared_ptr<Theme> theme)
: theme_(theme) : theme_(theme)
{ {
} }
@@ -46,14 +45,14 @@ class Projects : public WebPage
} }
private: private:
Theme::ptr_t theme_; std::shared_ptr<Theme> theme_;
}; };
class Careers : public WebPage class Careers : public WebPage
{ {
public: public:
Careers(Theme::ptr_t theme) Careers(std::shared_ptr<Theme> theme)
: theme_(theme) : theme_(theme)
{ {
} }
@@ -64,7 +63,7 @@ class Careers : public WebPage
} }
private: private:
Theme::ptr_t theme_; std::shared_ptr<Theme> theme_;
}; };
class DarkTheme : public Theme class DarkTheme : public Theme
@@ -96,7 +95,7 @@ class AquaTheme : public Theme
int main() int main()
{ {
Theme::ptr_t darkTheme = std::make_shared<DarkTheme>(); std::shared_ptr<Theme> darkTheme = std::make_shared<DarkTheme>();
About about(darkTheme); About about(darkTheme);
Careers careers(darkTheme); Careers careers(darkTheme);

View File

@@ -6,7 +6,6 @@
class Employee class Employee
{ {
public: public:
typedef std::shared_ptr<Employee> ptr_t;
virtual std::string getName(void) = 0; virtual std::string getName(void) = 0;
virtual void setSalary(float salary) = 0; virtual void setSalary(float salary) = 0;
virtual float getSalary(void) = 0; virtual float getSalary(void) = 0;
@@ -84,7 +83,7 @@ class Designer : public Employee
class Organization class Organization
{ {
public: public:
void addEmployee(Employee::ptr_t employee) void addEmployee(std::shared_ptr<Employee> employee)
{ {
employees_.push_back(employee); employees_.push_back(employee);
} }
@@ -100,14 +99,14 @@ class Organization
} }
private: private:
std::vector<Employee::ptr_t> employees_; std::vector<std::shared_ptr<Employee>> employees_;
}; };
int main() int main()
{ {
// Prepare the employees. // Prepare the employees.
Employee::ptr_t jane = std::make_shared<Developer>("Jane Doe", 50000); std::shared_ptr<Employee> jane = std::make_shared<Developer>("Jane", 50000);
Employee::ptr_t john = std::make_shared<Designer>("John Doe", 45000); std::shared_ptr<Employee> john = std::make_shared<Designer>("John", 45000);
// Add them to the organization. // Add them to the organization.
Organization org; Organization org;

View File

@@ -5,7 +5,6 @@
class Coffee class Coffee
{ {
public: public:
typedef std::shared_ptr<Coffee> ptr_t;
virtual float getPrice(void) = 0; virtual float getPrice(void) = 0;
virtual std::string getDescription(void) = 0; virtual std::string getDescription(void) = 0;
}; };
@@ -27,7 +26,7 @@ class SimpleCoffee : public Coffee
class MilkCoffee : public Coffee class MilkCoffee : public Coffee
{ {
public: public:
MilkCoffee(Coffee::ptr_t coffee) MilkCoffee(std::shared_ptr<Coffee> coffee)
: coffee_(coffee) : coffee_(coffee)
{ {
} }
@@ -43,13 +42,13 @@ class MilkCoffee : public Coffee
} }
private: private:
Coffee::ptr_t coffee_; std::shared_ptr<Coffee> coffee_;
}; };
class WhipCoffee : public Coffee class WhipCoffee : public Coffee
{ {
public: public:
WhipCoffee(Coffee::ptr_t coffee) WhipCoffee(std::shared_ptr<Coffee> coffee)
: coffee_(coffee) : coffee_(coffee)
{ {
} }
@@ -65,13 +64,13 @@ class WhipCoffee : public Coffee
} }
private: private:
Coffee::ptr_t coffee_; std::shared_ptr<Coffee> coffee_;
}; };
class VanillaCoffee : public Coffee class VanillaCoffee : public Coffee
{ {
public: public:
VanillaCoffee(Coffee::ptr_t coffee) VanillaCoffee(std::shared_ptr<Coffee> coffee)
: coffee_(coffee) : coffee_(coffee)
{ {
} }
@@ -87,30 +86,30 @@ class VanillaCoffee : public Coffee
} }
private: private:
Coffee::ptr_t coffee_; std::shared_ptr<Coffee> coffee_;
}; };
int main() int main()
{ {
Coffee::ptr_t simple = std::make_shared<SimpleCoffee>(); std::shared_ptr<Coffee> simple = std::make_shared<SimpleCoffee>();
// Output: 3 // Output: 3
std::cout << simple->getPrice() << std::endl; std::cout << simple->getPrice() << std::endl;
// Output: Simple coffee // Output: Simple coffee
std::cout << simple->getDescription() << std::endl; std::cout << simple->getDescription() << std::endl;
Coffee::ptr_t milk = std::make_shared<MilkCoffee>(simple); std::shared_ptr<Coffee> milk = std::make_shared<MilkCoffee>(simple);
// Output: 3.5 // Output: 3.5
std::cout << milk->getPrice() << std::endl; std::cout << milk->getPrice() << std::endl;
// Output: Simple coffee, milk // Output: Simple coffee, milk
std::cout << milk->getDescription() << std::endl; std::cout << milk->getDescription() << std::endl;
Coffee::ptr_t whip = std::make_shared<WhipCoffee>(milk); std::shared_ptr<Coffee> whip = std::make_shared<WhipCoffee>(milk);
// Output: 5.5 // Output: 5.5
std::cout << whip->getPrice() << std::endl; std::cout << whip->getPrice() << std::endl;
// Output: Simple coffee, milk, whip // Output: Simple coffee, milk, whip
std::cout << whip->getDescription() << std::endl; std::cout << whip->getDescription() << std::endl;
Coffee::ptr_t vanilla = std::make_shared<VanillaCoffee>(whip); std::shared_ptr<Coffee> vanilla = std::make_shared<VanillaCoffee>(whip);
// Output: 6.5 // Output: 6.5
std::cout << vanilla->getPrice() << std::endl; std::cout << vanilla->getPrice() << std::endl;
// Output: Simple coffee, milk, whip, vanilla // Output: Simple coffee, milk, whip, vanilla

View File

@@ -4,8 +4,6 @@
class Computer class Computer
{ {
public: public:
typedef std::shared_ptr<Computer> ptr_t;
void makeBootSound(void) void makeBootSound(void)
{ {
std::cout << "Beep!" << std::endl; std::cout << "Beep!" << std::endl;
@@ -34,7 +32,7 @@ class Computer
class ComputerFacade class ComputerFacade
{ {
public: public:
ComputerFacade(Computer::ptr_t computer) ComputerFacade(std::shared_ptr<Computer> computer)
: computer_(computer) : computer_(computer)
{ {
} }
@@ -53,12 +51,12 @@ class ComputerFacade
} }
private: private:
Computer::ptr_t computer_; std::shared_ptr<Computer> computer_;
}; };
int main() int main()
{ {
Computer::ptr_t computer = std::make_shared<Computer>(); std::shared_ptr<Computer> computer = std::make_shared<Computer>();
ComputerFacade facade(computer); ComputerFacade facade(computer);
// Output: // Output:

View File

@@ -2,18 +2,14 @@
#include <memory> #include <memory>
#include <unordered_map> #include <unordered_map>
class Tea struct Tea
{ {
public:
typedef std::shared_ptr<Tea> ptr_t;
}; };
class TeaMaker class TeaMaker
{ {
public: public:
typedef std::shared_ptr<TeaMaker> ptr_t; std::shared_ptr<Tea> make(const std::string& preference)
Tea::ptr_t make(const std::string& preference)
{ {
auto match = availableTea_.find(preference); auto match = availableTea_.find(preference);
if (match == availableTea_.end()) { if (match == availableTea_.end()) {
@@ -29,13 +25,13 @@ class TeaMaker
} }
private: private:
std::unordered_map<std::string, Tea::ptr_t> availableTea_; std::unordered_map<std::string, std::shared_ptr<Tea>> availableTea_;
}; };
class TeaShop class TeaShop
{ {
public: public:
TeaShop(TeaMaker::ptr_t maker) TeaShop(std::shared_ptr<TeaMaker> maker)
: maker_(maker) : maker_(maker)
{ {
} }
@@ -58,13 +54,13 @@ class TeaShop
} }
private: private:
TeaMaker::ptr_t maker_; std::shared_ptr<TeaMaker> maker_;
std::unordered_map<int, Tea::ptr_t> orders_; std::unordered_map<int, std::shared_ptr<Tea>> orders_;
}; };
int main() int main()
{ {
TeaMaker::ptr_t maker = std::make_shared<TeaMaker>(); std::shared_ptr<TeaMaker> maker = std::make_shared<TeaMaker>();
TeaShop shop(maker); TeaShop shop(maker);
// No orders have been taken, so there are no available teas. // No orders have been taken, so there are no available teas.

View File

@@ -5,7 +5,6 @@
class Door class Door
{ {
public: public:
typedef std::shared_ptr<Door> ptr_t;
virtual void open(void) = 0; virtual void open(void) = 0;
virtual void close(void) = 0; virtual void close(void) = 0;
}; };
@@ -27,7 +26,7 @@ class LabDoor : public Door
class SecuredDoor class SecuredDoor
{ {
public: public:
SecuredDoor(Door::ptr_t door) SecuredDoor(std::shared_ptr<Door> door)
: door_(door) : door_(door)
{ {
} }
@@ -52,12 +51,12 @@ class SecuredDoor
return password == "Bond007"; return password == "Bond007";
} }
Door::ptr_t door_; std::shared_ptr<Door> door_;
}; };
int main() int main()
{ {
Door::ptr_t labDoor = std::make_shared<LabDoor>(); std::shared_ptr<Door> labDoor = std::make_shared<LabDoor>();
SecuredDoor securedDoor(labDoor); SecuredDoor securedDoor(labDoor);
securedDoor.open("invalid"); // Output: No way, Jose! securedDoor.open("invalid"); // Output: No way, Jose!