Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Developing U++ » U++ Developers corner » Refactoring Moveable
Refactoring Moveable [message #60744] Fri, 23 August 2024 08:52 Go to next message
mirek is currently offline  mirek
Messages: 14261
Registered: November 2005
Ultimate Member
In order to make U++ more compatible and future proof, I am changing Moveable mechanisms a bit. U++ will now use C++17 inline template features to to simplify Moveable and allow putting "non-U++ guest types" in Vector/BiVector/Index. On the way I hope to fix some other problems (e.g. auto [a, b] = MakeTuple("x", 1) does not work yet) and remove all "dangerous" (ok, all possibly undefined behaviour) code, except Moveable, which is de facto standard now anyway ( https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p11 44r10.html).

Development is so far in the branch Core2024, critical part for your kind review:

https://github.com/ultimatepp/ultimatepp/blob/3638778b2e0e18 19622424a70a7f04ef0950741d/uppsrc/Core/Topt.h#L158

This now works:

template <>
inline constexpr bool Upp::is_upp_guest<std::string> = true;

template<> inline hash_t Upp::GetHashValue(const std::string& a)
{
	return memhash(a.data(), a.length());
}


CONSOLE_APP_MAIN
{
	{
		Vector<std::string> h;
		for(int i = 0; i < 20; i++)
			h << AsString(i).ToStd();
		RDUMP(h);
		Vector<int> rem = { 1, 2, 3 };
		h.Remove(rem);
		RDUMP(h);
		h.RemoveIf([&](int i) { return h[i].back() == '8'; });
		RDUMP(h);
		Vector<std::string> n = { "21", "22", "23" };
		h.Insert(2, n);
		RDUMP(h);
		h.Insert(2, pick(n));
		RDUMP(h);
		h.Remove(2, 3);
		RDUMP(h);
	}
	{
		Index<std::string> x { "one", "two", "three" };
		RDUMP(x);
		RDUMP(x.Find("two"));
	}
}


(This works legally, using std::move instead of memmove/memcpy for std::string).

[Updated on: Fri, 23 August 2024 08:54]

Report message to a moderator

Re: Refactoring Moveable [message #60775 is a reply to message #60744] Sun, 08 September 2024 13:18 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1210
Registered: August 2007
Senior Contributor
Hello Mirek,

Good to be on C++17

Hovewer, this seems to break a lot of things.

For example, If I derive something from MoveableAndDeepCopyOption<T>, which is now derived from TriviallyRelocatable<T> (Say, T = Vector<T>, which was possible up until now) then I can't access the methods or members of T.

Reason: TriviallyRelocatable<T> is defined as:


template <class T>
struct TriviallyRelocatable {};



Any ideas on how to proceed, or am I missing something?

Best regards,
Oblivion


[Updated on: Sun, 08 September 2024 13:19]

Report message to a moderator

Re: Refactoring Moveable [message #60776 is a reply to message #60775] Sun, 08 September 2024 15:41 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14261
Registered: November 2005
Ultimate Member
Oblivion wrote on Sun, 08 September 2024 13:18
Hello Mirek,

Good to be on C++17

Hovewer, this seems to break a lot of things.

For example, If I derive something from MoveableAndDeepCopyOption<T>, which is now derived from TriviallyRelocatable<T> (Say, T = Vector<T>, which was possible up until now) then I can't access the methods or members of T.

Reason: TriviallyRelocatable<T> is defined as:


template <class T>
struct TriviallyRelocatable {};



Any ideas on how to proceed, or am I missing something?

Best regards,
Oblivion


Uhm, normal use is like

struct Foo : MoveableAndDeepCopyOption<Foo> {
...
};


- obviously, you can access methods of Foo in Foo...

Example of what you need?

Note: There is one small issue I was unable to solve. U++ had two parameter Moveable, where second parameter was optional base class. It is supposed to help with MSC++ big with empty base class optimisations. It does not seem possible to use template magic with that which would go well MSC++ optimiser, putting Moveable first in the base class list seems to work fine wrt MSC++ optimisation and it really was used very sparsely even in U++ code and I guess almost never in client code.

Anyway

struct Foo : Moveable<Foo, FooBase> ...


now has to be rewritten as

struct Foo : Moveable<Foo>, FooBase ...

[Updated on: Sun, 08 September 2024 15:42]

Report message to a moderator

Re: Refactoring Moveable [message #60777 is a reply to message #60776] Sun, 08 September 2024 15:44 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14261
Registered: November 2005
Ultimate Member
Ah, and another issue, more positive change: PODs do not need Moveable anymore as all std::is_trivial_copyable types are now trivially relocatable (aka Moveable)
Re: Refactoring Moveable [message #61378 is a reply to message #60777] Thu, 02 January 2025 21:41 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1430
Registered: December 2006
Ultimate Contributor
A little bit of criticism.
Code below won't compile out of the box:
namespace test {
    struct Test;
}

namespace test {
    struct Test : Moveable<Test> {

        Vector<Test> children;
    };
}

Adding of
template <> inline constexpr bool is_upp_guest<test::Test> = true;

won't help.
You need to add
template <> inline constexpr bool is_trivially_relocatable<test::Test> = true;

All this stuff is inconvenient and unnatural.

And I have no idea how to make code below compile.
struct Test01;

struct Test01 {

    struct Test02 : Moveable<Test02> {

        Vector<Test02> children;
    };
};


Regards,
Novo
Re: Refactoring Moveable [message #61380 is a reply to message #61378] Fri, 03 January 2025 08:37 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14261
Registered: November 2005
Ultimate Member
Novo wrote on Thu, 02 January 2025 21:41
A little bit of criticism.
Code below won't compile out of the box:
namespace test {
    struct Test;
}

namespace test {
    struct Test : Moveable<Test> {

        Vector<Test> children;
    };
}

Adding of
template <> inline constexpr bool is_upp_guest<test::Test> = true;

won't help.
You need to add
template <> inline constexpr bool is_trivially_relocatable<test::Test> = true;

All this stuff is inconvenient and unnatural.

And I have no idea how to make code below compile.
struct Test01;

struct Test01 {

    struct Test02 : Moveable<Test02> {

        Vector<Test02> children;
    };
};


Well, it is sort of obvious, right?

Anyway, easy fix is to move the static_assert to destructor. It however has the price of less clear error and also only gets triggered when you instatiate Test02.

Do we want to go there? Or any other ideas?
Re: Refactoring Moveable [message #61383 is a reply to message #61380] Sat, 04 January 2025 06:46 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1430
Registered: December 2006
Ultimate Contributor
mirek wrote on Fri, 03 January 2025 02:37

Do we want to go there?

Something has to be done. IMHO, a situation when very simple code cannot be compiled is unacceptable.
mirek wrote on Fri, 03 January 2025 02:37

Or any other ideas?

Please give me some time. I'll check with my old code where I was doing autodetection. Maybe I'll find something interesting.


Regards,
Novo
Re: Refactoring Moveable [message #61384 is a reply to message #61383] Sat, 04 January 2025 09:05 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14261
Registered: November 2005
Ultimate Member
Moving static_assert here

template <class T>
inline typename std::enable_if_t<!is_trivially_relocatable<T>> Relocate(T *dst, T *src)
{
	static_assert(is_upp_guest<T>);
	new(dst) T(pick(*src));
	Destruct(src);
}


instead of destructor makes a lot of sense and perhaps adds a bit of self-explanation to the error, but there is still that small disadvantage that it only gets displayed when building, not while editing. Is that acceptable drawback?
Re: Refactoring Moveable [message #61385 is a reply to message #61384] Sat, 04 January 2025 09:10 Go to previous message
mirek is currently offline  mirek
Messages: 14261
Registered: November 2005
Ultimate Member
OK, I have for now changed the code (experimentally), let me know if this is better.

https://github.com/ultimatepp/ultimatepp/commit/f6e62772853c 3de391879d70da8cbf11672eb74a
Previous Topic: Dynamic skin changes...
Next Topic: Broken compilation
Goto Forum:
  


Current Time: Wed Jun 11 03:55:02 CEST 2025

Total time taken to generate the page: 0.05911 seconds