Skip to content

Commit

Permalink
String-define safety: stringification + unquoting (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdegeus authored Feb 20, 2021
1 parent 3309c45 commit 18b40de
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ jobs:
- name: Python tests
run: |
python test/basic-python/version.py
python test/basic-python/MeshQuad4.py
py:
Expand Down Expand Up @@ -127,4 +128,5 @@ jobs:
- name: Python tests
run: |
python test/basic-python/version.py
python test/basic-python/MeshQuad4.py
10 changes: 5 additions & 5 deletions include/GooseFEM/Allocate.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Common allocation methods.
namespace GooseFEM {

/**
"Broadcast" a scalar stored in an array (e.g.: ``[r, s]``) to the same scalar of all
"Broadcast" a scalar stored in an array (e.g. ``[r, s]``) to the same scalar of all
tensor components of a tensor of certain rank (e.g. for rank 2: ``[r, s, i, j]``).
\tparam dim Number of dimensions of scalar array (rank of the input).
Expand All @@ -27,7 +27,7 @@ template <size_t dim, size_t rank, class T>
inline void asTensor(const xt::xtensor<T, dim>& arg, xt::xtensor<T, dim + rank>& ret);

/**
"Broadcast" a scalar stored in an array (e.g.: ``[r, s]``) to the same scalar of all
"Broadcast" a scalar stored in an array (e.g. ``[r, s]``) to the same scalar of all
tensor components of a tensor of certain rank (e.g. for rank 2: ``[r, s, i, j]``).
\tparam dim Number of dimensions of scalar array (rank of the input).
Expand All @@ -43,7 +43,7 @@ inline xt::xtensor<T, dim + rank> AsTensor(
const std::array<size_t, rank>& shape);

/**
"Broadcast" a scalar stored in an array (e.g.: ``[r, s]``) to the same scalar of all
"Broadcast" a scalar stored in an array (e.g. ``[r, s]``) to the same scalar of all
tensor components of a tensor of certain rank (e.g. for rank 2: ``[r, s, n, n]``).
\tparam dim Number of dimensions of scalar array (rank of the input).
Expand All @@ -56,7 +56,7 @@ template <size_t dim, size_t rank, class T>
inline xt::xtensor<T, dim + rank> AsTensor(const xt::xtensor<T, dim>& arg, size_t n);

/**
"Broadcast" a scalar stored in an array (e.g.: ``[r, s]``) to the same scalar of all
"Broadcast" a scalar stored in an array (e.g. ``[r, s]``) to the same scalar of all
tensor components of a tensor of certain rank (e.g. for rank 2: ``[r, s, i, j]``).
\tparam T Type of the data.
Expand All @@ -72,7 +72,7 @@ inline xt::xarray<typename T::value_type> AsTensor(
const std::vector<size_t>& shape);

/**
"Broadcast" a scalar stored in an array (e.g.: ``[r, s]``) to the same scalar of all
"Broadcast" a scalar stored in an array (e.g. ``[r, s]``) to the same scalar of all
tensor components of a tensor of certain rank (e.g. for rank 2: ``[r, s, n, n]``).
\param rank Number of tensor dimensions (number of dimensions to add to the input).
Expand Down
3 changes: 3 additions & 0 deletions include/GooseFEM/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ Basic configuration:

using namespace xt::placeholders;

#define Q(x) #x
#define QUOTE(x) Q(x)

#define UNUSED(p) ((void)(p))

/**
Expand Down
4 changes: 3 additions & 1 deletion include/GooseFEM/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ If ``PKG_VERSION`` is defined the version string will be read from that variable
namespace GooseFEM {

/**
Return version string, e.g. "0.8.0"
Return version string, e.g.::
"0.8.0"
\return std::string
*/
Expand Down
29 changes: 18 additions & 11 deletions include/GooseFEM/version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@

namespace GooseFEM {

namespace detail {

inline std::string unquote(const std::string& arg)
{
std::string ret = arg;
ret.erase(std::remove(ret.begin(), ret.end(), '\"'), ret.end());
return ret;
}

}

inline std::string version()
{
std::stringstream ss;
ss << GOOSEFEM_VERSION;
std::string ret;
ss >> ret;
return ret;
return detail::unquote(std::string(QUOTE(GOOSEFEM_VERSION)));
}

inline std::vector<std::string> version_dependencies()
Expand All @@ -27,16 +34,16 @@ inline std::vector<std::string> version_dependencies()
ret.push_back("goosefem=" + version());

ret.push_back("xtensor=" +
std::to_string(XTENSOR_VERSION_MAJOR) + "." +
std::to_string(XTENSOR_VERSION_MINOR) + "." +
std::to_string(XTENSOR_VERSION_PATCH));
detail::unquote(std::string(QUOTE(XTENSOR_VERSION_MAJOR))) + "." +
detail::unquote(std::string(QUOTE(XTENSOR_VERSION_MINOR))) + "." +
detail::unquote(std::string(QUOTE(XTENSOR_VERSION_PATCH))));

#if defined(GOOSEFEM_EIGEN) || defined(EIGEN_WORLD_VERSION)

ret.push_back("eigen=" +
std::to_string(EIGEN_WORLD_VERSION) + "." +
std::to_string(EIGEN_MAJOR_VERSION) + "." +
std::to_string(EIGEN_MINOR_VERSION));
detail::unquote(std::string(QUOTE(EIGEN_WORLD_VERSION))) + "." +
detail::unquote(std::string(QUOTE(EIGEN_MAJOR_VERSION))) + "." +
detail::unquote(std::string(QUOTE(EIGEN_MINOR_VERSION))));

#endif

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
build.c_opts['msvc'] += ['/DXTENSOR_USE_XSIMD']

build.c_opts['unix'] += ['-DGOOSEFEM_VERSION="{0:s}"'.format(version)]
build.c_opts['msvc'] += ['/DGOOSEFEM_VERSION=\\"{0:s}\\"'.format(version)]
build.c_opts['msvc'] += ['/DGOOSEFEM_VERSION="{0:s}"'.format(version)]

ext_modules = [Extension(
'GooseFEM',
Expand Down
4 changes: 4 additions & 0 deletions test/basic-python/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import GooseFEM as gf

print(gf.version())
print(gf.version_dependencies())

0 comments on commit 18b40de

Please sign in to comment.