[Avida-SVN] r2850 - in development/source: main script

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Wed Oct 15 17:08:39 PDT 2008


Author: brysonda
Date: 2008-10-15 20:08:39 -0400 (Wed, 15 Oct 2008)
New Revision: 2850

Modified:
   development/source/main/cAvidaConfig.cc
   development/source/main/cAvidaConfig.h
   development/source/script/ASAvidaLib.cc
   development/source/script/cASNativeObjectMethod.h
Log:
AS: register additional cAvidaConfig methods.

Modified: development/source/main/cAvidaConfig.cc
===================================================================
--- development/source/main/cAvidaConfig.cc	2008-10-15 12:53:30 UTC (rev 2849)
+++ development/source/main/cAvidaConfig.cc	2008-10-16 00:08:39 UTC (rev 2850)
@@ -29,6 +29,7 @@
 #include "defs.h"
 #include "cActionLibrary.h"
 #include "cDriverManager.h"
+#include "cDriverStatusConduit.h"
 #include "cInitFile.h"
 #include "cStringIterator.h"
 #include "tDictionary.h"
@@ -54,14 +55,14 @@
   }
 }
 
-void cAvidaConfig::Load(const cString& filename, const bool& crash_if_not_found = false)
+void cAvidaConfig::Load(const cString& filename, bool crash_if_not_found)
 {
   tDictionary<cString> mappings;
   Load(filename, mappings, crash_if_not_found);
 }
 
 
-void cAvidaConfig::Load(const cString& filename, const tDictionary<cString>& mappings, const bool& crash_if_not_found = false)
+void cAvidaConfig::Load(const cString& filename, const tDictionary<cString>& mappings, bool crash_if_not_found)
 {
   // Load the contents from the file.
   cInitFile init_file(filename, mappings);
@@ -69,28 +70,25 @@
   if (!init_file.WasOpened()) {
     tConstListIterator<cString> err_it(init_file.GetErrors());
     const cString* errstr = NULL;
-    while ((errstr = err_it.Next())) cerr << "Error: " << *errstr << endl;
+    while ((errstr = err_it.Next())) cDriverManager::Status().SignalError(*errstr);
     if (init_file.WasFound()) {
       // exit the program if the requested configuration was found but could not be loaded
-      cerr << "Error: failed to load '" << filename << "'.  Ending the program." << endl;
-      exit(-1);
+      cDriverManager::Status().SignalFatalError(-1, cString("unable to open configuration file '") + filename + "'");
     } else if (crash_if_not_found) {
       // exit the program if the requested configuration file is not found
-      cerr << "Error: Unable to find file '" << filename 
-           << "'.  Ending the program." << endl;
-      exit(-1);
+      cDriverManager::Status().SignalFatalError(-1, cString("configuration file '") + filename + "' not found"); 
     } else {
       // If we failed to open the config file, try creating it.
-      cerr << "Warning: Unable to find file '" << filename 
-           << "'.  Creating default." << endl;
+      cDriverManager::Status().NotifyWarning(
+        cString("configuration file '") + filename + "' not found, creating default config...");
       Print(filename);
     }
   }
   
   cString version_id = init_file.ReadString("VERSION_ID", "Unknown");
   if (version_id != VERSION) {
-    cerr << "Warning: Configuration file version number mismatch." << endl;
-    cerr << "         Avida Version: \"" << VERSION << "\".  Config Version: \"" << version_id << "\"" << endl;
+    cDriverManager::Status().NotifyWarning(
+      cString("config file version number mismatch -- Avida: '") + VERSION + "'  File: '" + version_id + "'");
   }
   
 
@@ -139,7 +137,7 @@
   // Print out the collected warnings and messages
   tConstListIterator<cString> err_it(init_file.GetErrors());
   const cString* errstr = NULL;
-  while ((errstr = err_it.Next())) cerr << "Warning: " << *errstr << endl;
+  while ((errstr = err_it.Next())) cDriverManager::Status().NotifyWarning(*errstr);
 }
 
 /* Routine to create an avida configuration file from internal default values */
@@ -384,11 +382,12 @@
   return false;
 }
 
-cString cAvidaConfig::GetAsString(const cString& entry)
+cString cAvidaConfig::GetAsString(const cString& entry) const
 {
   // Default to empty string on lookup failure
   cString rtn("");
-  Get(entry, rtn);
+  if (!Get(entry, rtn))
+    cDriverManager::Status().SignalError(cString("config entry '") + entry + "' not found");
   return rtn;
 }
 

Modified: development/source/main/cAvidaConfig.h
===================================================================
--- development/source/main/cAvidaConfig.h	2008-10-15 12:53:30 UTC (rev 2849)
+++ development/source/main/cAvidaConfig.h	2008-10-16 00:08:39 UTC (rev 2850)
@@ -566,15 +566,18 @@
   
 #endif
   
