Skip to content

Commit

Permalink
feat: add cache for home factory.
Browse files Browse the repository at this point in the history
  • Loading branch information
wgy8283335 committed Mar 2, 2024
1 parent 4512ac3 commit f5c766c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 8 deletions.
2 changes: 1 addition & 1 deletion include/effective_c++/Lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace effective {
private:
//> RAII类只需要一个共享指针成员变量,就可以实现reference-counting copying的行为。
std::shared_ptr<std::mutex> mutex_ptr_;
std::string* users; // 足够大的一个string数组,存储哪些调用者持有此mutex
std::string* users = new std::string[1]; // 足够大的一个string数组,存储哪些调用者持有此mutex
};
} // namespace effective
#endif // EFFECTIVE_LOCK_H_
18 changes: 17 additions & 1 deletion include/effective_c++/home/home_factory.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once
#include <memory>
#ifndef EFFECTIVE_HOME_FACTORY_H_
# define EFFECTIVE_HOME_FACTORY_H_
# include <iostream>
# include <string>
# include <unordered_map>

# include "effective_c++/home/cave.h"
# include "effective_c++/home/home.h"
Expand Down Expand Up @@ -37,12 +39,26 @@ namespace effective {
auto makeHome(std::string type) {
std::unique_ptr<Home, decltype(delHome)> result(nullptr, delHome);
if (type == "Nest") {
result.reset(new Nest());
result.reset(new Nest("ParaNest"));
} else if (type == "Cave") {
result.reset(new Cave("ParaMountain"));
}
return result;
}
/**
* @brief Get home by type from cache.
* @param type the type of the home.
* @return The return type is shared_ptr, it points to a home.
*/
std::shared_ptr<Home> getHome(std::string type);
/**
* @brief Get the cache size.
* @return The return the size of the cache.
*/
int getCacheSize();

private:
std::unordered_map<std::string, std::weak_ptr<Home>> cache;
};
} // namespace effective
#endif // EFFECTIVE_HOME_FACTORY_H_
6 changes: 6 additions & 0 deletions include/effective_c++/home/nest.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ namespace effective {
Nest& operator=(Nest&& right) = delete;
virtual ~Nest() = default;

/**
* @brief Construct the Nest object with address.
* @param name The address of the Home.
*/
explicit Nest(std::string address);

private:
virtual std::string doHealthValue() const override;
virtual std::string doDraw(int color) const override;
Expand Down
17 changes: 17 additions & 0 deletions source/effective_c++/home/home_factory.cc
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(); }
9 changes: 6 additions & 3 deletions source/effective_c++/home/nest.cc
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_; }
14 changes: 11 additions & 3 deletions test/source/home_factory_test.cpp
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);
}

0 comments on commit f5c766c

Please sign in to comment.