How to use a custom type u64 of "typedef unsigned long long u64;" in a header file without actually including another header file - typedef

How to use a custom type u64 of "typedef unsigned long long u64;" in a header file without actually including another header file? I have seen Q&A for typedef struct and all using forward declaration, but what when it comes to something like the below snippet:
//my_type.h
typedef int s32;
typedef unsigned long long u64;
typedef long long s64;
//my_foo.h
s32 foo(s32 value1);

Just copy the typedef into your own header/source file.

Related

Are there now two ways to use typedef in Dart?

I see multiple forms of typedef throughout dart and flutter libraries, but I can't quite make sense of it. There's this example in framework.dart:
typedef ElementVisitor = void Function(Element element);
And there's this example (https://medium.com/#castellano.mariano/typedef-in-dart-40e96d3941f9):
typedef String Join(String a, String b);
I don't quite understand the difference of their uses. Maybe this has something to do with why I can't find the definition of "Function" anywhere in the Dart or Flutter libraries. But then again I can find other typedef's just fine in the framework.dart file.
As docs refers
There is an old and new way of typedef
in general: the new way is a bit clearer and more readable.
in details:
typedef G = List<int> Function(int); // New form.
typedef List<int> H(int i); // Old form.
Note that the name of the parameter is required in the old form, but the type may be omitted. In contrast, the type is required in the new form, but the name may be omitted.
As well as
The reason for having two ways to express the same thing is that the new form seamlessly covers non-generic functions as well as generic ones, and developers might prefer to use the new form everywhere, for improved readability.
There is a difference between declaring a generic function type and declaring a typedef which takes a type argument. The former is a declaration of a single type which describes a certain class of runtime entities: Functions that are capable of accepting some type arguments as well as some value arguments, both at runtime. The latter is a compile-time mapping from types to types: It accepts a type argument at compile time and returns a type, which may be used, say, as a type annotation. We use the phrase parameterized typedef to refer to the latter. Dart has had support for parameterized typedefs for a while, and the new syntax supports parameterized typedefs as well. Here is an example of a parameterized typedef, and a usage thereof:
typedef I<T> = List<T> Function(T); // New form.
typedef List<T> J<T>(T t); // Old form.
I<int> myFunction(J<int> f) => f;
For more info
A typedef can be used to specify a function signature that we want specific functions to match. A function signature is defined by a function’s parameters (including their types). The return type is not a part of the function signature.
typedef function_name(parameters)
A variable of typedef can point to any function having the same signature as typedef.
typedef var_name = function_name
One declared a type while other assigns it to a typedef variable
https://dart.dev/guides/language/language-tour#typedefs

Does Vala have typedefs?

Does Vala have some capability to do something similar to typedef in C or alias in D? I've checked its sporadic documentation, and couldn't find anything related to this.
Not a typedef as such, but you can extend most types, including the primitives:
public struct foo : int8 { }
This will generate a typedef in the generated C, but, strictly speaking, it isn't one in Vala because it isn't a type alias (i.e., int8 and foo are not automatically interconvertible).
This doesn't work for delegates.

Linux device driver application

In my device driver I use
write_Parport_data(struct parport *p, unsigned char data);
In my application, I have to create variable of type struct parport and assign the first element to a value.
such as:
struct parport strApp;
strApp.base = 0x378; // Statement 1
Then:
write_Parport_data(&strApp, 0xff);
gcc compiler error about statement 1
error: invalid use of undefined type 'struct parport'
Even if I include in my application #include
I get the same error.
Even if I use insmod, the mknod to make the driver ready, still compiling the application will give the same error. How my application should use struct parport, what is missing?
Thanks
Are you sure you are including linux/include/linux/parport.h? And giving the compiler the path in the compilation? In other words, are there other instances of defining a var with the parport structure that does not get a compiler error?
Where you have defined this structure ? You should include that header.
You should include that file which has definition of your struct parport
If you have defined that structure in C file, then you should extern that struct definition in header file and include that header file.

why this complicated structure constants is internal linkage

as you know, constants defaults to internal linkage.
const int Buf = 1000; // defaults to internal linkage
Buf can be defined in a header file, it's visible only within the files where it is defined and cannot be seen at link time by other translation units.
however, if some complicated structure constants are defined as below:
- constants.h
const complicatedClass myObject("I'm a const object","internal linkage",5);
complicatedClass definition:
class complicatedClass
{
private :
char* charArry;
std::string strTemp;
static int numbers;
int mSize;
public:
complicatedClass();
complicatedClass(char* pChrArry, std::string temp, int size);
~complicatedClass();
public:
void print() const;
std::string getStrTemp() const;
};
it seems that compile must create storage for complicated structure constants, thus it should be external linkage. however, everything is ok when this constants header file (constants.h) was included in multiple files. I assume the linker error should be raised, myObject shouldn't be defined in many places(in multiple files)
can anyone explain this issue? thanks in advance.
Internal linkage does not mean no storage. Rather it means the variable is not visible in other translation units.
In C++ const allows the compiler to either create storage for the variable or not. Whether it does so or not depends on whether it needs it.
So in your example the compiler will create storage for myObject only if it needs it (which it probably does) because it is const. Also because it is const, myObject will also have internal linkage which means each translation unit will have its own copy of myObject if storage is required.
A simple test you can do to see this in action is to take the address of myObject in a number of different translation units (effectively in different cpp files) and print it out. This will do two things: force storage to be created for myObject even if it wasn't already; and you will see two different addresses because of the internal linkage.

Using C++ struct with iPhone app

As I heard it is possible to use C++ Code within an iPhone (Objective C) project, I want to use an encryption library which is written in C++. However, the library uses a C++ type struct which uses a constructor, that I can't get right.
The Struct looks like this:
struct SBlock
{
//Constructors
SBlock(unsigned int l=0, unsigned int r=0) : m_uil(l), m_uir(r) {}
//Copy Constructor
SBlock(const SBlock& roBlock) : m_uil(roBlock.m_uil), m_uir(roBlock.m_uir) {}
SBlock& operator^=(SBlock& b) { m_uil ^= b.m_uil; m_uir ^= b.m_uir; return *this; }
unsigned int m_uil, m_uir;
};
full source is available here: http://www.codeproject.com/KB/security/blowfish.aspx
what's the easiest way to get around that issue?
I've read the article about using c++ code on apple's developer site, but that didn't help much.
It's definitely possible and the trick is extremely simple: when you are going to use C++ code in your Objective-C++ applications, name your files .mm instead of .m.
So if you have YourViewController.h and YourViewController.m, rename the latter to be YourViewController.mm. It will cause XCODE to use C++ compiler instead of C compiler with your Objective-C++ code.
YourViewController.mm:
- (void) yourMessage {
// will compile just fine and call the appropriate C++ constructor
SBlock sb(1,1);
}
Just change the filename extension of your .m file to .mm and include the C++ headers. Wow, I type too slow, lol.