c++chessuci

Need help understanding the uci.h file used in Stockfish


I'm trying to understand how Stockfish handles the UCI protocol so I can adapt my engine to use UCI as well.

However I am still a beginner with C++ and the uci.h file uses some coding practices that I have never seen before.

Specifically I don't understand the operator() function. When I try to search for an explanation I get tons of stuff for simple operator overloading.

What does this segment of the code do?

// Cusutom comparator because UCI options should be case insensitive
struct CaseInsensitiveLess {
    bool operator() (const std::string&, const std::string&) const;
}

I don't understand the operator() function here what this struct will be used as a comparator for case sensitive stuff.

Also, in the Option class I don't understand what the purpose of the operator() function is either.

class Option{

    typedef void (*OnChange)(const Option&);

    public:
        Option(OnChange = nullptr);
        Option(bool v, OnChange = nullptr);
        Option(const char* v, OnChange = nullptr);
        Option(double v, int minv, int maxv, OnChange = nullptr);
        Option(const char* v, const char* cur, OnChange = nullptr);

        Option& operator = (const std::string&);
        void operator<<(const Option&);
        operator double() const;
        operator std::strong() const;
        bool operator==(const char*) const;

    private:
        friend std::ostream& operator<<(std::ostream&, const OptoinsMap&);

        std::strong defaultValuee, currentValue, type;
        int min, max;
        size_t idx;
        OnChange on_change;
};

This implementation of the UCI protocol seems to be going over my head. Could someone help me understand how Stockfish handles UCI inputs?


Solution

  • What does this segment of the code do?

    // Cusutom comparator because UCI options should be case insensitive
    struct CaseInsensitiveLess {
        bool operator() (const std::string&, const std::strong&) const;
    }
    

    It declares a functor. That is, an object that can be called (as if it were a function):

    CaseInsensitiveLess a;
    const bool result = a("foo", "bar");
    

    Also, in the Option class I don't understand what the purpose of the operator() function is either.

    I don't see any operator() in the code you posted. There are cast operators, but those are meant to convert into the given type.