[Avida-SVN] r2842 - in development: Avida.xcodeproj source/script

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Mon Oct 13 18:46:27 PDT 2008


Author: brysonda
Date: 2008-10-13 21:46:27 -0400 (Mon, 13 Oct 2008)
New Revision: 2842

Added:
   development/source/script/cASCPPParamNativeObjectSupport.h
   development/source/script/cASNativeObjectMethod.h
Modified:
   development/Avida.xcodeproj/project.pbxproj
   development/source/script/ASAvidaLib.cc
   development/source/script/cASCPPParameter.h
   development/source/script/cASNativeObject.h
Log:
AS: work on cASCPPParameter native object support.

Modified: development/Avida.xcodeproj/project.pbxproj
===================================================================
--- development/Avida.xcodeproj/project.pbxproj	2008-10-13 23:37:21 UTC (rev 2841)
+++ development/Avida.xcodeproj/project.pbxproj	2008-10-14 01:46:27 UTC (rev 2842)
@@ -438,6 +438,8 @@
 		70436B250C36C64000A05ABA /* platform.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = platform.h; sourceTree = "<group>"; };
 		70436B260C36C64000A05ABA /* PlatformExpert.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = PlatformExpert.cc; sourceTree = "<group>"; };
 		70436B270C36C64000A05ABA /* PlatformExpert.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = PlatformExpert.h; sourceTree = "<group>"; };
+		7048A95E0EA417CD0087B7BD /* cASNativeObjectMethod.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cASNativeObjectMethod.h; sourceTree = "<group>"; };
+		7048A9A40EA431140087B7BD /* cASCPPParamNativeObjectSupport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cASCPPParamNativeObjectSupport.h; sourceTree = "<group>"; };
 		7049F2D70A66859300640512 /* cHardwareTransSMT.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cHardwareTransSMT.cc; sourceTree = "<group>"; };
 		7049F2D80A66859300640512 /* cHardwareTransSMT.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cHardwareTransSMT.h; sourceTree = "<group>"; };
 		7049F3520A66A8F500640512 /* instset-classic.cfg */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = "instset-classic.cfg"; sourceTree = "<group>"; };
@@ -1252,6 +1254,8 @@
 				70AE2D3B0E7DF6C500A520B5 /* cASCPPParameter.h */,
 				70BB2A290E9FA12F008269D2 /* ASAvidaLib.h */,
 				70BB2A2A0E9FA12F008269D2 /* ASAvidaLib.cc */,
+				7048A95E0EA417CD0087B7BD /* cASNativeObjectMethod.h */,
+				7048A9A40EA431140087B7BD /* cASCPPParamNativeObjectSupport.h */,
 			);
 			path = script;
 			sourceTree = "<group>";

Modified: development/source/script/ASAvidaLib.cc
===================================================================
--- development/source/script/ASAvidaLib.cc	2008-10-13 23:37:21 UTC (rev 2841)
+++ development/source/script/ASAvidaLib.cc	2008-10-14 01:46:27 UTC (rev 2842)
@@ -24,11 +24,16 @@
 
 #include "ASAvidaLib.h"
 
-#include "cASNativeObject.h"
+#include "cASCPPParamNativeObjectSupport.h"
 #include "cASLibrary.h"
 
 #include "cAvidaConfig.h"
+#include "cWorld.h"
 
+#include <cstring>
+
+
+
 template<class FunctionType> class tASNativeObjectInstantiate;
 
 template<class NativeClass> 
@@ -88,6 +93,9 @@
   tASNativeObject<cAvidaConfig>::InitializeMethodRegistrar();
   tASNativeObject<cAvidaConfig>::
     RegisterMethod(new tASNativeObjectMethod<cAvidaConfig, cString (const cString&)>(&cAvidaConfig::GetAsString), "Get");
+
+
+  tASNativeObject<cWorld>::InitializeMethodRegistrar();
 };
 
 
@@ -96,4 +104,6 @@
   setupNativeObjects();
   
   lib->RegisterFunction(new tASNativeObjectInstantiate<cAvidaConfig ()>("Config"));
