Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored the use of ref_t. Also fixes a crash. #146

Merged
merged 4 commits into from
Oct 26, 2017

Conversation

ethouris
Copy link
Collaborator

There was quite a mistake in the design of ref_t solution.

It was initially designed to make the arguments for a function call, that had to be references, passed with an explicit marking that these are references, not values, and not just pointers. The solution initially was done with the use of a C++11 type, std::reference_wrapper, so that the standard std::ref function can be used as the aforementioned marker, however this has been later changed in the standard to be able to accept a reference in a non-explicit constructor, which completely broke the intention. Therefore a new type has been defined, ref_t, which in C++11 version only derives from std::reference_wrapper, but applies also appropriate fixes. Still, due to interoperability problems between C++03 compiled SRT and C++11 compiled applications, the halfway solution was still to create a custom Ref function as a helper, which in C++11 version resolves to std::ref, but in C++03 version it's a custom function, and so ref_t is a custom class, although based on the sources of std::reference_wrapper.

The problematic intention of the std::reference_wrapper, however, is that the assignment operator is intended to rebind the reference, rather than assign a value. It has been a design flaw then to add an assignment operator for a value for the ref_t type because in this case the form of assignment to a ref_t variable from a designated type value and from another ref_t variable were two completely different and unrelated operations, while the second one did not copy the value, but rebound the reference instead.

Because of that, the design of ref_t has been changed so that the direct access to the designated reference is no longer implicitly available. It was earlier designed to use the get() method to access the designated reference, and so it remains, but the operator Type&() function has been removed (in C++11 version the derived method has been deleted). For convenience, the operator* method has been added so that the reference can be still obtained easily, but still also explicitly. This makes the whole solution look more like a pointer, but still as it is made impossible to create a ref_t value with no argument, it is ensured enough that a null pointer can't be passed by ref_t (or, better said, to make a null pointer passed here one must do it very intentionally and explicitly). There was also left the overloaded the operator-> function, which allows an easier access to the pointer value, in case when the ref_t type variable wraps a pointer variable - this has been stated as not dangerous, as it still doesn't give a direct access to modifying the ref_t variable itself. Might be further discussed, whether the implicit returning a value copy of the designated reference has some reason; currently it's not added because it slightly violates the ideology of ref_t as per current design, where it wraps rather than just designates the referred variable.

For problems that might arise during value-or-reference resolution dilemmas, there's also added operator+, which returns a copy of the value held by the designated reference. This follows the general understanding of operator+ where it simply returns the value that follows this operator.

@ethouris
Copy link
Collaborator Author

Ok, fixed:

  • no more operator +
  • no more assignment (ref_t can't be rebound, only initialized, like a system reference)
  • ref_t has a common definition for C++11 and C++03, with one extra constructor in C++11 to be used by std::ref

@rndi rndi merged commit e105138 into Haivision:dev Oct 26, 2017
@ethouris ethouris deleted the dev-fix-invalid-ref branch January 17, 2018 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants