Ignore:
Timestamp:
Oct 19, 2020, 4:33:47 AM (12 months ago)
Author:
Peter
Message:

Implement move constructo and move-assignment operator for
DiscreteGeneral? (closes #964). Using swap idiom also for
copy-assignment as it gives stronger exception safety, i.e., if
preproc throws, lhs is not modified (whereas before ::p_ was).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/random/random.cc

    r4009 r4010  
    241241
    242242
     243  DiscreteGeneral::DiscreteGeneral(DiscreteGeneral&& other)
     244    : Discrete(std::move(other)), gen_(nullptr), p_(std::move(other.p_))
     245  {
     246    gen_ = std::move(other.gen_);
     247  }
     248
     249
    243250  DiscreteGeneral::~DiscreteGeneral(void)
    244251  {
     
    267274  DiscreteGeneral& DiscreteGeneral::operator=(const DiscreteGeneral& rhs)
    268275  {
    269     free();
    270     p_ = rhs.p_;
    271     preproc();
     276    DiscreteGeneral tmp(rhs);
     277    swap(*this, tmp);
    272278    return *this;
    273279  }
    274280
    275281
     282  DiscreteGeneral& DiscreteGeneral::operator=(DiscreteGeneral&& rhs)
     283  {
     284    DiscreteGeneral tmp(std::move(rhs));
     285    swap(*this, tmp);
     286    return *this;
     287  }
     288
     289
    276290  unsigned long DiscreteGeneral::operator()(void) const
    277291  {
    278292    return gsl_ran_discrete(rng_->rng(), gen_);
     293  }
     294
     295
     296  void swap(DiscreteGeneral& lhs, DiscreteGeneral& rhs)
     297  {
     298    std::swap(lhs.gen_, rhs.gen_);
     299    std::swap(lhs.p_, rhs.p_);
    279300  }
    280301
Note: See TracChangeset for help on using the changeset viewer.