Comments (3)
I don't quite get it. What is its advantage compared to directly defining the struct?
struct MyStruct{
uint_16 fFoo;
uint_16 fBar;
uint_16 fBaz;
};
auto a = Data<MyStruct>{};
from r3broot.
Because that way, you end up with stuff like this, e.g. each variable needs three lines of macros like:
EXT_STR_ITEM_INFO_LIM(ok,si,offset,struct_t,printerr,\
CALIFA_TRGENE, UINT32,\
"CALIFA_TRGENE",2); \
By contrast, my way needs only a different macro invocation instead of macro definitions to be able to do the equivalent of the above.
Naturally, my code is still a toy example. In reality, you would have to call ext_data_struct_info_item
(or one of the wrapper macros) in fOnInit
. I guess it would be simplest to have fOnInit::value_type
take a (basically void) pointer to the beginning of the actual struct and just calculate the char* difference to &(this->VARNAME).
When R3BUcesbSource is rewritten, there is nothing to stop us from using template<typenname ReaderT> AddReader(std::string prefix)
which could
- check that ReaderT::h101_t still fits on the buffer allocated for all the h101 objects
- Use placement new to allocate the h101:
new (buffer+current_offset) ReaderT::h101_t(buffer, prefix)
, passing the struct both the string prefix (used to decide which names to map) and the start of buffer pointer (used to calculate offsets) - Instantiate ReaderT and give it a pointer to the just created h101
- increment current_offset by sizeof(h101)
While it would be tempting to also autogenerate the FooReader code altogether, it is probably not feasible for readers due to resource overhead.
(Of course, there is the way to define every class as a template <typename Base> class Foo: public Base {...};
and then create the actual class by some template<template<typename> typename... classes> struct AllHelper
, which just cascades all of the argument templates (and uses a boring empty class as the base case). This would be completely inlineable without any lambda expressions having to be evaluated at runtime. On the other hand, the embed-in-a-lambda-type trick would then involve type variadic lambda expressions which retrieve the type of their argument and use this as the base type for making their stuff inherit from, but that might lead to code which is kind of hard to understand, especially when having to add the processing steps we actually do in the readers (like going from 4x uint16_t -> uint64_t for wrts)).
from r3broot.
Alright, maybe we could first see what are the "industrial standards" to serialise or deserialise C++ data structures.
As far as I know, the most popular one is google protobuf. It's very similar to our home-made ucesb, which requires users to define the data structure in a separate file. Then it generates a header file containing the corresponding C++ structure.
"cereal" is another type of serialisation tool, which instead requires user to define a serialisation method inside the data structure, such as:
struct MyRecord
{
uint8_t x, y;
float z;
template <class Archive>
void serialize( Archive & ar )
{
ar( x, y, z );
}
};
And then you call a global function to serialise/deserialise the data structure.
The third method (the best one in my opinion) can be found in libraries like "boost::serialization" or "zpp_bits", which requires nothing from users except the plain definition of the C++ structure:
struct MyStruct{
uint_16 fFoo;
uint_16 fBar;
uint_16 fBaz;
} myStruct;
auto data = std::vector<std::byte>{};
Serialize(data, myStruct);
Deserialize(data, myStruct);
And what is blowing my mind is that the third one is very simple to implement and there is no heavy usage on C macros. The trick of imitating a reflection on data structure is using the structure binding. But we need to numerate every single case of such kind of bindings:
if constexpr (size == 2) auto&& [m1, m2] = myStruct;
else if constexpr (size ==3) auto && [m1, m2, m3] = myStruct;
//...
And this can get quite long.
So the best solution is to wait for the new feature of C++26, when we can do something like:
auto && [ ... m ] = myStruct;
from r3broot.
Related Issues (20)
- UCESB cannot populate items in client_struct_info with client.setup HOT 2
- Handling new vs old experiment Readers/code HOT 1
- Clang-tidy setting HOT 24
- Design Guidelines for R3BRoot HOT 1
- Unknown Type name 'FairMCTracks' HOT 1
- Error in <TExMap::Add>: key is not unique HOT 1
- Clang-formatting - what should be included, what should be removed, future additions HOT 10
- File to generate Hit Parameters using Los not in GitHub repository HOT 5
- Proposal to remove ext files from dev branch HOT 19
- Bug CALIFA QPID Simulation HOT 4
- Forbid pull requests from the dev branch? HOT 6
- Spell fixes by `codespell` HOT 3
- FATAL error in unpacker HOT 4
- Policy discussion: on pull requests, merging and reviews HOT 12
- Unknown CMake command "fair_pad" HOT 2
- Added new repository for lmds and analysis CI tests HOT 2
- About clang-tidy and code standard HOT 11
- Problems with the compilation of new reader data structures: TTTX HOT 4
- [Proposal] Metadata file for experiments? HOT 24
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from r3broot.