I am trying to do simulation in OMNeT++ and I have used Veins and SUMO as well in the simulation, but I have got this error:
Model error: TraCI Server reported error executing command 0xcc("View
'#0' is not known")..
I am using the header file TraCICommandInterface.h and in that using the below mentioned class
class GuiView {
public:
GuiView(TraCICommandInterface* traci, std::string viewId) : traci(traci), viewId(viewId) {
connection = &traci->connection;
}
void setScheme(std::string name);
void setZoom(double zoom);
void setBoundary(Coord p1, Coord p2);
void takeScreenshot(std::string filename = "");
protected:
TraCICommandInterface* traci;
TraCIConnection* connection;
std::string viewId;
};
GuiView guiView(std::string viewId) {
return GuiView(this, viewId);
}
I am trying to use the takeScreenshot() function but in order to do so first I am creating an object of this class using GuiView guiView(std::string viewId).
So my question is what is viewId?
The viewId refers to which SUMO window to take a screenshot of.
For example, this screenshot
shows five views. Visible in the window titles are their names: here,
they are called View #0 to View #4.
Related
working on implementing an Serial receive library for a specific hardware sending information to a ESP8266 device, I came across the following issue
for some background:
I use sloeber the eclipse IDE for arduino programming, with Arduino IDE the same issue exists
__cplusplus gives me 201103, so I assume I am on c++11
explanation of the setup:
I have derived classes which represents interpreted packages received from serial
these classes are all derived form on base class which implements some common methods, here methodA (in reality: length of
data, and getter for the data)
to forward these packets around I have created a class which has a member of a struct (sData) which has a tagged union inside. for simplicity I only use sData here not the class containing it.
the union uUnion is the one holding the packets content in form of derived packages, only one at a time, but able to contain every derived class available.
i do not use any dynamic object creation (no new), to prevent memory leaks
maybe there are better solution to this problem. Ideas are appreciated. But I would like to focus on why my implementation is not working
problem
the usage of the members-functions of the derived classes out of the union.
I can call them directly without problem.
But I am not able to create a pointer out of the union to the derived class instance and call that member.
//this is the base class
class cBaseA{
public:
virtual void methodA(void){
Serial.print(" A ");
Serial.println(i);
}
int i; //some attribute to work with
private:
};
//first derived class
class cDerivedA: public cBaseA{
public:
void methodA(void) {
Serial.print(" DerivedA ");
Serial.print(i);
Serial.print(" ");
Serial.println(ii);
}
int ii; //additional attribute
private:
};
//second derived class
class cDerivedB: public cBaseA{
public:
void methodA(void) {
Serial.print(" DerivedB ");
Serial.print(i);
Serial.print(" ");
Serial.println(ii);
}
int ii;
private:
};
//third derived class
class cDerivedC: public cBaseA{
public:
void methodA(void) {
Serial.print(" DerivedC");
Serial.print(i);
Serial.print(" ");
Serial.println(ii);
}
int ii;
private:
};
//this is the structure to pass different derived instances around
struct sData{
enum eDataType{
eunkown,
eDerivedA,
eDerivedB,
eDerivedC
} DataType;
union uUnion{
cDerivedA DerivedA;
cDerivedB DerivedB;
cDerivedC DerivedC;
~uUnion(){};
uUnion(){};
} ;
uUnion DataUnion;
sData(void){DataType=eDataType::eunkown;};
sData(const sData &Data){
this->DataType=Data.DataType;
switch(this->DataType){
case eDataType::eDerivedA:
this->DataUnion.DerivedA=Data.DataUnion.DerivedA;break;
case eDataType::eDerivedB:
this->DataUnion.DerivedB=Data.DataUnion.DerivedB;break;
case eDataType::eDerivedC:
this->DataUnion.DerivedC=Data.DataUnion.DerivedC;break;
case eDataType::eunkown:
break;
}
}
~sData(){};
};
void DataFunction(struct sData *Data){
Serial.println("A1:");
Data->DataUnion.DerivedB.methodA(); //works fine
cDerivedB *DerivedB;
DerivedB=&(Data->DataUnion.DerivedB); //this works
DerivedB->methodA(); //compiles, but execution exception, code 28
}
void setup(){
Serial.begin(9600);
delay(2000);
sData Data;
cDerivedB DerivedB1;
DerivedB1.i=1;
DerivedB1.ii=2;
Data.DataType=sData::eDataType::eDerivedB;
Data.DataUnion.DerivedB=DerivedB1;
DataFunction(&Data);
}
what I tried so far:
the absence of the virtual destructor of cBaseA has no influence (I tried already with it)
to make the union anonymous did not changed anything
to make a reference to the unions content results in the same error:
cDerivedB &DerivedB;
DerivedB=&(Data->DataUnion.DerivedB);
DerivedB.methodA();
I am able to make copy to out of the union to the base class, but this causes slicing, and the call ends in the base class, not as I need in the derived class
the question is: why does this exception happen, if the direct call is possible?
What is the right way to get a handle (pointer, reference) of the unions content and call the member? I know that there are discussions out there, that unions should only contain simple data types. Is this just a flaw of the compiler (c+11) letting me write this?
But still, direct access is possible. Why not via pointer?
many thanks in advance if somebody is able to put that cloud away I can not see through.
How to achieve gtest or gmock for the private and protected member function. I am new to gtest and gmock. Below is the code for which i need to do gtest or gmock along with my attemp.
constexpr static char _session[]{"S_ID"};
typedef struct {
int session;
} Session;
typedef std::function<void(const Session &)> SessionCallback_t;
class Service : public ParentService {
public:
Service();
void registerCallback(const SessionCallback_t & callback);
protected:
virtual void notifyHandler(const Json::Value & data) override;
virtual void notifyState();
private:
Session mSession;
SessionCallback_t mCallback;
void jsonParse(const Json::Value & json_data);
};
My Attemp which doesn't compile
class TestService : public Service {
public:
TestService(): Service() {
}
bool registerCallback(const SessionCallback_t & cb) {
// how to achive this?
}
};
class MyTestService : public ::testing::Test {
protected:
virtual void SetUp() {
}
virtual void TearDown() {
}
};
TEST_F(MyTestService , registerCallbackTest) {
TestService service;
EXPECT_TRUE(service.registerCallback(SessionCallback_t));
}
I am stuck with the below interface
1.registerCallback()
2.notifyHandler()
3.notifyState()
4.jsonParse()
Please though some light to proceed further.
Welcome to Stack Overflow!
First, let me recommend this recent episode of CppCast on Designing for Testing. The podcast notes that if you find your code is hard to test, it means it is too tightly coupled and is thus poorly designed.
It also (rightly, IMHO) recommends that you test only public functions. If you find yourself needing to test the private functions, you should probably refactor the code.
One way to do that is to break your code into multiple classes with the public functions you want to test. Then, your composite class can either create and own the class directly (appropriate if it's a basic type with no dependencies or complex resources of its own such as a vector or string class) or can use dependency injection to pass in the dependencies as constructor or method parameters (appropriate for databases, network connections, file systems, etc.).
Then in testing, you pass in a test double, such as a mocked object or a simplified implementation like an in-memory database instead of an out-of-process database connection, that acts like the object but does what you need in the test situation.
That's the basic advice. In your specific case, it looks like you're trying to override a non-virtual function in your TestService. What are you wanting to test exactly?
I wouldn't expect EXPECT_TRUE(service.registerCallback(SessionCallback_t)); to compile because SessionCallback_t names a type, not an instance of a type, so you can't pass it in. Again, what are you trying to accomplish?
Update to comment:
Mocking requires virtual functions (or duck typing) and dependency injection.
If you just want to test registerCallback(), I suspect you don't need a mock at all. Rather, you need to look at the function's documentation to see what it says it will do -- sometimes called the contract. For instance, what are the preconditions and postconditions of the function? What are the error cases it might encounter? These are what a unit test should cover.
For instance, does it retain only one callback (hint: as written, yes)? What happens when you call it when there is already a callback registered? Does it tolerate default-initialized std::function objects being passed in?
The bigger question is, how do you validate that your test is correct. If you start triggering notifications on your callback, you're venturing beyond the scope of testing this function in isolation. Instead, you could create an accessor class in your test to publicize what is private so you can validate. Still, you can't compare std::function for equality, so the best you can do is to invoke it and check that an expected side effect happens:
class TestService : public Service {
public:
const SessionCallback_t& getCallback() const { return mCallback; }
};
struct TestCallback
{
int mCount = 0;
void operator()( const Session& ) { ++mCount; }
};
Then in your test, you can write a tests like:
TEST_F(MyTestService , Test_registerCallback_BadCallback) {
auto service = TestService{};
EXPECT_THROW( service.registerCallback( SessionCallback_t{} ), std::out_of_range );
}
// Register and check that it's our callback
TEST_F(MyTestService , Test_registerCallback_CallbackSaved) {
auto service = TestService{};
auto callback = TestCallback{};
EXPECT_TRUE( service.registerCallback( callback ) );
EXPECT_EQ( callback.mCount, 0 );
auto actualCallback = service.getCallback();
EXPECT_TRUE( actualCallback );
actualCallback();
EXPECT_EQ( callback.mCount, 1 );
}
TEST_F(MyTestService , Test_registerCallback_CallbackOverwrite) {
auto service = TestService{};
auto callback1 = TestCallback{};
auto callback2 = TestCallback{};
EXPECT_TRUE( service.registerCallback( callback1 ) );
EXPECT_TRUE( service.registerCallback( callback2 ) );
EXPECT_EQ( callback1.mCount, 0 );
EXPECT_EQ( callback2.mCount, 0 );
auto actualCallback = service.getCallback();
EXPECT_TRUE( actualCallback );
actualCallback();
EXPECT_EQ( callback1.mCount, 0 );
EXPECT_EQ( callback2.mCount, 1 );
}
We write to ask how to correctly call a method of Titanium Appelerator native module.
Our development environment is as the following:
Titanium SDK :5.2.0
Appcelerator CLI: 5.2.0
TabletPC:Windows10pro
Our module has been developed with the following procedures;
(1) we created a module referring to the document of https://github.com/appcelerator/titanium_mobile_windows#module-development
An additional method aaa() was added to the module.
cd MY_WORKSPACE
ti create -p windows -t module //create a module project
cd MY_MODULE_NAME/windows
ti build -p windows -T ws-local //build my module project
(2) A windows’ application was created to call the above module, and there was no error while we call the module from the application. However, while we call the method aaa(), Titanium error message as the following occurred;
“aaa() method does not exist”.
In the Titanium windows platform for windows10, the dll for module was created correctly. Then, we are just wondering which parts of our source are not correct;
(1) The definition of a method aaa()is not correct.
(2) The source code to call the native module is not correct.
(3) The source code to call the aaa() method is not correct.
We attached our source code as the following and thank you in advance for your comment and advice.
The code is as below.
windows/src/JpNativeModuleExample.cpp
#include "JpNativeModuleExample.hpp"
#include "Titanium/detail/TiImpl.hpp"
namespace Jp
{
NativeModuleExample::NativeModuleExample(const JSContext& js_context) TITANIUM_NOEXCEPT
: JSExportObject(js_context)
{
TITANIUM_LOG_DEBUG("NativeModuleExample::ctor Initialize");
}
void NativeModuleExample::postInitialize(JSObject& js_object)
{
}
void NativeModuleExample::postCallAsConstructor(const JSContext& js_context, const std::vector<JSValue>& arguments)
{
}
//add new method
void NativeModuleExample::aaa() TITANIUM_NOEXCEPT
{
}
void NativeModuleExample::JSExportInitialize()
{
JSExport<NativeModuleExample>::SetClassVersion(1);
JSExport<NativeModuleExample>::SetParent(JSExport<JSExportObject>::Class());
}
}
windows/include/JpNativeModuleExample.hpp
#ifndef _JPNATIVEMODULEEXAMPLE_HPP_
#define _JPNATIVEMODULEEXAMPLE_HPP_
#include "JpNativeModuleExample_EXPORT.h"
#include "Titanium/detail/TiBase.hpp"
#include "Titanium/Module.hpp"
namespace Jp
{
using namespace HAL;
class JPNATIVEMODULEEXAMPLE_EXPORT NativeModuleExample : public JSExportObject, public JSExport<NativeModuleExample>
{
public:
NativeModuleExample(const JSContext&) TITANIUM_NOEXCEPT;
void aaa() TITANIUM_NOEXCEPT;
virtual void postInitialize(JSObject& js_object) override;
virtual void postCallAsConstructor(const JSContext& js_context, const std::vector<JSValue>& arguments) override;
virtual void aaa(); //add new method
virtual ~NativeModuleExample() = default;
NativeModuleExample(const NativeModuleExample&) = default;
NativeModuleExample& operator=(const NativeModuleExample&) = default;
#ifdef TITANIUM_MOVE_CTOR_AND_ASSIGN_DEFAULT_ENABLE
NativeModuleExample(NativeModuleExample&&) = default;
NativeModuleExample& operator=(NativeModuleExample&&) = default;
#endif
static void JSExportInitialize();
};
}
#endif // _JPNATIVEMODULEEXAMPLE_HPP_
sample code
var nativemoduleexample = require('jp.NativeModuleExample');
Ti.API.info("module is => " + nativemoduleexample); //no problem
var aaa = nativemoduleexample.aaa(); //titanium error is displayed
$.index.open();
In order to enable your function, you need to register it using TITANIUM_ADD_FUNCTION at JSExportInitialize(). You might want to check out working examples under TitaniumKit such as Ti.UI.Button and Ti.UI.Window.
void NativeModuleExample::JSExportInitialize()
{
JSExport<NativeModuleExample>::SetClassVersion(1);
JSExport<NativeModuleExample>::SetParent(JSExport<JSExportObject>::Class());
TITANIUM_ADD_FUNCTION(NativeModuleExample, aaa);
}
And then use TITANIUM_FUNCION to define function like this...
TITANIUM_FUNCTION(NativeModuleExample, aaa)
{
aaa();
return get_context().CreateUndefined()
}
I want to use an exception hierarchy where the base exception class derives from boost::exception so that I can get the nice and useful diagnostic information that that class has to offer and QtConcurrent::Exception so that I can throw my exceptions across threads.
Hence, my base exception class looks like:
class MyException : public QtConcurrent::Exception, public boost::exception
{
public:
MyException() { };
virtual ~MyException() throw() { }
// required by QtConcurrent::Exception to be implemented
virtual void raise() const { throw *this; }
virtual MyException* clone() const { return new MyException(*this); }
};
Per QtConcurrent::Exception's documentation, raise() and clone() must be implemented in any class derived from QtConcurrent::Exception. So, the rest of my code may look something like:
void foo()
{
BOOST_THROW_EXCEPTION(MyException());
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
try
{
foo();
}
catch (const MyException& me)
{
std::cerr << boost::diagnostic_information(me);
}
return 0;
}
However, using the BOOST_THROW_EXCEPTION() macro causes the following compilation error:
error C2555: 'boost::exception_detail::clone_impl::clone':
overriding virtual function return type differs and is not covariant
from 'MyException::clone'
I am not entirely sure what this error is telling me (my fault, not the errors, I'm sure!).
If I instead use throw MyException(); the code compiles just fine. As I mentioned above, I'd like to use BOOST_THROW_EXCEPTION() so that I get the diagnostic information in my exceptions.
I know that one possible work-around could be another class derived from just QtConcurrent::Exception that has a boost::exception member, essentially a container for the actual error. But if possible, I would like to continue to have the MyException class inherit from both QtConcurrent::Exception and boost::exception.
Can someone offer some insight into what the error is saying? Is there any way to accomplish what I want?
I'm going nuts trying to find the problem here. I have a main window, and a form type window made in Qt. I'm using the Visual Studio 2010 addon. For some reason, my SLOT is never called in the main window; however, the signal appears to be emitted.
Here's what I've done:
This is the form:
class ScalerValuesWindow : public QWidget
{
Q_OBJECT
private:
Ui::ScalerValuesWindow ui;
// Variables
std::vector<int> scalerValues;
public slots:
void storeScalerValues();
signals:
void ScalerValues(std::vector<int>);
public:
ScalerValuesWindow(QWidget *parent = 0);
};
void ScalerValuesWindow::storeScalerValues()
{
emit ScalerValues(scalerValues);
hide();
}
Then here's my main window connection line in my constructor:
scalerValuesWindow = new ScalerValuesWindow;
connect(scalerValuesWindow, SIGNAL(ScalerValues(std::vector<int>)), this, SLOT(RetrieveScalerValues(std::vector<int>)));
This is in my main window's class declaration:
public slots:
void RetrieveScalerValues(vector<int> scalerValues);
And this is the slot:
void RelayduinoGuiThreading::RetrieveScalerValues(vector<int> scalerVals)
{
scalerValues = scalerVals;
}
I have Q_OBJECT declared in both. I have no idea what could be causing this.
Any advice would be greatly appreciated.
You must define your slot as:
public slots:
void RetrieveScalerValues(std::vector<int> scalerValues);
^^^
(Inspect the generated moc file to see exactly what signal/slot signatures are being generated.)
Don't use using namespace std; in your headers, that's bad practice anyway (pulls in that huge namespace into all the users of that header, which is impolite).