diff --git a/App/src/TestDiffuseScattering.cpp b/App/src/TestDiffuseScattering.cpp index 71e9a3d907374fc7866cbc4af775e8ad8f9a54cf..570a3bf70955aabb0007ef6fe5a653a100c13b09 100644 --- a/App/src/TestDiffuseScattering.cpp +++ b/App/src/TestDiffuseScattering.cpp @@ -61,16 +61,17 @@ void TestDiffuseScattering::execute() mySample.print(); - std::cout << "--- Attempt to clone MultiLayer" << std::endl; - - MultiLayer *newSample = mySample.clone(); - newSample->print(); - - MultiLayer *newSample2 = newSample->clone(); - - delete newSample; - - newSample2->print(); +// std::cout << "--- Attempt to clone MultiLayer" << std::endl; +// MultiLayer *newSample = mySample.clone(); +// newSample->print(); +// MultiLayer *newSample2 = newSample->clone(); +// delete newSample; +// newSample2->print(); + + kvector_t k1(0.1,0.2,0.3); + kvector_t k2(0.1,0.2,0.8); + kvector_t k3 = k2 - k1; + std::cout << k3 << std::endl; } diff --git a/Core/Core.pro b/Core/Core.pro index f185fd201f1e5bf800e43ebfe5c13142e69b4907..506ebf8798334e23e2c5a83d744c4b86ab62d095 100644 --- a/Core/Core.pro +++ b/Core/Core.pro @@ -27,6 +27,7 @@ SOURCES += \ src/MaterialManager.cpp \ src/StochasticGaussian.cpp \ src/MathFunctions.cpp \ + src/Types.cpp HEADERS += \ inc/ISample.h \ diff --git a/Core/inc/ISample.h b/Core/inc/ISample.h index 7bdc412fdfba4efb0bea0efa32bf54dfd9b3a9b3..44d548d4e5fe9eb3f7192b15e27f23d480178feb 100644 --- a/Core/inc/ISample.h +++ b/Core/inc/ISample.h @@ -10,6 +10,9 @@ public: virtual ~ISample() {} virtual void add(ISample* p_child); + + virtual ISample *clone() const =0; + // virtual void remove(ISample* p_child); // virtual ISample* getChild(size_t index); }; diff --git a/Core/inc/Layer.h b/Core/inc/Layer.h index a69c58db50b40b7b63460a18445e9efb5ea9e058..ec0c4182400fa25dd8539b19e483c6935efcd01d 100644 --- a/Core/inc/Layer.h +++ b/Core/inc/Layer.h @@ -32,6 +32,9 @@ public: Layer &operator=(const Layer &other); virtual ~Layer(); + //! make layer's clone + virtual Layer *clone() const; + /// set layer thickness in _angstrom_ virtual void setThickness(double thickness); diff --git a/Core/inc/MultiLayer.h b/Core/inc/MultiLayer.h index 858d7134eed5ea30fd49ab4e3f08f3c9babf48e5..c06ac9c4c44c66e8771fe6c64af33d3b148c9827 100644 --- a/Core/inc/MultiLayer.h +++ b/Core/inc/MultiLayer.h @@ -67,7 +67,7 @@ public: void clear(); /// return clone of multilayer with clones of all layers and recreated interfaces between layers - MultiLayer *clone(); + virtual MultiLayer *clone() const; /// print structure of multilayer void print(); diff --git a/Core/inc/Types.h b/Core/inc/Types.h index de86f383febd9cbaab70311dbb5126dac4b86a8b..8e0b6f8cd2d44a50b38d14896798e1822e1a3d7a 100644 --- a/Core/inc/Types.h +++ b/Core/inc/Types.h @@ -9,6 +9,11 @@ typedef std::complex<double > complex_t; +// we need forward declaration here to be able to redefine ostream as friend +template<typename T> class KVector; +template<typename T> std::ostream& operator<< (std::ostream& o, const KVector<T>& ); + + template<typename T> class KVector { public: @@ -35,12 +40,21 @@ public: inline T z() const { return m_z; } inline T mag() const { return std::sqrt(m_mag2); } inline T mag2() const { return m_mag2; } + + KVector<T> &operator=(const KVector<T> &other); + KVector<T> &operator+=(const KVector<T> &other); + KVector<T> &operator-=(const KVector<T> &other); + friend std::ostream &operator<< <T> (std::ostream &ostr, KVector<T> const &k); private: T m_x; T m_y; T m_z; T m_mag2; }; + +template<typename T> KVector<T> operator+(const KVector<T> &a, const KVector<T> &b); +template<typename T> KVector<T> operator-(const KVector<T> &a, const KVector<T> &b); + typedef KVector<double > kvector_t; diff --git a/Core/src/Layer.cpp b/Core/src/Layer.cpp index 67f4bfe25136b3dbc1b8170d5c3952c2ccd9e3e7..71d06b84e9f4da06dca62d97c3b9741f93ad337a 100644 --- a/Core/src/Layer.cpp +++ b/Core/src/Layer.cpp @@ -32,6 +32,12 @@ Layer::~Layer() } +/* ************************************************************************* */ +Layer *Layer::clone() const { + return new Layer(*this); +} + + /* ************************************************************************* */ void Layer::setThickness(double thickness) { diff --git a/Core/src/MaterialManager.cpp b/Core/src/MaterialManager.cpp index 64e46f580d3267de812e6d45110afe4081a2902f..827572bd2abd106e3699f4d8ef7d1b0979bea51c 100644 --- a/Core/src/MaterialManager.cpp +++ b/Core/src/MaterialManager.cpp @@ -27,9 +27,9 @@ MaterialManager::~MaterialManager() /* ************************************************************************* */ MaterialManager &MaterialManager::instance() { - // check if not exists, then initialise + // check if exists, if not, then initialise if( !pInstance) { - // check for dead reference + // check for dead reference (i.e. object has been initialised but then somebody managed to delete it) if( m_destroyed ) { onDeadReference(); } else { diff --git a/Core/src/MultiLayer.cpp b/Core/src/MultiLayer.cpp index ab9f3db9939c11d557a0eedfc8aeed4e39a02151..bbe5f627668593094b27ac0b96ab73c2efaf07c0 100644 --- a/Core/src/MultiLayer.cpp +++ b/Core/src/MultiLayer.cpp @@ -37,7 +37,7 @@ void MultiLayer::clear() /* ************************************************************************* */ // clone MultiLayer contents including interfaces /* ************************************************************************* */ -MultiLayer *MultiLayer::clone() +MultiLayer *MultiLayer::clone() const { MultiLayer *newMultiLayer = new MultiLayer(); diff --git a/Core/src/Types.cpp b/Core/src/Types.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a12e690d28e252e6cd22e8b36d3db54b0dbb2391 --- /dev/null +++ b/Core/src/Types.cpp @@ -0,0 +1,56 @@ +#include "Types.h" + + + +template<typename T> +KVector<T> &KVector<T>::operator=(const KVector<T> &other) +{ + m_x=other.m_x; m_y = other.m_y; m_z = other.m_z; + return *this; +} + +template<typename T> KVector<T> &KVector<T>::operator+=(const KVector<T> &b) +{ + m_x += b.m_x; m_y += b.m_y; m_z += b.m_z; + return *this; +} + +template<typename T> KVector<T> &KVector<T>::operator-=(const KVector<T> &b) +{ + m_x -= b.m_x; m_y -= b.m_y; m_z -= b.m_z; + return *this; +} + +template<typename T> KVector<T> operator+(const KVector<T> &a, const KVector<T> &b) +{ + KVector<T> target = a; + target += b; + return target; +} + +template<typename T> KVector<T> operator-(const KVector<T> &a, const KVector<T> &b) +{ + KVector<T> target = a; + target -= b; + return target; +} + +template<typename T> +std::ostream &operator<<(std::ostream &o, const KVector<T> &a) +{ + o << a.m_x << " " << a.m_y << " " << a.m_z; + return o; +} + + + +// to force compiler to generate necessary code in shared library +template class KVector<double>; +//template KVector<double> operator= <double> (const KVector<double> &a); +//template KVector<double> operator+= <double> (const KVector<double> &a); +//template KVector<double> operator-= <double> (const KVector<double> &a); +template KVector<double> operator+ <double> (const KVector<double> &a, const KVector<double> &b); +template KVector<double> operator- <double> (const KVector<double> &a, const KVector<double> &b); +template std::ostream &operator<< <double> (std::ostream &o, const KVector<double> &b); + +