+  lib->RegisterFunction(new tASNativeObjectInstantiate<cWorld (cAvidaConfig*)>("World"));
+    // @TODO - world takes ownership of config, but I don't handle that here... world could delete it without AS knowing
 }

Added: development/source/script/cASCPPParamNativeObjectSupport.h
===================================================================
--- development/source/script/cASCPPParamNativeObjectSupport.h	                        (rev 0)
+++ development/source/script/cASCPPParamNativeObjectSupport.h	2008-10-14 01:46:27 UTC (rev 2842)
@@ -0,0 +1,39 @@
+/*
+ *  cASCPPParamNativeObjectSupport.h
+ *  Avida
+ *
+ *  Created by David on 10/13/08.
+ *  Copyright 2008 Michigan State University. All rights reserved.
+ *
+ *
+ *  This program 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; version 2
+ *  of the License.
+ *
+ *  This program 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 this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ */
+
+#ifndef cASCPPParamNativeObjectSupport_h
+#define cASCPPParamNativeObjectSupport_h
+
+#include "cASCPPParameter.h"
+#include "cASNativeObject.h"
+
+
+template<typename T> inline T cASCPPParameter::Get() const
+{
+  if (strcmp(m_nobj->GetType(), typeid(T).name()) != 0)
+    Avida::Exit(AS_EXIT_INTERNAL_ERROR); // @TODO - report type mismatch error
+  return (T)m_nobj->GetObject();
+}
+
+#endif

Modified: development/source/script/cASCPPParameter.h
===================================================================
--- development/source/script/cASCPPParameter.h	2008-10-13 23:37:21 UTC (rev 2841)
+++ development/source/script/cASCPPParameter.h	2008-10-14 01:46:27 UTC (rev 2842)
@@ -30,8 +30,9 @@
 
 #include "cString.h"
 
-class cASNativeObject;
+template<class NativeClass> class tASNativeObject;
 
+
 class cASCPPParameter
 {
 private:
@@ -54,10 +55,13 @@
   void Set(cString* val) { m_string = val; }
   void Set(const cString& val) { m_string = new cString(val); }
   void Set(cASNativeObject* val) { m_nobj = val; }
+  template<class NativeClass> void Set(NativeClass* val) { m_nobj = new tASNativeObject<NativeClass>(val); }
   
-  template<typename T> inline T Get() const { Avida::Exit(AS_EXIT_INTERNAL_ERROR); return m_int; }
+  template<typename T> inline T Get() const;
+  
 };
 
+
 template<> inline bool cASCPPParameter::Get<bool>() const { return m_bool; }
 template<> inline char cASCPPParameter::Get<char>() const { return m_char; }
 template<> inline int cASCPPParameter::Get<int>() const { return m_int; }

Modified: development/source/script/cASNativeObject.h
===================================================================
--- development/source/script/cASNativeObject.h	2008-10-13 23:37:21 UTC (rev 2841)
+++ development/source/script/cASNativeObject.h	2008-10-14 01:46:27 UTC (rev 2842)
@@ -28,11 +28,10 @@
 #include "AvidaScript.h"
 
 #include "cASCPPParameter.h"
+#include "cASNativeObjectMethod.h"
 #include "tArray.h"
 #include "tDictionary.h"
 
-#include <typeinfo>
-
 class cString;
 
 
@@ -46,6 +45,7 @@
   virtual ~cASNativeObject() { ; }
   
   virtual const char* GetType() = 0;
+  virtual void* GetObject() = 0;
   
   virtual cASCPPParameter CallMethod(int mid, cASCPPParameter args[]) const = 0;
   
@@ -63,51 +63,6 @@
 
 
 
