Code_TYMPAN  4.2.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 
20 #ifdef HAVE_CONFIG_H
21  #include "config.h"
22 #endif // HAVE_CONFIG_H
23 
24 #if _MSC_VER > 1000
25  #pragma once
26 #endif // _MSC_VER > 1000
27 #ifdef _MSC_VER
28  #pragma warning( disable : 4275 4800 4355 4273 4786 4018 4503 4081)
29 #endif
30 
31 #include <memory>
32 #include <unordered_map>
33 #include <iostream>
34 
35 #include <boost/exception/error_info.hpp>
36 
37 #ifndef _NDEBUG
38  #include <QDebug>
39 #endif // _NDEBUG
40 
41 #include "Tympan/core/defines.h"
42 #include "Tympan/core/macros.h"
43 #include "Tympan/core/idgen.h"
45 #if TY_USE_IHM
48 #endif // TY_USE_IHM
49 #include "TYDefines.h"
50 #include "TYXMLTools.h"
51 
52 
53 // Nom de la categorie pour la sauvegarde des preferences.
54 #define TYDIRPREFERENCEMANAGER "UserPreferences"
55 
57 // Definition de macros pour la creation de nouveaux types
58 // derives de OPrototype.
59 
67 #define OPROTODECL(classname) \
68  public: \
69  virtual OPrototype* clone() const { \
70  return new classname(); \
71  } \
72  virtual const char* getClassName() const { return #classname; } \
73  static classname* safeDownCast(OPrototype * pObject) { \
74  if (pObject) { \
75  classname * pTypedObject = dynamic_cast<classname *>(pObject); \
76  if (pTypedObject != nullptr) { \
77  return pTypedObject; \
78  } \
79  } \
80  return nullptr;\
81  } \
82 
83 
85 namespace tympan
86 {
87  typedef boost::error_info < struct tag_classname, std::string >
89 } // namespace tympan
90 
91 
202 {
203  // Methodes
204 public:
208  virtual ~OPrototype();
209 
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 { return "OPrototype"; }
249 
258  bool isA(const char* className) const;
259 
269  static OPrototype* safeDownCast(OPrototype* pObject);
270 
278  {
279  public:
280  typedef std::unique_ptr<IOProtoFactory> ptr_type;
281  virtual std::unique_ptr<OPrototype> make() = 0;
282  };
283 
292  template<typename T>
293  class Factory : public IOProtoFactory
294  {
295  public:
296  typedef std::unique_ptr<T> ptr_type;
297 
298  static_assert(std::is_base_of<OPrototype, T>::value,
299  "Factory<T> : T must inherit from OPrototype");
300 
301  ptr_type typed_make () { return ptr_type(new T()); }
302  virtual std::unique_ptr<OPrototype> make () { return typed_make(); }
303  };
304 
308  static void add_factory(const char*, IOProtoFactory::ptr_type factory);
309 
310 protected:
311 
315  OPrototype();
316 
317 // Membres
318 private:
319 
324  static std::unordered_map<std::string, IOProtoFactory::ptr_type> _factory_map;
325 
326 };
327 
335 template<typename T>
336 std::unique_ptr<OPrototype::Factory<T> > build_factory(){return std::unique_ptr<OPrototype::Factory<T> >( new OPrototype::Factory<T>() ); }
337 
338 typedef std::vector<LPTYElement> LPTYElementArray;
339 
340 typedef std::map<TYUUID, TYElement*> TYElementContainer;
341 
346 #if TY_USE_IHM
347 
348 #if defined(WIN32)
349 #define TY_EXTENSION_DECL(classname) \
350  friend class classname; \
351  public: \
352  virtual TYEditWidget* getEditWidget() { \
353  return new classname##Widget(this); \
354  }
355 #else
356 #define TY_EXTENSION_DECL(classname) \
357  public: \
358  virtual TYEditWidget* getEditWidget() { \
359  return new classname##Widget(this); \
360  }
361 #endif
362 
363 #define TY_EXTENSION_DECL_ONLY(classname) \
364  public: \
365  virtual TYEditWidget* getEditWidget() ;
366 
367 #define TY_EXTENSION_INST(classname) \
368  TYEditWidget* classname::getEditWidget() { \
369  return new classname##Widget(this); \
370  }
371 
372 #else // TY_USE_IHM
373 
374 #define TY_EXTENSION_DECL(classname)
375 #define TY_EXTENSION_DECL_ONLY(classname)
376 #define TY_EXTENSION_INST(classname)
377 #endif // TY_USE_IHM
378 
379 
383 #if TY_USE_IHM
384 
385 #if defined(WIN32)
386 #define TY_EXT_GRAPHIC_DECL(classname) \
387  friend class classname; \
388  public: \
389  virtual LPTYElementGraphic getGraphicObject() { \
390  if (!_pGraphicObject) _pGraphicObject = new classname##Graphic(this); \
391  return _pGraphicObject; \
392  }
393 #else
394 #define TY_EXT_GRAPHIC_DECL(classname) \
395  public: \
396  virtual LPTYElementGraphic getGraphicObject() { \
397  if (!_pGraphicObject) _pGraphicObject = new classname##Graphic(this); \
398  return _pGraphicObject; \
399  }
400 #endif
401 
402 #define TY_EXT_GRAPHIC_DECL_ONLY(classname) \
403  public: \
404  virtual LPTYElementGraphic getGraphicObject();
405 
406 #define TY_EXT_GRAPHIC_INST(classname) \
407  LPTYElementGraphic classname::getGraphicObject() { \
408  if (!_pGraphicObject) _pGraphicObject = new classname##Graphic(this); \
409  return _pGraphicObject; \
410  }
411 
412 #else
413 
414 #define TY_EXT_GRAPHIC_DECL(classname)
415 #define TY_EXT_GRAPHIC_DECL_ONLY(classname)
416 #define TY_EXT_GRAPHIC_INST(classname)
417 typedef void* LPTYElementGraphic;
418 #endif // TY_USE_IHM
419 
423 typedef class OGenID TYUUID;
424 
425 
426 template<typename T>
428 {
429  return T::safeDownCast(elt);
430 }
431 
432 
436 class TYElement : public IRefCount, public OPrototype
437 {
439 
440  // Methodes
441 public:
446  TYElement();
447 
455  TYElement(TYElement* pParent, bool PutInInstanceList = true);
456 
461  TYElement(const TYElement& other, bool PutInInstanceList = true);
462 
466  virtual ~TYElement();
467 
469 
474 
483  virtual DOM_Element toXML(DOM_Element& domElement) /* const */;
492  virtual int fromXML(DOM_Element domElement);
493 
495 
500 
512  virtual void updateCurrentCalcul(TYListID& listID, bool recursif = true);
513 
517  virtual void getChilds(LPTYElementArray& childs, bool recursif = true) {}
518 
522  virtual void setInCurrentCalcul(bool state, bool recurschild = true, bool recursparent = true);
526  virtual bool isInCurrentCalcul() { return _inCurrentCalcul; }
527 
531  void addCopy() { _copyCount++; }
532 
536  void setCopyCount(const unsigned int copyCount) { _copyCount = copyCount; }
537 
538 private:
539 
543  void OnChildInCalculStatusChange();
544 
546 
547 public:
548 
549 #if TY_USE_IHM
550 
555 
561  virtual LPTYElementGraphic getGraphicObject() { return NULL; }
562 
566  void drawGraphic(bool draw = true);
567 
571  virtual void updateGraphic();
572 
576  virtual void updateGraphicTree();
577 
579 
584 
593  virtual TYEditWidget* getEditWidget();
594 
605  int edit(TYEditWidget* pParent = NULL);
606 
608 #endif
609 
614 
618  void setID(TYUUID id);
619  void setID(const QString& str_id);
620 
624  const TYUUID& getID() const;
628  QString getStringID() const { return getID().toString();}
629 
630  bool hasNullID() const { return _uuid.isNull(); }
631 
635  void regenerateID();
636 
640  void setName(QString name) { _name = name; }
641 
642  void setName(std::string name) { _name = QString::fromStdString(name); }
643 
647  virtual QString getName() const { return _name; }
648 
652  void setParent(TYElement* pParent) { _pParent = pParent; }
656  TYElement* getParent() const { return _pParent; }
657 
659 
664 
668  virtual void setIsAcousticModified(bool isModified);
672  bool getIsAcousticModified() { return _isAcousticModified; }
673 
677  virtual void setIsGeometryModified(bool isModified);
678 
682  bool getIsGeometryModified() { return _isGeometryModified; }
683 
685 
690 
694  TYElement& operator=(const TYElement& other);
695 
699  bool operator==(const TYElement& other) const;
700 
704  bool operator!=(const TYElement& other) const;
705 
718  virtual bool deepCopy(const TYElement* pOther, bool copyId = true, bool pUseCopyTag = false);
719 
721 
726 
727 
728 
735  virtual std::string toString() const { return "TYElement"; };
736 
747  bool callFromXMLIfEqual(DOM_Element& domElement, int* pRetVal = NULL);
748 
756  std::string getMetierName();
757 
759 
770  static LPTYElementArray findTypeCollectionAndCallFromXML(DOM_Element parentElem, const char* type);
771 
778  static void setLogInstances(bool log) { _logInstances = log; }
785  static bool getLogInstances() { return _logInstances; }
786 
795  static TYElement* getInstance(TYUUID uuid);
796 
800  static void purgeInstances();
801 
805  static bool testId(const TYUUID& id, const TYElement* pElem);
806 
807 
813  static TYUUID newID();
814 
819  static TYUUID fromString(QString id);
820 
825  static QString toString(TYUUID& uuid);
826 
831  static uint64 getConstructorCount();
832 
837  static uint64 getDestructorCount();
838 
843  static uint64 getIdGenerationCount();
844 
848  static bool getIsSavedOk() { return _toSave; }
849 
853  static void setIsSavedOk(const bool& toSave) { _toSave = toSave; }
854 
858  static void setRegenerateID(const bool& bRegenerate) { _bRegenerateID = bRegenerate; }
859  static bool getRegenerateID() { return _bRegenerateID; }
860 
861  void* getAllUses() { return _allUses; }
862  void setAllUses( void* allUses ) { _allUses = allUses; }
863  void cleanAllUses() { free(_allUses); _allUses = nullptr; }
864 
865 private:
870  void addInstance();
871 
876  void remInstance();
877 
878  // Membres
879 private:
881  mutable TYUUID _uuid;
882 
883 protected:
885  QString _name;
886 
889 
890  //Pour eviter de grossir la liste d'instance avec des objets temporaires:
892 
893  // Decompte du nombre de copies
894  unsigned int _copyCount;
895 
898 
901 
904 
907 
909  void *_allUses;
910 
911 private:
913  static bool _logInstances;
915  //XXX This should be a map (of weak pointers) not a list !
917  static TYElementContainer& getInstances();
919  static bool _toSave;
921  static bool _bRegenerateID;
922 
926 };
927 
928 // Some simple auxilliary fonction to help debugging and reporting
929 
930 inline const char* str_qt2c(const QString& qstr)
931 { return qstr.toLocal8Bit().data(); } // TODO Clarifiy consistency wrt Local8Bit vs Utf8
932 
933 inline QString xml2qstring(const QDomNode& node)
934 {
935  QString msg; QTextStream str(&msg);
936  node.save(str, 0);
937  return msg;
938 }
939 
940 inline const char* xml2cstring(const QDomNode& node)
941 { return str_qt2c(xml2qstring(node)); }
942 
943 #ifndef _NDEBUG
944 inline QDebug _debugXml(const char* expr, const QDomNode& node)
945 { return qDebug() << expr << xml2qstring((node)); }
946 
947 #define debugXml(expr) _debugXml(#expr, (expr))
948 #else // _NDEBUG
949 #define debugXml(expr) ((void)0)
950 #endif // _NDEBUG
951 
952 #endif // __TY_ELEMENT__
static uint64 ty_regen_id_counter
Definition: TYElement.h:925
LPTYElementGraphic _pGraphicObject
L&#39;object graphique metier associe a cet element.
Definition: TYElement.h:906
static void setLogInstances(bool log)
Definition: TYElement.h:778
std::map< TYUUID, TYElement * > TYElementContainer
Definition: TYElement.h:340
void * getAllUses()
Definition: TYElement.h:861
QDomElement DOM_Element
Definition: QT2DOM.h:31
static bool _toSave
Indicateur de modification. Passe a true si un TYElement a ete modifie, Indique la necessite de sauve...
Definition: TYElement.h:919
void setParent(TYElement *pParent)
Definition: TYElement.h:652
TYElement * _pParent
Reference sur l&#39;element parent.
Definition: TYElement.h:888
void setName(QString name)
Definition: TYElement.h:640
T fromString(const std::string &str, std::ios_base &(*f)(std::ios_base &)=std::dec)
Definition: macros.h:216
virtual void getChilds(LPTYElementArray &childs, bool recursif=true)
Definition: TYElement.h:517
const char * name
bool getIsGeometryModified()
Definition: TYElement.h:682
std::unique_ptr< T > ptr_type
Definition: TYElement.h:296
std::vector< LPTYElement > LPTYElementArray
Definition: TYElement.h:338
static bool getRegenerateID()
Definition: TYElement.h:859
static TYElementContainer * _instances
Collection de toutes les instances de type TYElement et derivees.
Definition: TYElement.h:916
static bool _logInstances
Indique si on souhaite registrer toutes les instances de type TYElement et derivees.
Definition: TYElement.h:913
This file provides class for solver configuration.
void TYEditWidget
Declarations de types pour l&#39;API IHM et l&#39;Impression.
Definition: TYDefines.h:53
bool getIsAcousticModified()
Definition: TYElement.h:672
virtual std::string toString() const
Definition: TYElement.h:735
virtual bool isInCurrentCalcul()
Definition: TYElement.h:526
void setName(std::string name)
Definition: TYElement.h:642
static void setRegenerateID(const bool &bRegenerate)
Definition: TYElement.h:858
bool _isGeometryModified
Indicateur de modification de la geometrie.
Definition: TYElement.h:903
void setCopyCount(const unsigned int copyCount)
Modifie la vaeur du compteur.
Definition: TYElement.h:536
const char * str_qt2c(const QString &qstr)
Definition: TYElement.h:930
QString xml2qstring(const QDomNode &node)
Definition: TYElement.h:933
std::unique_ptr< OPrototype::Factory< T > > build_factory()
Definition: TYElement.h:336
void * _allUses
Multi purpose void pointer (use for compatibility actually)
Definition: TYElement.h:909
unsigned int _copyCount
Definition: TYElement.h:894
static bool _bRegenerateID
Indicateur de regeneration d&#39;ID true si regeneration d&#39;ID a la lecture d&#39;un fichier XML...
Definition: TYElement.h:921
static bool getIsSavedOk()
Definition: TYElement.h:848
unsigned long long uint64
Definition: defines.h:66
void setAllUses(void *allUses)
Definition: TYElement.h:862
outil IHM pour un element (fichier header)
TYElement * getParent() const
Definition: TYElement.h:656
static bool getLogInstances()
Definition: TYElement.h:785
#define OPROTODECL(classname)
Definition: TYElement.h:67
QString _name
Nom courant de l&#39;element.
Definition: TYElement.h:885
boost::error_info< struct tag_classname, std::string > oproto_classname_errinfo
Definition: TYElement.h:88
QString getStringID() const
Definition: TYElement.h:628
ptr_type typed_make()
Definition: TYElement.h:301
void * LPTYElementGraphic
Definition: TYElement.h:417
static std::unordered_map< std::string, IOProtoFactory::ptr_type > _factory_map
Definition: TYElement.h:324
virtual const char * getClassName() const
Definition: TYElement.h:248
TYUUID _uuid
Identifiant unique de l&#39;element.
Definition: TYElement.h:881
virtual std::unique_ptr< OPrototype > make()
Definition: TYElement.h:302
bool _bPutInInstanceList
Definition: TYElement.h:891
Definition: idgen.h:29
static void setIsSavedOk(const bool &toSave)
Definition: TYElement.h:853
class OGenID TYUUID
Definition: TYDefines.h:63
const char * xml2cstring(const QDomNode &node)
Definition: TYElement.h:940
Representation graphique d&#39;un element de base (fichier header)
T * downcast(TYElement *elt)
Definition: TYElement.h:427
void cleanAllUses()
Definition: TYElement.h:863
std::list< TYUUID > TYListID
Collection d&#39;identifiants.
Definition: TYDefines.h:336
bool _isAcousticModified
Indicateur de modification acoustique.
Definition: TYElement.h:900
virtual QString getName() const
Definition: TYElement.h:647
void addCopy()
Increment le compteur de nombre de copies.
Definition: TYElement.h:531
static uint64 ty_created_counter
Definition: TYElement.h:923
QDebug _debugXml(const char *expr, const QDomNode &node)
Definition: TYElement.h:944
std::unique_ptr< IOProtoFactory > ptr_type
Definition: TYElement.h:280
bool _inCurrentCalcul
Indique si cet element est actif dans le Calcul courant.
Definition: TYElement.h:897
bool hasNullID() const
Definition: TYElement.h:630
static uint64 ty_destroyed_counter
Definition: TYElement.h:924