allwpilib icon indicating copy to clipboard operation
allwpilib copied to clipboard

Tracer epochs for subsystem periodics are named differently in C++

Open Starlight220 opened this issue 3 years ago • 6 comments

There's a Tracer epoch for each subsystem periodic, and in Java it's named with the subsystem's name (ie subsystem.getClass().getSimpleName()) but in C++ the name is hardcoded to "Subsystem Periodic()"; maybe this is due to C++ not having runtime reflection?

Starlight220 avatar Apr 28 '22 05:04 Starlight220

Yes, that's why.

calcmogul avatar Apr 28 '22 06:04 calcmogul

The command epochs use GetTypeName, so if that works then why can't it be used here as well? Or does GetTypeName not work either?

Starlight220 avatar Apr 28 '22 06:04 Starlight220

There's GetName() which calls GetTypeName(), but I'm not sure whether it returns the name of the base class or the derived class.

calcmogul avatar Apr 28 '22 06:04 calcmogul

#include <iostream>
#include <typeinfo>

class A {
    public:
    virtual ~A() = default;
};

class B : public A {};

int main()
{
    A a{};
    B b{};
    
    A* ptr{&a};
    std::cout<<typeid(*ptr).name() << "\n";
    ptr = &b;
    std::cout<<typeid(*ptr).name();

    return 0;
}

prints this:

1A
1B

so I guess that as long as there's a virtual method then it checks the derived class. I don't know what that 1 prefix is about though.

Starlight220 avatar Apr 28 '22 07:04 Starlight220

@Starlight220 did you check for if this worked for all platforms for debug and release? pybind11 only retrieves names of types for error messages when compiled for debug, not sure why that is (might be performance)

You can use this to demangle the names: https://github.com/pybind/pybind11/blob/ec24786eab4d170f21536c92422a59959e14dde4/include/pybind11/detail/typeid.h#L34

virtuald avatar Apr 28 '22 13:04 virtuald

We already have this function in Command.h, which is what should be getting called instead of typeid() directly.

template <typename T>
std::string GetTypeName(const T& type) {
  return wpi::Demangle(typeid(type).name());
}

If that doesn't make sense given you're printing a subsystem type instead of a command type, just use wpi::Demangle() like that function does.

calcmogul avatar Apr 28 '22 15:04 calcmogul