Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

sfwtest.C

00001 /* $Id: sfwtest.C,v 1.3 2000/11/26 04:18:27 rpav Exp $
00002  *
00003  * SFW
00004  * Copyright (C) 2000  Ryan Pavlik
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  *
00020  */
00021 
00022 /*
00023  * Test all or most of the Studio FrameWork
00024  *
00025  */ 
00026 
00027 #include <iostream>
00028 #include <cstdlib>
00029 #include <SFW/Application.H>
00030 #include <SFW/GC.H>
00031 #include <SFW/Plugin.H>
00032 #include "../../config.h"
00033 
00034 #ifdef SFW_HAS_JS
00035 #    include <SFW/JavaScript.H>
00036 #endif
00037 
00038 using namespace SFW;
00039 
00040 #ifdef SFW_HAS_JS
00041 
00042 class HelloMethod : public JavaScriptMethod {
00043 
00044 public:
00045     TYPE_INFO(HelloMethod, SFW::JavaScriptMethod);
00046     
00047     HelloMethod() : JavaScriptMethod("hello") { }
00048     
00049     virtual bool proc(JSTArray &_jstav, JavaScriptType &ret,
00050                       string &error) {
00051         cout << "HelloMethod World!" << endl;
00052         cout << "Number of args: " << _jstav.size() << endl;
00053     }
00054 };
00055 
00056 class FooClass : public JavaScriptClass {
00057     struct Context {
00058         int value;
00059     };
00060 
00061 public:
00062     TYPE_INFO(FooClass, SFW::JavaScriptClass);
00063     
00064     FooClass() : JavaScriptClass("Foo") {
00065         JSCLASS_DEF;
00066 
00067         JSPROP_ADD(value, 0);
00068     }
00069 
00070     JSCONSTRUCTOR(void **_context, JSTArray &_argv, string &_error) {
00071         Context *ctx = new Context;
00072         ctx->value   = 42;
00073         *_context    = (void*)ctx;
00074     }
00075 
00076     JSDESTRUCTOR(void *_context) {
00077         delete (Context*)_context;
00078     }
00079 
00080     JSPROP(value);
00081     JSPROP_SET(value)(void *_context, JavaScriptType &_t, string &_error) {
00082         Context *ctx = (Context*)_context;
00083 
00084         if(_t.getJSType() != JS_TYPE_INTEGER) {
00085             _error = "Integers only!";
00086             return false;
00087         }
00088 
00089         if((int)_t < ctx->value) {
00090             _error = "Greater values only!";
00091             return false;
00092         }
00093 
00094         ctx->value = (int)_t;
00095     }
00096     JSPROP_GET(value)(void *_context, JavaScriptType &_t, string &_error) {
00097         Context *ctx = (Context*)_context;
00098 
00099         _t = ctx->value;
00100     }
00101 
00102     JavaScriptType makeFoo(JavaScriptInterpreter &_jsi, int _value) {
00103         Context *ctx = new Context;
00104         ctx->value   = _value;
00105 
00106         return instantiate(_jsi, ctx, FooClass::destructCB);
00107     }
00108 };
00109 
00110 class MethodPtrTest : public Object {
00111 public:
00112     TYPE_INFO(MethodPtrTest, SFW::Object);
00113     METHOD_PTR(MethodPtrTest);
00114 
00115     Object* func1(Object *o) { cout << "func 1" << endl; }
00116     Object* func2(Object *o) { cout << "func 2" << endl; }
00117 };
00118 
00119 TYPE_INIT(MethodPtrTest);
00120 TYPE_INIT(MethodPtrTest::Method);
00121 
00122 #endif /* SFW_HAS_JS */
00123 
00124 class TestApp : public Application {
00125 
00126 public:
00127     TYPE_INFO(TestApp, SFW::Application);
00128     
00129     int main(ArgMap &_argm, ArgList &_argl) {
00130 #ifdef SFW_HAS_JS
00131     JavaScriptInterpreter jsi;
00132         JavaScriptType myFoo;
00133     JSReadLine  jsrl;
00134         HelloMethod hm;
00135         FooClass fc;
00136 
00137         MethodPtrTest *mpt = new MethodPtrTest;
00138         MethodPtrTest::Method mp;
00139 
00140         mp = &MethodPtrTest::func1;
00141         mp(mpt, 0);
00142 
00143         mp = &MethodPtrTest::func2;
00144         mp(mpt, 0);
00145 
00146         /* Note we really should use GC */
00147         delete mpt;
00148 
00149     jsi.define(jsrl);
00150         jsi.define(hm);
00151         jsi.define(fc);
00152 
00153         myFoo = fc.makeFoo(jsi, 13);
00154         jsi.setVar("bar", myFoo);
00155 
00156         jsi.eval("var foo = new Foo(); print(foo.value);");
00157         jsi.eval("print(bar.value);");
00158     jsi.eval("print('hello world!')");
00159         jsi.eval("hello(true); hello(1,2,3,4);");
00160     string s = jsi.eval("ReadLine.readline('Enter something: ')");
00161 
00162     cout << "you entered: " << s << endl;
00163 #endif /* SFW_HAS_JS */
00164         
00165     return 0;
00166     }
00167 
00168     void usage() { }
00169 };
00170 
00171 #ifdef SFW_HAS_JS
00172 TYPE_INIT(HelloMethod);
00173 TYPE_INIT(FooClass);
00174 #endif
00175 
00176 TYPE_INIT(TestApp);
00177 PLUGIN_INIT(SFW::Application, "Test Application", new TestApp);

Generated at Tue Jan 2 15:38:34 2001 for SFW by doxygen1.2.4 written by Dimitri van Heesch, © 1997-2000