shared_ptr与weak_ptr的例子

12.20 编写程序,逐行读入一个输入文件,将内容存入一个StrBlob中,用一个StrBlobPtr打印出StrBlob的每个元素。

StrBlob.h

#ifndef STRBLOB_H
#define STRBLOB_H
#include<iostream>
#include<string>
#include<vector>
#include<memory>
using namespace std;
class StrBlobPtr;
class StrBlob
{
friend class StrBlobPtr;
public:
    typedef string::size_type size_type;
    //构造函数
    StrBlob();
    explicit StrBlob(initializer_list<string> il);

    size_type size() const { return data->size(); }
    bool empty() const { return data->empty();}
    void push_back(const string &t) { data->push_back(t);}

    void pop_back();
    string& front();
    string& back();
    string& front() const;
    string& back() const;
    size_type count()
    {
        return data.use_count();
    }
    StrBlobPtr begin();
    StrBlobPtr end();
private:
    shared_ptr<vector<string>> data;
    void check(size_type i,const string msg) const;
};
#endif // STRBLOB_H

StrBlob.cpp

#include"StrBlob.h"
#include"StrBlobPtr.h"
StrBlob::StrBlob():data(make_shared<vector<string>>())
{
}

StrBlob::StrBlob(initializer_list<string> il):data(make_shared<vector<string>>(il))
{
}

void StrBlob::pop_back()
{
    check(0,"pop_back");
    data->pop_back();
}

string& StrBlob::back()
{
    check(0,"back");
    return data->back();
}

string& StrBlob::front()
{
    check(0,"front");
    return data->front();
}

string& StrBlob::back() const
{
    check(0,"back");
    return data->back();
}

string& StrBlob::front() const
{
    check(0,"front");
    return data->front();
}
void StrBlob::check(size_type i, const string msg) const
{
    if(i>=data->size())
        throw out_of_range(msg);
}

StrBlobPtr StrBlob::begin()
{
    return StrBlobPtr(*this);
}

StrBlobPtr StrBlob::end()
{
    return StrBlobPtr(*this,data->size());
}

StrBlobPtr.h

#ifndef STRBLOBPTR_H
#define STRBLOBPTR_H
#include<string>
#include<vector>
#include<memory>
using namespace std;

class StrBlobPtr
{
public:
    StrBlobPtr():curr(0) {}
    StrBlobPtr(StrBlob &a,size_t sz=0):wptr(a.data),curr(sz) {}

    string& deref() const;
    StrBlobPtr& incr();
private:
    shared_ptr<vector<string>> check(size_t,const string &) const;
    weak_ptr<vector<string>> wptr;
    size_t curr;
};
#endif

StrBlobPtr.cpp

#include"StrBlob.h"
#include"StrBlobPtr.h"

shared_ptr<vector<string>> StrBlobPtr::check(size_t i, const string& msg) const
{
    shared_ptr<vector<string>> ret=wptr.lock();
    if(!ret)
        throw runtime_error("unbound StrBlobPtr");
    if(i>=ret->size())
        throw out_of_range(msg);
    return ret;
}

string& StrBlobPtr::deref() const
{
    auto ret=check(curr,"deference");
    return (*ret)[curr];
}

StrBlobPtr& StrBlobPtr::incr()
{
    check(curr,"increment");
    ++curr;
    return *this;
}

useStrBlob.cpp

#include"StrBlob.h"
#include"StrBlobPtr.h"
#include<fstream>

int main()
{
    ifstream in("1.txt");
    StrBlob Str;
    StrBlobPtr StrP(Str);
    string tmp;
    while(getline(in,tmp))
    {
        Str.push_back(tmp);
    }
    size_t l=Str.size();
    while(l)
    {
        cout<<StrP.deref()<<endl;
        StrP.incr();
        --l;
    }
    return 0;
}

 

posted @ 2014-08-19 20:26  Jessica程序猿  阅读(589)  评论(0编辑  收藏  举报