Skip to content

Commit

Permalink
subsitute std for boost
Browse files Browse the repository at this point in the history
  • Loading branch information
huangrt01 committed Oct 23, 2022
1 parent b9b67c9 commit 4cd2308
Showing 1 changed file with 41 additions and 48 deletions.
89 changes: 41 additions & 48 deletions thread/test/Factory.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#include <map>

#include <boost/bind.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>

#include "../Mutex.h"

#include <assert.h>
Expand Down Expand Up @@ -40,10 +35,10 @@ class StockFactory : boost::noncopyable
{
public:

boost::shared_ptr<Stock> get(const string& key)
std::shared_ptr<Stock> get(const string& key)
{
muduo::MutexLockGuard lock(mutex_);
boost::shared_ptr<Stock>& pStock = stocks_[key];
std::shared_ptr<Stock>& pStock = stocks_[key];
if (!pStock)
{
pStock.reset(new Stock(key));
Expand All @@ -54,7 +49,7 @@ class StockFactory : boost::noncopyable

private:
mutable muduo::MutexLock mutex_;
std::map<string, boost::shared_ptr<Stock> > stocks_;
std::map<string, std::shared_ptr<Stock> > stocks_;
};

}
Expand All @@ -65,11 +60,11 @@ namespace version2
class StockFactory : boost::noncopyable
{
public:
boost::shared_ptr<Stock> get(const string& key)
std::shared_ptr<Stock> get(const string& key)
{
boost::shared_ptr<Stock> pStock;
std::shared_ptr<Stock> pStock;
muduo::MutexLockGuard lock(mutex_);
boost::weak_ptr<Stock>& wkStock = stocks_[key];
std::weak_ptr<Stock>& wkStock = stocks_[key];
pStock = wkStock.lock();
if (!pStock)
{
Expand All @@ -81,7 +76,7 @@ class StockFactory : boost::noncopyable

private:
mutable muduo::MutexLock mutex_;
std::map<string, boost::weak_ptr<Stock> > stocks_;
std::map<string, std::weak_ptr<Stock> > stocks_;
};

}
Expand All @@ -93,16 +88,16 @@ class StockFactory : boost::noncopyable
{
public:

boost::shared_ptr<Stock> get(const string& key)
std::shared_ptr<Stock> get(const string& key)
{
boost::shared_ptr<Stock> pStock;
std::shared_ptr<Stock> pStock;
muduo::MutexLockGuard lock(mutex_);
boost::weak_ptr<Stock>& wkStock = stocks_[key];
std::weak_ptr<Stock>& wkStock = stocks_[key];
pStock = wkStock.lock();
if (!pStock)
{
pStock.reset(new Stock(key),
boost::bind(&StockFactory::deleteStock, this, _1));
[this](Stock *ps) {this->deleteStock(ps);});
wkStock = pStock;
}
return pStock;
Expand All @@ -121,31 +116,29 @@ class StockFactory : boost::noncopyable
delete stock; // sorry, I lied
}
mutable muduo::MutexLock mutex_;
std::map<string, boost::weak_ptr<Stock> > stocks_;
std::map<string, std::weak_ptr<Stock> > stocks_;
};

}

namespace version4
{

class StockFactory : public boost::enable_shared_from_this<StockFactory>,
class StockFactory : public std::enable_shared_from_this<StockFactory>,
boost::noncopyable
{
public:

boost::shared_ptr<Stock> get(const string& key)
std::shared_ptr<Stock> get(const string& key)
{
boost::shared_ptr<Stock> pStock;
std::shared_ptr<Stock> pStock;
muduo::MutexLockGuard lock(mutex_);
boost::weak_ptr<Stock>& wkStock = stocks_[key];
std::weak_ptr<Stock>& wkStock = stocks_[key];
pStock = wkStock.lock();
if (!pStock)
{
pStock.reset(new Stock(key),
boost::bind(&StockFactory::deleteStock,
shared_from_this(),
_1));
[factory = shared_from_this()](Stock* stock) {factory->deleteStock(stock);});
wkStock = pStock;
}
return pStock;
Expand All @@ -164,38 +157,38 @@ class StockFactory : public boost::enable_shared_from_this<StockFactory>,
delete stock; // sorry, I lied
}
mutable muduo::MutexLock mutex_;
std::map<string, boost::weak_ptr<Stock> > stocks_;
std::map<string, std::weak_ptr<Stock> > stocks_;
};

}

