Is it possible to declare class member of another class type in Python like C?
for ex. struct transmit_message below has a member config of struct config_data type.
in C:
enum message_type
{
MESSAGE_NONE,
MESSAGE_PING,
WRITE_DATA,
READ_DATA,
};
struct read_data
{
int gender;
unsigned int age;
};
struct write_data
{
bool flag;
char name[10];
};
struct config_data
{
write_data write_config;
read_data read_config;
};
struct transmit_message
{
message_type type;
config_data config;};
transmit_message message; // **how to create similar object in python?**
Related
First of all, I'm new to Google test, so please forgive my ignorance, if any.
I'm writing unit tests using google test framework for the below class..
class MsgHandler
{
public:
MsgHandler(){}
~MsgHandler(){}
bool decode_data(unsigned char*, unsigned int,char *);
bool handle_req_state_data(char *, char *);
};
bool MsgHandler::handle_req_state_data(char *a, char *b)
{
printf("handle_req_state_data called\n");
return true;
}
bool MsgHandler::decode_data(unsigned char *a, unsigned int b,char *c)
{
printf("decode_data called\n");
handle_req_state_data(c, c);
return true;
}
As a first step, I have created the mock class as below
class MsgHandlerMock : public MsgHandler
{
public:
MsgHandlerMock()
{
}
virtual ~MsgHandlerMock() {}
MOCK_METHOD(bool, handle_req_state_data, (char *, char *), (const));
//MOCK_METHOD(int, decode_data, (unsigned char*, unsigned int ,char* ));
private:
};
Below is the test function
TEST_F(TestClass, test01)
{
MsgHandlerMock mockObj;
//EXPECT_CALL(mockObj, decode_data(::testing::_,::testing::_,::testing::_)).Times(1);
EXPECT_CALL(mockObj, handle_req_state_data(::testing::_,::testing::_)).Times(1);
mockObj.decode_data(0,0,0);
}
My intention of this test is to make sure 'handle_req_state_data' is called when I call 'decode_data' with a certain message. But my test fails with below error
decode_data called
handle_req_state_data called
...
Actual function call count doesn't match EXPECT_CALL(mockObj, handle_req_state_data(::testing::_,::testing::_))...
Expected: to be called once
Actual: never called - unsatisfied and active
[ FAILED ] TestClass.test01 (1 ms)
[----------] 1 test from TestClass (1 ms total)
Can someone help me on how to validate the inner method calls with EXPECT_CALL validators?
The function is not virtual, so can't be overridden by the mock, decode_data does not use dynamic dispatch and call the original method MsgHandler::handle_req_state_data.
virtual bool handle_req_state_data(char *, char *);
MOCK_METHOD(bool, handle_req_state_data, (char *, char *), (override));
I wish to partially initialize an array of structures like in a C++ POD type. The String^ would normally be a char* but managed C++ doesn't allow that.
#include "stdafx.h"
using namespace System;
ref struct Field
{
String^ name;
int fences;
int length;
};
int main(array<System::String ^> ^args)
{
array<Field^>^ farm =
{
{ "eenie", 10 },
{ "meenie", 20 },
{ "miny", 4 }
};
for each (Field^ field in farm)
{
field->length = field->fences * 22;
}
return 0;
}
This results in
1>arrayinit.cpp(18): error C2440: 'initializing' : cannot convert from 'const char [6]' to 'Field ^'
1> Reason: cannot convert from 'const char *' to 'Field ^'
1> No user-defined-conversion operator available, or
1> Cannot convert an unmanaged type to a managed type
So I tried
#include "stdafx.h"
using namespace System;
ref struct Field
{
String^ name;
int fences;
int length;
};
int main(array<System::String ^> ^args)
{
array<Field^>^ farm =
{
{ String("eenie"), 10 },
{ String("meenie"), 20 },
{ String("miny"), 4 }
};
for each (Field^ field in farm)
{
field->length = field->fences * 22;
}
return 0;
}
Now I get
1>arrayinit.cpp(18): error C2440: 'initializing' : cannot convert from 'System::String' to 'Field ^'
1> No user-defined-conversion operator available, or
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>arrayinit.cpp(18): error C2078: too many initializers
Almost every example I've looked at only tells how to initialize an array of strings or integers. I haven't found out a way of initializing an array of structures containing strings.
Is there a simple way of doing this or do I have to create a special constructor and gcnew every element?
I found that I can gcnew every element with a special constructor. Is there a simpler way of doing this similar to a POD initialization?
#include "stdafx.h"
using namespace System;
ref struct Field
{
String^ name;
int fences;
int length;
Field(String^ x, int in_fences)
{
name = x;
fences = in_fences;
}
};
int main(array<System::String ^> ^args)
{
array<Field^>^ farm =
{
gcnew Field("eenie", 10 ),
gcnew Field("meenie", 20 ),
gcnew Field("miny", 4 )
};
for each (Field^ field in farm)
{
field->length = field->fences * 22;
}
return 0;
}
Alternatively, if Field is changed to a value instead of a reference,
#include "stdafx.h"
using namespace System;
value struct Field
{
String^ name;
int fences;
int length;
Field(String^ x, int in_fences)
{
name = x;
fences = in_fences;
}
void Init()
{
length = fences * 22;
}
};
int main(array<System::String ^> ^args)
{
array<Field>^ farm =
{
Field("eenie", 10 ),
Field("meenie", 20 ),
Field("miny", 4 )
};
for each (Field% field in farm)
{
field.Init();
}
return 0;
}
This is slightly better than gcnewing every field.
class abc
{
};
int main()
{
std::map<abc, int> m;
abc ob, ob1;
m.insert(std::make_pair(ob, 1));
m.insert(std::make_pair(ob1, 2));
}
Error coming is:
/usr/lib/gcc/i686-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/stl_function.h:237:22: note: ‘const abc’ is not derived from ‘const std::multimap<_Key, _Tp, _Compare, _Alloc>’
Does any addition function need be added in class so that it can be made hashable?
Got it. Since std::map sorts when entering a new key in map, so if key is a class object, we must overload class's "<" operator, then it would work fine.
class abc
{
public:
bool operator< (const abc& userObj) const
{
return true;
}
};
int main()
{
std::map<abc, int> m;
abc ob;
abc ob1;
m.insert(std::make_pair(ob, 1));
m.insert(std::make_pair(ob1, 2));
}
I am storing points in a custom container and I would like to build the Delaunay triangulation on a subset of these points.
As the points already exist in the container I don't want the Delaunay triangulation to store copies of these points.
My point class is derived from Point_3 and contains several informations (booleans and ints).
In order to do that, I created a custom triangulation_vertex class :
template < typename GT, typename Pt, typename DSVb = Triangulation_ds_vertex_base_3<> >
class Convection_vertex : public DSVb
{
public:
typedef typename DSVb::Cell_handle Cell_handle;
typedef GT Geom_traits;
typedef typename GT::Point_3 Point;
typedef typename Pt::Point_handle Point_handle;
template < typename TDS2 >
struct Rebind_TDS {
typedef typename DSVb::template Rebind_TDS<TDS2>::Other DSVb2;
typedef Convection_vertex<GT, Pt, DSVb2> Other;
};
private:
static int rank_id;
int number_id;
bool discovered;
Point_handle _ph;
public:
Convection_vertex() : DSVb(), number_id(rank_id++), discovered(false) {}
Convection_vertex(const Point_handle& p) : DSVb(), _ph(p), number_id(rank_id++), discovered(false) {}
Convection_vertex(const Point_handle& p, const Cell_handle& c) : DSVb(c), _ph(p), number_id(rank_id++), discovered(false) {}
Convection_vertex(const Cell_handle& c) : DSVb(c), number_id(rank_id++), discovered(false) {}
const Point& point() const
{ return (*_ph); }
Point& point()
{ return (*_ph); }
void set_point(const Point& p){ }
void set_point(const Point_handle& ph)
{ _ph = ph; }
void set_point_handle(Point_handle ph)
{ _ph = ph; }
const Point_handle& point_handle() const
{ return _ph; }
Point_handle& point_handle()
{ return _ph; }
};
To insert a point in the Delaunay triangulation I do:
DVertex_handle dvh = dt.insert(*p);
dvh->set_point_handle(p);
Where p is a point_handle (ie My_point*).
To delete a point in the Delaunay triangulation I do:
dt.remove(dvh);
where dvh is a vertex_handle.
Inserting points in the triangulation is working fine, but I'm having issues removing points. Is my custom vertex class incorrect ?
Is there a better way to do that ?
--edit-----
dt is the Delaunay triangulation:
typedef CGAL::Convection_vertex<K,Point> Conv_Vb3d;
typedef CGAL::Convection_cell<K> Ce3d;
typedef CGAL::Triangulation_data_structure_3<Conv_Vb3d,Ce3d > Tds3d;
typedef CGAL::Delaunay_triangulation_3<K,Tds3d > Dh;
Dh dt;
--
#sloriot: Is this a good start ?
template < typename CK, bool UseStaticFilters, typename Pt >
struct Convection_traits
: public Filtered_kernel_adaptor<
Type_equality_wrapper<
typename CK:: template Base< Convection_traits<CK, UseStaticFilters,Pt> >::Type,
Convection_traits<CK, UseStaticFilters,Pt> >,
UseStaticFilters >
{
typedef Pt Point_3;
[...] // functors
};
Here is what I have done to use my point_handle as point in the Delaunay_triangulation:
template < typename K_, typename Pt >
class My_traits
{
K_ K;
public:
typedef Pt Point_3;
typedef My_traits<K_, Pt> Self;
//triangulation traits
typedef typename K_::Segment_3 Segment_3;
typedef typename K_::Tetrahedron_3 Tetrahedron_3;
typedef typename K_::Triangle_3 Triangle_3;
typedef typename K_::Construct_segment_3 Construct_segment_3;
typedef typename K_::Construct_triangle_3 Construct_triangle_3;
typedef typename K_::Construct_tetrahedron_3 Construct_tetrahedron_3;
typedef typename K_::Compare_xyz_3 Compare_xyz_3;
typedef typename K_::Coplanar_orientation_3 Coplanar_orientation_3;
typedef typename K_::Orientation_3 Orientation_3;
Construct_tetrahedron_3 construct_tetrahedron_3_object () const{
return K.construct_tetrahedron_3_object ();
}
Construct_triangle_3 construct_triangle_3_object () const{
return K.construct_triangle_3_object ();
}
Construct_segment_3 construct_segment_3_object () const{
return K.construct_segment_3_object ();
}
Compare_xyz_3 compare_xyz_3_object () const{
return K.compare_xyz_3_object ();
}
Coplanar_orientation_3 coplanar_orientation_3_object () const{
return K.coplanar_orientation_3_object ();
}
Orientation_3 orientation_3_object () const{
return K.orientation_3_object ();
}
//delaunay triangulation traits
typedef typename K_::Line_3 Line_3;
typedef typename K_::Object_3 Object_3;
typedef typename K_::Ray_3 Ray_3;
typedef typename K_::Coplanar_side_of_bounded_circle_3 Coplanar_side_of_bounded_circle_3;
typedef typename K_::Side_of_oriented_sphere_3 Side_of_oriented_sphere_3;
typedef typename K_::Compare_distance_3 Compare_distance_3;
Coplanar_side_of_bounded_circle_3 coplanar_side_of_bounded_circle_3_object() const{
return K.coplanar_side_of_bounded_circle_3_object();
}
Side_of_oriented_sphere_3 side_of_oriented_sphere_3_object() const{
return K.side_of_oriented_sphere_3_object();
}
Compare_distance_3 compare_distance_3_object() const{
return K.compare_distance_3_object();
}
};
I need to get Data from a C DLL to a C# Application. Here is the error :
Cannot marshal field 'Counters' of type 'KnittingWago.Common.WAGO_DATA_TO_USER_T': Invalid managed/unmanaged type combination (Array fields must be paired with ByValArray or SafeArray).
Here is the DLL .h
#define WAGO_NB_COUNTERS_C 80
#define WAGO_NB_ENCODERS_C 10
struct WAGO_DATA_TO_USER_T
{
unsigned int Counters[WAGO_NB_COUNTERS_C];
int Encoders[WAGO_NB_ENCODERS_C];
unsigned int Weight;
bool CalibrationValid;
bool LastCalibrationFailed;
};
Here is the C# Struct declaration :
const int WAGO_NB_COUNTERS_C = 80;
const int WAGO_NB_ENCODERS_C = 10;
struct WAGO_DATA_TO_USER_T
{
[MarshalAs(UnmanagedType.U4, SizeConst = GlobalConstant.WAGO_NB_COUNTERS_C)]
UInt32[] Counters;
[MarshalAs(UnmanagedType.I4, SizeConst = GlobalConstant.WAGO_NB_ENCODERS_C)]
Int32[] Encoders;
UInt32 Weight;
Boolean CalibrationValid;
Boolean LastCalibrationFailed;
};
How do I declare the C# struct to get the rigth data without error ?
Thanks
EDIT :
I've rewrite the Struct as :
internal struct WAGO_DATA_TO_USER_T
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = GlobalConstant.WAGO_NB_COUNTERS_C)]
UInt32[] Counters;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = GlobalConstant.WAGO_NB_ENCODERS_C)]
Int32[] Encoders;
UInt32 Weight;
Boolean CalibrationValid;
Boolean LastCalibrationFailed;
};
No more Error, but the Values are not as expected.
Should be like :
Counter[0] = ~ 5000
Counter[1] = ~ 30000
Counter[2-79] = 0
Really are :
Counter[0] = 40
Counter[1] = 1080228
Counter[2] = 82964616
Counter[3-79] = All have a non-zero value
The Marshalling was correct but the Device I was calling was not initialized by the external unmanaged dll. So I was reading uninitialisez memory that lead to that unvalid data