Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYElement.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2014> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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
47 #endif // TY_USE_IHM
48 #include "TYDefines.h"
49 #include "TYXMLTools.h"
50 
51 // Nom de la categorie pour la sauvegarde des preferences.
52 #define TYDIRPREFERENCEMANAGER "UserPreferences"
53 
55 // Definition de macros pour la creation de nouveaux types
56 // derives de OPrototype.
57 
65 #define OPROTODECL(classname) \
66 public: \
67  virtual OPrototype* clone() const \
68  { \
69  return new classname(); \
70  } \
71  virtual const char* getClassName() const \
72  { \
73  return #classname; \
74  } \
75  static classname* safeDownCast(OPrototype* pObject) \
76  { \
77  if (pObject) \
78  { \
79  classname* pTypedObject = dynamic_cast<classname*>(pObject); \
80  if (pTypedObject != nullptr) \
81  { \
82  return pTypedObject; \
83  } \
84  } \
85  return nullptr; \
86  }
87 
89 
90 namespace tympan
91 {
92 typedef boost::error_info<struct tag_classname, std::string> oproto_classname_errinfo;
93 } // namespace tympan
94 
204 {
205  // Methodes
206 public:
210  virtual ~OPrototype();
211 
222  static OPrototype* findAndClone(const char* className);
223 
233  static int findPrototype(const char* className);
234 
241  virtual OPrototype* clone() const = 0;
242 
249  virtual const char* getClassName() const
250  {
251  return "OPrototype";
252  }
253 
262  bool isA(const char* className) const;
263 
273  static OPrototype* safeDownCast(OPrototype* pObject);
274 
282  {
283  public:
284  typedef std::unique_ptr<IOProtoFactory> ptr_type;
285  virtual std::unique_ptr<OPrototype> make() = 0;
286  };
287 
296  template <typename T> class Factory : public IOProtoFactory
297  {
298  public:
299  typedef std::unique_ptr<T> ptr_type;
300 
301  static_assert(std::is_base_of<OPrototype, T>::value, "Factory<T> : T must inherit from OPrototype");
302 
304  {
305  return ptr_type(new T());
306  }
307  virtual std::unique_ptr<OPrototype> make()
308  {
309  return typed_make();
310  }
311  };
312 
316  static void add_factory(const char*, IOProtoFactory::ptr_type factory);
317 
318 protected:
322  OPrototype();
323 
324  // Membres
325 private:
330  static std::unordered_map<std::string, IOProtoFactory::ptr_type> _factory_map;
331 };
332 
340 template <typename T> std::unique_ptr<OPrototype::Factory<T>> build_factory()
341 {
342  return std::unique_ptr<OPrototype::Factory<T>>(new OPrototype::Factory<T>());
343 }
344 
345 typedef std::vector<LPTYElement> LPTYElementArray;
346 
347 typedef std::map<TYUUID, TYElement*> TYElementContainer;
348 
353 #if TY_USE_IHM
354 
355  #if defined(WIN32)
356  #define TY_EXTENSION_DECL(classname) \
357  friend class classname; \
358  \
359  public: \
360  virtual TYEditWidget* getEditWidget() \
361  { \
362  return new classname##Widget(this); \
363  }
364  #else
365  #define TY_EXTENSION_DECL(classname) \
366  public: \
367  virtual TYEditWidget* getEditWidget() \
368  { \
369  return new classname##Widget(this); \
370  }
371  #endif
372 
373  #define TY_EXTENSION_DECL_ONLY(classname) \
374  public: \
375  virtual TYEditWidget* getEditWidget();
376 
377  #define TY_EXTENSION_INST(classname) \
378  TYEditWidget* classname::getEditWidget() \
379  { \
380  return new classname##Widget(this); \
381  }
382 
383 #else // TY_USE_IHM
384 
385  #define TY_EXTENSION_DECL(classname)
386  #define TY_EXTENSION_DECL_ONLY(classname)
387  #define TY_EXTENSION_INST(classname)
388 #endif // TY_USE_IHM
389 
393 #if TY_USE_IHM
394 
395  #if defined(WIN32)
396  #define TY_EXT_GRAPHIC_DECL(classname) \
397  friend class classname; \
398  \
399  public: \
400  virtual LPTYElementGraphic getGraphicObject() \
401  { \
402  if (!_pGraphicObject) \
403  _pGraphicObject = new classname##Graphic(this); \
404  return _pGraphicObject; \
405  }
406  #else
407  #define TY_EXT_GRAPHIC_DECL(classname) \
408  public: \
409  virtual LPTYElementGraphic getGraphicObject() \
410  { \
411  if (!_pGraphicObject) \
412  _pGraphicObject = new classname##Graphic(this); \
413  return _pGraphicObject; \
414  }
415  #endif
416 
417  #define TY_EXT_GRAPHIC_DECL_ONLY(classname) \
418  public: \
419  virtual LPTYElementGraphic getGraphicObject();
420 
421  #define TY_EXT_GRAPHIC_INST(classname) \
422  LPTYElementGraphic classname::getGraphicObject() \
423  { \
424  if (!_pGraphicObject) \
425  _pGraphicObject = new classname##Graphic(this); \
426  return _pGraphicObject; \
427  }
428 
429 #else
430 
431  #define TY_EXT_GRAPHIC_DECL(classname)
432  #define TY_EXT_GRAPHIC_DECL_ONLY(classname)
433  #define TY_EXT_GRAPHIC_INST(classname)
434 typedef void* LPTYElementGraphic;
435 #endif // TY_USE_IHM
436 
440 typedef class OGenID TYUUID;
441 
442 template <typename T> T* downcast(TYElement* elt)
443 {
444  return T::safeDownCast(elt);
445 }
446 
450 class TYElement : public IRefCount, public OPrototype
451 {
453 
454  // Methodes
455 public:
460  TYElement();
461 
471  TYElement(TYElement* pParent, bool PutInInstanceList = true);
472 
479  TYElement(const TYElement& other, bool PutInInstanceList = true);
480 
484  virtual ~TYElement();
485 
487 
492 
501  virtual DOM_Element toXML(DOM_Element& domElement) /* const */;
510  virtual int fromXML(DOM_Element domElement);
511 
513 
518 
527  virtual void updateCurrentCalcul(TYListID& listID, bool recursif = true);
528 
532  virtual void getChilds(LPTYElementArray& childs, bool recursif = true) {}
533 
537  virtual void setInCurrentCalcul(bool state, bool recurschild = true, bool recursparent = true);
541  virtual bool isInCurrentCalcul()
542  {
543  return _inCurrentCalcul;
544  }
545 
549  void addCopy()
550  {
551  _copyCount++;
552  }
553 
557  void setCopyCount(const unsigned int copyCount)
558  {
559  _copyCount = copyCount;
560  }
561 
562 private:
567 
569 
570 public:
571 #if TY_USE_IHM
572 
577 
583  virtual LPTYElementGraphic getGraphicObject()
584  {
585  return NULL;
586  }
587 
591  void drawGraphic(bool draw = true);
592 
596  virtual void updateGraphic();
597 
601  virtual void updateGraphicTree();
602 
604 
609 
618  virtual TYEditWidget* getEditWidget();
619 
630  int edit(TYEditWidget* pParent = NULL);
631 
633 #endif
634 
639 
643  void setID(TYUUID id);
644  void setID(const QString& str_id);
645 
649  const TYUUID& getID() const;
653  QString getStringID() const
654  {
655  return getID().toString();
656  }
657 
658  bool hasNullID() const
659  {
660  return _uuid.isNull();
661  }
662 
666  void regenerateID();
667 
671  void setName(QString name)
672  {
673  _name = name;
674  }
675 
676  void setName(std::string name)
677  {
678  _name = QString::fromStdString(name);
679  }
680 
684  virtual QString getName() const
685  {
686  return _name;
687  }
688 
692  void setParent(TYElement* pParent)
693  {
694  _pParent = pParent;
695  }
700  {
701  return _pParent;
702  }
703 
705 
710 
714  virtual void setIsAcousticModified(bool isModified);
719  {
720  return _isAcousticModified;
721  }
722 
726  virtual void setIsGeometryModified(bool isModified);
727 
732  {
733  return _isGeometryModified;
734  }
735 
737 
742 
746  TYElement& operator=(const TYElement& other);
747 
751  bool operator==(const TYElement& other) const;
752 
756  bool operator!=(const TYElement& other) const;
757 
771  virtual bool deepCopy(const TYElement* pOther, bool copyId = true, bool pUseCopyTag = false);
772 
774 
779 
786  virtual std::string toString() const
787  {
788  return "TYElement";
789  };
790 
801  bool callFromXMLIfEqual(DOM_Element& domElement, int* pRetVal = NULL);
802 
810  std::string getMetierName();
811 
813 
824  static LPTYElementArray findTypeCollectionAndCallFromXML(DOM_Element parentElem, const char* type);
825 
832  static void setLogInstances(bool log)
833  {
834  _logInstances = log;
835  }
842  static bool getLogInstances()
843  {
844  return _logInstances;
845  }
846 
855  static TYElement* getInstance(TYUUID uuid);
856 
860  static void purgeInstances();
861 
865  static bool testId(const TYUUID& id, const TYElement* pElem);
866 
872  static TYUUID newID();
873 
878  static TYUUID fromString(QString id);
879 
884  static QString toString(TYUUID& uuid);
885 
890  static uint64 getConstructorCount();
891 
896  static uint64 getDestructorCount();
897 
902  static uint64 getIdGenerationCount();
903 
907  static bool getIsSavedOk()
908  {
909  return _toSave;
910  }
911 
915  static void setIsSavedOk(const bool& toSave)
916  {
917  _toSave = toSave;
918  }
919 
923  static void setRegenerateID(const bool& bRegenerate)
924  {
925  _bRegenerateID = bRegenerate;
926  }
927  static bool getRegenerateID()
928  {
929  return _bRegenerateID;
930  }
931 
932  void* getAllUses()
933  {
934  return _allUses;
935  }
936  void setAllUses(void* allUses)
937  {
938  _allUses = allUses;
939  }
941  {
942  free(_allUses);
943  _allUses = nullptr;
944  }
945 
946 private:
951  void addInstance();
952 
957  void remInstance();
958 
959  // Membres
960 private:
962  mutable TYUUID _uuid;
963 
964 protected:
966  QString _name;
967 
970 
971  // Pour eviter de grossir la liste d'instance avec des objets temporaires:
973 
974  // Decompte du nombre de copies
975  unsigned int _copyCount;
976 
979 
982 
985 
988 
990  void* _allUses;
991 
992 private:
994  static bool _logInstances;
996  // XXX This should be a map (of weak pointers) not a list !
1001  static bool _toSave;
1003  static bool _bRegenerateID;
1004 
1008 };
1009 
1010 // Some simple auxilliary fonction to help debugging and reporting
1011 
1012 inline const char* str_qt2c(const QString& qstr)
1013 {
1014  return qstr.toLocal8Bit().data();
1015 } // TODO Clarifiy consistency wrt Local8Bit vs Utf8
1016 
1017 inline QString xml2qstring(const QDomNode& node)
1018 {
1019  QString msg;
1020  QTextStream str(&msg);
1021  node.save(str, 0);
1022  return msg;
1023 }
1024 
1025 inline const char* xml2cstring(const QDomNode& node)
1026 {
1027  return str_qt2c(xml2qstring(node));
1028 }
1029 
1030 #ifndef _NDEBUG
1031 inline QDebug _debugXml(const char* expr, const QDomNode& node)
1032 {
1033  return qDebug() << expr << xml2qstring((node));
1034 }
1035 
1036  #define debugXml(expr) _debugXml(#expr, (expr))
1037 #else // _NDEBUG
1038  #define debugXml(expr) ((void)0)
1039 #endif // _NDEBUG
1040 
1041 #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)
outil IHM pour un element (fichier header)
QDebug _debugXml(const char *expr, const QDomNode &node)
Definition: TYElement.h:1031
std::unique_ptr< OPrototype::Factory< T > > build_factory()
Definition: TYElement.h:340
const char * str_qt2c(const QString &qstr)
Definition: TYElement.h:1012
std::vector< LPTYElement > LPTYElementArray
Definition: TYElement.h:345
T * downcast(TYElement *elt)
Definition: TYElement.h:442
void * LPTYElementGraphic
Definition: TYElement.h:434
#define OPROTODECL(classname)
Definition: TYElement.h:65
std::map< TYUUID, TYElement * > TYElementContainer
Definition: TYElement.h:347
const char * xml2cstring(const QDomNode &node)
Definition: TYElement.h:1025
QString xml2qstring(const QDomNode &node)
Definition: TYElement.h:1017
const char * name
Definition: idgen.h:28
virtual std::unique_ptr< OPrototype > make()
Definition: TYElement.h:307
ptr_type typed_make()
Definition: TYElement.h:303
std::unique_ptr< T > ptr_type
Definition: TYElement.h:299
virtual std::unique_ptr< OPrototype > make()=0
std::unique_ptr< IOProtoFactory > ptr_type
Definition: TYElement.h:284
virtual ~OPrototype()
Definition: TYElement.cpp:30
static std::unordered_map< std::string, IOProtoFactory::ptr_type > _factory_map
Definition: TYElement.h:330
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:249
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:1001
bool _bPutInInstanceList
Definition: TYElement.h:972
void setID(TYUUID id)
Definition: TYElement.cpp:186
bool getIsGeometryModified()
Definition: TYElement.h:731
static uint64 ty_regen_id_counter
Definition: TYElement.h:1007
static TYElementContainer & getInstances()
Definition: TYElement.cpp:95
TYElement * getParent() const
Definition: TYElement.h:699
void setName(QString name)
Definition: TYElement.h:671
virtual bool isInCurrentCalcul()
Definition: TYElement.h:541
static bool _logInstances
Indique si on souhaite registrer toutes les instances de type TYElement et derivees.
Definition: TYElement.h:994
void addInstance()
Definition: TYElement.cpp:226
static void setLogInstances(bool log)
Definition: TYElement.h:832
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
Definition: TYElement.cpp:307
TYUUID _uuid
Identifiant unique de l'element.
Definition: TYElement.h:962
void remInstance()
Definition: TYElement.cpp:235
static void setIsSavedOk(const bool &toSave)
Definition: TYElement.h:915
virtual DOM_Element toXML(DOM_Element &domElement)
Definition: TYElement.cpp:368
void setCopyCount(const unsigned int copyCount)
Modifie la vaeur du compteur.
Definition: TYElement.h:557
std::string getMetierName()
Definition: TYElement.cpp:567
bool _isGeometryModified
Indicateur de modification de la geometrie.
Definition: TYElement.h:984
QString _name
Nom courant de l'element.
Definition: TYElement.h:966
bool operator==(const TYElement &other) const
Definition: TYElement.cpp:282
void cleanAllUses()
Definition: TYElement.h:940
void * getAllUses()
Definition: TYElement.h:932
static void purgeInstances()
Definition: TYElement.cpp:171
static bool getRegenerateID()
Definition: TYElement.h:927
TYElement & operator=(const TYElement &other)
Definition: TYElement.cpp:265
static TYUUID fromString(QString id)
Definition: TYElement.cpp:623
void OnChildInCalculStatusChange()
Definition: TYElement.cpp:440
static uint64 getConstructorCount()
Definition: TYElement.cpp:634
bool callFromXMLIfEqual(DOM_Element &domElement, int *pRetVal=NULL)
Definition: TYElement.cpp:544
static bool testId(const TYUUID &id, const TYElement *pElem)
Definition: TYElement.cpp:216
static bool getLogInstances()
Definition: TYElement.h:842
static uint64 getIdGenerationCount()
Definition: TYElement.cpp:643
bool _isAcousticModified
Indicateur de modification acoustique.
Definition: TYElement.h:981
void regenerateID()
Definition: TYElement.cpp:243
bool hasNullID() const
Definition: TYElement.h:658
static uint64 getDestructorCount()
Definition: TYElement.cpp:638
void addCopy()
Increment le compteur de nombre de copies.
Definition: TYElement.h:549
const TYUUID & getID() const
Definition: TYElement.cpp:176
bool operator!=(const TYElement &other) const
Definition: TYElement.cpp:302
static TYUUID newID()
Definition: TYElement.cpp:615
void * _allUses
Multi purpose void pointer (use for compatibility actually)
Definition: TYElement.h:990
virtual std::string toString() const
Definition: TYElement.h:786
virtual QString getName() const
Definition: TYElement.h:684
TYElement * _pParent
Reference sur l'element parent.
Definition: TYElement.h:969
virtual void updateCurrentCalcul(TYListID &listID, bool recursif=true)
Definition: TYElement.cpp:460
bool getIsAcousticModified()
Definition: TYElement.h:718
LPTYElementGraphic _pGraphicObject
L'object graphique metier associe a cet element.
Definition: TYElement.h:987
virtual void setInCurrentCalcul(bool state, bool recurschild=true, bool recursparent=true)
Definition: TYElement.cpp:410
virtual void getChilds(LPTYElementArray &childs, bool recursif=true)
Definition: TYElement.h:532
static void setRegenerateID(const bool &bRegenerate)
Definition: TYElement.h:923
void setName(std::string name)
Definition: TYElement.h:676
static TYElementContainer * _instances
Collection de toutes les instances de type TYElement et derivees.
Definition: TYElement.h:997
bool _inCurrentCalcul
Indique si cet element est actif dans le Calcul courant.
Definition: TYElement.h:978
void setParent(TYElement *pParent)
Definition: TYElement.h:692
static LPTYElementArray findTypeCollectionAndCallFromXML(DOM_Element parentElem, const char *type)
Definition: TYElement.cpp:572
virtual ~TYElement()
Definition: TYElement.cpp:139
virtual int fromXML(DOM_Element domElement)
Definition: TYElement.cpp:381
void setAllUses(void *allUses)
Definition: TYElement.h:936
QString getStringID() const
Definition: TYElement.h:653
virtual void setIsAcousticModified(bool isModified)
Definition: TYElement.cpp:248
unsigned int _copyCount
Definition: TYElement.h:975
static uint64 ty_destroyed_counter
Definition: TYElement.h:1006
static bool getIsSavedOk()
Definition: TYElement.h:907
static uint64 ty_created_counter
Definition: TYElement.h:1005
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:1003
virtual void setIsGeometryModified(bool isModified)
Definition: TYElement.cpp:253
This file provides class for solver configuration.
unsigned long long uint64
Definition: defines.h:65
boost::error_info< struct tag_classname, std::string > oproto_classname_errinfo
Definition: TYElement.h:92