Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

C/C++ programmers, please confirm

For general discussion related FlowStone

C/C++ programmers, please confirm

Postby tulamide » Sat Aug 04, 2018 10:53 pm

A question for those who know C/C++. Well, two questions. Please, correct everything that's wrong from the following, or confirm that my assumptions are correct!

1) enum is a list of specific integer values that are accessed by their name
Code: Select all
enum {
          D_Mon ,
          D_Tue ,
          D_Wen ,
          D_Thu ,
          D_Fri ,
          D_Sat ,
          D_Sun ,
};

Now, enum D_Mon is an integer with the value 0, D_Wen has the value 2, D_Sat the value 5

2) typedef is a way to rename existing types without changing their content or structure.
Code: Select all
typedef unsigned short USHORT;

Now I can access "unsigned short" with USHORT.

3) if both is used at once, it just means that an enum is defined and at the same time given an alternative name?
Code: Select all
typedef enum _FILEOPENDIALOGOPTIONS {
  FOS_OVERWRITEPROMPT           ,
  FOS_STRICTFILETYPES           ,
  FOS_NOCHANGEDIR               ,
  FOS_PICKFOLDERS               ,
  FOS_FORCEFILESYSTEM           ,
  FOS_ALLNONSTORAGEITEMS        ,
  FOS_NOVALIDATE                ,
  FOS_ALLOWMULTISELECT          ,
  FOS_PATHMUSTEXIST             ,
  FOS_FILEMUSTEXIST             ,
  FOS_CREATEPROMPT              ,
  FOS_SHAREAWARE                ,
  FOS_NOREADONLYRETURN          ,
  FOS_NOTESTFILECREATE          ,
  FOS_HIDEMRUPLACES             ,
  FOS_HIDEPINNEDPLACES          ,
  FOS_NODEREFERENCELINKS        ,
  FOS_OKBUTTONNEEDSINTERACTION  ,
  FOS_DONTADDTORECENT           ,
  FOS_FORCESHOWHIDDEN           ,
  FOS_DEFAULTNOMINIMODE         ,
  FOS_FORCEPREVIEWPANEON        ,
  FOS_SUPPORTSTREAMABLEITEMS
} ;

The enum is now accessible as _FILEOPENDIALOGOPTIONS, and the content is following standard rules:
FOS_OVERWRITEPROMPT == 0
FOS_STRICTFILETYPES == 1
FOS_NOCHANGEDIR == 2
FOS_PICKFOLDERS == 3
etc.

Does it matter, if I call something with an enum named integer or directly use the integer (I think not)?
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2686
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: C/C++ programmers, please confirm

Postby TheOm » Sun Aug 05, 2018 1:07 am

tulamide wrote:1) enum is a list of specific integer values that are accessed by their name

You can think of it that way, though it's not technically true. An enum value like D_Mon is of some enum type (in your example its an unnamed enum type), but it is guaranteed to be implicitely convertible to its underlying type.
You can also specify the underlying type of the enum.
Code: Select all
enum Test : unsigned char { a, b } // underlying type is unsigned char

If you don't specify a type then the enum values are guaranteed to be convertible to int, or some larger integer type or unsigned integer type if the enum values don't fit into an int.

Note that if you don't need the implicit conversions, you should prefer
Code: Select all
enum class MyEnum { val }


tulamide wrote:2) typedef is a way to rename existing types without changing their content or structure.

Yes typedef will introduce an alias name. You can then refer to the same type using either its original name or the new alias.
You can also use this equivalent syntax, which is less confusing with more complex types IMO:
Code: Select all
using USHORT = unsigned short;


tulamide wrote:3) if both is used at once, it just means that an enum is defined and at the same time given an alternative name?

Yes, although your code is missing the new name, _FILEOPENDIALOGOPTIONS is the enum's original name. I think you mean:
Code: Select all
typedef enum _FILEOPENDIALOGOPTIONS { // this _FILEOPENDIALOGOPTIONS can also be left out
    //...
} FILEOPENDIALOGOPTIONS;

This is equivalent to:
Code: Select all
enum _FILEOPENDIALOGOPTIONS {
    //...
};
typedef _FILEOPENDIALOGOPTIONS FILEOPENDIALOGOPTIONS;

The same technique can also be used with structs/classes.
This style of defining structs or enums is used in C so that the name of the type can be used without having to write struct or enum in front of it when defining instances of that type.

tulamide wrote:Does it matter, if I call something with an enum named integer or directly use the integer (I think not)?

It does not really matter, but you may have to cast the integer to the enum type. But needless to say, using the enum values if possible is strongly preferable for readability.


BTW if you want to open a file dialog, I can probably help you with that.
TheOm
 
Posts: 103
Joined: Tue Jan 28, 2014 7:35 pm
Location: Germany

Re: C/C++ programmers, please confirm

Postby tulamide » Sun Aug 05, 2018 9:04 am

Ah, TheOm, thank you so much for going with me through the details of it! I wasn't aware that enum is a type by itself (I rather compared it to an array of sorts).

