Assignement conditionel

Assignement conditionel - C++ - Programmation

Marsh Posté le 30-04-2005 à 18:09:44    

Je savais pas trop comment poser ma question, mais j'aimerai faire un truc comme ça:
 

Code :
  1. class truc
  2. {
  3. float a,b;
  4. int i,j;
  5. public:
  6.    InitFromXml( const XmlElement &Xml );
  7. }
  8. truc::InitFromXml( const XmlElement &Xml )
  9. {
  10.    a=10.5;
  11.    b=20.3;
  12.    i=5;
  13.    j=6;
  14.    a = Xml[ "param_a" ];
  15.    b = Xml[ "param_b" ];
  16.    i = Xml[ "param_i" ];
  17. }


 
bref, faire un ' a = Xml[ "param_a" ] ' qui marche c'est pas le prob, le prob c'est que j'aimerai que si l'attribut Xml (en l'occurance) n'est pas trouvé, a ne soit pas assigné.
 
parce qu'avec un truc style:
 
GetXml( "param_a", a );
 
utilisant un GetXml( const std::string &, float & );
par exemple OK, là je peux ne pas modifier 'a' si j'ai en envie, mais dans le cas d'un assignement, ou cast en passant par une structure intermédiaire, je vois pas comment ruser le lapin vert.


Message édité par bjone le 30-04-2005 à 18:10:21
Reply

Marsh Posté le 30-04-2005 à 18:09:44   

Reply

Marsh Posté le 30-04-2005 à 18:29:29    

Moi je me satisferais entièrement d'un GetXml("param_a", a);
 
Parce que bon, les implications d'un "a=b;" qui est exécuté parfois, ça doit pas être joli à voir...

Reply

Marsh Posté le 30-04-2005 à 21:19:02    

uifgy

Reply

Marsh Posté le 30-04-2005 à 21:23:57    

t'as qu'à faire en sorte que a ne soit plus un int et se bien surcharger constructeurs et operator= et tu peux avoir quelque chose comme tu veux. Mais ça nuit à la lisibilité puisque lors de la surcharge d'opérateurs, on chercher à conserver la sémantique originale.


Message édité par Taz le 30-04-2005 à 21:24:25
Reply

Marsh Posté le 01-05-2005 à 00:06:52    

oué, maos ça me tentais bien d'avoir un truc le plus court possible à lire/écrire.

Reply

Marsh Posté le 01-05-2005 à 16:39:05    

ouep, finalement je vais faire un << pour ça, ça sera moins ambigu.

Reply

Marsh Posté le 02-05-2005 à 23:51:25    

Bon j'ai testé une approche qui permet d'écrire comme ça:
ça me plait pas trop mal.
 
avec un fichier xmeul pas compliant style:

Code :
  1. <Yop A="84" B="95.776505" Test="20" wesh="7139.703064" zorg="true" />
  2. <Youpi rg="24" />
  3. <Yop A="90" Test="20" wesh="-9577.649780" B="-119.720622" zorg="false" />
  4. <Youpa rg="644" />
  5. <Yop B="-167.608908" zorg="false" Test="20" wesh="-14018.199585" A="94" />
  6. <zz a="98" />


 

Code :
  1. #include <iostream>
  2. #include "XmlElement.h"
  3. using namespace std;
  4. int main()
  5. {
  6. TiXmlDocument Doc;
  7. if( Doc.LoadFile("test.xml" ) )
  8. {
  9.  XmlElement Xml=Doc.FirstChildElement("Yop" );
  10.  while( Xml )
  11.  {
  12.   int A=12;
  13.   float B=-20;
  14.   bool zorg=false;
  15.   Xml["A"]>>A;
  16.   Xml["B"]>>B;
  17.   Xml["zorg"]>>zorg;
  18.   cout<<A<<" "<<B<<" : "<<zorg<<"\n";
  19.   Xml["A"]<<A+2;
  20.   Xml["B"]<<B*1.1;
  21.   Xml["wesh"]<<A*B;
  22.   Xml["zorg"]<<(B>A);
  23.   ++Xml;
  24.  }
  25.  Doc.SaveFile();
  26. }
  27. }


 
bon c'est une surcouche à TinyXml, en mode cracra const char * et pas STL, demain je fais la variante STL et je m'y colle, sinon coté code ça donne ça:
 
XmlElement.h

Code :
  1. #pragma once
  2. #include <tinyxml.h>
  3. struct XmlElementAttribute
  4. {
  5. TiXmlElement *Element;
  6. const char *Attribute;
  7. XmlElementAttribute( TiXmlElement *Elem, const char *Attr ) : Element(Elem), Attribute(Attr) {};
  8. const char *GetValue() const
  9. {
  10.  if( !Element || !Attribute )
  11.   return NULL;
  12.  return Element->Attribute(Attribute);
  13. }
  14. operator const char*() const
  15. {
  16.  return GetValue();
  17. }
  18. };
  19. class XmlElement
  20. {
  21. public:
  22. TiXmlElement *Element;
  23. XmlElement( TiXmlElement *Elem=NULL ) : Element(Elem) {};
  24. XmlElement &operator = (TiXmlElement *Elem )
  25. {
  26.  Element=Elem;
  27.  return *this;
  28. }
  29. operator TiXmlElement * () const
  30. {
  31.  return Element;
  32. }
  33. TiXmlElement * operator -> () const
  34. {
  35.  return Element;
  36. }
  37. XmlElement &operator ++ ();
  38. const XmlElementAttribute operator [] ( const char *AttributeName ) const
  39. {
  40.  return XmlElementAttribute( Element, AttributeName );
  41. }
  42. };
  43. int operator >> ( const XmlElementAttribute &ElemAttr, int &Value );
  44. float operator >> ( const XmlElementAttribute &ElemAttr, float &Value );
  45. double operator >> ( const XmlElementAttribute &ElemAttr, double &Value );
  46. bool operator >> ( const XmlElementAttribute &ElemAttr, bool &Value );
  47. const char * operator << ( const XmlElementAttribute &ElemAttr, const char *Value  );
  48. int operator << ( const XmlElementAttribute &ElemAttr, int Value  );
  49. double operator << ( const XmlElementAttribute &ElemAttr, double Value  );
  50. bool operator << ( const XmlElementAttribute &ElemAttr, bool Value  );


 
XmlElement.cpp

Code :
  1. include <assert.h>
  2. #include "XmlElement.h"
  3. #include "Size.h"
  4. XmlElement &XmlElement::operator ++ ()
  5. {
  6. if( Element )
  7. {
  8.  const char *Name=Element->Value();
  9.  if( Name )
  10.   Element=Element->NextSiblingElement(Name);
  11.  else
  12.   Element=NULL;
  13. }
  14. return *this;
  15. }
  16. //////////////////////////////////////////////////////////////////////////////
  17. //////////////////////////////////////////////////////////////////////////////
  18. bool ExtractInt( const char *Str, int &Value )
  19. {
  20. assert(Str);
  21. if( sscanf(Str,"%d",&Value) == 1 || sscanf( Str,"%x",&Value) == 1 )
  22.  return true;
  23. return false;
  24. }
  25. bool ExtractFloat( const char *Str, float &Value )
  26. {
  27. assert(Str);
  28. if( sscanf(Str,"%f",&Value) == 1 )
  29.  return true;
  30. else
  31. {
  32.  int iValue;
  33.  if( ExtractInt( Str, iValue ) )
  34.  {
  35.   Value=(float)iValue;
  36.   return true;
  37.  }
  38. }
  39. return false;
  40. }
  41. bool ExtractDouble( const char *Str, double &Value )
  42. {
  43. assert(Str);
  44. if( sscanf(Str,"%lf",&Value) == 1 )
  45.  return true;
  46. else
  47. {
  48.  float fValue;
  49.  if( ExtractFloat( Str, fValue) )
  50.  {
  51.   Value=fValue;
  52.   return true;
  53.  }
  54. }
  55. return false;
  56. }
  57. int operator >> ( const XmlElementAttribute &ElemAttr, int &Value )
  58. {
  59. if( const char *Attribute=ElemAttr.GetValue() )
  60.  ExtractInt( Attribute, Value );
  61. return Value;
  62. }
  63. float operator >> ( const XmlElementAttribute &ElemAttr,  float &Value )
  64. {
  65. if( const char *Attribute=ElemAttr.GetValue() )
  66.  ExtractFloat( Attribute, Value );
  67. return Value;
  68. }
  69. double operator >> ( const XmlElementAttribute &ElemAttr, double &Value )
  70. {
  71. if( const char *Attribute=ElemAttr.GetValue() )
  72.  ExtractDouble( Attribute, Value );
  73. return Value;
  74. }
  75. bool operator >> ( const XmlElementAttribute &ElemAttr, bool &Value )
  76. {
  77. const char *TrueStrings[]={ "true", "valid", "enable", "enabled" };
  78. const char *FalseStrings[]={ "false", "invalid", "disable", "disabled" };
  79. if( const char *Attribute=ElemAttr.GetValue() )
  80. {
  81.  for( size_t i=0 ; i < size(TrueStrings) ; ++i )
  82.   if( !stricmp(TrueStrings[i],Attribute) )
  83.    return Value=true;
  84.  for( size_t i=0 ; i < size(FalseStrings) ; ++i )
  85.   if( !strcmpi(FalseStrings[i],Attribute) )
  86.    return Value=false;
  87.  int iValue;
  88.  if( ExtractInt( Attribute, iValue ) )
  89.   return Value=(iValue!=0);
  90. }
  91. return Value;
  92. }
  93. //////////////////////////////////////////////////////////////////////////////
  94. //////////////////////////////////////////////////////////////////////////////
  95. const char * operator << ( const XmlElementAttribute &ElemAttr, const char *Value  )
  96. {
  97. if( ElemAttr.Element )
  98.  ElemAttr.Element->SetAttribute(ElemAttr.Attribute,Value);
  99. return Value;
  100. }
  101. int operator << ( const XmlElementAttribute &ElemAttr, int Value  )
  102. {
  103. if( ElemAttr.Element )
  104.  ElemAttr.Element->SetAttribute( ElemAttr.Attribute, Value );
  105. return Value;
  106. }
  107. double operator << ( const XmlElementAttribute &ElemAttr, double Value  )
  108. {
  109. if( ElemAttr.Element )
  110.  ElemAttr.Element->SetDoubleAttribute( ElemAttr.Attribute, Value );
  111. return Value;
  112. }
  113. bool operator << ( const XmlElementAttribute &ElemAttr, bool Value  )
  114. {
  115. if( ElemAttr.Element )
  116.  ElemAttr.Element->SetAttribute( ElemAttr.Attribute, Value ? "true" : "false" );
  117. return Value;
  118. }


 
bon oki, const char*, strcmpi, sscanf, const char * blog[] ça pue du bec, mais c'était un essai.
des remarques ?
y'a moyen de simplifier ?
c'est de la mairde ça sert à rien ?
j'ai que du temps à perdre ?
sapu c pas libre ? (en fait si)


Message édité par bjone le 02-05-2005 à 23:52:15
Reply

Marsh Posté le 03-05-2005 à 11:02:58    

diantre c'est si pourri que ça ?

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed