Changeset 1614 for trunk/yat


Ignore:
Timestamp:
Nov 5, 2008, 9:08:55 PM (12 years ago)
Author:
Peter
Message:

implementing copying in RNG_state. Previously compiler generated versions were used which was not sound. Also disallowed assignment of singleton RNG. Assignment of a singleton class does not make sense.

Location:
trunk/yat/random
Files:
2 edited

Legend:

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

    r1487 r1614  
    2525#include "yat/utility/Exception.h"
    2626
     27#include <cassert>
    2728#include <fstream>
    2829#include <sstream>
     
    115116  RNG_state::RNG_state(const RNG* rng)
    116117  {
    117     if (!(rng_ = gsl_rng_clone(rng->rng())))
    118       throw utility::GSL_error("RNG_state::RNG_state failed to allocate memory");
     118    clone(*rng->rng());
     119  }
     120 
     121
     122  RNG_state::RNG_state(const RNG_state& state)
     123  {
     124    clone(*state.rng());
    119125  }
    120126 
     
    129135  {
    130136    return rng_;
     137  }
     138
     139
     140  void RNG_state::clone(const gsl_rng& rng)
     141  {
     142    assert(rng_!=&rng);
     143    if (!(rng_ = gsl_rng_clone(&rng)))
     144      throw utility::GSL_error("RNG_state::RNG_state failed to allocate memory");
     145  }
     146
     147  RNG_state& RNG_state::operator=(const RNG_state& rhs)
     148  {
     149    if (this != &rhs) {
     150      gsl_rng_free(rng_);
     151      clone(*rhs.rng());
     152    }
     153    return *this;
    131154  }
    132155
  • trunk/yat/random/random.h

    r1487 r1614  
    154154    RNG(const RNG&);
    155155
     156    /**
     157       There can be only one RNG so assignment is always
     158       self-assignment and we do not allow it
     159    */
     160    RNG& operator=(const RNG&);
     161
    156162    virtual ~RNG(void);
    157163
     
    172178    RNG_state(const RNG*);
    173179
     180    /**
     181       Copy Constructor
     182
     183       \since Explicitely declared since yat 0.5
     184     */
     185    RNG_state(const RNG_state&);
     186
    174187    ///
    175188    /// @brief Destructor
     
    182195    const gsl_rng* rng(void) const;
    183196
     197    /**
     198       Assignment operator
     199
     200       \since Explicitely declared since yat 0.5
     201     */
     202    RNG_state& operator=(const RNG_state&);
     203
    184204  private:
    185205    gsl_rng* rng_;
    186206
     207    void clone(const gsl_rng&);
    187208  };
    188209   
Note: See TracChangeset for help on using the changeset viewer.