Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYElement.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2024> <EDF-DTG> <FRANCE>
3  * This file is part of Code_TYMPAN (R).
4  * Code_TYMPAN (R) is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * Code_TYMPAN (R) is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License along
13  * with Code_TYMPAN (R). If not, see <https://www.gnu.org/licenses/>.
14  */
15 
16 #ifndef __TY_ELEMENT__
17 #define __TY_ELEMENT__
18 
19 #ifdef HAVE_CONFIG_H
20  #include "config.h"
21 #endif // HAVE_CONFIG_H
22 
23 #if _MSC_VER > 1000
24  #pragma once
25 #endif // _MSC_VER > 1000
26 #ifdef _MSC_VER
27  #pragma warning(disable : 4275 4800 4355 4273 4786 4018 4503 4081)
28 #endif
29 
30 #include <memory>
31 #include <unordered_map>
32 #include <iostream>
33 
34 #include <boost/exception/error_info.hpp>
35 
36 #ifndef _NDEBUG
37  #include <QDebug>
38 #endif // _NDEBUG
39 
40 #include "Tympan/core/defines.h"
41 #include "Tympan/core/macros.h"
42 #include "Tympan/core/idgen.h"
44 #if TY_USE_IHM
46 #endif // TY_USE_IHM
47 #include "TYDefines.h"
48 #include "TYXMLTools.h"
49 
50 // Nom de la categorie pour la sauvegarde des preferences.
51 #define TYDIRPREFERENCEMANAGER "UserPreferences"
52 
54 // Definition de macros pour la creation de nouveaux types
55 // derives de OPrototype.
56 
64 #define OPROTODECL(classname) \
65 public: \
66  virtual OPrototype* clone() const \
67  { \
68  return new classname(); \
69  } \
70  virtual const char* getClassName() const \
71  { \
72  return #classname; \
73  } \
74  static classname* safeDownCast(OPrototype* pObject) \
75  { \
76  if (pObject) \
77  { \
78  classname* pTypedObject = dynamic_cast<classname*>(pObject); \
79  if (pTypedObject != nullptr) \
80  { \
81  return pTypedObject; \
82  } \
83  } \
84  return nullptr; \
85  }
86 
88 
89 namespace tympan
90 {
91 typedef boost::error_info<struct tag_classname, std::string> oproto_classname_errinfo;
92 } // namespace tympan
93 
203 {
204  // Methodes
205 public:
209  virtual ~OPrototype();
210 
221  static OPrototype* findAndClone(const char* className);
222 
232  static int findPrototype(const char* className);
233 
240  virtual OPrototype* clone() const = 0;
241 
248  virtual const char* getClassName() const
249  {
250  return "OPrototype";
251  }
252 
261  bool isA(const char* className) const;
262 
272  static OPrototype* safeDownCast(OPrototype* pObject);
273 
281  {
282  public:
283  typedef std::unique_ptr<IOProtoFactory> ptr_type;
284  virtual std::unique_ptr<OPrototype> make() = 0;
285  };
286 
295  template <typename T> class Factory : public IOProtoFactory
296  {
297  public:
298  typedef std::unique_ptr<T> ptr_type;
299 
300  static_assert(std::is_base_of<OPrototype, T>::value, "Factory<T> : T must inherit from OPrototype");
301 
303  {
304  return ptr_type(new T());
305  }
306  virtual std::unique_ptr<OPrototype> make()
307  {
308  return typed_make();
309  }
310  };
311 
315  static void add_factory(const char*, IOProtoFactory::ptr_type factory);
316 
317 protected:
321  OPrototype();
322 
323  // Membres
324 private:
329  static std::unordered_map<std::string, IOProtoFactory::ptr_type> _factory_map;
330 };
331 
339 template <typename T> std::unique_ptr<OPrototype::Factory<T>> build_factory()
340 {
341  return std::unique_ptr<OPrototype::Factory<T>>(new OPrototype::Factory<T>());
342 }
343 
344 typedef std::vector<LPTYElement> LPTYElementArray;
345 
346 typedef std::map<TYUUID, TYElement*> TYElementContainer;
347 
352 #if TY_USE_IHM
353 
354  #if defined(WIN32)
355  #define TY_EXTENSION_DECL(classname) \
356  friend class classname; \
357  \
358  public: \
359  virtual TYEditWidget* getEditWidget() \
360  { \
361  return new classname##Widget(this); \
362  }
363  #else
364  #define TY_EXTENSION_DECL(classname) \
365  public: \
366  virtual TYEditWidget* getEditWidget() \
367  { \
368  return new classname##Widget(this); \
369  }
370  #endif
371 
372  #define TY_EXTENSION_DECL_ONLY(classname) \
373  public: \
374  virtual TYEditWidget* getEditWidget();
375 
376  #define TY_EXTENSION_INST(classname) \
377  TYEditWidget* classname::getEditWidget() \
378  { \
379  return new classname##Widget(this); \
380  }
381 
382 #else // TY_USE_IHM
383 
384  #define TY_EXTENSION_DECL(classname)
385  #define TY_EXTENSION_DECL_ONLY(classname)
386  #define TY_EXTENSION_INST(classname)
387 #endif // TY_USE_IHM
388 
392 #if TY_USE_IHM
393 
394  #if defined(WIN32)
395  #define TY_EXT_GRAPHIC_DECL(classname) \
396  friend class classname; \
397  \
398  public: \
399  virtual LPTYElementGraphic getGraphicObject() \
400  { \
401  if (!_pGraphicObject) \
402  _pGraphicObject = new classname##Graphic(this); \
403  return _pGraphicObject; \
404  }
405  #else
406  #define TY_EXT_GRAPHIC_DECL(classname) \
407  public: \
408  virtual LPTYElementGraphic getGraphicObject() \
409  { \
410  if (!_pGraphicObject) \
411  _pGraphicObject = new classname##Graphic(this); \
412  return _pGraphicObject; \
413  }
414  #endif
415 
416  #define TY_EXT_GRAPHIC_DECL_ONLY(classname) \
417  public: \
418  virtual LPTYElementGraphic getGraphicObject();
419 
420  #define TY_EXT_GRAPHIC_INST(classname) \
421  LPTYElementGraphic classname::getGraphicObject() \
422  { \
423  if (!_pGraphicObject) \
424  _pGraphicObject = new classname##Graphic(this); \
425  return _pGraphicObject; \
426  }
427 #else
428 
429  #define TY_EXT_GRAPHIC_DECL(classname)
430  #define TY_EXT_GRAPHIC_DECL_ONLY(classname)
431  #define TY_EXT_GRAPHIC_INST(classname)
432 typedef void* LPTYElementGraphic;
433 #endif // TY_USE_IHM
434 
438 typedef class OGenID TYUUID;
439 
440 template <typename T> T* downcast(TYElement* elt)
441 {
442  return T::safeDownCast(elt);
443 }
444 
448 class TYElement : public IRefCount, public OPrototype
449 {
451 
452  // Methodes
453 public:
458  TYElement();
459 
469  TYElement(TYElement* pParent, bool PutInInstanceList = true);
470 
477  TYElement(const TYElement& other, bool PutInInstanceList = true);
478 
482  virtual ~TYElement();
483 
485 
490 
499  virtual DOM_Element toXML(DOM_Element& domElement) /* const */;
508  virtual int fromXML(DOM_Element domElement);
509 
511 
516 
525  virtual void updateCurrentCalcul(TYListID& listID, bool recursif = true);
526 
530  virtual void getChilds(LPTYElementArray& childs, bool recursif = true) {}
531 
535  virtual void setInCurrentCalcul(bool state, bool recurschild = true, bool recursparent = true);
539  virtual bool isInCurrentCalcul()
540  {
541  return _inCurrentCalcul;
542  }
543 
547  void addCopy()
548  {
549  _copyCount++;
550  }
551 
555  void setCopyCount(const unsigned int copyCount)
556  {
557  _copyCount = copyCount;
558  }
559 
560 private:
565 
567 
568 public:
569 #if TY_USE_IHM
570 
575 
581  virtual LPTYElementGraphic getGraphicObject()
582  {
583  return NULL;
584  }
585 
591  virtual LPTYElementGraphic getGraphicObject() const
592  {
593  return NULL;
594  }
598  void drawGraphic(bool draw = true);
599 
603  virtual void updateGraphic();
604 
608  virtual void updateGraphicTree();
609 
611 
616 
625  virtual TYEditWidget* getEditWidget();
626 
637  int edit(TYEditWidget* pParent = NULL);
638 
640 #endif
641 
646 
650  void setID(TYUUID id);
651  void setID(const QString& str_id);
652 
656  const TYUUID& getID() const;
660  QString getStringID() const
661  {
662  return getID().toString();
663  }
664 
665  bool hasNullID() const
666  {
667  return _uuid.isNull();
668  }
669 
673  void regenerateID();
674 
678  void setName(QString name)
679  {
680  _name = name;
681  }
682 
683  void setName(std::string name)
684  {
685  _name = QString::fromStdString(name);
686  }
687 
691  virtual QString getName() const
692  {
693  return _name;
694  }
695 
699  void setParent(TYElement* pParent)
700  {
701  _pParent = pParent;
702  }
707  {
708  return _pParent;
709  }
710 
712 
717 
721  virtual void setIsAcousticModified(bool isModified);
726  {
727  return _isAcousticModified;
728  }
729 
733  virtual void setIsGeometryModified(bool isModified);
734 
736 
741 
745  TYElement& operator=(const TYElement& other);
746 
750  bool operator==(const TYElement& other) const;
751 
755  bool operator!=(const TYElement& other) const;
756 
770  virtual bool deepCopy(const TYElement* pOther, bool copyId = true, bool pUseCopyTag = false);
771 
773 
778 
785  virtual std::string toString() const
786  {
787  return "TYElement";
788  };
789 
800  bool callFromXMLIfEqual(DOM_Element& domElement, int* pRetVal = NULL);
801 
809  std::string getMetierName();
810 
812 
823  static LPTYElementArray findTypeCollectionAndCallFromXML(DOM_Element parentElem, const char* type);
824 
831  static void setLogInstances(bool log)
832  {
833  _logInstances = log;
834  }
841  static bool getLogInstances()
842  {
843  return _logInstances;
844  }
845 
854  static TYElement* getInstance(TYUUID uuid);
855 
859  static void purgeInstances();
860 
864  static bool testId(const TYUUID& id, const TYElement* pElem);
865 
871  static TYUUID newID();
872 
877  static TYUUID fromString(QString id);
878 
883  static QString toString(TYUUID& uuid);
884 
889  static uint64 getConstructorCount();
890 
895  static uint64 getDestructorCount();
896 
901  static uint64 getIdGenerationCount();
902 
906  static bool getIsSavedOk()
907  {
908  return _toSave;
909  }
910 
914  static void setIsSavedOk(const bool& toSave)
915  {
916  _toSave = toSave;
917  }
918 
922  static void setRegenerateID(const bool& bRegenerate)
923  {
924  _bRegenerateID = bRegenerate;
925  }
926  static bool getRegenerateID()
927  {
928  return _bRegenerateID;
929  }
930 
931  void* getAllUses()
932  {
933  return _allUses;
934  }
935  void setAllUses(void* allUses)
936  {
937  _allUses = allUses;
938  }
940  {
941  free(_allUses);
942  _allUses = nullptr;
943  }
944 
945 private:
950  void addInstance();
951 
956  void remInstance();
957 
958  // Membres
959 private:
961  mutable TYUUID _uuid;
962 
963 protected:
965  QString _name;
966 
969 
970  // Pour eviter de grossir la liste d'instance avec des objets temporaires:
972 
973  // Decompte du nombre de copies
974  unsigned int _copyCount;
975 
978 
981 
984 
986  void* _allUses;
987 
988 private:
990  static bool _logInstances;
992  // XXX This should be a map (of weak pointers) not a list !
997  static bool _toSave;
999  static bool _bRegenerateID;
1000 
1004 };
1005 
1006 // Some simple auxilliary fonction to help debugging and reporting
1007 
1008 inline const char* str_qt2c(const QString& qstr)
1009 {
1010  return qstr.toLocal8Bit().data();
1011 } // TODO Clarifiy consistency wrt Local8Bit vs Utf8
1012 
1013 inline QString xml2qstring(const QDomNode& node)
1014 {
1015  QString msg;
1016  QTextStream str(&msg);
1017  node.save(str, 0);
1018  return msg;
1019 }
1020 
1021 inline const char* xml2cstring(const QDomNode& node)
1022 {
1023  return str_qt2c(xml2qstring(node));
1024 }
1025 
1026 #ifndef _NDEBUG
1027 inline QDebug _debugXml(const char* expr, const QDomNode& node)
1028 {
1029  return qDebug() << expr << xml2qstring((node));
1030 }
1031 
1032  #define debugXml(expr) _debugXml(#expr, (expr))
1033 #else // _NDEBUG
1034  #define debugXml(expr) ((void)0)
1035 #endif // _NDEBUG
1036 
1037 #endif // __TY_ELEMENT__
QDomElement DOM_Element
Definition: QT2DOM.h:30
void TYEditWidget
Declarations de types pour l'API IHM et l'Impression.
Definition: TYDefines.h:50
class OGenID TYUUID
Definition: TYDefines.h:59
std::list< TYUUID > TYListID
Collection d'identifiants.
Definition: TYDefines.h:331
Representation graphique d'un element de base (fichier header)
QDebug _debugXml(const char *expr, const QDomNode &node)
Definition: TYElement.h:1027
std::unique_ptr< OPrototype::Factory< T > > build_factory()
Definition: TYElement.h:339
const char * str_qt2c(const QString &qstr)
Definition: TYElement.h:1008
std::vector< LPTYElement > LPTYElementArray
Definition: TYElement.h:344
T * downcast(TYElement *elt)
Definition: TYElement.h:440
void * LPTYElementGraphic
Definition: TYElement.h:432
#define OPROTODECL(classname)
Definition: TYElement.h:64
std::map< TYUUID, TYElement * > TYElementContainer
Definition: TYElement.h:346
const char * xml2cstring(const QDomNode &node)
Definition: TYElement.h:1021
QString xml2qstring(const QDomNode &node)
Definition: TYElement.h:1013
const char * name
Definition: idgen.h:28
virtual std::unique_ptr< OPrototype > make()
Definition: TYElement.h:306
ptr_type typed_make()
Definition: TYElement.h:302
std::unique_ptr< T > ptr_type
Definition: TYElement.h:298
virtual std::unique_ptr< OPrototype > make()=0
std::unique_ptr< IOProtoFactory > ptr_type
Definition: TYElement.h:283
virtual ~OPrototype()
Definition: TYElement.cpp:30
static std::unordered_map< std::string, IOProtoFactory::ptr_type > _factory_map
Definition: TYElement.h:329
static void add_factory(const char *, IOProtoFactory::ptr_type factory)
Definition: TYElement.cpp:32
virtual OPrototype * clone() const =0
static int findPrototype(const char *className)
Definition: TYElement.cpp:55
virtual const char * getClassName() const
Definition: TYElement.h:248
static OPrototype * safeDownCast(OPrototype *pObject)
Definition: TYElement.cpp:71
static OPrototype * findAndClone(const char *className)
Definition: TYElement.cpp:37
bool isA(const char *className) const
Definition: TYElement.cpp:65
static bool _toSave
Definition: TYElement.h:997
bool _bPutInInstanceList
Definition: TYElement.h:971
void setID(TYUUID id)
Definition: TYElement.cpp:186
static uint64 ty_regen_id_counter
Definition: TYElement.h:1003
static TYElementContainer & getInstances()
Definition: TYElement.cpp:95
TYElement * getParent() const
Definition: TYElement.h:706
void setName(QString name)
Definition: TYElement.h:678
virtual bool isInCurrentCalcul()
Definition: TYElement.h:539
static bool _logInstances
Indique si on souhaite registrer toutes les instances de type TYElement et derivees.
Definition: TYElement.h:990
void addInstance()
Definition: TYElement.cpp:226
static void setLogInstances(bool log)
Definition: TYElement.h:831
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
Definition: TYElement.cpp:305
TYUUID _uuid
Identifiant unique de l'element.
Definition: TYElement.h:961
void remInstance()
Definition: TYElement.cpp:235
static void setIsSavedOk(const bool &toSave)
Definition: TYElement.h:914
virtual DOM_Element toXML(DOM_Element &domElement)
Definition: TYElement.cpp:366
void setCopyCount(const unsigned int copyCount)
Modifie la vaeur du compteur.
Definition: TYElement.h:555
std::string getMetierName()
Definition: TYElement.cpp:565
QString _name
Nom courant de l'element.
Definition: TYElement.h:965
bool operator==(const TYElement &other) const
Definition: TYElement.cpp:280
void cleanAllUses()
Definition: TYElement.h:939
void * getAllUses()
Definition: TYElement.h:931
static void purgeInstances()
Definition: TYElement.cpp:171
static bool getRegenerateID()
Definition: TYElement.h:926
TYElement & operator=(const TYElement &other)
Definition: TYElement.cpp:263
static TYUUID fromString(QString id)
Definition: TYElement.cpp:621
void OnChildInCalculStatusChange()
Definition: TYElement.cpp:438
static uint64 getConstructorCount()
Definition: TYElement.cpp:632
bool callFromXMLIfEqual(DOM_Element &domElement, int *pRetVal=NULL)
Definition: TYElement.cpp:542
static bool testId(const TYUUID &id, const TYElement *pElem)
Definition: TYElement.cpp:216
static bool getLogInstances()
Definition: TYElement.h:841
static uint64 getIdGenerationCount()
Definition: TYElement.cpp:641
bool _isAcousticModified
Indicateur de modification acoustique.
Definition: TYElement.h:980
void regenerateID()
Definition: TYElement.cpp:243
bool hasNullID() const
Definition: TYElement.h:665
static uint64 getDestructorCount()
Definition: TYElement.cpp:636
void addCopy()
Increment le compteur de nombre de copies.
Definition: TYElement.h:547
const TYUUID & getID() const
Definition: TYElement.cpp:176
bool operator!=(const TYElement &other) const
Definition: TYElement.cpp:300
static TYUUID newID()
Definition: TYElement.cpp:613
void * _allUses
Multi purpose void pointer (use for compatibility actually)
Definition: TYElement.h:986
virtual std::string toString() const
Definition: TYElement.h:785
virtual QString getName() const
Definition: TYElement.h:691
TYElement * _pParent
Reference sur l'element parent.
Definition: TYElement.h:968
virtual void updateCurrentCalcul(TYListID &listID, bool recursif=true)
Definition: TYElement.cpp:458
bool getIsAcousticModified()
Definition: TYElement.h:725
LPTYElementGraphic _pGraphicObject
L'object graphique metier associe a cet element.
Definition: TYElement.h:983
virtual void setInCurrentCalcul(bool state, bool recurschild=true, bool recursparent=true)
Definition: TYElement.cpp:408
virtual void getChilds(LPTYElementArray &childs, bool recursif=true)
Definition: TYElement.h:530
static void setRegenerateID(const bool &bRegenerate)
Definition: TYElement.h:922
void setName(std::string name)
Definition: TYElement.h:683
static TYElementContainer * _instances
Collection de toutes les instances de type TYElement et derivees.
Definition: TYElement.h:993
bool _inCurrentCalcul
Indique si cet element est actif dans le Calcul courant.
Definition: TYElement.h:977
void setParent(TYElement *pParent)
Definition: TYElement.h:699
static LPTYElementArray findTypeCollectionAndCallFromXML(DOM_Element parentElem, const char *type)
Definition: TYElement.cpp:570
virtual ~TYElement()
Definition: TYElement.cpp:139
virtual int fromXML(DOM_Element domElement)
Definition: TYElement.cpp:379
void setAllUses(void *allUses)
Definition: TYElement.h:935
QString getStringID() const
Definition: TYElement.h:660
virtual void setIsAcousticModified(bool isModified)
Definition: TYElement.cpp:248
unsigned int _copyCount
Definition: TYElement.h:974
static uint64 ty_destroyed_counter
Definition: TYElement.h:1002
static bool getIsSavedOk()
Definition: TYElement.h:906
static uint64 ty_created_counter
Definition: TYElement.h:1001
static TYElement * getInstance(TYUUID uuid)
Definition: TYElement.cpp:158
static bool _bRegenerateID
Indicateur de regeneration d'ID true si regeneration d'ID a la lecture d'un fichier XML.
Definition: TYElement.h:999
virtual void setIsGeometryModified(bool isModified)
Definition: TYElement.cpp:253
This file provides class for solver configuration.
unsigned long long uint64
Definition: defines.h:80
boost::error_info< struct tag_classname, std::string > oproto_classname_errinfo
Definition: TYElement.h:91