Parsing template tuple parameters in D - templates

I'm implementing a multi-dimensional tensor for a linear algebra library in D and this is basically what I was aiming to for the base class:
class Tensor( T_scalar, T_dimensions ..., int T_storageOrder = StorageOrder.columnMajor )
{
}
In the idea, the user can define the characteristics of the tensor through template parameters and as a result I can deduce as many things as possible during compile-time, a bit like Eigen does.
Unfortunately the compiler is not so happy with that definition and triggers an error such as:
Tensor(T_scalar,T_args...,int T_storageOrder = StorageOrder.columnMajor) template tuple parameter must be last one
I'm not so sure why this restriction is in place but I ended up doing what I consider being a hack... basically, having defined the StorageOrder as an enum allows me to check if the last argument of the template tuple parameter matches one of the values from the enum, and if so I can use it to set the value of the StorageOrder for that tensor, otherwise I set it up with a default value.
enum StorageOrder : int
{
columnMajor = -1,
rowMajor = -2
}
class Tensor( T_scalar, T_args ... )
{
private:
alias TensorTraits!( T_scalar, T_args ) traits;
alias traits.dimensions T_dimensions;
alias traits.storageOrder T_storageOrder;
}
struct TensorTraits( T_scalar, T_args ... )
if ( areTemplateParametersValid!( T_scalar, T_args )() )
{
static immutable auto dimensions = mixin( extractDataFromTemplateTupleParameter.dimensions );
static immutable int storageOrder = extractDataFromTemplateTupleParameter.storageOrder;
private:
static auto extractDataFromTemplateTupleParameter()
{
Tuple!( string, "dimensions", int, "storageOrder" ) templateTupleParameterData;
static if ( T_args[$ - 1] == StorageOrder.columnMajor || T_args[$ - 1] == StorageOrder.rowMajor )
{
alias TypeTuple!( T_args[0 .. $ - 1] ) dimensionsTuple;
templateTupleParameterData.storageOrder = T_args[$ - 1];
}
else
{
alias TypeTuple!( T_args ) dimensionsTuple;
templateTupleParameterData.storageOrder = StorageOrder.columnMajor;
}
static assert( dimensionsTuple.length > 0,
"No dimensions have been defined." );
foreach ( dimension; dimensionsTuple )
{
static assert( isIntegral!( typeof( dimension ) ),
"Dimensions sizes needs to be defined as integrals." );
static assert( dimension >= 0,
"Dimensions sizes cannot be negative." );
}
templateTupleParameterData.dimensions = dimensionsTuple.stringof;
return templateTupleParameterData;
}
}
static bool areTemplateParametersValid( T_scalar, T_args ... )()
{
static assert( isNumeric!( T_scalar ),
"The 'T_scalar' template argument is not a numeric type." );
static assert( T_args.length > 0,
"No dimensions have been defined." );
return true;
}
Since I've just started with D, and since I'm not so sure about this hack, I would like to know if this sounds good to you guys or if there's maybe a better way of handling this?

Like you say, it's a hack, and you should avoid hacks where unnecessary.
One (obvious) solution is to move the storage order before the dimensions, although I'm guessing you want to use that default parameter.
To work around that, you could create have specific templates for row and column major:
// Generic Tensor with storage order before dimensions.
class Tensor( T_scalar, int T_storageOrder, T_dimensions... )
{
}
template TensorRowOrder( T_scalar, T_dimensions... )
{
alias Tensor( T_scalar, StorageOrder.rowMajor, T_dimensions ) TensorRowOrder;
}
template TensorColumnOrder( T_scalar, T_dimensions... )
{
alias Tensor( T_scalar, StorageOrder.columnMajor, T_dimensions ) TensorColumnOrder;
}
You can then use TensorRowOrder or TensorColumnOrder in user code, or just Tensor when you need the generic T_storageOrder.

FYI this is what I ended up doing.
class Array( T_scalar, T_args ... )
{
private:
alias ArrayTraits!( T_scalar, T_args ) traits;
alias traits.isDynamic T_isDynamic;
alias traits.shapeAtCompileTime T_shapeAtCompileTime;
alias traits.sizeAtCompileTime T_sizeAtCompileTime;
alias traits.storageOrder T_storageOrder;
alias traits.dataType T_dataType;
}
struct ArrayTraits( T_scalar, T_args ... )
if ( areTemplateParametersValid!( T_scalar, T_args )() )
{
private:
static if ( hasFlag( Flags.storageOrder ) )
alias T_args[0 .. $ - 1] shapeTuple;
else
alias T_args shapeTuple;
public:
static immutable bool isDynamic = hasFlag( Flags.dynamic ) ? true : false;
static immutable auto shapeAtCompileTime = getShapeAtCompileTime();
static immutable size_t sizeAtCompileTime = getSizeAtCompileTime();
static immutable StorageOrder storageOrder = hasFlag( Flags.storageOrder ) ?
T_args[$ - 1] : defaultStorageOrder;
static if ( hasFlag( Flags.dynamic ) == true )
alias T_scalar[] dataType;
else
alias T_scalar[sizeAtCompileTime] dataType;
public:
static auto getShapeAtCompileTime()
{
static if ( hasFlag( Flags.dynamic ) == true )
{
static assert( shapeTuple.length == 1,
"The shape of a dynamic array needs to be defined at run-time." );
size_t[1] shapeAtCompileTime = [Storage.dynamic];
return shapeAtCompileTime;
}
else
{
static assert( shapeTuple.length > 0,
"No dimensions have been defined." );
size_t[shapeTuple.length] shapeAtCompileTime;
foreach ( i, dimension; shapeTuple )
{
static assert( isIntegral!( typeof( dimension ) ),
"Dimensions sizes for a static array needs to be defined as integrals." );
static assert( dimension > 0,
"Dimensions sizes for a static array cannot be null or negative." );
shapeAtCompileTime[i] = dimension;
}
return shapeAtCompileTime;
}
}
static size_t getSizeAtCompileTime()
{
if ( hasFlag( Flags.dynamic ) == true )
return 0;
size_t size = 1;
foreach ( dimension; shapeAtCompileTime )
size *= dimension;
return size;
}
private:
/++ Parses the template tuple parameter to extract the different flags passed, if any. +/
static int getFlags()
{
int flags = 0;
if ( is( typeof( T_args[0] ) == Storage ) && T_args[0] == Storage.dynamic )
flags |= Flags.dynamic;
if ( is( typeof( T_args[$ - 1] ) == StorageOrder ) )
flags |= Flags.storageOrder;
return flags;
}
/++ Checks if the template tuple parameter contains a specific flag. +/
static bool hasFlag( Flags flag )
{
return (getFlags() & flag) == 0 ? false : true;
}
private:
enum Flags : int
{
dynamic = 1 << 0,
storageOrder = 1 << 1
}
}
bool areTemplateParametersValid( T_scalar, T_args ... )()
{
static assert( T_args.length > 0,
"No dimensions have been defined." );
return true;
}

Related

Adding XPointer/XPath searches to ALL(?) C++ JSON libraries, is it doable?

Is it possible to extend all(?) existing C++ JSON libraries with XPath/XPointer or subset with just one C++ implementation? At least those with iterators for object and array values?
I have reviewed three C++ JSON libraries (reviewing nlohmann, Boost.JSON and RapidJSON) to see the internals and check their search functionality. Some have implemented Json pointer. Json pointer is basic, almost like working with json as a name-value list.
XML has XPath and XPointer searches and rules are standardized. With XPath and XPointer you can do more.
One reason to reviewing these libraries was to see if it is possible to extend any of them with better search functionality. Or might it be possible to extend all(?) C++ JSON libraries at once?
A longer text describing this can be found here, trying to be brief.
I tried to do one traverse method that selects json values with one specific property name and that method should work an all tested JSON libraries. If I got that to work it may be possible to add more search logic and get it to work on almost all C++ JSON.
I got this C++ templated function to work an all tested json libraries. It can walk the JSON tree and select json values on all tested libraries.
What is needed to is to implement specializations of is_object, is_array, compare_name, get_value, begin and end. That are just one liners so it's easy.
template<typename json_value>
bool is_object( const json_value* p )
{ static_assert(sizeof(json_value) == 0, "Only specializations of is_object is allowed"); }
template<typename json_value>
bool is_array( const json_value* p )
{ static_assert(sizeof(json_value) == 0, "Only specializations of is_array is allowed"); }
template<typename iterator>
bool compare_name( iterator it, std::string_view stringName )
{ static_assert(sizeof(it) == 0, "Only specializations of compare_name is allowed"); }
template<typename iterator, typename json_value>
const json_value* get_value( iterator it )
{ static_assert(sizeof(it) == 0, "Only specializations of get_value is allowed"); }
template<typename iterator, typename json_value>
iterator begin( const json_value& v ) { return std::begin( v ); }
template<typename iterator, typename json_value>
iterator end( const json_value& v ) { return std::end( v ); }
// ------------------------------------------------
// Selects all json values that match property name
template<typename json_value, typename object_iterator,typename array_iterator = object_iterator>
uint32_t select( const json_value& jsonValue, std::string_view stringQuery, std::vector<const json_value*>* pvectorValue = nullptr )
{ assert( is_object( &jsonValue ) || is_array( &jsonValue ) );
uint32_t uCount = 0;
if( is_object( &jsonValue ) == true ) // found object ?
{
for( auto it = begin<object_iterator,json_value>( jsonValue ); it != end<object_iterator,json_value>( jsonValue ); it++ )
{
if( is_object( get_value<object_iterator,json_value>( it ) ) == true )
{ // found object, scan it
auto value = get_value<object_iterator,json_value>( it );
uCount += select<json_value,object_iterator>( *value, stringQuery, pvectorValue );
}
else if( is_array( get_value<object_iterator,json_value>( it ) ) == true )
{ // found array, scan it
auto parray = get_value<object_iterator,json_value>( it );
uCount += select<json_value,object_iterator,array_iterator>( *parray, stringQuery, pvectorValue );
}
else if( compare_name<object_iterator>( it, stringQuery ) == true )
{ // property name matches, store value if pointer to vector
if( pvectorValue != nullptr ) pvectorValue->push_back( get_value<object_iterator,json_value>( it ) );
uCount++;
}
}
}
else if( is_array( &jsonValue ) == true ) // found array
{
for( auto it = begin<array_iterator,json_value>( jsonValue ); it != end<array_iterator,json_value>( jsonValue ); it++ )
{
if( is_object( get_value<array_iterator,json_value>( it ) ) == true )
{ // found object, scan it
auto value = get_value<array_iterator,json_value>( it );
uCount += select<json_value,object_iterator>( *value, stringQuery, pvectorValue );
}
else if( is_array( get_value<array_iterator,json_value>( it ) ) == true )
{ // found array, scan it
auto parray = get_value<array_iterator,json_value>( it );
uCount += select<json_value,object_iterator,array_iterator>( *parray, stringQuery, pvectorValue );
}
}
}
return uCount;
}
if this works and if I haven't forgot something, shouldn't it be possible to extend all libraries with just one implementation? The additional logic for XPath and XPointer is not dependent on the implementation of these C++ JSON libraries.
Am I missing something

Is there a way to create a c++ template with different return types?

I'm wondering if there's a way to write a C++ template with different return types.
My use case is a method returning the largest values from a list.
But since I'm using the Qt framework, this function shall be able to deal with numeric and QString values. When feeding this function with a list of QString, the function shall return the length of the largest string. In case of passing numeric values, the input type shall be the return type.
What I've written is this:
template< class T >
auto getMax( QList< T > aList ) -> decltype( std::is_arithmetic< T >::value ? T : int( 0 ) )
{
if ( std::is_arithmetic< T >::value )
{
T Result( aList.isEmpty() ? 0 : aList.first() );
for ( auto lElement : aList )
{
Result = std::max( Result, lElement );
}
return Result;
}
if ( std::is_same< T, QString >::value )
{
// List contains QString -> return length of largest string
int Result( aList.isEmpty() ? 0 : aList.first().length() );
for ( const QString & lrcsElement : aList )
{
Result = std::max( lrcsElement.length(), Result );
}
return Result;
}
return 0;
}
This code compiles with VS 2017.
But when I want to use the template function like this
const QString sError ( tr( "Error" ) );
const QString sWarning( tr( "Warning" ) );
const QString sInfo ( tr( "Information" ) );
const QString sDebug ( tr( "Debug " ) );
auto iMaxTextLength( SMUtils::getMax< QString >( { sError, sWarning, sInfo, sDebug } ) );
the compiler gives me some error messages:
Error C2672: "SMUtils::getMax": no matching overloaded function found.
Error C2893: Failed to specialize function template "unknown-type SMUtils::getMax(QList)".
Error C2119: "li32MaxTextLength": the type for "auto" cannot be deduced from an empty initializer.
Of course I could write a specialized getMax( QStringList ) method, but I was wondering if it's possible to use only one template function.
Is that even possible and if so, how?
Thanks,
Sören
-> decltype( std::is_arithmetic< T >::value ? T : int( 0 ) )
should be
-> std::conditional_t<std::is_arithmetic<T>::value, T, int>;
or even omit it completely and let compiler deduce it (but requires correct return types, so following if constexpr).
and your
if ( std::is_arithmetic< T >::value )
should be
if constexpr ( std::is_arithmetic< T >::value )

Can I have a single template with a variable object?

I currently have the following code:
template< class Obj, class ObjResult >
CLStatus convertObjToResult2( const Obj & xFrom, ObjResult & xTo )
{
CLStatus eStatus = CLSTATUS_SUCCESS;
switch ( xTo.eType )
{
case CEPTFull:
xTo.xData.xFull = xFrom;
break;
case CEPTBrief:
eStatus = Convert( xFrom, xTo.xData.xBrief );
break;
default:
eStatus = CLSTATUS_INVALIDPROJECTIONTYPE;
}
return eStatus;
}
template< class Obj, class ObjResult >
CLStatus convertObjToResult1( const Obj & xFrom, ObjResult & xTo )
{
CLStatus eStatus = CLSTATUS_SUCCESS;
switch ( xTo.eType )
{
case CEPTFull:
xTo.xData.xFull = xFrom;
break;
default:
eStatus = CLSTATUS_INVALIDPROJECTIONTYPE;
}
return eStatus;
}
All ObjResults have an xFull, but only some have an xBrief, where xData is a union. This resulted in me writing the two different templates above, but it would be great if I could somehow have just one template.
I can't simply use convertObjToResult2, since it will fail to compile with object types that do not have an xBrief. I looked at this answer to see if it would help, but I don't understand at all what it's doing.
Since C++ does not and won't have a static if feature, you need a work around.
If you can't overload Convert as you said in comments, I thought of a layer above it which can be specialized depending on if ObjResult has the member or not.
template <class Obj, class ObjResult, class = void>
struct ConvertIfHasBrief {
static auto Convert(Obj const &, ObjResult &) -> CLStatus {
return {};// dymmy value, not used
}
};
template <class Obj, class ObjResult>
struct ConvertIfHasBrief <Obj, ObjResult,
std::void_t<decltype(std::declval<ObjResult>().xData.xBrief)>> {
static auto Convert(Obj const &xFrom, ObjResult &xTo) {
return ::Convert(xFrom, xTo.xData.xBrief);
}
};
template< class Obj, class ObjResult>
CLStatus convertObjToResult( const Obj & xFrom, ObjResult & xTo )
{
CLStatus eStatus = CLSTATUS_SUCCESS;
switch ( xTo.eType )
{
case CEPTFull:
xTo.xData.xFull = xFrom;
break;
case CEPTBrief:
eStatus = ConvertIfHasBrief<Obj, ObjResult>::Convert(xFrom, xTo);
break;
default:
eStatus = CLSTATUS_INVALIDPROJECTIONTYPE;
}
return eStatus;
}
std::void_t is not yet part of the standard, but the implementation is simple and can be found on the linked page. Just be sure not to declare it on the std namespace.
proof it works

generate unique key for std::map

I have a map with a string as the key and stores lambdas.
I've so far tried
std::map <int, auto> callbackMap
And put a lambda where there isn't one with the same number already. Is this possible ? I keep getting errors saying functions can't have auto as constructors.
It's because auto is just a compile time "feature" that converts the type you need to a very defined type! You are maybe confusing it with a "variant" type... it doesn't work this way.
auto X = 3;
It doesn't mean X is a "variant". It's like the compiler converts it to:
int X = 3;
So, notice that X has a very defined type.
You CAN store functions (lambda is the operator) in your map, no problem. But with your std::function<...> very defined. Example:
std::map< int, std::function< int( int ) > > callbackMap;
callbackMap[ 0 ] = std::function< int( int ) >( [] ( int a ) { return a + 1; } );
callbackMap[ 1 ] = std::function< int( int ) >( [] ( int a ) { return a - 1; } );
callbackMap[ 2 ] = std::function< int( int ) >( [] ( int a ) { return a * 2; } );
callbackMap[ 3 ] = std::function< int( int ) >( [] ( int a ) { return a / 2; } );
Notice that you still need to know the signature of your functions... (here in my example int( int a ), but you can define of course the way you want).
If you decide to store "pointers to functions" you will have the same problem. You have to know the signature! Nothing different.

C++ runtime field access

class SuperClass{
/* ==================== METHODS ======================================= */
void
setValue (
std::string name,
int i ) {
MemberMapIterator it = memberMap_.find ( name );
if ( it != memberMap_.end ( ) ) {
void* ptr = ( *it ).second;
long long classPtr = reinterpret_cast< long long > ( this );
long long memberPtr = reinterpret_cast< long long > ( ptr );
int* value = reinterpret_cast< int* > ( classPtr + memberPtr );
( *value ) = i;
}
} // setValue
int
getValue (
std::string name ) {
MemberMapIterator it = memberMap_.find ( name );
if ( it != memberMap_.end ( ) ) {
void* ptr = ( *it ).second;
long long classPtr = reinterpret_cast< long long > ( this );
long long memberPtr = reinterpret_cast< long long > ( ptr );
int* value = reinterpret_cast< int* > ( classPtr + memberPtr );
return *value;
}
return -234234;
} // getValue
protected:
/* ==================== METHODS ======================================= */
void
Build ( ) {
configure ( );
} // Build
void
AddMember (
std::string name,
void* ptr ) {
memberMap_.insert ( MemberMapPair ( name, ptr ) );
} // AddMember
/* ==================== STATIC METHODS======================================= */
virtual void
configure ( ) = 0;
private:
/* ==================== METHODS ======================================= */
/* ==================== DATA MEMBERS ======================================= */
MemberMap memberMap_;
};
class SubClass: public SuperClass {
public:
/* ==================== LIFECYCLE ======================================= */
SubClass( ) : age_ ( 0 ) {
Build ( );
} /* constructor */
~SubClass( ) /* destructor */
{ }
protected:
/* ==================== STATIC METHODS======================================= */
void
configure ( ) {
long long classPtr = reinterpret_cast< long long > ( this );
long long agePtr = reinterpret_cast< long long > ( &this->age_ );
void* ptr = reinterpret_cast< void* > ( agePtr - classPtr );
this->AddMember ( "age", ptr );
} // configure
private:
/* ==================== DATA MEMBERS ======================================= */
int age_;
}
In SubClass, I add the offset of the private class field (thinking about the class as a C structure) the Super class map using string name as a key.I will make to execute configure only once and then I want to use this offset for every Person instance to access to its private fields at runtime (this + offset = field). Will be this safe? I tested this code and its work it is doing what I want. But should I expect any memory violations or something else (assuming that it won't be intentional violation (programmer errors))?
First of all, it's worth knowing that C++ classes are not like C structs. The compiler puts extra things in classes like the vtable pointer, which could be in the beginning, the end, or somewhere else depending on the compiler. There is one kind of class that behaves like a C struct (i.e. a bag of bits) and they're called Plain Old Data types (POD). You can find plenty on them on StackOverflow. Since you're using inheritance, you're not using a POD.
If you're trying to forcefully access private members, you probably need to rework your design. You should ask yourself why they're private in the first place. Based on what it looks like you're going for in the code, I can think of more straightforward approaches:
Cast the base class to the subclass, then set the private member with a setter function.
You could make setValue and getValue virtual and override it in the subclass.
Use friendship.