Added MonoState to hold instantiated script classes.
authorJD Marble <[email protected]>
Tue, 2 Oct 2007 18:48:37 +0000 (2 11:48 -0700)
committerJD Marble <[email protected]>
Sun, 7 Oct 2007 05:53:25 +0000 (6 22:53 -0700)
mono/src/main/MonoPropertyFactory.cpp
mono/src/main/MonoScript.cpp
mono/src/main/MonoState.cpp [new file with mode: 0644]
mono/src/main/MonoState.hpp [new file with mode: 0644]
mono/src/main/SConscript
mono/src/unittest/SConscript
mono/src/unittest/monoPropertyFactory_test.cpp
mono/src/unittest/monoState_test.cpp [new file with mode: 0644]
mono/src/unittest/test.cs

index b61a575..51c1fef 100644 (file)
@@ -20,6 +20,7 @@ MidoriGraph; if not, write to the Free Software Foundation, Inc.,
 #include "MonoPropertyFactory.hpp"
 #include "MonoScriptManager.hpp"
 #include "MonoScript.hpp"
+#include "MonoState.hpp"
 
 namespace MidoriGraph {
 
@@ -30,6 +31,7 @@ MonoPropertyFactory::MonoPropertyFactory() {
        addPropertyType<MonoScriptManager>("ScriptManager");
        
        addPropertyType<MonoScript>();
+       addPropertyType<MonoState>();
 
 }
 
index 717f940..beb7573 100644 (file)
@@ -17,11 +17,11 @@ MidoriGraph; if not, write to the Free Software Foundation, Inc.,
 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
-#include<iostream>
-
-#include <mono/jit/jit.h>
-#include <mono/metadata/object.h>
-#include <mono/metadata/assembly.h>
+extern "C" {
+    #include <mono/jit/jit.h>
+    #include <mono/metadata/object.h>
+    #include <mono/metadata/assembly.h>
+}
 
 #include "MonoScriptManager.hpp"
 #include "MonoScript.hpp"
diff --git a/mono/src/main/MonoState.cpp b/mono/src/main/MonoState.cpp
new file mode 100644 (file)
index 0000000..06a4a37
--- /dev/null
@@ -0,0 +1,109 @@
+/* 
+Copyright 2007, JD Marble <[email protected]>
+This file is part of MidoriGraph.
+MidoriGraph is free software; you can redistribute it and/or modify it under the
+terms of the GNU General Public License as published by the Free Software 
+Foundation; either version 2 of the License, or (at your option) any later
+version.
+MidoriGraph is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+You should have received a copy of the GNU General Public License along with
+MidoriGraph; if not, write to the Free Software Foundation, Inc.,
+51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+extern "C" {
+    #include <mono/jit/jit.h>
+    #include <mono/metadata/object.h>
+    #include <mono/metadata/assembly.h>
+}
+
+#include "MonoScriptManager.hpp"
+#include "MonoScript.hpp"
+#include "MonoState.hpp"
+
+namespace MidoriGraph {
+
+using namespace std;
+
+
+MonoState::MonoState(AttributeMap attributes) {
+
+    AttributeMap::const_iterator found;
+
+    found = attributes.find("image");
+    if (found == attributes.end()) {
+        throw AttributeNotFound("image");
+    }
+    imageName = boost::any_cast<string>(found->second);
+
+    found = attributes.find("namespace");
+    if (found == attributes.end()) {
+        namespaceName = string("");
+    } else {
+        namespaceName = boost::any_cast<string>(found->second);
+    }
+
+    found = attributes.find("class");
+    if (found == attributes.end()) {
+        throw AttributeNotFound("class");
+    }
+    className = boost::any_cast<string>(found->second);
+
+}
+
+
+MonoState::~MonoState() {
+
+    
+}
+
+
+void MonoState::initialize(const Node& owner) {
+
+    Property::initialize(owner);
+
+    /*
+
+    */
+
+}
+
+
+void MonoState::connect(const Node& newParent) {
+    // Get a MonoDomain from the ScriptManager to load the assembly into.
+    MonoScriptManager* scriptManager =
+        owner.getAncestorProperty<MonoScriptManager>();
+    // \todo Test for no scriptManager found.
+
+    MonoDomain* domain = scriptManager->getDomain();
+    MonoAssembly* assembly = scriptManager->getAssembly(imageName);
+    
+    // Load the State class from the assembly.
+    MonoClass *klass = mono_class_from_name_case(
+        mono_assembly_get_image(assembly), 
+        namespaceName.c_str(), className.c_str());
+    if (!klass) {
+        throw runtime_error("Class or namespace not found.");
+    }
+
+    object = mono_object_new(domain, klass);
+
+    // This runs the default, argumentless constructor.
+    mono_runtime_object_init(object);
+}
+
+
+void MonoState::disconnect(const Node& oldParent) {
+
+}
+
+
+} //namespace MidoriGraph
+
diff --git a/mono/src/main/MonoState.hpp b/mono/src/main/MonoState.hpp
new file mode 100644 (file)
index 0000000..c710d40
--- /dev/null
@@ -0,0 +1,68 @@
+/* 
+Copyright 2007, JD Marble <[email protected]>
+This file is part of MidoriGraph.
+
+MidoriGraph is free software; you can redistribute it and/or modify it under theterms of the GNU General Public License as published by the Free Software 
+Foundation; either version 2 of the License, or (at your option) any later
+version.
+
+MidoriGraph is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along with
+MidoriGraph; if not, write to the Free Software Foundation, Inc.,
+51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#ifndef _MONO_STATE_HPP_
+#define _MONO_STATE_HPP_
+
+extern "C" {
+    #include <mono/jit/jit.h>
+}
+
+#include "Property.hpp"
+
+namespace MidoriGraph {
+
+
+class MonoState: public Property {
+
+  public:
+    
+    MonoState(AttributeMap attributes = AttributeMap());
+
+    virtual ~MonoState();
+
+    virtual void initialize(const Node& owner);
+    virtual void connect(const Node& newParent);
+    virtual void disconnect(const Node& oldParent);
+
+    static const std::string name() { return "MonoState"; }    
+    virtual const std::string getName() const { return name(); }
+
+    virtual const InterfaceList getInterfaces() const { 
+
+        InterfaceList interfaces = InterfaceList();
+        interfaces.push_back(name());
+        return interfaces;
+    }  
+
+    
+  private:
+
+    std::string imageName;
+    std::string namespaceName;
+    std::string className;
+
+    MonoObject* object;
+
+};
+
+
+} //namespace MidoriGraph
+
+#endif //_MONO_STATE_HPP_
+
index b611fa7..67afe99 100644 (file)
@@ -15,6 +15,7 @@ env.Append(CPPPATH=[
 mono_objects = [env.Object("MonoPropertyFactory.cpp"),
                 env.Object("MonoScriptManager.cpp"),
                 env.Object("MonoScript.cpp"),
+                env.Object("MonoState.cpp"),
                ]
 
 Return("mono_objects")
index 6116540..308fb74 100644 (file)
@@ -31,6 +31,7 @@ monounittest = env.Program("monounittest",
                          "monoPropertyFactory_test.cpp",
                          "monoScriptManager_test.cpp",
                          "monoScript_test.cpp",
+                         "monoState_test.cpp",
                         ])
 
 monounittest_passed = env.Test("monounittest.passed", monounittest)
index 8798f90..5e265a8 100644 (file)
@@ -37,4 +37,15 @@ BOOST_AUTO_TEST_CASE (MonoPropertyFactory_create) {
             ("filename", string("test.dll"))
         );
 
+    create<MonoPropertyFactory>("MonoScript", "MonoScript",
+        list_of< pair<string, any> > 
+            ("filename", string("test.dll"))
+        );
+
+    create<MonoPropertyFactory>("MonoState", "MonoState",
+        list_of< pair<string, any> > 
+            ("image", string("test"))
+            ("namespace", string(""))
+            ("class", string("State"))
+        );
 }
diff --git a/mono/src/unittest/monoState_test.cpp b/mono/src/unittest/monoState_test.cpp
new file mode 100644 (file)
index 0000000..c34ea1d
--- /dev/null
@@ -0,0 +1,147 @@
+/* 
+Copyright 2007, JD Marble <[email protected]>
+This file is part of MidoriGraph.
+
+MidoriGraph is free software; you can redistribute it and/or modify it under the 
+terms of the GNU General Public License as published by the Free Software 
+Foundation; either version 2 of the License, or (at your option) any later
+version.
+
+MidoriGraph is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along with
+MidoriGraph; if not, write to the Free Software Foundation, Inc.,
+51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#include <boost/test/auto_unit_test.hpp>
+#include <boost/assign/list_of.hpp> // for 'map_list_of()'
+
+#include "Node.hpp"
+#include "Property.hpp"             // AttributeNotFound
+#include "MonoScriptManager.hpp"
+#include "MonoScript.hpp"
+#include "MonoState.hpp"
+
+using namespace std;
+using boost::any;
+using boost::assign::map_list_of;
+using namespace MidoriGraph;
+
+
+class MonoState_testFramework {
+
+  public:
+    
+    MonoState_testFramework() {
+        
+        root = Node::makeRoot();
+        root.addProperty(new MonoScriptManager());
+
+        script = root.createChild();
+        Property::AttributeMap attributes;
+        attributes.insert(make_pair("filename", string("test.dll")));
+        script.addProperty(new MonoScript(attributes));
+    }
+
+    Node root;
+    Node script;
+};
+
+
+BOOST_AUTO_TEST_CASE (MonoState_initialize) {
+
+    MonoState_testFramework test;
+
+    Node stateNode = test.root.createChild();
+    Property::AttributeMap attributes;
+    attributes.insert(make_pair("image", string("test")));
+    attributes.insert(make_pair("class", string("empty")));
+    
+    BOOST_CHECK_NO_THROW(
+        stateNode.addProperty(new MonoState(attributes))
+    );
+
+}
+
+
+BOOST_AUTO_TEST_CASE (MonoState_initialize_noImage) {
+
+    MonoState_testFramework test;
+
+    Node stateNode = test.root.createChild();
+    Property::AttributeMap attributes;
+    attributes.insert(make_pair("class", string("empty")));
+
+    BOOST_CHECK_THROW(
+        stateNode.addProperty(new MonoState(attributes)),
+        AttributeNotFound
+    )
+}
+
+
+BOOST_AUTO_TEST_CASE (MonoState_initialize_noClass) {
+
+    MonoState_testFramework test;
+
+    Node stateNode = test.root.createChild();
+    Property::AttributeMap attributes;
+    attributes.insert(make_pair("image", string("test")));
+
+    BOOST_CHECK_THROW(
+        stateNode.addProperty(new MonoState(attributes)),
+        AttributeNotFound
+    )
+}
+
+
+BOOST_AUTO_TEST_CASE (MonoState_initialize_badImage) {
+
+    MonoState_testFramework test;
+
+    Node stateNode = test.root.createChild();
+    Property::AttributeMap attributes;
+    attributes.insert(make_pair("image", string("foo")));
+    attributes.insert(make_pair("class", string("empty")));
+
+    BOOST_CHECK_THROW(
+        stateNode.addProperty(new MonoState(attributes)),
+        runtime_error
+    )
+}
+
+
+BOOST_AUTO_TEST_CASE (MonoState_initialize_badClass) {
+
+    MonoState_testFramework test;
+
+    Node stateNode = test.root.createChild();
+    Property::AttributeMap attributes;
+    attributes.insert(make_pair("image", string("test")));
+    attributes.insert(make_pair("class", string("bar")));
+
+    BOOST_CHECK_THROW(
+        stateNode.addProperty(new MonoState(attributes)),
+        runtime_error
+    )
+}
+
+
+BOOST_AUTO_TEST_CASE (MonoState_initialize_badNamespace) {
+
+    MonoState_testFramework test;
+
+    Node stateNode = test.root.createChild();
+    Property::AttributeMap attributes;
+    attributes.insert(make_pair("image", string("test")));
+    attributes.insert(make_pair("namespace", string("foobar")));
+    attributes.insert(make_pair("class", string("empty")));
+
+    BOOST_CHECK_THROW(
+        stateNode.addProperty(new MonoState(attributes)),
+        runtime_error
+    )
+}
index ecf2217..6321c6d 100644 (file)
@@ -1,9 +1,16 @@
-using System;
-using System.Runtime.CompilerServices;
 
-class State {
+// The goggles do nothing!
+class empty {
+}
+
+
+class stateful {
+
+    string state = "Constructed";
 
-   State() {
-        Console.WriteLine ("State is constructed!");
+    public string State {
+        get {
+            return state;
+        }
     }
 }