-  void Load(const cString& filename, const bool& crash_if_not_found);
-  void Load(const cString& filename, const tDictionary<cString>& mappings, const bool& crash_if_not_found);
+  inline void Load(const cString& filename) { Load(filename, false); }
+  void Load(const cString& filename, bool crash_if_not_found);
+  void Load(const cString& filename, const tDictionary<cString>& mappings, bool crash_if_not_found = false);
   void Print(const cString& filename);
   void Status();
   void PrintReview();
   
   
   bool Get(const cString& entry, cString& ret) const;
-  cString GetAsString(const cString& entry);
+  cString GetAsString(const cString& entry) const;
+  bool HasEntry(const cString& entry) const { cString rtn; return Get(entry, rtn); }
+  
   bool Set(const cString& entry, const cString& val);
   void Set(tDictionary<cString>& sets);
   

Modified: development/source/script/ASAvidaLib.cc
===================================================================
--- development/source/script/ASAvidaLib.cc	2008-10-15 12:53:30 UTC (rev 2849)
+++ development/source/script/ASAvidaLib.cc	2008-10-16 00:08:39 UTC (rev 2850)
@@ -46,22 +46,28 @@
 
 static void setupNativeObjects()
 {
-#define REGISTER_METHOD(CLASS, NAME, METHOD, SIGNATURE) \
+#define REGISTER_S_METHOD(CLASS, NAME, METHOD, SIGNATURE) \
   tASNativeObject<CLASS>::RegisterMethod(new tASNativeObjectMethod<CLASS, SIGNATURE>(&CLASS::METHOD), NAME);
+#define REGISTER_C_METHOD(CLASS, NAME, METHOD, SIGNATURE) \
+  tASNativeObject<CLASS>::RegisterMethod(new tASNativeObjectMethodConst<CLASS, SIGNATURE>(&CLASS::METHOD), NAME);
 
   
   tASNativeObject<cAvidaConfig>::InitializeMethodRegistrar();
-  REGISTER_METHOD(cAvidaConfig, "Get", GetAsString, cString (const cString&));
+  REGISTER_C_METHOD(cAvidaConfig, "Get", GetAsString, cString (const cString&));
+  REGISTER_C_METHOD(cAvidaConfig, "HasEntry", HasEntry, bool (const cString&));
+  REGISTER_S_METHOD(cAvidaConfig, "Load", Load, void (const cString&));
+  REGISTER_S_METHOD(cAvidaConfig, "Set", Set, bool (const cString&, const cString&));
   
   
   tASNativeObject<cDefaultRunDriver>::InitializeMethodRegistrar();
-  REGISTER_METHOD(cDefaultRunDriver, "Run", Run, void ());
+  REGISTER_S_METHOD(cDefaultRunDriver, "Run", Run, void ());
 
   
   tASNativeObject<cWorld>::InitializeMethodRegistrar();
   
   
 #undef REGISTER_METHOD
+#undef REGISTER_METHOD_CONST
 };
 
 

Modified: development/source/script/cASNativeObjectMethod.h
===================================================================
--- development/source/script/cASNativeObjectMethod.h	2008-10-15 12:53:30 UTC (rev 2849)
+++ development/source/script/cASNativeObjectMethod.h	2008-10-16 00:08:39 UTC (rev 2850)
@@ -48,6 +48,7 @@
 
 
 template<class NativeClass, class FunctionType> class tASNativeObjectMethod;
+template<class NativeClass, class FunctionType> class tASNativeObjectMethodConst;
 
 
 template<class NativeClass>
@@ -71,13 +72,93 @@
   
   cASCPPParameter Call(NativeClass* object, cASCPPParameter args[]) const
   {
-    cASCPPParameter rvalue;
     (object->*m_method)();
     return cASCPPParameter(); // @TODO - return actual void value?
   }
 };
 
 
