• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            牽著老婆滿街逛

            嚴以律己,寬以待人. 三思而后行.
            GMail/GTalk: yanglinbo#google.com;
            MSN/Email: tx7do#yahoo.com.cn;
            QQ: 3 0 3 3 9 6 9 2 0 .

            C++程序中嵌入Ruby腳本系統

            作者: 楊粼波
            Email: 
            tx7do#yahoo.com.cn

                突發奇想的想要把Ruby嵌入到C++的程序里面去,想了于是就去做了.現在只在Windows下面測試通過,其他系統下,我還沒有試過,不過基本過程大差不差的.

            需要準備些什么?
                1.Ruby(http://www.ruby-lang.org/en/downloads/),去頁面中下載最新版本的Ruby 的安裝包;
                2.Swig(http://www.swig.org/download.html),去頁面中下載最新的安裝包.

            什么是Ruby?
                Ruby,一種為簡單快捷面向對象編程(面向對象程序設計)而創的腳本語言,由日本人松本行弘(まつもとゆきひろ,英譯:Yukihiro Matsumoto,外號matz)開發,遵守GPL協議和Ruby License。Ruby的作者認為Ruby > (Smalltalk + Perl) / 2,表示Ruby是一個語法像Smalltalk一樣完全面向對象、腳本執行、又有Perl強大的文字處理功能的編程語言。

            什么是SWIG?
                SWIG(Simplified Wrapper and Interface Generator)是個幫助使用C或者C++編寫的軟件能與其它各種高級編程語言進行嵌入聯接的開發工具。SWIG能應用于各種不同類型的語言包括常用腳本編譯語言例如Perl, PHP, Python, Tcl, Ruby and PHP。
              簡單來說,主要用于導出C/C++程序庫給腳本語言使用的一個自動化工具.導出的工作是非常機械化,而且繁復的.

            編譯環境設置
                Ruby在Windows下:
                頭文件在$RUBY_HOME/lib/ruby/1.8/i386-mswin32;
                lib在$RUBY_HOME/lib,為msvcrt-ruby18.lib;
                dll在RUBY_HOME/bin,其實只有一個dll,就是:msvcrt-ruby18.dll.
                在這里需要注意到的是,$RUBY_HOME/lib/ruby/1.8/i386-mswin32/config.h這個文件對VC的版本做了限制:

            #if _MSC_VER != 1200
            #error MSC version unmatch
            #endif
                所以,如果VC不是這個版本的話,編譯是通不過的,對此問題,最簡單的辦法就是:將這三行代碼注釋掉,就可以了.

            C++解釋器包裹代碼
            頭文件

            #ifndef __RubyInterpreter_H__
            #define __RubyInterpreter_H__


            #include 
            <string>

            typedef unsigned 
            long    VALUE;
            typedef std::
            string        String;

            typedef VALUE(
            *staticValueMethod)();
            typedef VALUE(
            *ProtectedMethod)(VALUE);

            class RubyInterpreter
            {
            public:
                RubyInterpreter();
                
            virtual ~RubyInterpreter();

            public:
                
            /// 初始化解釋器
                void initializeInterpreter();

                
            /// 終止解釋器
                void finalizeInterpreter();

                
            /// 設置
                void setOutputFunction(staticValueMethod func);

                
            /// 加入引用庫的搜索路徑
                void addSearchPath(const String& path);

            public:
                
            /// 執行語句
                bool execute(const String& command);
                
                
            /// 執行文件
                bool executeFile(String rubyfile);

            private:
                
            /// 記錄錯誤日志
                void logRubyErrors(const std::string& intro, int errorcode);
                
                
            /// 
                void loadProtected(ProtectedMethod func, VALUE args,
                    
            const std::string& msg, bool exitOnFail = false);

                
            /// 
                static VALUE loadDlls(VALUE);
            }
            ;


            #endif
            源文件

            #include 
            "StdAfx.h"
            #include 
            "RubyInterpreter.h"

            #include 
            "FixRubyHeaders.h"
            #include 
            <ruby.h>
            #include 
            "FixRubyHeaders.h"


            RubyInterpreter::RubyInterpreter()
            {

            }


            RubyInterpreter::
            ~RubyInterpreter()
            {

            }


            void RubyInterpreter::initializeInterpreter()
            {
            #if defined(NT)
                
            static int dummyargc(0);
                
            static char** vec;
                NtInitialize(
            &dummyargc, &vec);
            #endif

                
            // 初始化Ruby
                ruby_init();

                
            // 使用UTF8編碼
                execute( "$KCODE = 'u'" );

                
            // addSearchPath();

                
            // 初始化腳本加載路徑
                ruby_init_loadpath();

                
            // 設置安全級別
                rb_set_safe_level(0);

                
            // 
                ruby_script("ruby");

                
            //loadProtected(&RubyInterpreter::loadDlls, 0, "Ruby error while loading dlls");
            }


            void RubyInterpreter::finalizeInterpreter()
            {
                ruby_finalize();
            }


            void RubyInterpreter::setOutputFunction(staticValueMethod func)
            {
                rb_defout 
            = rb_str_new(""0);

                
            // 定義一個虛擬類的方法
                rb_define_singleton_method(rb_defout, "write", func, 1);
            }


            void RubyInterpreter::addSearchPath(const String& path)
            {
                ruby_incpush(path.c_str());
            }


            VALUE RubyInterpreter::loadDlls(VALUE val)
            {
                String lib;

                
            // 
                return rb_require(lib.c_str());
            }


            void RubyInterpreter::loadProtected(ProtectedMethod func,
                                                VALUE val, 
                                                
            const std::string& msg, 
                                                
            bool exitOnFail)
            {
                
            int error = 0;
                rb_protect(func, val, 
            &error);
                logRubyErrors(
            "Ruby error while initializing", error);
            }


            void RubyInterpreter::logRubyErrors(const std::string& intro, int errorcode)
            {
                
            if (errorcode != 0)
                
            {
                    VALUE info 
            = rb_inspect(ruby_errinfo);
                    rb_backtrace();
                    
            if (intro.length() > 0)
                    
            {
                    }

                }

            }


            bool RubyInterpreter::execute(const String& command)
            {
                
            int status = -1;

                rb_eval_string_protect(command.c_str(), 
            &status);

                logRubyErrors(
            "", status);

                
            if ( status )
                
            {
                    rb_eval_string_protect(
            "print $!"&status);
                    
            return false;
                }


                
            return true;
            }


            bool RubyInterpreter::executeFile(String rubyfile)
            {
                
            bool error = execute("load '" + rubyfile + "'");
                
            return error;
            }



            SWIG的使用
            步驟大致為:
            1. 編寫后綴為.i的腳本;
            2. 使用swig生成導出代碼,假如腳本名為:sample.i,那么生成的源碼文件名規則就為:sample_wrap.cpp/.c.
            3. 將生成的cpp加入動態鏈接庫,然后編譯.

            最簡單的.i腳本為:
            %module Export4ScriptLib
            %{
            #include 
            "Player.h"
            %}



            %include "stl.i"
            %include "Player.h"
            Edit:如果想要使用STL的導出類,那就需要添加%include "stl.i"
            假如說,頭文件里面定義的所有的類,類所有的方法,你都要將之導出,那么以上就足夠了.但是,假如你只需要導出部分的類,部分的類的方法.那么你就需要自己手動寫入到.i腳本里面去了.

            生成代碼的命令為:
            swig.exe -c++ -ruby Exports.i
            這樣寫的前提是你已經吧swig的路徑加入到環境變量里面去了,其中第一個參數表示的是導出的代碼為c++,第二個參數表示的目標腳本語言是誰,第三個參數是.i腳本的路徑名.我寫了一個批處理:invoke_swig.bat,做這件事情.不過更完美的做法是在VC項目里面的"預生成事件"加入此語句.

            剩下的事情就是把生成的代碼和要導出的代碼編譯一邊,就可以開始使用導出的C++庫了.


            測試
            在實例代碼里面:Export4ScriptLib工程是動態鏈接庫工程,testRubyInterpreter是測試用的可執行程序工程.
            測試用的Ruby代碼test.rb如下:
            require 'Export4ScriptLib'

            print "hello 你好!\n"


            ply 
            = Export4ScriptLib::Player.new
            ply.Jump();
            ply.Move(
            1002000);
            測試用C++代碼如下:
            class testClient
            {
            public:
                testClient()
                {
                    mRubyInterpreter 
            = new RubyInterpreter();
                    mRubyInterpreter
            ->initializeInterpreter();
                }
                
                
            ~testClient()
                {
                    delete mRubyInterpreter;
                }

                void 
            exec()
                {
                    
            // 執行語句
                    mRubyInterpreter
            ->execute("print \"This is C++ call Ruby print funtion!\n\"");
                    
                    
            // 執行文件
                    mRubyInterpreter
            ->executeFile("test.rb");
                }

            private:
                RubyInterpreter
            * mRubyInterpreter;
            };



            源代碼下載
            testRubyInterpreter.rar


            參考資料
            1. 什么是Ruby http://www.kuqin.com/beginner/ruby.html
            2. SWIG http://swig.minidx.com/
            3.  http://raylinn.javaeye.com/blog/629329

            posted on 2010-06-08 21:49 楊粼波 閱讀(7661) 評論(8)  編輯 收藏 引用 所屬分類: 原創文章

            評論

            # re: C++程序中嵌入Ruby腳本系統 2010-06-08 23:03 陳梓瀚(vczh)

            我現在正在開發的vlpp使用的方法是

            BasicFunctionExecutor<int(int*, int)> sum=vm.get(compile(code),...);

            int ns[]={1,2,3,4,5};
            int s=sum(ns, 5);  回復  更多評論   

            # re: C++程序中嵌入Ruby腳本系統[未登錄] 2010-06-08 23:13 楊粼波

            這個類只是用來驗證和講解用的,
            沒有必要做那么復雜的設計,那是會讓人閱讀上有些困難的.
            任何東西并不是一味的復雜就是好的.  回復  更多評論   

            # re: C++程序中嵌入Ruby腳本系統 2010-06-08 23:23 陳梓瀚(vczh)

            @楊粼波
            實現復雜沒關系,用起來要簡單。接口要做好,但是完全沒必要因為別人看不懂而扭曲自己的設計。那只是implementation。  回復  更多評論   

            # re: C++程序中嵌入Ruby腳本系統[未登錄] 2010-06-08 23:31 楊粼波

            --!這也不是扭曲自己的設計.
            因為如果一個東西是給人用的,那接口簡單明了就OK了.
            但是要是分解其原理,
            那就不必要做復雜了.
            因為接口本身就是為了屏蔽復雜,
            而我現在就是要展示接口背后的東西,
            顯然的需要化復雜為簡單了^_^  回復  更多評論   

            # re: C++程序中嵌入Ruby腳本系統 2010-06-09 11:45 mega

            vczh是要你夸夸他呢 哈哈  回復  更多評論   

            # re: C++程序中嵌入Ruby腳本系統[未登錄] 2010-06-09 11:47 楊粼波

            --!這樣子?
            汗,是我不解風情了?
            呃.....  回復  更多評論   

            # re: C++程序中嵌入Ruby腳本系統 2010-06-09 14:00 陳梓瀚(vczh)

            @mega
            nono,要傳播知識。  回復  更多評論   

            # re: C++程序中嵌入Ruby腳本系統 2010-06-09 14:01 楊粼波

            中國急需要性教育,哈哈哈.....  回復  更多評論   

            久久97精品久久久久久久不卡| 久久综合一区二区无码| 久久久久亚洲AV成人网| 久久精品成人国产午夜| 国产精品久久久久影院嫩草| 漂亮人妻被黑人久久精品| 日韩av无码久久精品免费| 亚洲精品tv久久久久久久久| 久久亚洲日韩看片无码| 久久精品国产亚洲AV香蕉| 中文字幕久久精品无码| AV无码久久久久不卡蜜桃| 亚洲国产另类久久久精品黑人| 久久国产欧美日韩精品| 久久永久免费人妻精品下载| 久久精品国产亚洲AV无码麻豆| 久久国产亚洲精品无码| 91精品国产综合久久四虎久久无码一级| 91久久香蕉国产熟女线看| 久久久精品视频免费观看| 久久久久亚洲av成人网人人软件 | 99久久无码一区人妻| 久久精品国产99久久丝袜| 久久精品免费一区二区| 99久久精品国产免看国产一区| 99久久精品国产一区二区| 久久久久亚洲AV片无码下载蜜桃| 色8久久人人97超碰香蕉987| 久久综合狠狠色综合伊人| 久久精品视频一| 久久亚洲欧美日本精品| 久久久久久久91精品免费观看| 九九精品99久久久香蕉| 性做久久久久久久久| 国产欧美一区二区久久| 亚洲综合久久夜AV | 久久99国产精品久久| 久久婷婷色综合一区二区| 国产成人精品久久一区二区三区av| 中文成人无码精品久久久不卡| 日本精品久久久久中文字幕8|