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

Serialization. #916

Open
ankitkk opened this issue Sep 12, 2021 · 3 comments
Open

Serialization. #916

ankitkk opened this issue Sep 12, 2021 · 3 comments
Labels

Comments

@ankitkk
Copy link

ankitkk commented Sep 12, 2021

What is the canonical way to serialize/deserialize CNL types?

Its not entirely clear how to access the underlying data structures from the documentation or construct cnl objects from raw bytes. a concise example would help, similar to existing examples.

What environment are you working in?

gcc 8+

@johnmcfarlane
Copy link
Owner

Hi.

Thanks for the suggestion. A similar question has been asked before in #401. That answer may help.

Because they are class templates - and not strictly types themselves - it means that ability to serialise depends on the numeric type with which they are specialised. So if you specialise with an easily serialisable type, e.g. int, then you can do as you would normally, e.g. the example from fstream::write

#include <iostream>
 
int main()
{
    int n = 0x41424344;
    std::cout.write(reinterpret_cast<char*>(&n), sizeof n) << '\n';
}

should work just fine with scaled_integer of int:

#include <cnl/all.h>
#include <iostream>

int main()
{
    cnl::scaled_integer<int, cnl::power<-16>> n = 16706.26275634765625;
    std::cout.write(reinterpret_cast<char*>(&n), sizeof n) << '\n';
}

(example)

But if you use something non-trivial, e.g. a number type containing a pointer to dynamic memory, that's a bad idea!

Action: add serialisation FAQ to front page.

@ankitkk
Copy link
Author

ankitkk commented Sep 12, 2021

Thank you. This helps.

@ankitkk ankitkk closed this as completed Sep 12, 2021
@johnmcfarlane
Copy link
Owner

You're welcome! Thanks for closing the issue. Ppl often don't. However, in this instance, I'm going to keep it open as a reminder to change the FAQ. After all, you're the second person to ask about it!

@johnmcfarlane johnmcfarlane reopened this Sep 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants