Groups inside structs - data-structures

Can I have groups inside a struct?
pseudo-code:
typedef struct {
input_group {
logic a;
}
output_group {
logic b;
}
} my_signals_list

Short answer: no.
If you want to have signals grouped like this, why not create a struct for the input group and a struct for your output group?
typedef struct {
logic a;
} input_group_s;
typedef struct {
logic b;
} output_group_s;
typedef struct {
input_group_s input_group;
output_group_s output_group;
} my_signals_list;
As Greg points out in the comments, you can also have nested struct definitions inside the main struct:
typedef struct {
struct { logic a; } input_group;
struct { logic b; } output_group;
} my_signals_list;
If you want to specify signals for a module in a nice encapsulated fashion, I would suggest using an interface, though.

Related

golang Define struct once and use it in another struct definition

Define struct once and use it in another struct defination
type FormAction struct {
Data bool `yaml:"data,omitempty" json:"data,omitempty"`
Self bool `yaml:"self,omitempty" json:"self,omitempty"`
Blob bool `yaml:"blob,omitempty" json:"blob,omitempty"`
}
type ManifestSrc struct {
Data bool `yaml:"data,omitempty" json:"data,omitempty"`
Self bool `yaml:"self,omitempty" json:"self,omitempty"`
Blob bool `yaml:"blob,omitempty" json:"blob,omitempty"`
}
type PrefetchSrc struct {
Data bool `yaml:"data,omitempty" json:"data,omitempty"`
Self bool `yaml:"self,omitempty" json:"self,omitempty"`
Blob bool `yaml:"blob,omitempty" json:"blob,omitempty"`
}
how we can reduce the redundancy of same members ?
Probably the most idiomatic way of defining the same fields in multiple structs without repeating yourself is to use embedding, because it still allows you to add other fields, eg:
type entity struct {
Data bool `yaml:"data,omitempty" json:"data,omitempty"`
Self bool `yaml:"self,omitempty" json:"self,omitempty"`
Blob bool `yaml:"blob,omitempty" json:"blob,omitempty"`
}
type FormAction struct {
entity
}
type ManifestSrc struct {
entity
}
type PrefetchSrc struct {
entity
AnotherField string // For example
}

Is it possible to extend a tagged union with more variants in Cap'n proto while being binary-compatible with any old data?

I'm looking into protocol schema languages, and it seems like Cap'n'proto will suit my needs, but there's one critical feature I need which I cannot find in the docs:
Let's say I have this tagged union with two struct members in C-like syntax:
struct taggedUnion {
int tag;
union {
struct a {
int x;
}
struct b {
float x;
}
}
}
Can I then in the future add another struct to the tagged union, while still being able to read the old data?
struct taggedUnion {
int tag;
union {
struct a {
int x;
}
struct b {
float y;
}
struct c {
int z;
bool b;
}
}
}
It feels like it should be doable, but I can't find anything in the docs saying that it is. There's a note on groups being extensible without breaking wire-compatibility (new fields are zeroed out for old data).
If it's possible, how would I declare this change in cap'n proto schema syntax? A before/after example would be great!
Found it. ... new fields may be added to existing groups and unions seems like it could be an answer to this question.

Pointers to member as variadic template parameters

Is it possible to pass pointers-to-member as variadic template arguments. I can't seem to figure out the syntax.
for a function call it works like this:
struct A
{
int a;
float b;
}
template <typename ... TArgs> void f(A *obj, TArgs ... params)
{
void *members[] { (&(obj->*params))... };
// ... do something ...
}
That can be used like this:
f(obj, &A::a, &A::b);
I would like to pass params in a similar fashion to a class template
template <[something] ... params> class Foo
{
void Bar(A *obj)
{
void *members[] { (&(obj->*params))... };
// ... do something ...
}
};
That should be used like this:
Foo<&A::a, &A::b> foo;
foo.bar(obj);
I'm having trouble figuring out what [something] should be.
If member type is known and there is only one parameter, it can be done like this:
template <int A::*ptr> //...
Is there a way to generalize this for variadic parameter list of member pointers where members are of different unknown beforehand types?
Update: Variadic argument pack for member pointers of fixed known types is declared like so:
template<int A::*...ptr> struct Foo {};
Now I just need to replace int with typename that can be deduced.
And with C++17, following works perfectly:
template<auto A::*...ptr> struct Foo {};
Unfortunately I need a solution that will work with C++14
In C++14, you can use another level of indirection to do that:
struct A {
int a;
float b;
};
template<typename... T>
struct Bar {
template <T A::*... params>
struct Foo {
void Bar(A *obj) {
void *members[] { (&(obj->*params))... };
// ... do something ...
(void)members;
}
};
};
int main() {
A a;
Bar<int, float>::Foo<&A::a, &A::b> foo;
foo.Bar(&a);
}
auto keyword (introduced with the C++17 for non-type template parameters, as you mentioned) solves more or less this kind of issues. Think of std::integral_constant and how would it be more user-friendly if you hadn't to specify each time the type as the first argument...

how to access array of structure within a structure

struct students
{
char name[256];
int Roll_number;
};
struct colleges
{
char name[256];
Student students[100];
};
How to access student[0].name, I have tried to access using -> and . operator is is not accessable
Structure within Structure : Nested Structure
Structure written inside another structure is called as nesting of two structures.
Nested Structures are allowed in C Programming Language.
We can write one Structure inside another structure as member of another structure.
as member of another structure
#include <stdio.h>
struct students {
char name[256];
int Roll_number;
};
struct colleges {
char name[256];
struct students students[100];
};
int main(void)
{
struct colleges c = { };
printf("%s\n", c.students[0].name);
return 0;
}

boost::bimap find a relation

I just want to ask whether boost::bimap provides a method to find the relation in the bimap?
since I have a bimap with unordered_multiset at both side, I will need a function to check whether these is a relation between two objects.
I read some documentation but didnt find that.
class MyClass
{
std::string s1;
std::string s2;
bool operator == (MyClass const& myClass)
{
return (s1 == myClass.s1 && s2 == myClass.s2);
}
};
namespace std
{
template<>
struct hash<MyClass>
{
std::size_t operator()(const MyClass& myClass) const
{
std::size_t Seed = 0;
boost::hash_combine(Seed, myClass.s1);
boost::hash_combine(Seed, myClass.s2);
return Seed;
}
}
}
int main()
{
typedef boost::bimaps::bimap<boost::bimaps::unordered_multiset_of<client,std::hash<MyClass>, std::equal_to>, boost::bimaps::bimap<boost::bimaps::unordered_multiset_of<client,std::hash<MyClass>, std::equal_to>> MyBiMap;
MyBiMap MAP;
Map.value_type myRelation;
MAP.insert(myRelation(myClassObject1,myClassObject2));
MAP.insert(myRelation(myClassObject1,myClassObject4));
MAP.insert(myRelation(myClassObject3,myClassObject4));
MAP.insert(myRelation(myClassObject3,myClassObject6));
MAP.insert(myRelation(myClassObject5,myClassObject2));
// I want to check whether there is a relation between myClassObject1,myClassObject4
// for example MAP.find(myRelation(myClassObject1,myClassObject4)) returns the iterator
// and MAP.find(myRelation(myClassObject1,myClassObject6)) returns end();
}

Resources