trying a code to check expect statement in specman - specman

The following gives error for event a, b declaration as "unrecognized struct member". Please help me understand.
unit true_match_op {
event a, b;
my_task() #sys.any is {
emit a;
wait [2]*cycle;
emit b;
wait [5]*cycle;
emit a;
expect #b => {#a ; ~[..]};
};
run() is also {
start my_task();
stop_run();
};
};
extend sys {
my_unit : true_match_op is instance;
};

First of all, you have to add a different event declaration for each event (this isn't C/C++). Also, an expect is a declarative construct. You have to declare it outside of the method:
unit true_match_op {
event a;
event b;
my_task() #sys.any is {
emit a;
wait [2]*cycle;
emit b;
wait [5]*cycle;
emit a;
};
expect #b => {#a ; ~[..]};
};

Related

Loose coupling in Dart

I was trying to implement loose coupling in one of my Flutter projects. It was not able to find the method.
Have replicated the same in a simple Dart code, how can I fix this, and is there some way to achieve loose coupling in Dart?
abstract class A{}
class B extends A{
void help(){
print("help");
}
}
class C {
A b;
C({required this.b});
void test(){
b.help();
}
}
void main() {
var c = C(b:B());
c.test();
}
Giving error at b.help(), the method does on exist.
Exact error
The method 'help' isn't defined for the type 'A'.
b is known to be of type A, and the A interface does not provide a help method.
I don't know exactly what your definition of "loose coupling" is (it'd be better to describe a specific problem that you're trying to solve), but if you want help to be callable on type A, then you must add it to the A interface.
You alternatively could explicitly downcast b to B with a runtime check:
class C {
A b;
C({required this.b});
void test() {
// Shadow `this.b` with a local variable so that the local
// variable can be automatically type-promoted.
final b = this.b;
if (b is B) {
b.help();
}
}
}
Or if you want duck typing, you could declare (or cast) b as dynamic:
class C {
dynamic b;
C({required this.b});
void test() {
try {
b.help();
} on NoSuchMethodError {}
}
}
although I would consider the last form to be bad style.

Shall I build a destructor in this classes?

I am currently working on building an ABM model using C++.
I have classes that have the need to interact with each other, because e.g. class B needs to examine values in class A and return some evaluation on it, which then class C might want to read. Classes need not to change other classes values, only to read from them.
Class B in my current implementation has a po
inter to a vector containing all members of Class A. The pointer is there for two order of reason: it makes easier to initialize the vector, and the vector is left in the scope of main so that I can access and loop over it, calling the members of class A for each agent.
My MCVE:
#include <iostream>
#include <vector>
using namespace std;
class A; // Forward declaration
class B{
int id,
some_value;
vector<A> * A_vec;
public:
// Overloaded constructor
B(int ID, vector<A> & PTR)
{
A_vec = & PTR;
id = ID;
some_value = 0;
};
// Copy Constructor
B( const B& that ):
id(that.id),
some_value(that.some_value)
{
// Pointer ??
};
// Non-default destructor -> uncomment leads to seg_fault
/*
~B(){ delete [] A_vec;};
*/
// Assignment operator
B& operator=(const B& that)
{
id = that.id;
some_value = that.some_value;
// Pointer ??
return *this;
};
//Methods to update different variables go here ..
void do_stuff();
};
class A{
B & class2_ref;
vector<double> o;
public:
int stuff;
// Overloaded constructor
A(int STUFF, B & REF, vector<double> O):
class2_ref(REF),
o(O)
{
stuff = STUFF;
};
// Methods to update different variables go here ..
};
void B::do_stuff()
{
int L = A_vec->size();
for(int l = 0; l<L; l++) some_value += (*A_vec)[l].stuff; // Perform some operation
};
int main(){
int I = 5; // Number of objects of A
vector<double> O(12,2); // Some numbers in here
B b(0,A_vec);
for(int i = 0; i< I; i++)
{
A a(i,b,O);
A_vec.push_back(a);
}
b.do_stuff();
cout<< "Debugging MCVE" << endl;
return 0;
}
My question then is:
Should I implement the destructor/copy constructor/assignment operator in class B? What about class A ? If so, can you please point me to the correct syntax(for the destructor the one above in comments leads to seg fault).
My understanding is that this might be one of the case in which I am happy with a "shallow" destruction of the pointer, because both class B and vector<A> will go out of scope at the return statement. class B owns the pointer, which gets destructed when it is due, and the same for vector.
But then, what about the other member from the rule of three?
There is only one object of class B planned, but I might (small chance) want to generalize later on.
if a class have a pointer type, you should implement a destructor, and i would suggest implementing a copy and an assignment operator as well, else you will be dealing with the same object from 2 different places, which could cause you some errors, for example -
void someFunction(B &b)
{
B a = b;
}
B b(0,A_vec);
someFunction(b); //After finishing someFunction, it will try to delete the vector from a , but it is the same vector you used in b.
b.do_stuff(); // Would cause a seg error
And for the destructor syntax, just delete the vector, not its content, it will use the vector default destrctor on the content:
delete A_vec
just make sure you dont use it if its not initialized, i would suggest just building a empty vector on each ctor of the class, that way you wont get a seg fault and you can use delete.

How to overload the << operator based on a method display already defined?

