c++学习之通用句柄的使用

// private, abstract class acts as a base class for concrete query types
class Query_base {
    friend class Query;  Query 类的虚函数要访问Query_base,所以在Query_base 声明友元类
protected:
    typedef TextQuery::line_no line_no;
    virtual ~Query_base() { }
private:
    // eval returns the |set| of lines that this Query matches
    virtual std::set<line_no>  eval(const TextQuery&) const = 0; 含有纯虚函数即有抽象类。
    // display prints the query
    virtual std::ostream&  display(std::ostream& = std::cout) const = 0;虚函数构成多态:
什么是多态?
一个引用在不同情况下的多种状态,通过父类的指针来调用子类中实现的方法。
也叫动态绑定,必须通过基类的引用或者指针调用虚成员
};

//Query类为Query_base继承层次提供接口。保存Query_base指针和引用计数。这个指针可能指向的是
Query_base子类,
// handle class to manage the Query_base inheritance hierarchy
class Query {
    // these operators need access to the Query_base* constructor
    friend Query operator~(const Query &);//由于构造函数定义了成私有,这时加上友元,才能访问Query
    friend Query operator|(const Query&, const Query&);
    friend Query operator&(const Query&, const Query&);//调用时,Query("aa")&Query("bb")
    inline Query operator&(const Query &lhs, const Query &rhs) 分别创建不同的接口
{
    return new AndQuery(lhs, rhs);
}

inline Query operator|(const Query &lhs, const Query &rhs)
{
    return new OrQuery(lhs, rhs);
}

inline Query operator~(const Query &oper)
{
    return new NotQuery(oper);
}
public:
    Query(const std::string&);  // builds a new WordQuery
inline
Query::Query(const std::string &s): q(new WordQuery(s)),//q(new WordQuery(s)) 这句就使得Query成了封闭Query_base子类的接口
                               use(new std::size_t(1)) { }
    // copy control to manage pointers and use counting
    Query(const Query &c): q(c.q), use(c.use) { ++*use; }
    ~Query() { decr_use(); }
    Query& operator=(const Query&);

    // interface functions: will call corresponding Query_base operations
    std::set<TextQuery::line_no> eval(const TextQuery &t) const { return q->eval(t); }
    std::ostream &display(std::ostream &os) const{ return q->display(os); }//根据子类不同 动态绑定
private:
    Query(Query_base *query): q(query),use(new std::size_t(1)) { }
    Query_base *q;//这里不会存储基类指针,一般都是子类的
    std::size_t *use;
    void decr_use()
    { if (--*use == 0) { delete q; delete use; } }
};

inline Query& Query::operator=(const Query &rhs)
{
    ++*rhs.use; //有引用计数时,这个加1
    decr_use(); //由于自身的指针被覆盖,所以减1
    q = rhs.q;
    use = rhs.use;
    return *this;
}

inline std::ostream& operator<<(std::ostream &os, const Query &q)
{
    return q.display(os);
}

class WordQuery: public Query_base {
    friend class Query; // Query uses the WordQuery constructor,
    WordQuery(const std::string &s): query_word(s) { }默认protected类型

    // concrete class: WordQuery defines all inherited pure virtual functions
    std::set<line_no> eval(const TextQuery &t) const { return t.run_query(query_word); }
    std::ostream& display (std::ostream &os) const { return os << query_word; }
    std::string query_word;   // word for which to search
};

class NotQuery: public Query_base {
    friend Query operator~(const Query &);
    NotQuery(Query q): query(q) { }

    // concrete class: NotQuery defines all inherited pure virtual functions
    std::set<line_no> eval(const TextQuery&) const;
    std::ostream& display(std::ostream &os) const
          { return os << "~(" << query << ")"; }
    const Query query;
};

class BinaryQuery: public Query_base {
protected:
    BinaryQuery(Query left, Query right, std::string op):
          lhs(left), rhs(right), oper(op) { }

    // abstract class: BinaryQuery doesn't define eval
    std::ostream& display(std::ostream &os) const
    { return os << "(" << lhs  << " " << oper << " "
                       << rhs << ")"; }

    const Query lhs, rhs;   // right- and left-hand operands
    const std::string oper; // name of the operator
};
   
class AndQuery: public BinaryQuery {
    friend Query operator&(const Query&, const Query&);
    AndQuery(Query left, Query right):
                        BinaryQuery(left, right, "&") { }

    // concrete class: AndQuery inherits display and defines remaining pure virtual
    std::set<line_no> eval(const TextQuery&) const;
};

class OrQuery: public BinaryQuery {
    friend Query operator|(const Query&, const Query&);
    OrQuery(Query left, Query right):
                BinaryQuery(left, right, "|") { }

    // concrete class: OrQuery inherits display and defines remaining pure virtual
    std::set<line_no> eval(const TextQuery&) const;
};

 

std::ifstream& open_file(std::ifstream&, const std::string&);
TextQuery build_textfile(const std::string&);
bool get_word(std::string&);
bool get_words(std::string&, std::string&);
void print_results(const std::set<TextQuery::line_no>&, const TextQuery&);

#endif
// returns lines not in its operand's result set
set<TextQuery::line_no>
NotQuery::eval(const TextQuery& file) const
{
    // virtual call through the Query handle to eval
    set<TextQuery::line_no> has_val = query.eval(file);

    set<line_no> ret_lines;

    // for each line in the input file, check whether that line is in has_val
    // if not, add that line number to ret_lines
    for (TextQuery::line_no n = 0; n != file.size(); ++n)
        if (has_val.find(n) == has_val.end())
            ret_lines.insert(n);
    return ret_lines;
}

// returns intersection of its operands' result sets
set<TextQuery::line_no>
AndQuery::eval(const TextQuery& file) const
{
    // virtual calls through the Query handle to get result sets for the operands
    set<line_no> left = lhs.eval(file),
                 right = rhs.eval(file);

    set<line_no> ret_lines;  // destination to hold results

    // writes intersection of two ranges to a destination iterator
    // destination iterator in this call adds elements to ret
    set_intersection(left.begin(), left.end(),
                  right.begin(), right.end(),
                  inserter(ret_lines, ret_lines.begin()));
    return ret_lines;
}

// returns union of its operands' result sets
set<TextQuery::line_no>
OrQuery::eval(const TextQuery& file) const
{
    // virtual calls through the Query handle to get result sets for the operands
    set<line_no> right = rhs.eval(file),
             ret_lines = lhs.eval(file);  // destination to hold results

    // inserts the lines from right that aren't already in ret_lines
    ret_lines.insert(right.begin(), right.end());

    return ret_lines;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值