-
Notifications
You must be signed in to change notification settings - Fork 0
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
Extend Stream library to support non-trivially-copyable objects #277
Comments
This article has some details about how to detect the presence of a given member: I was hoping there would be a built in |
I generally support this and #278. In map, we have both a WriteMap and WriteSavedGame. I'm guessing that class would not be a candidate for this improvement unless we separated into two classes, Map and SavedGame. Also, for Bitmap, we have named the write function WriteIndexed. I like this because it explicitly tells the user they cannot write a non-indexed bitmap. This function could just be renamed to write, and a comment added about only supporting index bitmaps. In C# you can decorate functions although I haven't researched the concept in C++. There might be a way to decorate a different named function to serve as the 'Write' function, although I don't know if we want to support it or not. It starts adding more complexity to the project since less people will be familiar with decorations. The proposed syntax will certainly improve the readability when reading/writing. -Brett |
That's a good point, yes. I suppose SavedGame could be made a subclass of Map. Though I'm not sure if that's something we want to do. The other thought is, the class itself knows if it has only map data, or full saved game data, so it can choose which to write. Though that doesn't play well with the idea of extracting a map from saved game data. Another possibility is to allow specifying a serialization method. Example: an Image class could serialize as BMP, JPEG, PNG, TIFF, etc. For Bitmap, maybe the class should be named IndexedBitmap? Or if there is a desire to extend it to support other bitmap types, to just name the methods accordingly, and throw a not supported error if used outside of its current design. Sounds like this idea may need a bit of refinement. |
I noticed this today, and wanted to add a reference to it. Some of the techniques in that thread may be of use here for detecting a special |
I was doing a bit of research today. I took a second look at the link above, and also an additional link that seems to be useful: In both threads, there are examples of the newer trailing return type syntax. It seems to play really nice with feature detection, in terms of deciding up front if a given line of code will compile. I may start to prefer that syntax over placing template <typename C, typename... Args>
struct is_call_possible {
private:
template<typename T>
static auto check(int)
-> decltype( std::declval<T>().operator()(std::declval<Args>()...),
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// overload is removed if this expression is ill-formed
std::true_type() );
template<typename>
static std::false_type check(...);
public:
static constexpr bool value = decltype(check<C>(0))::value;
}; In the |
I noticed there are a number of structs being used for serialization, but due to complex members, such as
std::vector
, they are notstd::is_trivially_copyable
. As such, they can not be automatically serialized.To get around this, such objects often have a custom
Write(Stream::Writer&)
style method, which is manually called. I was thinking perhaps the Stream library could be updated to support auto calling such a member function on non-trivially-copyable objects. This would make serialization of such complex objects more consistent with traditional trivially-copyable objects.Example:
On it's own, this small syntax adjustment doesn't buy much. It merely swaps the order of the
writer
and the object. However, combine this with a small update to vector writes, and we would be able to automatically serialize astd::vector<T>
of non-trivial types. See the proposal in Issue #278.The text was updated successfully, but these errors were encountered: