// genDElement - An abstract data element library
// Copyright (C) 2013-2026 Eric Bechet, Frederic Duboeuf
//
// See the LICENSE file for license information.
// Please report all bugs and problems to <bechet@cadxfem.org> or <duboeuf@outlook.com>.
//
// Initial design: Frederic Duboeuf (rev.1507)


#include "genDataBuilder.h"
#include "genDElement.h"
#include "genDElementManager.h"

#include "MElement.h"
#include "GEntity.h"

template<class Data> Data* buildData(MElement* e, const char* s)
{
  Data* data(0);
  DElementBase<MElement>* de = KeyManager<MElement>::findDElement(e);
  if (!de)
    de = KeyManager<MElement>::createDElement<Data>(e,s);
  genDataGroup* dataGroup;
  dataGroup = de->getDatas();
  dataGroup->getData(s,data);
  if (!data)
  {
    data = new Data(s,de);
    dataGroup->setData(s,data);
  }
  return data;
}

template<class Data,class Iterator> void buildData(Iterator itbegin, Iterator itend, const char* s)
{
  for (Iterator it = itbegin; it != itend; ++it)
  {
    MElement* e = *it;
    buildData<Data>(e,s);
  }
}

template<class Data,class GModel> void buildData(GModel* m, const char* s)
{
  std::vector<GEntity*> entities;
  m->getEntities(entities);
  for (int i=0; i<entities.size(); ++i)
  {
    GEntity* entity = entities[i];
    for (int j=0; j<entity->getNumMeshElements(); ++j)
    {
      MElement* e;
      e = entity->getMeshElement(j);
      buildData<Data>(e,s);
    }
  }
}