I am asking for an advice on how should I declare mouse coordinates that resides inside a namespace and use it frequently by all classes belonging to that namespace.
I use to declared its variables as static within a struct and while dealing it with other classes, puts me in some random error linking between them.
I like its scope to be globally declared but don't know how it should be done in proper way.
some of my implementations are like these:
struct Mouse {
static double X,Y,Z;
static int state,button;
}
//use it like
class Foo {
void func() {
Mouse::X = ?;
Mouse::Y = ?;
}
}
class Mouse {
double X,Y,Z;
int state, button;
}
//
class Foo {
static Mouse mouse;
//or
Mouse* mouse;
}
For me the example of class and using dynamic Memory allocation is the best
Related
I have a C function that expects a struct that contains a non-const String.
typedef struct _A {
char* str;
} A;
void myFunc(A* aptr) { ... }
I've tried for a long time to pass this thing via JNA but didn't manage so far.
public class A extends Structure {
public String str;
protected List getFieldOrder() { ...
}
doesn't work because String will be turned into a const char* instead of char* which I need.
public class A extends Structure {
public byte[] str;
protected List getFieldOrder() { ...
}
doesn't work because the byte array inside a struct gets turned into contiguous memory and not a pointer.
I know I can do it by using Memory and copying the String over. But I can't imagine that this is the preferred way to do it.
I also tried something like
public class NonConstStringMember extends Structure {
public static class ByReference extends NonConstStringMember implements Structure.ByReference {}
public byte[] stringMember;
protected List getFieldOrder() { ...
}
public class A extends Structure {
public NonConstStringMember.ByReference str;
}
but it doesn't work either, maybe because of alignment issues.
What is the preferred way to do this with JNA?
Assuming you want this to point to an arbitrary buffer which you may write to, use Pointer (and assign it a Memory value if you're allocating the buffer).
You then use Pointer.getString(0) or Pointer.setString(0, s) to manipulate the buffer contents.
EDIT
Windows libraries usually have an ascii or unicode version of most functions. While in most cases you can define a single Structure for use in both cases, sometimes you need to provide a little additional handling to ensure the correct types are used. Since the two modes are rarely, if ever, used simultaneously, JNA sets up options to default to one or the other (automatically mapping to the right function suffix, and automatically mapping String to either native const char* or const wchar_t* depending on the mode).
Use setWideString() if using unicode mode and setString() otherwise, or make some accessors on your Structure to handle that for you automatically. For example:
class MyStructure extends Structure {
private static final boolean ASCII = Boolean.getBoolean("w32.ascii");
public Pointer stringBuffer = new Memory(X);
void setStringBuffer(String s) {
stringBuffer.setString(0, s, ASCII);
}
String getStringBuffer() {
return stringBuffer.getString(0, ASCII);
}
}
EDIT
Please note that in most cases you should use a String as the field type and rely on a TypeMapper to use WString behind the scenes where appropriate (e.g. User32 and W32APIOptions.DEFAULT_OPTIONS).
When programming in Swift, what are the advantages and disadvantages for using a struct to hold all your Constants?
Normally, I would go for a class, but swift classes don't support stored type properties yet. I'd like to write my code in the form of Sample A rather in Sample B, purely because of readability, less lines of code and the single statement does what is read (a readable constant that can't be over-written).
// Sample A.swift
struct Constant {
static let PI = 3.14
}
//Sample B.swift
private let globalPI = 3.14
class Constant {
class var PI: Float {
return globalPI
}
}
If I am just referencing Constant.PI from a Struct perspective (for use in computations, printing, conditionals or whatever), am I being wasteful with the copies that are being made? Are there copies made, if I am just referencing the constant as Constant.PI in other parts of my code?
You can declare a class constant in Swift:
class Foo {
class var PI : Double { return 3.1416 }
}
The key is to define the custom getter and no setter.
Here are two classes that I need to map, on the left side:
class HumanSrc {
public int IQ;
public AnimalSrc Animal;
}
class AnimalSrc {
public int Weight;
}
on the right side are the same objects, but composed using inheritance:
class HumanDst : AnimalDst {
public int IQ;
}
class AnimalDst {
public int Weight;
}
so the mapping I need is:
humanSrc.IQ -> humanDst.IQ
humanSrc.Animal.Weight -> humanDst.Weight;
I can easily do this mapping explicitly, but I have several classes that all derive from Animal, and Animal class is large, so I would prefer to map Animal once, and then have that included in every derived class mapping.
I looked at .Include<> method, but I do not think it supports this scenario.
Here is the essence of what I am looking for (pseudo-code):
// define animal mapping
var animalMap = Mapper.CreateMap<AnimalSrc, AnimalDst>().ForMember(dst=>dst.Weight, opt=>opt.MapFrom(src=>src.Weight);
// define human mapping
var humanMap = Mapper.CreateMap<HumanSrc, HumanDst>();
humanMap.ForMember(dst=>dst.IQ, opt=>opt.MapFrom(src=>src.IQ));
// this is what I want. Basically I want to say:
// "in addition to that, map this child property on the dst object as well"
humanMap.ForMember(dst=>dst, opt=>opt.MapFrom(src=>src.Entity));
As a workaround you can add BeforeMap with mapping base class. Probably it is not the best solution but at least it requires less mapping configuration:
humanMap.BeforeMap((src, dst) =>
{
Mapper.Map(src.Animal, (AnimalDst)dst);
});
I have a ref class that contains a pointer to an unmanaged class. the class has some basic types and also a vector of objects of another class. I would like to know the best way to get and set the vector from managed code. Will a memcpy between unmangedb objects be efficient or setting each member variable of unmanagedb?
for ex (assume the class is complete. I am writing what is relevant to the question)
Assume we already have a managed wrapped for struct UnmanagedB called B.
struct UnmanagedA
{
int a;
vector<UnmanagedB> list;
};
public ref class A : public System::IDisposable
{
public:
// properties
property System::UInt32 a
{
System::UInt32 get();
void set(System::UInt32 value);
}
property array<B^>^ list
{
System::array<B^>^ get(); // what is the best way to set and get the vector
void set(array<B^>^ value);
}
private:
UnmanagedA* obj1;
};
This obviously won't be cleanly possible, since UnmanagedA contains a vector of UnmanagedB values, while A exposes an property of type array<B^>. If this is intended and not a typo, you will need to marshall the content of B^ into instances of UnmanagedB. Otherwise, let UnmanagedA hold a std::vector< B* > and take care of proper lifetime management.
How can I put in additional methods for manipulating color ?
Best would be to overload the struct System.Windows.Media.Color.
It is NOT a class (in c#).
Now i'm tinkering with putting (in the same file for testing or must i put it in a
different file) an namespace (Silverlight Application36 or System.Windows.Media ?)
and a partial struct Color Normalize (double R, ...).
I should see MyColor.Normalize() start being recognized by intellisense ?
I'm not. I'm looking to put in a suite of overloaded color manipulations using
floating and double numbers instead of unsigned byte integers.
Any hints while I wack at it ?
Cheers!
dr.K
Just use extension methods:
public static class ColorExtensions
{
public static Color Normalize(this Color)
{
return ...;
}
}
It's fine if its a struct.
I created a sample windows forms app and put in a .cs file:
namespace System.Windows.Media
{
public partial struct Color
{
public double Normalize(double r, double g, double b)
{
return r + g + b;
}
}
}
this causes the intellisense to show the Normalize method: