/***************************************************************************
**
**  This file is part of QGpCoreWave.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This file is distributed in the hope that it will be useful, but WITHOUT
**  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
**  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
**  License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
**
**  See http://www.geopsy.org for more information.
**
**  Created: 2006-09-26
**  Copyright: 2006-2019
**    Marc Wathelet
**    Marc Wathelet (LGIT, Grenoble, France)
**
***************************************************************************/

#ifndef MODALFACTORY_H
#define MODALFACTORY_H

#include <QGpCoreMath.h>

#include "QGpCoreWaveDLLExport.h"

namespace QGpCoreWave {

class ModalStorage;
class ModalCurve;
class Mode;

class QGPCOREWAVE_EXPORT ModalFactory
{
  TRANSLATIONS("ModalFactory")
public:
  ModalFactory() {}
  virtual ~ModalFactory() {}

  void setX (ModalFactory& f);
  void setX (const QList<ModalCurve>& curves);
  void linkX (QList<ModalCurve>& curves) const;
  void setModes (const QList<ModalCurve>& curves);

  void setX (const ModalCurve& c);
  void linkX (ModalCurve& c) const;
  void setModes (const ModalCurve& c);
  void setAngularFrequency();

  virtual void setMode (const Mode& m)=0;
  virtual const RealValue * mode(const Mode& m) const=0;
  virtual int storageCount() const=0;
  virtual void writeReportHeader(QDataStream& ) const {}
  virtual ModalStorage * storage(int storageIndex) const=0;

  const QVector<double> * x() const {return &_x;}
protected:
  virtual ModalStorage * newStorage(int nModes)=0;
  template <class StorageType> void upgrade(StorageType *& s, int requestedMode);

  QVector<double> _x;
};

template <class StorageType>
void ModalFactory::upgrade (StorageType *& s, int requestedMode)
{
  TRACE;
  if(!s || requestedMode>=s->modeCount()) {
    StorageType * tmp_s=static_cast<StorageType *>(newStorage(requestedMode+1));
    delete s;
    s=tmp_s;
  }
}

} // namespace QGpCoreWave

#endif // MODALFACTORY_H
