Библиотеки: Input_Struct - страница 5

 
fxsaber #:
This is also an approach
 



I found a strange BUG. As long as there are more than two characters in front of "Input", a 5040 error will be reported。

#define MACROS_MULTI  INPUT(bbInput09, bool, false)



  int FromString( string Str, const int PosIndex = 0 )
  {
//    ::ZeroMemory(this);
    int iAmount2 = 0;
    Str = StringDeleteChar_INPUT_STRUCT(Str);

  #define MACROS(A, B, C)                                                  \
    if ((Pos < iAmount) && (iIndexes[Pos] == iCount++))                    \
    {                                                                      \
      string StrTmp = GetString_INPUT_STRUCT(Str, MACROS1(A), PosIndex);   \
                                                                           \
      if (StrTmp == NULL)                                                  \
        StrTmp = GetString_INPUT_STRUCT(Str, "in" + MACROS1(A), PosIndex); \
                                                                           \
      if (StrTmp != NULL)                                                  \
      {                                                                    \
        Set_INPUT_STRUCT(this.A, StrTmp);                                  \
        iAmount2++;                                                        \
      }                                                                    \
                                                                           \
      Pos++;                                                               \
    }
    MACROS_MULTI_METHOD(true)
  #undef MACROS
    Print("err=", GetLastError());
    return(iAmount2);
  }
 
  static void Set_INPUT_STRUCT( bool &Value, const string &Str )
  {
    //Value = (bool)(double)Str || !::StringFind(Str, "true"); // GetLastError() == 5040
    Value = ::StringFind(Str, "false") == 0 || ::StringFind(Str, "true") == 0; // GetLastError() == 0
    Print(Str, " err=", GetLastError());
    return;
  }
fixed this issue
 
hini #:


I found a strange BUG. As long as there are more than two characters in front of "Input", a 5040 error will be reported。

void OnStart()
{
  string Str = "Hello World!";
  int i = (int)Str;
  
  Print(_LastError); // 5040
}


Thanks, fixed.

 

Author, I have a question I need your help with. I want to set a specific value based on a name. When the second parameter of SetValue is a double or int, it works normally. However, if the second parameter is a string, it fails to compile, with an error saying that string cannot be converted to bool. How can I solve this issue?

Would you like me to provide any suggestions or explanations regarding this programming issue?

Let me explain why I don't use Inputs.CustomLot = "0.01-0.02"; It's because it's dynamically controlled at runtime. Sometimes it's not setting CustomLot, but rather another input variable xxxx. I hope to write the code only once and be able to set all input parameters.
INPUT(CustomLot, string, "0.01-0.02-0.03-0.04-0.05")

Inputs.SetValue("CustomLot", "0.01-0.02");


  template <typename T>
  bool SetValue(const string &Name, T Value)
  {
     #define MACROS(A, B, C) if (Name == #A) { \
        this.A = (B)(Value); \
        return true; \
     }
     MACROS_MULTI
     #undef MACROS
     return false; // Not Found Name
  }
 
hini #:

Let me explain why I don't use Inputs.CustomLot = "0.01-0.02"; It's because it's dynamically controlled at runtime. Sometimes it's not setting CustomLot, but rather another input variable xxxx. I hope to write the code only once and be able to set all input parameters.

Memo.

Inputs = "CustomLot = 0.01-0.02";
 
fxsaber #:

Memo.

Thank you very much. I thought I could only set all parameters at once with Inputs = inInputsAll;
 


我发现了另一个奇怪的错误。当 INPUT 包含字符串时,字符串后面的所有参数都会重复一次。请使用以下输入参数进行验证

#define MACROS_MULTI                                           \
  SINPUT(AllowTrade,                bool,                 true)\
  INPUT(CustomStr,                  string,               "666666ss")\
  SINPUT(MinLots,                   double,               0.01)\
  SINPUT(AllowTrade1,                double,                 1)\
  SINPUT(AllowTrade2,                double,                 1)\
  SINPUT(AllowTrade3,                double,                 1)\
  SINPUT(AllowTrade4,                double,                 1)\
  SINPUT(AllowTrade5,                double,                 1)\
  SINPUT(AllowTrade7,                double,                 1)\
  SINPUT(AllowTrade8,                double,                 1)\
  SINPUT(AllowTrade9,                double,                 1)\
  SINPUT(AllowTrade10,                double,                 1)\

class SYSTEM {
public:
  SYSTEM( const string sInputs = NULL ) {}

  virtual void OnTick( void ) = NULL;
};

class MACD : public SYSTEM {
private:
  INPUT_STRUCT Inputs;

public:
  MACD( const string sInputs = NULL ) : SYSTEM(sInputs) {
    this.Inputs.Default();
    Print("len=", this.Inputs.ToString().Length());
    this.Inputs = sInputs;
    Print("len=", this.Inputs.ToString().Length());
    Print(this.Inputs[]);
  }

  virtual void OnTick( void ) {}
};

SYSTEM* System = NULL;

void OnInit() {
  System = new MACD(inInputsAll);
}
 
  
  bool SetDefaultValue(const string &Name)
  {
     #define MACROS(A, B, C) if (Name == #A) { this.A = (B)(C); return true; }
     MACROS_MULTI
     #undef MACROS
     return false; // Not Found Name
  }
  
  string ToJson() const
  { 
      bool firstItem = true;
      string Str = "{";
  
  #define MACROS(A, B, C)                                   \
      if ((Pos < iAmount) && (iIndexes[Pos] == iCount++)) { \
        if (!firstItem) Str += ",";                         \
        Str += "\"" + MACROS1(A) + "\":";                   \
        Str += "\"" + (string)(this.A) + "\"";              \
        firstItem = false;                                  \
        Pos++;                                              \
      }
      MACROS_MULTI_METHOD(true)
  #undef MACROS
      Str += "}";
      return Str;
  }
I hope the author will consider adding these two methods. I think they would be very useful.
 

Fixed this bug, but did not carefully test other situations, such as when containing '|'. Please author review

  static string GetString_INPUT_STRUCT(const string &Str, const string StrMatch, const int PosIndex = 0)
  {
    string StrOut = "";
    const string searchStr = StrMatch + "=";
    const int searchLen = StringLen(searchStr);
    int Pos = StringFind(Str, searchStr);

    while (Pos != -1)
    {
      if (Pos == 0 || Str[Pos - 1] == ',' || Str[Pos - 1] == '\n')
        break;
      Pos = StringFind(Str, searchStr, Pos + searchLen);
    }

    if (Pos != -1)
    {
      Pos += searchLen; 
      int EndPos = StringFind(Str, ",", Pos);
      if (EndPos == -1)
        EndPos = StringLen(Str);

      if (PosIndex == 0)
      {
        StrOut = StringSubstr(Str, Pos, EndPos - Pos);
      }
      else
      {
        int delimiterCount = 0;
        for (int i = Pos; i < EndPos; i++)
        {
          if (StringSubstr(Str, i, 2) == "||")
          {
            delimiterCount++;
            if (delimiterCount == PosIndex)
            {
              Pos = i + 2;
              break;
            }
            i++; 
          }
        }

        if (delimiterCount >= PosIndex)
        {
          int nextDelimiter = StringFind(Str, "||", Pos);
          if (nextDelimiter == -1 || nextDelimiter > EndPos)
            nextDelimiter = EndPos;

          StrOut = StringSubstr(Str, Pos, nextDelimiter - Pos);
         
          if (PosIndex == 4)
            StrOut = (StrOut == "Y") ? "1" : "";
        }
      }
    }

    return StrOut;
  }