Comments (2)
Found an even better solution via:
http://jhnet.co.uk/articles/cpp_magic
#include
#include
#include "variadic.h"
template
std::ostream & operator << (std::ostream &os,const std::vector & vec) {
os<< "[ "<<vec.size()<< " ] {\n";
for(int i=0;i<vec.size();i++){
os<< "\t"<<vec[i];
if ( i!=vec.size()-1 ) os <<",";
os<<std::endl;
}
os<< "}";
return os;
};
class MyParameters {
public:
GRID_DECL_CLASS_MEMBERS(MyParameters,
int, x1, int, y1, int, z1,
int, x2, int, y2, int, z2,
int, x3, int, y3, int, z3,
int, x4, int, y4, int, z4
);
};
class MyNesting {
public:
GRID_DECL_CLASS_MEMBERS(MyNesting, MyParameters, foo, std::vector, bar);
};
int main(int argc,char **argv)
{
MyNesting boo;
boo.bar.resize(2,13);
boo.foo.x1=1;
boo.foo.y1=2;
boo.foo.z1=3;
boo.foo.x2=5;
boo.foo.y2=6;
boo.foo.z2=7;
boo.foo.x3=8;
boo.foo.y3=10;
boo.foo.z3=11;
boo.foo.x4=13;
boo.foo.y4=14;
boo.foo.z4=15;
std::cout << boo<<std::endl;
}
Gives:
Peters-MacBook-Pro:variadic pab$ ./variadic
class MyNesting {
MyParameters foo = class MyParameters {
int x1 = 1 ;
int y1 = 2 ;
int z1 = 3 ;
int x2 = 5 ;
int y2 = 6 ;
int z2 = 7 ;
int x3 = 8 ;
int y3 = 10 ;
int z3 = 11 ;
int x4 = 13 ;
int y4 = 14 ;
int z4 = 15 ;
} ;
std::vector bar = [ 2 ] {
13,
13
} ;
}
Depends ONLY on variadic CPP macros and a bit of log2 substitutional magic. No dependence on
Boost, or EVEN on C++, let alone tuples and C++11.
When I looked into Hana and Boost.PP they are enumerating cases to hit all "possible" lengths
of variadic macros, so they have no magic. e.g. 64 class members allowed, but 65 is not !?!?
This approach uses a log2 divide and conquer -- 10 macros give 2^10 = 1024 coverage.
The "magic" is as follows:
Peters-MacBook-Pro:variadic pab$ more variadic.h
#ifndef GRID_VARIADIC_H
#define GRID_VARIADIC_H
#define GRID_MACRO_FIRST(a, ...) a
#define GRID_MACRO_SECOND(a, b, ...) b
#define GRID_MACRO_EMPTY()
#define GRID_MACRO_EVAL(...) GRID_MACRO_EVAL1024(VA_ARGS)
#define GRID_MACRO_EVAL1024(...) GRID_MACRO_EVAL512(GRID_MACRO_EVAL512(VA_ARGS))
#define GRID_MACRO_EVAL512(...) GRID_MACRO_EVAL256(GRID_MACRO_EVAL256(VA_ARGS))
#define GRID_MACRO_EVAL256(...) GRID_MACRO_EVAL128(GRID_MACRO_EVAL128(VA_ARGS))
#define GRID_MACRO_EVAL128(...) GRID_MACRO_EVAL64(GRID_MACRO_EVAL64(VA_ARGS))
#define GRID_MACRO_EVAL64(...) GRID_MACRO_EVAL32(GRID_MACRO_EVAL32(VA_ARGS))
#define GRID_MACRO_EVAL32(...) GRID_MACRO_EVAL16(GRID_MACRO_EVAL16(VA_ARGS))
#define GRID_MACRO_EVAL16(...) GRID_MACRO_EVAL8(GRID_MACRO_EVAL8(VA_ARGS))
#define GRID_MACRO_EVAL8(...) GRID_MACRO_EVAL4(GRID_MACRO_EVAL4(VA_ARGS))
#define GRID_MACRO_EVAL4(...) GRID_MACRO_EVAL2(GRID_MACRO_EVAL2(VA_ARGS))
#define GRID_MACRO_EVAL2(...) GRID_MACRO_EVAL1(GRID_MACRO_EVAL1(VA_ARGS))
#define GRID_MACRO_EVAL1(...) VA_ARGS
#define GRID_MACRO_DEFER1(m) m GRID_MACRO_EMPTY()
#define GRID_MACRO_DEFER2(m) m GRID_MACRO_EMPTY GRID_MACRO_EMPTY()()
#define GRID_MACRO_DEFER3(m) m GRID_MACRO_EMPTY GRID_MACRO_EMPTY GRID_MACRO_EMPTY()()()
#define GRID_MACRO_DEFER4(m) m GRID_MACRO_EMPTY GRID_MACRO_EMPTY GRID_MACRO_EMPTY GRID_MACRO_EMPTY()()()()
#define GRID_MACRO_IS_PROBE(...) GRID_MACRO_SECOND(VA_ARGS, 0)
#define GRID_MACRO_PROBE() ~, 1
#define GRID_MACRO_CAT(a,b) a ## b
#define GRID_MACRO_NOT(x) GRID_MACRO_IS_PROBE(GRID_MACRO_CAT(GRID_MACRO_NOT, x))
#define _GRID_MACRO_NOT_0 GRID_MACRO_PROBE()
#define GRID_MACRO_BOOL(x) GRID_MACRO_NOT(GRID_MACRO_NOT(x))
#define GRID_MACRO_IF_ELSE(condition) _GRID_MACRO_IF_ELSE(GRID_MACRO_BOOL(condition))
#define _GRID_MACRO_IF_ELSE(condition) GRID_MACRO_CAT(GRID_MACRO_IF, condition)
#define _GRID_MACRO_IF_1(...) VA_ARGS _GRID_MACRO_IF_1_ELSE
#define _GRID_MACRO_IF_0(...) _GRID_MACRO_IF_0_ELSE
#define _GRID_MACRO_IF_1_ELSE(...)
#define _GRID_MACRO_IF_0_ELSE(...) VA_ARGS
#define GRID_MACRO_HAS_ARGS(...) GRID_MACRO_BOOL(GRID_MACRO_FIRST(GRID_MACRO_END_OF_ARGUMENTS VA_ARGS)())
#define GRID_MACRO_END_OF_ARGUMENTS() 0
#define GRID_MACRO_MAP(m, first, second, ...)
m(first,second)
GRID_MACRO_IF_ELSE(GRID_MACRO_HAS_ARGS(VA_ARGS))(
GRID_MACRO_DEFER4(_GRID_MACRO_MAP)()(m, VA_ARGS)
)(
/* Do nothing, just terminate */
)
#define _GRID_MACRO_MAP() GRID_MACRO_MAP
#define GRID_MACRO_MEMBER(A,B) A B;
#define GRID_MACRO_OS_WRITE_MEMBER(A,B) os<< #A <<" "#B <<" = "<< obj. B <<" ; " <<std::endl;
#define GRID_DECL_CLASS_MEMBERS(cname,...)
GRID_MACRO_EVAL(GRID_MACRO_MAP(GRID_MACRO_MEMBER,VA_ARGS))
friend std::ostream & operator << (std::ostream &os, const cname &obj ) {
os<<"class "<<#cname<<" {"<<std::endl;
GRID_MACRO_EVAL(GRID_MACRO_MAP(GRID_MACRO_OS_WRITE_MEMBER,VA_ARGS))
os<<"}";
return os;
};
#endif
This is pretty cool, because it is, in my view, a complete solution to the problem of struct/object
serialisation in both C and C++ fixing a reflection problem that has persisted for decades, inspired
dozens of IDL "solutions" including the '80's RPC/RPCGEN, '90's CORBA, '00's Apache AVRO etc...
It does not even need C++.
from grid.
Closing issue: have committed XMLReader and general Reader/Writer interface along with PugiXML.
It all appears to work nicely with the member list macro working as expected.
from grid.
Related Issues (20)
- MPI2 romio321 library fails when reading >= 2GB per rank HOT 2
- Cannot compile the gparity and adjoint versions of the CompactWilsonCloverAction
- Compilation errors and warnings build targeting Nvidia GPUs HOT 2
- GPU Benchmark_ITT segfaults with MPI and ranks > 1 HOT 9
- Create a version of Benchmark_ITT including Clover instead of Wilson
- Grid fails to build for Nc != 3
- hipcc on Crusher: function bcopy undefined (compiler does not have openmp enabled?) HOT 1
- Certain operations involving SitePropagator::scalar_object won't compile with CUDA for Nc > 3
- make install doesn't install all headers due to duplicate Config.h and Version.h HOT 3
- Using ILDG checkpointer causes a crash during write HOT 2
- Develop is broken HOT 1
- ARM NEON is broken HOT 2
- Feature request: provenance tracking
- Add hint to shm error message
- Cuda error invalid device ordinal
- Recent commit causing Grid build to fail
- The configure options --enable-setdevice and --diable-setdevice have no effect
- Grid does not compile on Arm with CUDA HOT 9
- invalid configuration argument when running with 1 GPU
- FlightRecorder.cc breaks compilation for --enable-comms=none HOT 1
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 grid.