I would like to overload the << operator for my class from a method display already defined. I get an compiler error of no match for operator <<.
Here is a minimal example:
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass()
{}
ostream& display(ostream& out) const
{
out << "Display message" << endl;
return out;
}
ostream& operator<< (ostream& out) const
{
ostream& output = display(out);
return output;
}
};
int main()
{
MyClass C1;
cout << C1 << endl;
return 0;
}
Although C1.display(cout); woks without problems!
You have defined operator<< as a member function of MyClass. Therefore, you must call it like member functions are called (object on the left, parameter on the right), like this:
C1 << cout;
But that doesn't seem to be what you want. You probably want to be able to call it like this:
cout << C1;
In that case the function can't be a member of MyClass. It would have to be a member of cout, or a free function (outside any class). And in this case it must be a free function because you can't change the definition of cout.
So, to declare operator<< as a free function, it needs to have two arguments (left-hand-side and right-hand-side):
ostream& operator<< (ostream& out, const MyClass& c) { ... }
Now you can call it with an ostream on the left and a MyClass object on the right, like this:
cout << C1;

Inheritance with templates

#include<iostream>
using namespace std;
class C
{
public:
C (){};
virtual void nothing()=0;
virtual ~C (){};
};
class A : public C
{
public:
A (){};
virtual void nothing(){};
};
class B:public A
{
public:
B(){};
void nothing(){};
};
template <class T>
void hi(T){
cout << " i am something\n";
}
template <>
void hi<A>(A)
{
cout << " I am A\n";
}
template <>
void hi<B>(B)
{
cout << " I am B\n";
}
int main ( )
{
C *array [] = {new A,new B};
hi (*array [0]);
hi (*array [1]);
delete array [0];
delete array [1];
return 0;
}
Out:
i am something
I am something
Currently I am writing a program that has to deal with
Inherited types and specialised templates. In the example above I would l would like to see
I am A
I am B
Is there a way to properly invoke the functions corresponding to the objects although I am handling a base class array? I am not sure if type checking and conversion via dynamic_cast is the most elegant solution. Note that this is just an excerpt from a larger program.
Thank you in advance
In the main routine, the three lines shown below create an array of C*.
So any element of that array is treated as a C* regardless of
what the actual type of the element is.
That is, when you pass *array [0] to the function hi(),
the function that gets called is hi(C) which resolves to
the generic hi function, not either of the specialized functions.
C *array [] = {new A,new B};
hi (*array [0]);
hi (*array [1]);
In order to make hi<A> be invoked, you either have to store the pointer
to the new object in a variable of type A* or you need to cast the
C* to an A*.
In a case like this, a virtual function of C, overridden in A and B,
may serve the purpose better.

Python Garbage Collection causes SegFault when destructing a C++ object

I have an in-house C++ library that I've successfully exposed to Python using Boost.Python. It accepts a user-created Python object and then uses some methods within that object to perform certain tasks, and it works quite well for the most part.
The Python use of the library looks like:
class Foo(object):
def __init__(self, args):
"""create an instance of this class with instance-specific attributes"""
def Bar1(self, a, b, c):
"""do something with the given integers a, b and c"""
return a + (b*c)
def Bar2(self, a, b, c):
"""do something else with the given integers a, b and c"""
print (a*b) + c
import mylib
cheese = mylib.Wine()
qux = Foo()
cheese.setup(qux)
cheese.do_something(1)
cheese.do_something(2)
The "Wine" object in C++ looks like:
#include <boost/python.h>
#include <Python.h>
class Wine {
public:
Wine() {};
~Wine() {};
void setup(boost::python::object baz) {
baz_ = baz;
};
static void do_something(boost::python::object pyreq) {
int request = boost::python::extract<int>(pyreq);
int a = 1;
int b = 2;
int c = 3;
if (request == 1) {
int d = boost::python::extract<int>(baz_.attr("Bar1")(a, b, c));
};
else if (request == 2) {
baz_.attr("Bar2")(a, b, c);
};
};
private:
static boost::python::object baz_;
};
BOOST_PYTHON_MODULE(mylib)
{
using namespace boost::python;
class_<Wine>("Wine")
.def("do_something", &Wine::do_something)
.staticmethod("do_something")
.def("setup", &Wine::setup)
;
};
The problem is that, after successfully executing all of the tasks, the program terminates with a SegFault. This isn't really a huge deal because the code that I need to execute still executes, and the tasks that I need to perform are all performed. The SegFault only occurs on the destruction of this C++ "Wine" object. Still, it's an inelegant outcome and I'd like to fix the problem.
What I could gather from an online search implied that this is a problem with improper declaration of ownership to Python. The end result is that the C++ destructor gets called twice, and the second call causes a SegFault.
Unfortunately I haven't been able to remedy the problem so far. Available documentation only covers the basics and I haven't been able to replicate some success others have had using boost smart pointers and some fancy declaration/destruction tricks in C++ with it. Any guidance would be much appreciated.
The problem is that the static Wine::baz_ object is being destroyed during static/global destruction. This is after the Python runtime has been finalized, but since boost::python::object uses the Python C-API, its destruction requires a valid Python runtime (though possible not if the object refers to None.) By arranging for baz_ to be destroyed before Python finalization, you should be able to avoid the segfault. The cleanest approach overall might be to make baz_ a non-static member variable.