-template <class NativeClass>
-class cASNativeObjectMethod
-{
-public:
-  virtual ~cASNativeObjectMethod() { ; }
-  
-  virtual int GetArity() const = 0;
-  virtual const sASTypeInfo& GetArgumentType(int arg) const = 0;
-  virtual const sASTypeInfo& GetReturnType() const = 0;
-
-  virtual cASCPPParameter Call(NativeClass* object, cASCPPParameter args[]) const = 0;
-};
-
-
-template<class NativeClass, class FunctionType> class tASNativeObjectMethod;
-
-template<class NativeClass, class ReturnType, class Arg1Type>
-class tASNativeObjectMethod<NativeClass, ReturnType (Arg1Type)> : public cASNativeObjectMethod<NativeClass>
-{
-private:
-  sASTypeInfo m_rtype;
-  sASTypeInfo m_signature;
-  ReturnType (NativeClass::*m_method)(Arg1Type);
-  
-public:
-  tASNativeObjectMethod(ReturnType (NativeClass::*method)(Arg1Type)) : m_method(method)
-  {
-    m_rtype = AvidaScript::TypeOf<ReturnType>();
-    m_signature = AvidaScript::TypeOf<Arg1Type>();
-  }
-  
-  int GetArity() const { return 1; }
-  const sASTypeInfo& GetArgumentType(int arg) const { return m_signature; }
-  const sASTypeInfo& GetReturnType() const { return m_rtype; }
-  
-  cASCPPParameter Call(NativeClass* object, cASCPPParameter args[]) const
-  {
-    cASCPPParameter rvalue;
-    rvalue.Set((object->*m_method)(args[0].Get<Arg1Type>()));
-    return rvalue;
-  }
-};
-
-
-
 template<class NativeClass>
 class tASNativeObject : public cASNativeObject
 {
@@ -126,6 +81,7 @@
   ~tASNativeObject() { delete m_object; }
 
   const char* GetType() { return typeid(m_object).name(); }
+  void* GetObject() { return (void*)m_object; }
   
   cASCPPParameter CallMethod(int mid, cASCPPParameter args[]) const { return (*s_methods)[mid]->Call(m_object, args); }
 

Added: development/source/script/cASNativeObjectMethod.h
===================================================================
--- development/source/script/cASNativeObjectMethod.h	                        (rev 0)
+++ development/source/script/cASNativeObjectMethod.h	2008-10-14 01:46:27 UTC (rev 2842)
@@ -0,0 +1,81 @@
+/*
+ *  cASNativeObjectMethod.h
+ *  Avida
+ *
+ *  Created by David on 10/13/08.
+ *  Copyright 2008 Michigan State University. All rights reserved.
+ *
+ *
+ *  This program 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; version 2
+ *  of the License.
+ *
+ *  This program 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 this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ */
+
+#ifndef cASNativeObjectMethod_h
+#define cASNativeObjectMethod_h
+
+#include "AvidaScript.h"
+
+#include "cASCPPParameter.h"
+
+#include <typeinfo>
+
+
+template <class NativeClass>
+class cASNativeObjectMethod
+{
+public:
+  virtual ~cASNativeObjectMethod() { ; }
+  
+  virtual int GetArity() const = 0;
+  virtual const sASTypeInfo& GetArgumentType(int arg) const = 0;
+  virtual const sASTypeInfo& GetReturnType() const = 0;
+  
+  virtual cASCPPParameter Call(NativeClass* object, cASCPPParameter args[]) const = 0;
+};
+
+
+
+template<class NativeClass, class FunctionType> class tASNativeObjectMethod;
+
+
+template<class NativeClass, class ReturnType, class Arg1Type>
+class tASNativeObjectMethod<NativeClass, ReturnType (Arg1Type)> : public cASNativeObjectMethod<NativeClass>
+{
+private:
+  sASTypeInfo m_rtype;
+  sASTypeInfo m_signature;
+  ReturnType (NativeClass::*m_method)(Arg1Type);
+  
+public:
+  tASNativeObjectMethod(ReturnType (NativeClass::*method)(Arg1Type)) : m_method(method)
+  {
+    m_rtype = AvidaScript::TypeOf<ReturnType>();
+    m_signature = AvidaScript::TypeOf<Arg1Type>();
+  }
+  
+  int GetArity() const { return 1; }
+  const sASTypeInfo& GetArgumentType(int arg) const { return m_signature; }
+  const sASTypeInfo& GetReturnType() const { return m_rtype; }
+  
+  cASCPPParameter Call(NativeClass* object, cASCPPParameter args[]) const
+  {
+    cASCPPParameter rvalue;
+    rvalue.Set((object->*m_method)(args[0].Get<Arg1Type>()));
+    return rvalue;
+  }
+};
+
+
+#endif




More information about the Avida-cvs mailing list