Code Monkey home page Code Monkey logo

nupy's Introduction

Nupy C/C++ Library

Expose C/C++ structs to numpy

SYNOPSIS

#define nupyStruct(T) begin a declaration of struct T

#define nupyEnd() end a declaration

#define nupyBase(T) declare a base class T - C++ only

#define nupyM(M) declare a member M

#define nupyFAM(M) declare flexible array member M

DESCRIPTION

Nupy is a simple set of C++ macros to generate a dtype string for a class or a struct.

For example, C/C++ struct Line

struct Line
{
        double start[2];
        double end  [2];
        char   note [16];
};

can be "decorated" by nupyStruct(Line) and nupyEnd() macros and all members can be wrapped by nupyM(m)

struct Line
{
        nupyStruct(Line)

        double nupyM(start) [2];
        double nupyM(end  ) [2];
        char   nupyM(note ) [16];

        nupyEnd()
};

In plain C nupyStruct(T) and nupyEnd() evaluate to nothing and nupyM(M) evaluates to M producing a struct identical to the original.

In C++, the second definition generates nupy_dtype static member function

int Line::nupy_dtype(const char* str, size_t bufsz, size_t famsz = 0);

If bufsz is big enough, the function copies Line's dtype to buf producing a string similar to

"[('start','<f8',(2)),('end','<f8',(2)),('note','|S16')]"

A call to nupy_dtype(buf, bufsz) is equivalent to snprintf call

snprintf(buf, bufsz, "%s", dtypestr);

except that snprintf can be called multiple times.

In fact, nupy_dtype uses only snprintf from -lc. All other dependencies are C++ metaprogramming stuff.

No padding between members or at the end are allowed. If there is a mismatch between a total size of members wrapped by nupyM(M) and a size of a struct, compile-time assert will be triggered.

Class inheritance is supported with nupyBase(T). There is no compile-time check to detect a wrong order of nupyBase(T) for a class with multiple bases.

Only one member declaration per line is allowed.

Compile-time complexity of C++ code is proportional to a number of members and it also increases as a distance between nupyStruct(T) and nupyEnd() increases (even lines with comments count!). You may need to change a default value of template depth to compile big structs.

REQUIREMENTS

You need a decent C++ compiler that works with boost and that supports __typeof__.

DEPENDENCIES

The library is header-only. It depends on the following boost libraries (all header-only)

  • enable_if
  • mpl
  • preprocessor
  • type_traits

SEE ALSO

snprintf(3)

BUGS

At the moment, the library does not support big endian architectures.

AUTHOR

Alexander Nasonov

nupy's People

Contributors

alnsn avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.