首页 > 其他分享 >CPP2nd CRTP Facade 模式

CPP2nd CRTP Facade 模式

时间:2022-11-23 04:22:06浏览次数:83  
标签:std const derived iter CPP2nd Facade return CRTP type

书中源码不全,看看我这个?

#include <iostream>
#include <type_traits>
#include <vector>
#include <iterator>
template<typename Derived, typename Value, typename Category, typename Distance = std::ptrdiff_t>
struct IteratorFacade{
    using value_type = std::remove_cvref_t<Value>;
    using reference = value_type& ;
    using pointer = value_type*;
    using difference = Distance;
    using iterator_category = Category;

    reference operator*() const { return as_derived().dereference(); }

    Derived& operator++() {
        as_derived().increment();
        return as_derived();
    }

    Derived operator++(int){
        Derived ret(as_derived());
        as_derived().increment();
        return ret;
    }

    friend bool operator==(const IteratorFacade& lhs, const IteratorFacade& rhs) {
        return lhs.as_derived().equals(rhs.as_derived());
    }

    friend bool operator!=(const IteratorFacade& lhs, const IteratorFacade& rhs) {
        return !operator==(lhs, rhs);
    }




private:
    Derived& as_derived() { return *static_cast<Derived*>(this); }
    const Derived& as_derived() const { return *static_cast<const Derived*>(this); }
};


struct Person{
    std::string firstName;
    std::string lastName;
};


template<typename Iterator, typename T>
struct ProjectIterator: public IteratorFacade<ProjectIterator<Iterator, T>,
        T,
        typename std::iterator_traits<Iterator>::iterator_category,
        typename std::iterator_traits<Iterator>::difference_type>{


    using iter_value_type = typename std::iterator_traits<Iterator>::value_type;

    Iterator iter;
    T iter_value_type::* member; ;

    void increment(){
        ++iter;
    }
    void decrement () {
        --iter;
    }
    bool equals( ProjectIterator const& other) const {
        return iter == other.iter;
    }

    T &dereference(){ return  (*iter ).*member; }
    T &dereference() const { return  (*iter ).*member; }
    ProjectIterator(Iterator iter_v, T iter_value_type::* mem_v):iter(iter_v), member(mem_v) {}
};

template< typename Iterator, typename Base, typename T>
auto project( Iterator iter, T Base::* member)  {
    return ProjectIterator< Iterator,  T >(iter, member );
}

int main(){
    std::vector<Person> persons = {{"hello","world"},
                                   {"David","json"}

    };

    auto b = project( persons.begin(), &Person ::firstName );
    auto e = project(persons.end(), &Person::lastName);

    std::copy(b,e, std::ostream_iterator<std::string>(std::cout, "\n"));
}

Ref:

Cpp-Templates-2ed/11_inheritance.md at master · downdemo/Cpp-Templates-2ed (github.com)

标签:std,const,derived,iter,CPP2nd,Facade,return,CRTP,type
From: https://www.cnblogs.com/gearslogy/p/16917076.html

相关文章

  • CSharp: Facade Pattern in donet 6
     ///<summary>///外观模式FacadePattern///银行///</summary>publicclassBank{publicboolIsUntrustworthy(Custome......
  • RTCRtpSender - (void)setParameters:
    titleRTCRtpSender-(void)setParameters:(RTC_OBJC_TYPE(RTCRtpParameters)*)parameters{RTCRtpSender->RtpSenderBase:SetParametersRtpSenderBase->RtpSenderBase:......
  • Python: Facade Pattern
    DuFacade.pyimportosimportreimportthreading#外观模式FacadePatternclass_IgnitionSystem(object):@staticmethoddefproduce_spark():......
  • 设计模式 -- Facade(门面模式)
    门面模式(Facade)系统间耦合的复杂度对于客户系统和子系统之前存在很多的耦合的情况,如果不考虑设计的情况,那么会形成A方案的情况,系统的依赖严重,维护性大大降低。如果在......
  • CSharp: Facade Pattern in donet core 3
     ///<summary>///外观模式FacadePattern///geovindu,GeovinDueidt///机器人颜色///</summary>publicclassRobotColor{......
  • CSharp: Facade Patterns
     ///<summary>///SummarydescriptionforDBTable.///外观模式FacadePatterns///20220918///geovindu,GeovinDu,涂聚文///</summ......
  • 企业版idea编辑器的破解版安装教程+一些其它软件的安装(navicat+vscode+nodepad+Secure
    1、idea编辑器的安装,IDEA全称IntelliJIDEA,是用于java语言开发的集成环境(也可用于其他语言),IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手、代码自动......
  • java: Facade Patterns
     /***版权所有2022涂聚文有限公司*许可信息查看:*描述:*门面模式FacadePatterns*历史版本:JDK14.02*2022-09-12创建者geovindu*2022-09-12......