class StockFactory : public boost::enable_shared_from_this<StockFactory>,
class StockFactory : public std::enable_shared_from_this<StockFactory>,
boost::noncopyable
{
public:
boost::shared_ptr<Stock> get(const string& key)
std::shared_ptr<Stock> get(const string& key)
{
boost::shared_ptr<Stock> pStock;
std::shared_ptr<Stock> pStock;
muduo::MutexLockGuard lock(mutex_);
boost::weak_ptr<Stock>& wkStock = stocks_[key];
std::weak_ptr<Stock>& wkStock = stocks_[key];
pStock = wkStock.lock();
if (!pStock)
{
pStock.reset(new Stock(key),
boost::bind(&StockFactory::weakDeleteCallback,
boost::weak_ptr<StockFactory>(shared_from_this()),
_1));
[wkFactory = std::weak_ptr<StockFactory>(shared_from_this())] (Stock* stock) {
StockFactory::weakDeleteCallback(wkFactory, stock);
});
wkStock = pStock;
}
return pStock;
}

private:
static void weakDeleteCallback(const boost::weak_ptr<StockFactory>& wkFactory,
static void weakDeleteCallback(const std::weak_ptr<StockFactory>& wkFactory,
Stock* stock)
{
printf("weakDeleteStock[%p]\n", stock);
boost::shared_ptr<StockFactory> factory(wkFactory.lock());
std::shared_ptr<StockFactory> factory(wkFactory.lock());
if (factory)
{
factory->removeStock(stock);
Expand All @@ -222,15 +215,15 @@ class StockFactory : public boost::enable_shared_from_this<StockFactory>,

private:
mutable muduo::MutexLock mutex_;
std::map<string, boost::weak_ptr<Stock> > stocks_;
std::map<string, std::weak_ptr<Stock> > stocks_;
};

void testLongLifeFactory()
{
boost::shared_ptr<StockFactory> factory(new StockFactory);
std::shared_ptr<StockFactory> factory(new StockFactory);
{
boost::shared_ptr<Stock> stock = factory->get("NYSE:IBM");
boost::shared_ptr<Stock> stock2 = factory->get("NYSE:IBM");
std::shared_ptr<Stock> stock = factory->get("NYSE:IBM");
std::shared_ptr<Stock> stock2 = factory->get("NYSE:IBM");
assert(stock == stock2);
// stock destructs here
}
Expand All @@ -239,11 +232,11 @@ void testLongLifeFactory()

void testShortLifeFactory()
{
boost::shared_ptr<Stock> stock;
std::shared_ptr<Stock> stock;
{
boost::shared_ptr<StockFactory> factory(new StockFactory);
std::shared_ptr<StockFactory> factory(new StockFactory);
stock = factory->get("NYSE:IBM");
boost::shared_ptr<Stock> stock2 = factory->get("NYSE:IBM");
std::shared_ptr<Stock> stock2 = factory->get("NYSE:IBM");
assert(stock == stock2);
// factory destructs here
}
Expand All @@ -255,27 +248,27 @@ int main()
version1::StockFactory sf1;
version2::StockFactory sf2;
version3::StockFactory sf3;
boost::shared_ptr<version3::StockFactory> sf4(new version3::StockFactory);
boost::shared_ptr<StockFactory> sf5(new StockFactory);
std::shared_ptr<version3::StockFactory> sf4(new version3::StockFactory);
std::shared_ptr<StockFactory> sf5(new StockFactory);

{
boost::shared_ptr<Stock> s1 = sf1.get("stock1");
std::shared_ptr<Stock> s1 = sf1.get("stock1");
}

{
boost::shared_ptr<Stock> s2 = sf2.get("stock2");
std::shared_ptr<Stock> s2 = sf2.get("stock2");
}

{
boost::shared_ptr<Stock> s3 = sf3.get("stock3");
std::shared_ptr<Stock> s3 = sf3.get("stock3");
}

{
boost::shared_ptr<Stock> s4 = sf4->get("stock4");
std::shared_ptr<Stock> s4 = sf4->get("stock4");
}

{
boost::shared_ptr<Stock> s5 = sf5->get("stock5");
std::shared_ptr<Stock> s5 = sf5->get("stock5");
}

testLongLifeFactory();
Expand Down

0 comments on commit 4cd2308

Please sign in to comment.