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

Add idiomatic C++ functions in bezier namespace #131

Open
dhermes opened this issue Oct 11, 2018 · 3 comments
Open

Add idiomatic C++ functions in bezier namespace #131

dhermes opened this issue Oct 11, 2018 · 3 comments

Comments

@dhermes
Copy link
Owner

dhermes commented Oct 11, 2018

For example, helpers.h would become

#ifndef BEZIER_HELPERS_H
#define BEZIER_HELPERS_H

#include <stdbool.h>

#if defined(__cplusplus)
extern "C" {
#endif

void BEZ_cross_product(const double* vec0, const double* vec1, double* result);
...

#if defined(__cplusplus)
}
#endif

// BEGIN: New definitions
#if defined(__cplusplus)
#include <vector>

namespace bezier {
    double cross_product(const std::vector<double>& vec0, const std::vector<double>& vec1) {
        double result;
        BEZ_cross_product(vec0.data(), vec1.data(), &result);
        return result;
    }
    ...
}
#endif
//   END: New definitions

#endif /* BEZIER_HELPERS_H */

My biggest concern is about the level of effort needed to make an idiomatic C++ interface (e.g. using vector<double> instead of double*) and using RAII.

FWIW I'm not 100% sure how I'll get the C++ name mangling to work right with the symbols exported by Fortran.

See also: #166.

@dhermes dhermes changed the title Use "namespace Use C++ namespace and BEZ_ prefix for exported C ABI names Oct 11, 2018
@dhermes dhermes self-assigned this Oct 11, 2018
@dhermes
Copy link
Owner Author

dhermes commented Jan 10, 2020

UPDATE: The below is outdated, the main issue description now references this core idea (after #166 was completed).


From a StackOverflow answer:

namespace bezier {
    extern "C" {
        void cross_product(double* vec0, double* vec1, double* result);
        ...
    }
}

is possible, though if we want the C / ABI BEZ_ prefix, it'd probably have to instead be

extern "C" {
    void BEZ_cross_product(double* vec0, double* vec1, double* result);
    ...
}

namespace bezier {
    void cross_product(double* vec0, double* vec1, double* result) {
        BEZ_cross_product(double* vec0, double* vec1, double* result);
    }
    ...
}

cc @pazner

@dhermes dhermes changed the title Use C++ namespace and BEZ_ prefix for exported C ABI names Use bezier namespace for C++ parts of header files Jan 10, 2020
@dhermes
Copy link
Owner Author

dhermes commented Jan 15, 2020

Did some Googling and found a few things:

@dhermes
Copy link
Owner Author

dhermes commented May 27, 2020

I was curious about the overhead of the vector<double> example: https://godbolt.org/z/SkLvqF

bezier::cross_product(std::vector<double, std::allocator<double> > const&, std::vector<double, std::allocator<double> > const&):
        sub     rsp, 24
        mov     rsi, QWORD PTR [rsi]
        mov     rdi, QWORD PTR [rdi]
        lea     rdx, [rsp+8]
        call    BEZ_cross_product
        movsd   xmm0, QWORD PTR [rsp+8]
        add     rsp, 24
        ret

@dhermes dhermes changed the title Use bezier namespace for C++ parts of header files Add idiomatic C++ functions in bezier namespace Jun 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant