How to add a new method to Eigen3 base class? - eigen3

I want to a simple method to copy a matrix using Eigen3 MatrixXd class. For that I create a header file with the new method and I use the macro uEIGEN_MATRIXBASE_PLUGIN to include in the compilation.
I want to create a method named copyMatrix() that simply is identical to do
A = B
but in this format:
A.copyMatrix(B).
When I try to code it with the following code:
template<typename OtherDerived>
inline void copyMatrix(const MatrixBase<OtherDerived>& other) const
{
derived() = other.derived();
}
I have compilation errors such as:
error C2678: binary '=': no operator found which takes a left-hand operand of type 'const Eigen::Matrix' (or there is no acceptable conversion)
Which is correct syntax for this?

This is because your method copyMatrix is const, just remove it:
template<typename OtherDerived>
inline void copyMatrix(const MatrixBase<OtherDerived>& other)
{
derived() = other.derived();
}

Related

Is there a way to use designated initializers to initialize parts of the same array in separate files?

I have thousands of "commands" that are all defined in their own files, and I want to be able to do things with them programmatically using an enum as a key.
The only way I can think of to do this is to have an externally linked initialization function for each "command" that is called by some other code to add a function pointer to a data structure. This sucks because then I have to declare these initialization functions in a header, define them in the individual files, and call them from somewhere else.
This method adds several lines of boiler plate for each "command," which is made worse because the boilerplate is spread across several files.
If it is possible, I'd like to have a single const array of function pointers to these static functions. Is there anything in the C99 standard that would allow me to do this?
The following code is a simplified representation of my problem, and some of it is definitely wrong, because I don't know how to do this or if it is even possible in C99.
Header included in all files
typedef enum
{
EXAMPLE_ENUM_0
EXAMPLE_ENUM_1
EXAMPLE_ENUM_2
EXAMPLE_ENUM_MAX_NUM_T,
} example_enum_t;
typedef void (*p_func_t)(void);
File 0
static void static_function_0(void)
{
void * p_peripheral = 0x00000200;
*p_peripheral = PERIPH_0_CONFIG;
}
extern int initialized_in_multiple_files[EXAMPLE_ENUM_MAX_NUM_T] = {
[EXAMPLE_ENUM_0] = &static_function_0
};
File 1
static void static_function_1(void)
{
void * p_peripheral = 0x00000300;
*p_peripheral = PERIPH_1_CONFIG;
}
extern int initialized_in_multiple_files[EXAMPLE_ENUM_MAX_NUM_T] = {
[EXAMPLE_ENUM_1] = &static_function_1
};
File 2
static void static_function_2(void)
{
void * p_peripheral = 0x00000400;
*p_peripheral = PERIPH_2_CONFIG;
}
extern int initialized_in_multiple_files[EXAMPLE_ENUM_MAX_NUM_T] = {
[EXAMPLE_ENUM_2] = &static_function_2
};
File where I want to use all the function pointers
p_func_t initialized_in_multiple_files[EXAMPLE_ENUM_MAX_NUM_T] = {0U};
for (uint32_t index = 0U; index < EXAMPLE_ENUM_MAX_NUM_T; index += 1U)
{
p_func_t p_func = initialized_in_multiple_files[index];
if (NULL != p_func)
{
p_func();
}
}
Is there a way to use designated initializers to initialize parts of the same array in separate files?
There can be many declarations of the same object, but only one definition. A declaration that initializes the object is a definition. Designated initializers do not alter this. Moreover, there is no partial initialization. Any initializer for an object initializes the whole object.
I'd like to have a single const array of function pointers to these static functions. Is there anything in the C99 standard that would allow me to do this?
No, not if the functions have internal linkage and are defined in separate files. However, you can work around the constness part by declaring a corresponding pointer to const elements in the header but defining the array itself non-const. Code in the translation unit where it is defined could then pass it (decayed to a pointer) to various functions to fill in its elements, but code in other translation units, seeing its const declaration in the header, would not be able to alter its contents (at least, not without casting away the constness).
Alternatively, if you give the functions external linkage then you could write one big initializer in the file of your choice.
Either way, you might be able to write a code generator to help you. That could significantly reduce the amount of boilerplate code you have to maintain manually.

How to avoid lots of structPointer->memberVariable inside procedural code in C

Hey I am currently trying to switch from c++ to c and wonder if there is any way to avoid this because there are no more classes to use the implicit this->:
void func(Data *const longAndExpressiveName)
{
longAndExpressiveName->x += longAndExpressiveName->vel.x;
}
Because even if the name is not that long it still makes the code much harder to understand.
There is no syntax to accomplish your direct ask in C. The closest you can come is to create temporary variables.
int *xptr = &longAndExpressiveName->x;
int vel_x = longAndExpressiveName->vel.x;
*xptr += vel_x;
Your desire to use longAndExpressiveName is misplaced in this case, because in C++, you are coming from a syntax where longAndExpressiveName is not passed as a parameter, and the parameter is implicitly hidden by the implicit this->. The C++ syntax for the caller would be:
longAndExpressiveName->func();
And you are translating that into:
func(longAndExpressiveName);
And then you propagate that same name to the parameter name. While it may be appropriate for the caller to refer to the pointer with a long and expressive name, it is not really helpful to the "member" function. But, what would be helpful is if the function is named so that you know it is meant to do something to a particular kind of structure.
void func_Data(Data * const d) {
d->x += d->vel.x;
}
Which better captures the original C++ syntax:
void Data::func () {
x += vel.x;
}
C does not have any magic syntax to do what you want explicitly. However, you can just assign the parameter with a long and expressive name to a variable of the same or compatible type with a shorter name:
void func(Data *const longAndExpressiveName)
{
Data *const t = longAndExpressiveName;
t->x += t->vel.x;
}
This cuts down on typing if the long and expressive name would otherwise be repeated a lot in the function.

How to change an argument “const” to “non-const” when calling a function in C

I've got a problem when the function is called by other functions.
My functions are so:
void *table_lookup(const table *t) {
...
//Here I want to call my other function.
table_remove(t);
...
}
void table_remove(table *t) {
...
}
I got a warning when I compile it. The problem is that I cannot change the argument's type.
You must NOT cast away the const qualifier. Any attempt to thereafter modify the value that was qualified const invokes Undefined Behavior. See C11 Standard - 6.7.3 Type qualifiers(p6).
The table_lookup parameter is const qualified for a reason. It allows the compiler to optimize the use of t. If you cast away the const and attempt to modify t you are breaking your promise to the compiler that t won't be modified.
Instead, you should refactor your code so that the remove() function calls table_lookup inside it to obtain a pointer (presumably) to the node you wish to remove. Then remove the node. Don't try and add a remove() within table_lookup. Create a new function.
In C, you can directly cast it to remove the 'const' property.
void *table_lookup(const table *t)
//Here I want to call my other function.
table_remove((table*)t) // remove 'const' by directly casting.
...
return
void table_remove(table *t)
...
You can cast away the const qualifier: table_remove((table *)t); but you might run into problems if table_remove tries to modify the table structure, for example if it is stored in a read-only segment.
Therefore you should not do it. It is rather unexpected that a lookup function would modify the table anyway. If it does for good reasons, such as building a hash table or maintaining a cache, the argument should not be declared as const.
Yes That’s right. it was bad idea to cast away const qualifier. . I can’t add a new function either. ((table)*) gives problem.
You can try the code below:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
void *table_lookup(const table *t) {
table_remove((table *)t);
}
#pragma GCC diagnostic pop
From: https://stackoverflow.com/a/13253997/5093308

Casting void* to other types

I'm getting the error:
error: 'void*' is not a pointer-to-object type
For the following code:
Button config_b1 = Button("b1");
Button config_b2 = Button("b2");
Button config_b3 = Button("b3");
const void *const buttons[3] PROGMEM = {
&config_b1, &config_b2, &config_b3
};
in some function:
(Button)*buttons[1]->setText("Foo");
The casting to Button fails.
This is just a simplified example. The array "buttons" will have different types of objects, so I need to initialize it as void and then cast to the proper type inside the function.
I think this is a simple precedence error, and you need
((Button *) buttons[1])->setText("foo");
Are you sure this is C, by the way? That kind of call really looks like C++ (it could be C, but then you'd need an explicit this equivalent in most cases).

Swift - how to fill size_t from C API via pointer

I have a C API with
void Fill(size_t * val){
*val = 15;
}
How can I call this method from Swift 3 and fill the internal class variable var number? Also, what type should it have?
If you open the C header file in Xcode and select "Navigate->Jump to Generated Interface" from the Xcode menu then you'll see that the
function is imported to Swift as
public func Fill(_ val: UnsafeMutablePointer<Int>!)
You call it from Swift by passing the address of an initialized
variable with & as inout expression, compare "Interacting with C APIs":
Mutable Pointers
When a function is declared as taking an UnsafeMutablePointer<Type>
argument, it can accept any of the following:
...
An in-out expression that contains a mutable variable, property, or subscript reference of type Type, which is passed as a pointer to the address of the mutable value.
...
In your case:
var myval = 0 // Type is `Int`
Fill(&myval)
But Swift defines size_t as well as
public typealias size_t = Int
therefore you can (even better) define the variable as
var myval: size_t = 0
which would then also compile on other platforms which might define
size_t differently.
First, you'll need to find what is the actual primitive type of size_t on your platform. Then, you can use this table to find out what you'll need to use as this function's type. For instance, supposing size_t is a #define alias for unsigned long, the Swift signature for this function will be:
func Fill(_ val: UnsafeMutablePointer<CUnsignedLong>!)
Since Objective-C is a strict superset of C, you can add C files to your project the same way you would add Objective-C files: Create a bridging header and import the functions you want to use in it.
Once you've created a bridging header and imported the appropriate header file, you should be able to call Fill from Swift code with no issue. You should be able to use it as follows:
import Foundation
class SomeClass {
var number: CUnsignedLong = 0
func someMethod() {
Fill(&number)
}
}

Resources