+template<class NativeClass>
+class tASNativeObjectMethodConst<NativeClass, void ()> : public cASNativeObjectMethod<NativeClass>
+{
+private:
+  sASTypeInfo m_rtype;
+  sASTypeInfo m_signature;
+  void (NativeClass::*m_method)() const;
+  
+public:
+  tASNativeObjectMethodConst(void (NativeClass::*method)() const) : m_method(method)
+  {
+    m_rtype = AvidaScript::TypeOf<void>();
+    m_signature = AvidaScript::TypeOf<void>();
+  }
+  
+  int GetArity() const { return 0; }
+  const sASTypeInfo& GetArgumentType(int arg) const { return m_signature; }
+  const sASTypeInfo& GetReturnType() const { return m_rtype; }
+  
+  cASCPPParameter Call(NativeClass* object, cASCPPParameter args[]) const
+  {
+    (object->*m_method)();
+    return cASCPPParameter(); // @TODO - return actual void value?
+  }
+};
+
+
+template<class NativeClass, class Arg1Type>
+class tASNativeObjectMethod<NativeClass, void (Arg1Type)> : public cASNativeObjectMethod<NativeClass>
+{
+private:
+  sASTypeInfo m_rtype;
+  sASTypeInfo m_signature;
+  void (NativeClass::*m_method)(Arg1Type);
+  
+public:
+  tASNativeObjectMethod(void (NativeClass::*method)(Arg1Type)) : m_method(method)
+  {
+    m_rtype = AvidaScript::TypeOf<void>();
+    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
+  {
+    (object->*m_method)(args[0].Get<Arg1Type>());
+    return cASCPPParameter(); // @TODO - return actual void value?
+  }
+};
+
+
+template<class NativeClass, class Arg1Type>
+class tASNativeObjectMethodConst<NativeClass, void (Arg1Type)> : public cASNativeObjectMethod<NativeClass>
+{
+private:
+  sASTypeInfo m_rtype;
+  sASTypeInfo m_signature;
+  void (NativeClass::*m_method)(Arg1Type) const;
+  
+public:
+  tASNativeObjectMethodConst(void (NativeClass::*method)(Arg1Type) const) : m_method(method)
+  {
+    m_rtype = AvidaScript::TypeOf<void>();
+    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
+  {
+    (object->*m_method)(args[0].Get<Arg1Type>());
+    return cASCPPParameter(); // @TODO - return actual void value?
+  }
+};
+
+
 template<class NativeClass, class ReturnType, class Arg1Type>
 class tASNativeObjectMethod<NativeClass, ReturnType (Arg1Type)> : public cASNativeObjectMethod<NativeClass>
 {
@@ -106,4 +187,91 @@
 };
 
 
+template<class NativeClass, class ReturnType, class Arg1Type>
+class tASNativeObjectMethodConst<NativeClass, ReturnType (Arg1Type)> : public cASNativeObjectMethod<NativeClass>
+{
+private:
+  sASTypeInfo m_rtype;
+  sASTypeInfo m_signature;
+  ReturnType (NativeClass::*m_method)(Arg1Type) const;
+  
+public:
+  tASNativeObjectMethodConst(ReturnType (NativeClass::*method)(Arg1Type) const) : 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 ReturnType, class Arg1Type, class Arg2Type>
+class tASNativeObjectMethod<NativeClass, ReturnType (Arg1Type, Arg2Type)> : public cASNativeObjectMethod<NativeClass>
+{
+private:
+  sASTypeInfo m_rtype;
+  sASTypeInfo m_signature[2];
+  ReturnType (NativeClass::*m_method)(Arg1Type, Arg2Type);
+  
+public:
+  tASNativeObjectMethod(ReturnType (NativeClass::*method)(Arg1Type, Arg2Type)) : m_method(method)
+  {
+    m_rtype = AvidaScript::TypeOf<ReturnType>();
+    m_signature[0] = AvidaScript::TypeOf<Arg1Type>();
+    m_signature[1] = AvidaScript::TypeOf<Arg2Type>();
+  }
+  
+  int GetArity() const { return 2; }
+  const sASTypeInfo& GetArgumentType(int arg) const { return m_signature[arg]; }
+  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>(), args[1].Get<Arg2Type>()));
+    return rvalue;
+  }
+};
+
+
+template<class NativeClass, class ReturnType, class Arg1Type, class Arg2Type>
+class tASNativeObjectMethodConst<NativeClass, ReturnType (Arg1Type, Arg2Type)> : public cASNativeObjectMethod<NativeClass>
+{
+private:
+  sASTypeInfo m_rtype;
+  sASTypeInfo m_signature[2];
+  ReturnType (NativeClass::*m_method)(Arg1Type, Arg2Type) const;
+  
+public:
+  tASNativeObjectMethodConst(ReturnType (NativeClass::*method)(Arg1Type, Arg2Type) const) : m_method(method)
+  {
+    m_rtype = AvidaScript::TypeOf<ReturnType>();
+    m_signature[0] = AvidaScript::TypeOf<Arg1Type>();
+    m_signature[1] = AvidaScript::TypeOf<Arg2Type>();
+  }
+  
+  int GetArity() const { return 2; }
+  const sASTypeInfo& GetArgumentType(int arg) const { return m_signature[arg]; }
+  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>(), args[1].Get<Arg2Type>()));
+    return rvalue;
+  }
+};
+
+
 #endif




More information about the Avida-cvs mailing list