• Login
sketchucation logo sketchucation
  • Login
โ„น๏ธ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

Testing Ruby VALUES in C-extension.

Scheduled Pinned Locked Moved Developers' Forum
69 Posts 6 Posters 3.2k Views 6 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    tt_su
    last edited by 1 Sept 2014, 14:12

    Also, you're using out C++ example? Then you don't have to declare all variables in advance. That's very old C syntax. Even newer C syntax allow you to declare variables when you need them.

    1 Reply Last reply Reply Quote 0
    • J Offline
      jolran
      last edited by 1 Sept 2014, 16:18

      I am doing a C++ extension, yes.

      @unknownuser said:

      rb_intern returns ID, not VALUE

      ๐Ÿ˜ฎ Well that changes things. That could be it. Will test that in various combinations and report back.

      So would the syntaxrb_funcall(rb_intern("get_points")etc... be better then instead of storing it ?

      @unknownuser said:

      try to reproduce in a small standalone case.

      I have created a smallish testcase. Although I needed some classes for that.
      So the code become over 500 lines.
      I run that code in Alex ruby code editor while debug. Don't know if that can affect the scope of class or anything.

      And I have tested the data before sending in to c-extension, it's alright in Ruby.

      The C++ code may very well have logic errors I havent found yet. So I have to scan that through again. It looks like this is the main problem though, since I'm dependent of array length for next iterations. When all the arrays(curves) have same length code is working.

      @unknownuser said:

      That's very old C syntax. Even newer C syntax allow you to declare variables when you need them.

      I couldent tell whats proper syntax or not, yet. That does sound easier.
      Does that go for loops/iterators as well?

      1 Reply Last reply Reply Quote 0
      • T Offline
        tt_su
        last edited by 1 Sept 2014, 16:32

        @jolran said:

        @unknownuser said:

        rb_intern returns ID, not VALUE

        ๐Ÿ˜ฎ Well that changes things. That could be it. Will test that in various combinations and report back.

        Don't think it does any harm in Ruby 1.8 and 2.0 as both happen to be typedef of unsigned long - the compiler won't know the difference. So I don't think that is your issue here.
        But if the Ruby C API changes what types they map to you will get trouble.

        @jolran said:

        I couldent tell whats proper syntax or not, yet. That does sound easier.
        Does that go for loops/iterators as well?

        [/quote]
        Best practices usually states to declare variables right before you need them and scope them as appropriate.
        I'm not quite sure what you are asking for about loops and iterators though.

        1 Reply Last reply Reply Quote 0
        • J Offline
          jolran
          last edited by 1 Sept 2014, 16:39

          I meant hoisting variables before running loops. Speeding up the loops by not declaring them inside the loop.

          Changing to rb_funcall(curve, rb_intern("get_points"), 0); made no difference, so I will try that other macro you mentioned. Otherwise there must be some hole in my logic elsewhere..

          PS: I havent come to best practices part yet ๐Ÿ˜„

          Thanks for the help.

          1 Reply Last reply Reply Quote 0
          • J Offline
            jolran
            last edited by 1 Sept 2014, 17:55

            Well well. How dumb can one get seriously ๐Ÿ‘Š

            Have been copying and pasting in code from Notepad++ to sketchup ruby code editor.
            Totally unfocused on that part..
            At some point I must have pushed ctrl v instead of ctrl c, so I not noingly had duplicate code. ๐Ÿ˜ก

            Off course I wasent editing the first part that was sent in to Visual studio for step over.

            Hehum, thanks for the help Thomthom. All is not lost. You have provided some good information for the rest of us and I learned some new stuff ๐Ÿ’š

            1 Reply Last reply Reply Quote 0
            • T Offline
              tt_su
              last edited by 1 Sept 2014, 19:38

              @jolran said:

              I meant hoisting variables before running loops. Speeding up the loops by not declaring them inside the loop.

              That's a Ruby world optimization - because creating Ruby objects are expensive.
              Not the same in C++.

              So now you're getting the correct array size?

              1 Reply Last reply Reply Quote 0
              • J Offline
                jolran
                last edited by 1 Sept 2014, 20:12

                @unknownuser said:

                That's a Ruby world optimization - because creating Ruby objects are expensive.
                Not the same in C++.

                Ok understood. It's quite wierd comming from Ruby to C++.

                @unknownuser said:

                So now you're getting the correct array size?

                Yes. And the code work's as I wanted to ๐Ÿ˜„ So far anyway. Really fast.
                I included your MACRO just to be safe. The code worked compiled in SU8.

                I imagine I could even make it faster refactoring it. But.. Most importantly I want it to be proper, so I try to follow your advice and read up on best practices before I move on.

                On another different subject while we are at it. Probably should start a new topic, but it doesent fell improper to ask here as well.

                Can we use Windows forms or WXwidgets(not WX-rubyversion) or other GUI toolkit with c++ in Sketchup ?
                I've seen discussion about it, but it was long time ago and relevant to only Ruby.

                I've seen examples where they hook up MFC as resource to win32 projects. Also read here somewhere it can be problematic to hook that up to Sketchup window, since windialogs are under the desktop(?)
                Anyway, not an substitute for webdialogs, just could be handy for some simpler dialogs using visual studio dialog editor.

                1 Reply Last reply Reply Quote 0
                • T Offline
                  tt_su
                  last edited by 2 Sept 2014, 09:33

                  I can't speak for WXwidgets, not familiar with it. Though I seem to recall some issues where they changed the order the window hierarchy - but maybe that was just the Ruby wrapper, I don't know.

                  But in general you can use whatever C/C++ lib you want. Native dialogs, knock yourself out. ๐Ÿ˜„ I haven't done so myself yet, but I wanted to try out a simple hello world window once.
                  I have done some code that calls the system folder picking dialogs though. Works very well.

                  1 Reply Last reply Reply Quote 0
                  • J Offline
                    jolran
                    last edited by 2 Sept 2014, 11:48

                    @unknownuser said:

                    I can't speak for WXwidgets, not familiar with it. Though I seem to recall some issues where they changed the order the window hierarchy - but maybe that was just the Ruby wrapper, I don't know.

                    I think it was the Ruby wrapper that was causing problems.
                    The posts touching the subjects are very old, as mentioned..

                    @unknownuser said:

                    But in general you can use whatever C/C++ lib you want. Native dialogs, knock yourself out. ๐Ÿ˜„ I haven't done so myself yet, but I wanted to try out a simple hello world window once.
                    I have done some code that calls the system folder picking dialogs though. Works very well.

                    โ˜€ I thought it was a no go on that! I'll peek around a little then.
                    Report back if I find something exiting.

                    Thanks

                    1 Reply Last reply Reply Quote 0
                    • G Offline
                      Garry K
                      last edited by 19 Feb 2015, 16:07

                      I've just read all of this - very helpful and interesting.

                      Years ago I had a project that had to work with 3 compilers, CE, Visual C++ and C++Builder and I had a single code base.

                      I ended up learning new optimization procedures which when used right from the start created no extra work. For CE and risc processors you must design data structures that are memory aligned. This not only runs fine on cisc - it is automatically optimized. The key bit here is that if a 4 byte int crosses a memory boundary then cisc does 2 fetches and stitches it together, risc simply crashes.

                      I created a tList object that works with all 3 compilers. You can treat a tList as an array or a hash or a set. You can pass in objects and perform binary tree searches on any member of the object by simply passing it a function pointer to your own custom compare function. This is useful when you want many sort combinations on a single collection. This is a quicksort implementation.

                      How the tList works is it is essentially an array of pointers. The array is in contiguous memory. The array grows in a very specific way and is moved if you run out of free contiguous memory. When the tList object is first created you can provide an initial number of elements that you want thus avoiding repeated calls to malloc and avoiding moving memory. When sorting just the list of pointers sort - the collected objects stay where they are.

                      Here is a compare function that sorts on 2 fields. If first field is a tie then sort on second field
                      The void pointer is cast as the type of object that you collect in the tList.
                      fIndex is 1 or -1 which changes the sort from ascending to descending

                      
                      int __fastcall CompareCabsCabinet( void * Item1, void * Item2 )
                      {
                      	int res;
                      
                      	tCab *v1 = ( tCab *) Item1;
                      	tCab *v2 = ( tCab *) Item2;
                      
                      	res = strcmp( v1->Description.c_str(), v2->Description.c_str() );
                      
                      	if ( res == 0 )
                      		res = v1->CabNumber - v2->CabNumber;
                      
                      	return fIndex * res;
                      }
                      //---------------------------------------------------------------------------
                      
                      

                      When I wrote a GIS rendering engine I used a tList for all the points. I added a quad tree for static map data and an rtree for dynamic gps points.

                      I ended up with virtually instant renderings with a million points.

                      1 Reply Last reply Reply Quote 0
                      • T Offline
                        tt_su
                        last edited by 21 Feb 2015, 19:32

                        Came across this article today: http://silverhammermba.github.io/emberb/c/#exceptions

                        Nice little overview over the Ruby C API.

                        1 Reply Last reply Reply Quote 0
                        • G Offline
                          Garry K
                          last edited by 21 Feb 2015, 22:45

                          good article - it has already answered some of my questions.

                          1 Reply Last reply Reply Quote 0
                          • T Offline
                            tt_su
                            last edited by 22 Feb 2015, 02:19

                            It explains the important parts better than the Ruby C API readme:
                            https://github.com/ruby/ruby/blob/trunk/README.EXT

                            1 Reply Last reply Reply Quote 0
                            • jiminy-billy-bobJ Offline
                              jiminy-billy-bob
                              last edited by 22 Feb 2015, 11:41

                              I just came accross an issue you guys should know about (if you don't already) : We can't pass more than 15 arguments in a single function from ruby to C++.

                              25% off Skatter for SketchUcation Premium Members

                              1 Reply Last reply Reply Quote 0
                              • G Offline
                                Garry K
                                last edited by 22 Feb 2015, 16:34

                                It looks like I'll have to use the visual studio compiler for c extensions in sketchup.
                                There is an MS specific config file and no other counterparts for borland etc.

                                ThirdParty\include\ruby\2.0\win32\i386-mswin32_100\ruby\config.h

                                1 Reply Last reply Reply Quote 0
                                • T Offline
                                  tt_su
                                  last edited by 26 Feb 2015, 15:27

                                  @jiminy-billy-bob said:

                                  I just came accross an issue you guys should know about (if you don't already) : We can't pass more than 15 arguments in a single function from ruby to C++.

                                  You actually hit that limit? ๐Ÿ˜ฒ

                                  1 Reply Last reply Reply Quote 0
                                  • T Offline
                                    tt_su
                                    last edited by 26 Feb 2015, 15:28

                                    @garry k said:

                                    It looks like I'll have to use the visual studio compiler for c extensions in sketchup.
                                    There is an MS specific config file and no other counterparts for borland etc.

                                    ThirdParty\include\ruby\2.0\win32\i386-mswin32_100\ruby\config.h

                                    For the examples we provided projects only for Visual Studio on Windows and Xcode on OSX. If you want to compile with another compiler you are free to do so - but we don't provide examples for all compilers.

                                    1 Reply Last reply Reply Quote 0
                                    • jiminy-billy-bobJ Offline
                                      jiminy-billy-bob
                                      last edited by 26 Feb 2015, 15:59

                                      @tt_su said:

                                      @jiminy-billy-bob said:

                                      I just came accross an issue you guys should know about (if you don't already) : We can't pass more than 15 arguments in a single function from ruby to C++.

                                      You actually hit that limit? ๐Ÿ˜ฒ

                                      Well... ๐Ÿ˜• I try to limit the back-and-forth between ruby and C++, so I pass all the information in one go, and treat everything in C++.

                                      25% off Skatter for SketchUcation Premium Members

                                      1 Reply Last reply Reply Quote 0
                                      • T Offline
                                        tt_su
                                        last edited by 26 Feb 2015, 16:29

                                        @jiminy-billy-bob said:

                                        @tt_su said:

                                        @jiminy-billy-bob said:

                                        I just came accross an issue you guys should know about (if you don't already) : We can't pass more than 15 arguments in a single function from ruby to C++.

                                        You actually hit that limit? ๐Ÿ˜ฒ

                                        Well... ๐Ÿ˜• I try to limit the back-and-forth between ruby and C++, so I pass all the information in one go, and treat everything in C++.

                                        A Hash would be good to pass instead - much easier to manage whenever you change some of the data you pass. No need to worry about changing argument order etc.

                                        1 Reply Last reply Reply Quote 0
                                        • G Offline
                                          Garry K
                                          last edited by 27 Feb 2015, 14:07

                                          I have a good start on SUEX_HelloWorld using C++Builder 6

                                          I've added cbuilder folder at the same level as the i386-mswin32_100 folder. Then I added a ruby folder and copied over the config.h files. Then I've edited the config files. I'm happy to say that I didn't have to edit any other 3rd party file.

                                          Then I ran implib.exe -a msvcrt-ruby200.lib msvcrt-ruby200.dll and created Borland's flavor of each lib file.
                                          After that I set up a couple of include file paths and added msvcrt-ruby200.lib to the probject.
                                          Turned off precompiled headers and unchecked use dynamic rtl

                                          I now have it compiling - but currently just for the win32 flavor for SU 2014
                                          Now for a bit of testing.

                                          Hopefully I'm on the right path. I'm pretty sure I need to export the function as cdecl for this to work.

                                          1 Reply Last reply Reply Quote 0
                                          • 1
                                          • 2
                                          • 3
                                          • 4
                                          • 2 / 4
                                          • First post
                                            Last post
                                          Buy SketchPlus
                                          Buy SUbD
                                          Buy WrapR
                                          Buy eBook
                                          Buy Modelur
                                          Buy Vertex Tools
                                          Buy SketchCuisine
                                          Buy FormFonts

                                          Advertisement