Also offering your help is so kind! I'm just afraid you may withdraw it, when you hear what I'm up to.
Yes, I want to open a file dialog, but not in the usual way. I want to use it to select a folder. I don't like the old folder selection dialog (very uncomfortable for users). Microsoft recommends the following:
For Windows Vista or later, it is recommended that you use IFileDialog with the FOS_PICKFOLDERS option rather than the SHBrowseForFolder function. This uses the Open Files dialog in pick folders mode and is the preferred implementation.

That's my goal. Using the Microsoft API documentation it tells that you have to use the "SetOptions" method, and that method expects one or more FILEOPENDIALOGOPTIONS. The documentation that shows the FILEOPENDIALOGOPTIONS enum just as I copied it in my example (without a name). Here's the source page: https://docs.microsoft.com/de-de/windows/desktop/api/shobjidl_core/ne-shobjidl_core-_fileopendialogoptions

Unfortunately this is all much more complex than I expected, because (and that might be the point, where you want to withdraw your offer) I intended to run it from Ruby via Win32API within Flowstone. The reason therefore is that Flowstone doesn't offer a folder selection prim and I first thought it might be quicker to just hook into the WindowsAPI than programming a folder selection + GUI in Ruby.

If you still want to help and could maybe show me how IFileDialog with FOS_PICKFOLDERS would be called in C/C++ (including opening and closing, as I assume some callback function I'm not yet aware of), the Ruby interface to this whole process is much easier to implement. Should this be a modal dialog, there might be trouble ahead, since Flowstone's timeout control for Ruby could kick in.

Maybe it IS easier to program a dialog from scratch in Ruby?
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2686
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: C/C++ programmers, please confirm

Postby TheOm » Sun Aug 05, 2018 5:08 pm

I think opening a modern file dialog from within ruby is actually not possible.
You would need to call methods like SetOptions or Show through the function pointers in the IFileDialog VTable, and I don't think you can do this in ruby.
Reimplementing a file dialog in ruby would also be a huge task.
I think the simplest solution would be to make write a DLL in C++ and call functions in there with the DLL primitive.
TheOm
 
Posts: 103
Joined: Tue Jan 28, 2014 7:35 pm
Location: Germany

Re: C/C++ programmers, please confirm

Postby KG_is_back » Mon Aug 06, 2018 12:39 am

tulamide wrote:1) enum is a list of specific integer values that are accessed by their name


Well, kind of but not really. Enum is only required to have implicit conversion to char and signed/unsigned integers. The actual specific underlying type is implementation specific (though all compilers happen to default to type int, they are technically not required to do so - some automatically choose larger types if the specified values exceed bounds of int).

tulamide wrote:The enum is now accessible as _FILEOPENDIALOGOPTIONS, and the content is following standard rules:
FOS_OVERWRITEPROMPT == 0
FOS_STRICTFILETYPES == 1
FOS_NOCHANGEDIR == 2
FOS_PICKFOLDERS == 3
etc.

Does it matter, if I call something with an enum named integer or directly use the integer (I think not)?


"Technically" it does not matter. In practice it does, because the whole point of enum is that it's a placeholder for a value. If at some point you change the values in the enum, it will break your code if you reference them by a literal at some other place. If you intend to do so, just declare the values explicitly.
Code: Select all
//BAD CODE//
//v0.1
enum wokring_status{
works,
failed
}
...
if(status==1) //if failed do...
...
//v0.2
enum wokring_status{
works,
ready,
failed
}
...
if(status==1) //congratulations, this line now responds to "ready" instead of "failed"
//good luck trying to track this bug down in 2000 lines of code, when you have to manually look up what "1" means (and used to mean in previous versions) in this context
...

Code: Select all
//GOOD CODE
enum wokring_status{
works,
failed=1 //"failed" is now guaranteed to be 1, even if new values are added later
}
...
if(status==1) //1 is guaranteed to be equal "failed"
...

Code: Select all
//BEST CODE
enum wokring_status{
works,
failed
}
...
if(status==failed) //the specific value now doesn't even matter.
...

KG_is_back
 
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: C/C++ programmers, please confirm

Postby tulamide » Mon Aug 06, 2018 6:01 am

KG_is_back wrote:
tulamide wrote:1) enum is a list of specific integer values that are accessed by their name


Well, kind of but not really. Enum is only required to have implicit conversion to char and signed/unsigned integers. The actual specific underlying type is implementation specific (though all compilers happen to default to type int, they are technically not required to do so - some automatically choose larger types if the specified values exceed bounds of int).
Yes, I understand that after TheOm made this more clear. I thought of enums more like an array of sorts.


KG_is_back wrote:
tulamide wrote:Does it matter, if I call something with an enum named integer or directly use the integer (I think not)?


"Technically" it does not matter. In practice it does, because the whole point of enum is that it's a placeholder for a value. If at some point you change the values in the enum, it will break your code if you reference them by a literal at some other place. If you intend to do so, just declare the values explicitly.
Of course it will break the code! You know me for so many years now, and still think I'm that stupid? :lol: My question was related to (as explained in the other post) using IFileDialog from within Ruby. As you know, I have to define data types that I can pass to Win32API, but enum is not part of the possible types. Integers are. That's why I asked!

Do you happen to know a workaround for letting users select a folder? Flowstone, Ruby, Terminal? Any chance?
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2686
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany


Return to General

Who is online

Users browsing this forum: No registered users and 44 guests