Page MenuHomePhabricator

[SQLite] introduce Nullable entity
ClosedPublic

Authored by kamil on Nov 15 2023, 12:17 PM.
Tags
None
Referenced Files
Unknown Object (File)
Wed, Nov 6, 8:41 AM
Unknown Object (File)
Wed, Nov 6, 8:41 AM
Unknown Object (File)
Wed, Nov 6, 8:37 AM
Unknown Object (File)
Sep 28 2024, 9:30 PM
Unknown Object (File)
Sep 28 2024, 9:30 PM
Unknown Object (File)
Sep 28 2024, 9:25 PM
Unknown Object (File)
Sep 18 2024, 3:33 PM
Unknown Object (File)
Sep 15 2024, 11:15 PM
Subscribers

Details

Summary

We need to introduce something that emulates pointers in C++ because of Emscripten limitations.

Context here: ENG-5719

Test Plan
  1. In C++ tested by creating objects and calling method/using operator.
  2. In Emscripten tested later in the stack.

Diff Detail

Repository
rCOMM Comm
Branch
user-store-work
Lint
No Lint Coverage
Unit
No Test Coverage

Event Timeline

kamil held this revision as a draft.
kamil retitled this revision from [SQLite] introduce NullableString to [SQLite] introduce Nullable entity.Nov 22 2023, 3:36 AM
kamil edited the summary of this revision. (Show Details)
kamil edited the test plan for this revision. (Show Details)
kamil added a reviewer: michal.
kamil published this revision for review.Nov 22 2023, 4:22 AM

I'm guessing you've already considered std::optional?

marcin requested changes to this revision.Nov 22 2023, 5:25 AM
marcin added inline comments.
native/cpp/CommonCpp/DatabaseManagers/entities/Nullable.h
15 ↗(On Diff #33480)

We should use && here to enforce that unique_ptr is moved and value kept by this unique_ptr can't be used by other code after Nullable is constructed.

24 ↗(On Diff #33480)

Am I correct that this line forces T to have default constructor that takes zero arguments? If so, then can we guarantee that whatever we have to put in Nullable conforms to this condition?

This revision now requires changes to proceed.Nov 22 2023, 5:25 AM

I'm guessing you've already considered std::optional?

Yes, I did.

There is no support for std::optional yet, but I found an issue on Emsctipen link and PR link. I tried solutions suggested there but two things:

  1. It works fine when returning std::optional from C++ to JS, in the opposite direction it doesn't work that well - we need both.
  2. This struct in JS we can use directly cons x = { isNull: false, value: 'asdf' }, while std::optional needs to be constructed (assuming it works) using constructor, the deleted, etc, which make JS side code more complicated.

I need to check whether we should use && or & in the constructor

kamil requested review of this revision.Nov 27 2023, 3:28 AM
kamil added inline comments.
native/cpp/CommonCpp/DatabaseManagers/entities/Nullable.h
15 ↗(On Diff #33480)

I don't think so, here is why:
The std::unique_ptr<T>& ptr means that ptr is a reference to a std::unique_ptr. When *ptr is used, it dereferences the unique_ptr, and it give us the actual object that the unique_ptr is managing (an instance of type T), but the std::unique_ptr continues to own it.

In this case, in the constructor, we use the copy constructor of T to initialize the value (not copy of pointer, copy of instance pointer points to). Even with something like value = *ptr; we should be safe because the copy assignment operator of T should be called, and value gets copied and a distinct instance of T.

The issue could be when T value; in line 9 will be defined as T &value; but this I believe should be caught by the compiler.

24 ↗(On Diff #33480)

Am I correct that this line forces T to have default constructor that takes zero arguments?

Yes, that was my intention

If so, then can we guarantee that whatever we have to put in Nullable conforms to this condition?

Not sure if I understand the question, but I believe the answer is: yes, it's done by C++ itself at compile time, there is no additional work we need to do

This revision is now accepted and ready to land.Nov 27 2023, 4:49 AM
This revision was automatically updated to reflect the committed changes.