-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4512ac3
commit f5c766c
Showing
6 changed files
with
58 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "effective_c++/home/home_factory.h" | ||
|
||
using std::string; | ||
using namespace effective; | ||
|
||
std::shared_ptr<Home> HomeFactory::getHome(std::string type) { | ||
// std::weak_ptr::lock 会返回一个shared_ptr | ||
auto home_weak_ptr = cache[type]; | ||
auto home_shared_ptr = home_weak_ptr.lock(); | ||
if (!home_shared_ptr) { | ||
home_shared_ptr = makeHome(type); // unique_ptr可以赋值给shared_ptr | ||
cache[type] = home_shared_ptr; // shared_ptr可以赋值给weak_ptr | ||
} | ||
return home_shared_ptr; | ||
} | ||
|
||
int HomeFactory::getCacheSize() { return cache.size(); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
#include "effective_c++/home/nest.h" | ||
|
||
using namespace effective; | ||
using std::string; | ||
|
||
std::string Nest::doHealthValue() const { return "check health of full Nest"; } | ||
Nest::Nest(string address) : Home(address) {} | ||
|
||
std::string Nest::doDraw(int color) const { | ||
string Nest::doHealthValue() const { return "check health of full Nest"; } | ||
|
||
string Nest::doDraw(int color) const { | ||
if (color == 2) { | ||
return "let paint the house as Blue"; | ||
} | ||
return "bad color, can not paint."; | ||
} | ||
|
||
std::string Nest::onTick() const { return "My nest address is : " + address_; } | ||
string Nest::onTick() const { return "My nest address is : " + address_; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,28 @@ | ||
#include "effective_c++/home/home_factory.h" | ||
|
||
#include "doctest/doctest.h" | ||
#include "effective_c++/home/home.h" | ||
|
||
TEST_CASE("HomeFactory") { | ||
using namespace effective; | ||
auto home_factory = HomeFactory(); | ||
|
||
auto one_cave = home_factory.makeHome("Cave"); | ||
CHECK(one_cave->draw(3) == "let paint the house as Brown"); | ||
CHECK(one_cave->healthValue() == "before check health of full Cave after."); | ||
CHECK(one_cave->GetAddress("decorate ") == "decorate ParaMountain"); | ||
CHECK(one_cave->GetAddress() == "ParaMountain"); | ||
CHECK(one_cave->boardcastAddress() == "This from home begin send address Tick begin My cave address is : ParaMountainMy cave address is : ParaMountainMy cave address is : ParaMountainend."); | ||
|
||
auto one_nest = home_factory.makeHome("Nest"); | ||
CHECK(one_nest->draw(2) == "let paint the house as Blue"); | ||
CHECK(one_nest->healthValue() == "before check health of full Nest after."); | ||
CHECK(one_nest->GetAddress("decorate ") == "decorate "); | ||
CHECK(one_nest->boardcastAddress() == "This from home begin send address Tick begin My nest address is : My nest address is : My nest address is : end."); | ||
CHECK(one_nest->GetAddress("decorate ") == "decorate ParaNest"); | ||
CHECK(one_nest->boardcastAddress() == "This from home begin send address Tick begin My nest address is : ParaNestMy nest address is : ParaNestMy nest address is : ParaNestend."); | ||
|
||
CHECK(home_factory.getCacheSize() == 0); | ||
home_factory.getHome("Cave"); | ||
home_factory.getHome("Nest"); | ||
home_factory.getHome("Cave"); | ||
home_factory.getHome("Nest"); | ||
CHECK(home_factory.getCacheSize() == 2); | ||
} |