I have a block of code that i'm trying to covert from an old qt file into C# but i'm a little unclear what is going on in the struct within the union below. I'm not sure what the ':' does... i'm guessing it sets the size but could not find any documentation on this. Also since C# does not have unions what is the best practice for converting something like this. Thank you
union uAWord
{
uAWord()
: m_AWord(0) {}
struct sBcdAWord
{
quint32 m_O :8;
quint32 m_S :2;
quint32 m_D :18;
quint32 m_SS :3;
quint32 m_P :1;
}
sBcdAWord m_F;
quint32 m_AWord;
}
This is what is called BitFields. the portion sBcdWord is a 32 bit word, and each field is a portion of that word taking respectively 8,2,18,3,1 BIT:
So the word layout is as below:
Bit0-Bit7 m_0
Bit8-Bit9 m_S
Bit10-Bit27 m_D
Bit28-Bit30 m_ss
Bit31 m_P
How to port this in C# depends if you are convettually porting the code, or if you need to PInvoke. In the case of PInvoke the best solution is probably to map sBcdAWord as an Unit32, and create some accessor strategy to mask on reading writing. If it is a code port, use separeted properties would be good unless there is special needing in memory usage saving.
That syntax is used to declare bitfields. The number is the number of bits for that value. See for example http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc03defbitf.htm
A good conversion to C# depends on the case I guess. As long as you are not too space-conscious, I'd just keep all needed values in parallel in a class.
That initializes m_aWord to 0.
To answer your other question, in C# you'd likely want a struct, and you'd need to use attributes to get union-like behavior out of it.
This particular example could be somewhat like:
[StructLayout(LayoutKind.Explicit)]
struct uAWord {
[FieldOffset(0)]
private uint theWord = 0;
[FieldOffset(0)]
public int m_P;
[FieldOffset(1)]
public int m_S;
[FieldOffset(3)]
public int m_SS;
[FieldOffset(7)]
public int m_O;
[FieldOffset(18)]
public int m_D;
public uAWord(uint theWord){
this.theWord = theWord;
}
}
The LayoutKind.Explicit indicates you will tell it where in the memory to map each field and the FieldOffset(int) tells which bit to start each field on. See this for more details. You'd assign this struct by setting the uint theWord in the constructor, then each of the other properties would access a chunk starting at a different memory address.
Unfortunately, that actually isn't correct. You'll need to use properties and do some bitmasking/shifting to get it right. Like this:
struct uAWord {
private uint theWord = 0;
public int m_P {get {return (theWord & 0x01);}}
public int m_S {get {return (theWord & 0x02) << 2;}}
public int m_SS {get {return (theWord & 0x04) << 3;}}
public int m_0 {get {return (theWord & 0x18) << 6;}}
}
Related
I have a legacy C-dll to access a Hardware Device. The dll uses structures containing structure Arrays as function arguments. And I am having a hard time to get this working together with C# (in unsafe mode which is ok since access Speed is an issue here).
The original declaration from DeviceDll.h of the legacy C-dll Looks like:
typedef struct tag_RESULT_JUDGEMENT
{
short nJudgementResult;
} struc_RESULT_JUDGEMENT;
typedef struct tag_RESULT_FORMULA
{
float fFormulaResult[3];
float fAnalogResult;
} struc_RESULT_FORMULA;
typedef struct tag_RESULT_SCRIPT
{
short nScriptNo;
float fTime;
short nFormulaCount;
struc_RESULT_FORMULA sFormula[32];
short nJudgementCount;
struc_RESULT_JUDGEMENT sJudgement[8];
} struc_RESULT_SCRIPT;
WORD PASCAL GetResult(LPCTSTR pDeviceCode, struc_RESULT_SCRIPT_NO_LIST* pList, struc_RESULT_SCRIPT* pResult[]);
Declaring the C-dll functions in C# is done in the following way:
[DllImport("ExternalDevice.dll")]
public unsafe static extern int GetResult(StringBuilder pDeviceCode, struc_RESULT_SCRIPT_NO_LIST* pList, struc_RESULT_SCRIPT **ppResult);
and the structure which I want to access I have declared in a way which unfortunatly gives an error:
[StructLayout(LayoutKind.Explicit)]
public unsafe struct struc_RESULT_SCRIPT {
[FieldOffset(0)]
public Int16 nScriptNo;
[FieldOffset(2)]
public float fTime;
[FieldOffset(6)]
public Int16 iFormulaCount;
[FieldOffset(8)
public fixed struc_RESULT_FORMULA[32] oFormula;
}
Error: Buffer of a fixed size must be of "bool", "Byte", . . . or "double"
Is there a way to declare a fixed structure Array within a structure so that I can use a structure-type variable afterwards as an Argument for calling the legacy DLL ?
What I have tried so far:
Avoiding a structure Array within a structure (rather clumsy but working)
[StructLayout(LayoutKind.Explicit)]
public unsafe struct struc_RESULT_SCRIPT {
[FieldOffset(0)]
public Int16 nScriptNo;
[FieldOffset(2)]
public float fTime;
[FieldOffset(6)]
public Int16 iFormulaCount;
[FieldOffset(8)
public struc_RESULT_FORMULA oFormula01;
[FieldOffset(8 + 16)
public struc_RESULT_FORMULA oFormula02;
. . .
[FieldOffset(8 + 31*16)
public struc_RESULT_FORMULA oFormula32;
}
. . .
struc_RESULT_SCRIPT** ppResult; //local variable => allocated on the stack => so it's already fixed
. . .
int iRv = GetResult(sbMpmIp, &oScriptList, ppResult);
struc_RESULT_FORMULA oFormulaResult = ppResult[0]->oFormula01;
This is working - but accessing oFormula01 … oFormula32 for 32 structure-variables is rather clumsy. I would strongly prefer to get the result as an Array so that I can Access it like oFormula = arrayFormula[xx]; within a for - loop.
Is there a way to declare an unsafe, fixed structure Array within a structure in C# - or a feasable work-around ?
Thank you very much in advance!
According to the documentation for fixed sized buffers there is no way use structs in them.
The best option seem to do as you have done and declare 1..n fields. The anwer to this related question suggest the same thing. If the problem is simply not being able to loop over the fields, then consider adding a iterator block, like:
public IEnumerable<struc_RESULT_FORMULA> GetResultFormulas(){
yield return oFormula01;
...
yield return oFormula32;
}
An alternative would be a large switch statement/expression if you need random indexing.
I am building small components that will be used for a RPG later on, however my experience in this scale of project is very limited, which is why I choose to build them independant of each other. This part will cover character creation and handling.
So I have the following vector that stores class pointers of Gender. I know there will only be four genders, Unknown (for error-catching), Male, Female and Neuter.
std::vector<Gender*> mGenders;
However for some other vectors I do not know the exact amount.
std::vector<BClass*> mBClasses;
I have Creatures that have a mGenderID as well as a mBClassID. I have a game.h that initializes all genders and base classes (among many others). Let's say I have a
Creature* mPC = new Creature ("Name", GenderID, BClassID);
Should I then have a
Gender* Game::getGenderByID(int id) {
for (std::vector<Gender*>::iterator it = mGenders.begin(); it != mGenders.end(); ++i) {
if ((*it)->getID() == id) return (*it)
}
}
And use it the following way
std::cout << "Your name is " << mPC->getName() << ". Your gender is " << getCreatureByID(mPC->getGenderID())->getName();
So my question is, is this a good way of structuring the code? Imagine an infinite-engine like game, Baldur's Gate or so. Preferebly it should work for most, even if I were to do a Skyrim or GTA-like game.
Lastly, how easy would this be to port for C#? As I know C++ better than C#, I thought I'd give it a go in C++ first, to get the structure correct and work with something that can be used in larger-scale projects.
Edit:
Would it be better to directly store the Gender* in the Creature class? And no the ID of the creature?
Edit2:
class Gender {
public:
Gender::Gender(int id, std::string name, std::string desc, int coeBonus, int strBonus, int agiBonus, int attBonus, int intBonus, int chaBonus);
~Gender();
int getID () const;
std::string getName () const;
std::string getDesc () const;
int getStrBonus () const;
int getAgiBonus () const;
int getAttBonus () const;
int getIntBonus () const;
int getChaBonus () const;
int getCoeBonus () const;
private:
int mID;
std::string mName;
std::string mDesc;
int mStrBonus;
int mAgiBonus;
int mAttBonus;
int mIntBonus;
int mChaBonus;
int mCoeBonus;
};
The definition of gender.h
Thanks in advance
std::vector<Gender*> mGenders;
I can't see a good reason to be storing pointers in your vector. Why not just have a std::vector<Gender>?
Anyway, it seems like a strange idea to have a container of Genders. As you say, you have 4 possible genders, which you are then storing in this vector. So the vector is always size 4 and you never remove anything from it or add anything to it? And each element basically just represents a possible gender that a creature can have? That is, you're enumerating the possible genders. Sounds like you want an enum (or enum class) instead!
enum class gender {
unknown,
male,
female,
neuter
};
Then there's no need to mess around with ugly IDs that don't really mean anything (at least, they're hidden behind the enum now).
Then you could create a creature like so:
Creature mPC("Name", gender::male);
If you then want to print these out, you'll need some kind of mapping from enum value to strings. One way to do this is to just have a function that switches on the gender argument and returns an appropriate string. Alternatively, you can create a std::map like so:
std::map<gender, std::string> genderNames =
{{gender::unknown, "Unknown"}, /* ... */};
Then you can print a particular creatures gender just by doing:
std::cout << genderNames[mPC.getGender()];
You seem to have an unhealthy tendency to want to use dynamic allocation for your objects. Don't do it unless it's necessary, and when it is necessary, prefer smart pointers over raw pointers.
With much help from gamedev I am now happy with how much code is structed.
Instead of storing integers containing the IDs of genders, races, and so forth. I now store a pointer to them. Rendering the getGenderByID(int id) obsolete atm.
I am also currently working on a buff/debuff (effect) system instead of having all the different bonus variables.
These changes should make the code good enough for practice!
Gender::Gender(int id, std::string name, std::string desc, int coeBonus, int strBonus, int agiBonus, int attBonus, int intBonus, int chaBonus);
to
Gender(int id, std::string name, std::string desc, Effect* effect);
Currently working on the Effect class, so can't tell a lot about it.
Thanks for the comments.
in this case a binary file is written with the file format based on a struct
struct fileformat
{
struct mask
{
bool mem1present
bool mem2present
bool mem3present
//5 bits unused
}
//member only written in file if mem1present is true
byte mem1present
//member only written in file if mem2present is true
byte mem1present
//member only written in file if mem3present is true
byte mem1present
}
is this possible to be implemented in c#
Sure - you have to implement the serialization yourself to some extent, but you can do that easily enough.
It's unclear what sort of serialization you're using - if you're using the "raw" binary serialization from .NET, you want to override GetObjectData to only add the relevant data on serialization, and then in the protected constructor taking a SerializationInfo and a StreamingContext, populate your struct from the same data in reverse. See this MSDN article for some details.
I don't know what happens if you're using XML serialization.
If you're writing your own serialization (i.e. you've got a method such as WriteToStream) then you can choose to represent it however you want, of course.
EDIT: It sounds like you've probably got an existing file format you need to read in, but you can define your own types. It's easy to have a class or struct with multiple members and possibly a mask to say what's set, although without knowing more it may not be the best design. While you can use explicit layout to make this efficient in memory, it's probably easiest just to have separate members:
struct Foo
{
// Bit-set to determine which fields are actually used
private readonly byte mask;
private readonly int value1;
private readonly int value2;
private readonly int value3;
public Foo(byte mask, int value1, int value2, int value3)
{
this.mask = mask;
this.value1 = value1;
this.value2 = value2;
this.value3 = value3;
}
}
Then somewhere (either in the data type or not), something like:
Foo ReadFoo(Stream stream)
{
byte mask = stream.ReadByte();
int value1 = 0, value2 = 0, value3 = 0;
if ((mask & 1) == 1)
{
// However you do that, depending on your file format
value1 = ReadInt32FromStream(stream);
}
if ((mask & 2) == 2)
{
// However you do that, depending on your file format
value2 = ReadInt32FromStream(stream);
}
if ((mask & 4) == 4)
{
// However you do that, depending on your file format
value3 = ReadInt32FromStream(stream);
}
return new Foo(mask, value1, value2, value3);
}
By the way, I would seriously consider whether a struct is really the best approach here - consider using a class instead. I very rarely create my own structs.
Note: Your sample shows only the declaration of a nested struct type, not an instance of it.
From your question wording, you need an instance member.
struct fileformat
{
struct mask // type declaration only
{
bool mem1present
bool mem2present
bool mem3present
//5 bits unused
}
public mask mask; // <-- Member instance here
}
I apologize if I've misunderstood. Perhaps your struct was only to communicate the structure of the file to us?
I am trying to port a rather large source from VB6 to C#. This is no easy task - especially for me being fairly new to C#.net. This source uses numerous Windows APIs as well as numerous Types. I know that there is no equivalent to the VB6 Type in C# but I'm sure there is a way to reach the same outcome. I will post some code below to further explain my request.
VB6:
Private Type ICONDIRENTRY
bWidth As Byte
bHeight As Byte
bColorCount As Byte
bReserved As Byte
wPlanes As Integer
wBitCount As Integer
dwBytesInRes As Long
dwImageOffset As Long
End Type
Dim tICONDIRENTRY() As ICONDIRENTRY
ReDim tICONDIRENTRY(tICONDIR.idCount - 1)
For i = 0 To tICONDIR.idCount - 1
Call ReadFile(lFile, tICONDIRENTRY(i), Len(tICONDIRENTRY(i)), lRet, ByVal 0&)
Next i
I have tried using structs and classes - but no luck so far.
I would like to see a conversion of this Type structure, but if someone had any clue as to how to convert the entire thing it would be unbelievably helpful. I have spent countless hours on this small project already.
If it makes any difference, this is strictly for educational purposes only.
Thank you for any help in advance,
Evan
struct is the equivalent. You'd express it like this:
struct IconDirEntry {
public byte Width;
public byte Height;
public byte ColorCount;
public byte Reserved;
public int Planes;
public int BitCount;
public long BytesInRes;
public long ImageOffset;
}
You declare a variable like this:
IconDirEntry entry;
Generally, in C#, type prefixes are not used, nor are all caps, except possibly for constants. structs are value types in C#, so that means that they are always passed by value. It looks like you're passing them in to a method that's populating them. If you want that usage, you'll have to use classes.
I'm not exactly sure what your issue is but this is a small ex of how to use a struct.
struct aStrt
{
public int A;
public int B;
}
static void Main(string[] args)
{
aStrt saStrt;
saStrt.A = 5;
}
Your question is not clear ..
What issues are you facing when you are using either struct or class and define those field members? Are you not able to access those members using an instance created for that class ??
Else, declare the class as static and make all the members inside the class also as static , so that u can access them without any instance being created!!
Maybe you trying to get something like this?
struct IconDirEntry
{
public byte Width;
// etc...
}
IconDirEntry[] tICONDIRENTRY = new IconDireEntry[tICONDIR.idCount - 1];
I am reading existing code. I noticed that there are many data object files which have a struct and a class together to define a data object. Like the following one: do you think it is a good style?
In ONE file:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct LaneDataStruct
{
public ushort red;
public ushort yellow;
public ushort green;
public ushort blue;
public ushort orange;
}
public class LaneData
{
private LaneDataStruct laneDataStruct;
public LaneData(ushort red, ushort yellow, ushort green, ushort blue, ushort orange)
{
this.laneDataStruct.red = red;
this.laneDataStruct.yellow = yellow;
this.laneDataStruct.green = green;
this.laneDataStruct.blue = blue;
this.laneDataStruct.orange = orange;
}
public LaneData(ushort[] values)
{
this.laneDataStruct.red = values[0];
this.laneDataStruct.yellow = values[1];
this.laneDataStruct.green = values[2];
this.laneDataStruct.blue = values[3];
this.laneDataStruct.orange = values[4];
}
public LaneData(LaneDataStruct laneDataStruct)
{
this.laneDataStruct = laneDataStruct;
}
public LaneDataStruct getLaneDataStruct()
{
return this.laneDataStruct;
}
public string toString()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("LaneData.red=" + this.getLaneDataStruct().red + "\n");
stringBuilder.Append("LaneData.yellow=" + this.getLaneDataStruct().yellow + "\n");
stringBuilder.Append("LaneData.green=" + this.getLaneDataStruct().green + "\n");
stringBuilder.Append("LaneData.blue=" + this.getLaneDataStruct().blue + "\n");
stringBuilder.Append("LaneData.orange=" + this.getLaneDataStruct().orange);
return stringBuilder.ToString();
}
}
The fact that it's a mutable struct with public fields is bad style to start with.
I can't say I've seen it used, and I wouldn't want to really.
I would only call this a good practice in an exotic P/Invoke scenario, and even then it's questionable.
First, the struct is poorly designed since it is mutable. See this question for more information.
The class looks like it was written to address the fact that in C#, there is no deterministic way to initialize a structure. You can't give a struct a default constructor, and you can't cause a non-default constructor to be called. So it looks like the class wraps the structure and gives a guarantee that the structure will be initialized to some state. However, it does no validation so I strongly doubt there is any value to it at all.
This would only be "useful" if LaneDataStruct represents a structure that is used for P/Invoke (the LayoutKind attribute is a hint this may be true) and that can't have certain values. It would still be preferable to give it properties that perform validation and make the fields private. Usually, P/Invoke code is written in a separate layer where it's considered acceptable to have warts like "make sure to initialize this struct after creating one".
Structs in C# are POD types -- with slightly different semantics. MSDN says this:
The struct type is suitable for
representing lightweight objects such
as Point, Rectangle, and Color.
Although it is possible to represent a
point as a class, a struct is more
efficient in some scenarios.
Even if this distinction wasn't there (C++) it is good practise to differentiate POD types using a seperate keyword.
The only reason I would use a Struct in this manner, especially with a StructLayout Attribute is if I were doing PInvoke to a 3rd party or legacy C DLL, where I had to pass this structure to be marshalled, or if I were doing some sort of serial communications protocol over the wire where I wanted my structure to exactly match the packet I was sending/receiving.