Made menu_item::Name private and use cached VisualLength whenever possible.#1114
Made menu_item::Name private and use cached VisualLength whenever possible.#1114MKadaner wants to merge 1 commit into
menu_item::Name private and use cached VisualLength whenever possible.#1114Conversation
| return Name; | ||
| } | ||
|
|
||
| string& MutableName() noexcept |
There was a problem hiding this comment.
Most of the usages of this follow the MutableName() = NewValue; pattern.
Do we really need it?
Maybe something more conventional like SetName(NewValue) will do?
It's too easy to abuse it now by holding a mutable reference and modifying it randomly later.
There was a problem hiding this comment.
Of course, MutableName is not exactly a good idea. I started from SetName but hit a couple of awkward places:
append(ListItem.MutableName(), L" \""sv, strFriendlyName, L"\""sv);and especially
NewItem.MutableName().erase(NewItem.GetName().cbegin(), NewBegin);If you are OK with the use of SetName here, I will sure like it.
There was a problem hiding this comment.
append(ListItem.MutableName(), L" \""sv, strFriendlyName, L"\""sv);
ListItem.SetName(concat(ListItem.GetName(), ...))
NewItem.MutableName().erase(NewItem.GetName().cbegin(), NewBegin);
NewItem.SetName(NewItem.GetName().substr(NewBegin - NewItem.GetName().cbegin()))
Doesn't look too bad.
There was a problem hiding this comment.
I meant
ListItem.SetName(far::format(L"{} \"{}\""sv, ListItem.GetName(), strFriendlyName));Which is not really bad, considering.
| menu_item() = default; | ||
|
|
||
| template<typename T> requires std::constructible_from<string, T> | ||
| menu_item(T&& Name, LISTITEMFLAGS Flags, DWORD AccelKey = {}) |
There was a problem hiding this comment.
Do we need a template here?
We're going to construct a string anyway, so might as well just accept it by value and move it into Name.
There was a problem hiding this comment.
Leftover from when I experimented with overloaded constructors to reduce code churn at call sites.
P.S. C++ Core Guidelines suggest passing “will-move-from” parameters by rvalue reference (also, overview of parameter passing techniques).
This topic is somewhat controversial around me. What do you think?
There was a problem hiding this comment.
X&& binds to rvalues, which requires an explicit std::move at the call site if passing an lvalue
It is somewhat controversial indeed. What if we don't want to / can't move it?
There was a problem hiding this comment.
Then we would have to explicitly instantiate the object, like string{ MyUnmovableVar }. So, everything is very explicit at the call site, which from my point of view is an advantage. May help avoiding perf issues when we accidentally make a copy. I've seen it couple of times.
3263ce3 to
31d946b
Compare
|



Summary
With support of fullwidth characters, calculating the visual length of menu item name may be expensive.
Let's cache the visual length once it is calculated. The new field does not even increase
sizeof(menu_item).Checklist
If not checked, I accept that this work might be rejected in favor of a different great big ineffable plan.
Details
menu_item::Nameprivate.menu_item::Namewith accessors calls.