OSDN Git Service

refactoring and the event system developing
authornothan <nothan87@gmail.com>
Tue, 15 Feb 2011 20:39:20 +0000 (05:39 +0900)
committernothan <nothan87@gmail.com>
Tue, 15 Feb 2011 20:39:20 +0000 (05:39 +0900)
src/list.hpp
src/livemlparser.cpp
src/livemlparser.h
src/livemlrunner.cpp
src/livemlrunner.h
test/test.xml

index e705cfc..d14fab3 100644 (file)
@@ -288,4 +288,16 @@ public:
   }
 };
 
+template <class T>
+T* list_prev(T *i)
+{
+  return (T*)((list_item<T>*)i)->prev;
+}
+
+template <class T>
+T* list_next(T *i)
+{
+  return (T*)((list_item<T>*)i)->next;
+}
+
 #endif
index 5b0dbbc..5f156f6 100644 (file)
@@ -307,4 +307,12 @@ PARAM_VAR(registerParameterType(paramVar))
   po->set(0, PARAM_VAR, "name");
   po = reg->addParameter();
   po->set(1, PARAM_NUMERIC);
+
+  registerEventType();
+}
+
+void LiveMLParser::registerEventType(void)
+{
+  eventTypeContainer.add("init");
+  eventTypeContainer.add("release");
 }
index 2dbff65..fdd78de 100644 (file)
@@ -171,6 +171,8 @@ public:
 
   static variable_size variableEncoder(const char*, void*);
 protected:
+  void registerEventType(void);
+
   static void* paramText(const char*, XMLParser&);
   static void* paramNumeric(const char*, XMLParser&);
   static void* paramVar(const char*, XMLParser&);
index fd34494..004311d 100644 (file)
@@ -84,7 +84,12 @@ const char* LiveMLRunner::variableStringDecoder(variable_size id, void *work)
 
 namespace lmlCommand
 {
-  bool action_close(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
+  LML_CMDRTN action(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
+  {
+    return LML_CMDRTN_NEXT;
+  }
+
+  LML_CMDRTN action_close(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
   {
 #ifdef DEBUG
     printf("action closed [%s]\n", param.getTag(0)->param.getString(0));
@@ -95,15 +100,10 @@ namespace lmlCommand
       obj.activeActionStack.close();
     }
 
-    return obj.activeActionStack.has();
-  }
-
-  bool event(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
-  {
-    return true;
+    return LML_CMDRTN_STOP | LML_CMDRTN_REACTION;
   }
 
-  bool event_close(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
+  LML_CMDRTN event_close(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
   {
 #ifdef DEBUG
     printf("event closed [%s]\n", param.getTag(0)->param.getString(0));
@@ -114,35 +114,35 @@ namespace lmlCommand
       obj.activeEventStack.close();
     }
 
-    return obj.activeEventStack.has();
+    return LML_CMDRTN_STOP | LML_CMDRTN_REACTION;
   }
 
-  bool print(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
+  LML_CMDRTN print(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
   {
     printf(param.getText(0, runner));
     printf("\n");
 
-    return true;
+    return LML_CMDRTN_NEXT;
   }
 
-  bool active(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
+  LML_CMDRTN active(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
   {
 #ifdef DEBUG
     printf("active [%s]\n", param.getText(0, runner));
 #endif
     runner.addActor(param.getText(0, runner));
 
-    return true;
+    return LML_CMDRTN_NEXT;
   }
 
-  bool repeat(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
+  LML_CMDRTN repeat(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
   {
     *obj.repeatCounter.push_back() = param.has(1) ? param.getInteger(1, runner) : -1;
 
-    return true;
+    return LML_CMDRTN_NEXT;
   }
 
-  bool repeat_close(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
+  LML_CMDRTN repeat_close(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
   {
     unsigned int *counter = obj.repeatCounter.back();
     if (*counter > 1)
@@ -152,35 +152,34 @@ namespace lmlCommand
     }
     else obj.repeatCounter.remove(counter);
 
-    return true;
+    return LML_CMDRTN_NEXT;
   }
 
-  bool rcontinue(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
+  LML_CMDRTN rcontinue(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
   {
     Tag **t = &obj.activeActionStack.current()->tag;
     *t = param.getTag(0)->param.getTag(0);
-    *t = (Tag*)((list_item<Tag>*)*t)->prev;
 
-    return true;
+    return LML_CMDRTN_NULL;
   }
 
-  bool rbreak(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
+  LML_CMDRTN rbreak(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
   {
-    if (obj.repeatCounter.back() == NULL) return false;
+    if (obj.repeatCounter.back() == NULL) return LML_CMDRTN_NEXT;
     (*obj.repeatCounter.back()) = 0;
 
     return rcontinue(param, obj, runner);
   }
 
-  bool wait(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
+  LML_CMDRTN wait(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
   {
     if (obj.waitCounter) obj.waitCounter--;
     else obj.waitCounter = param.getInteger(0, runner);
 
-    return obj.waitCounter == 0;
+    return obj.waitCounter ? LML_CMDRTN_STOP : LML_CMDRTN_NEXT;
   }
 
-  bool val(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
+  LML_CMDRTN val(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
   {
     variable_size id = *(variable_size*)param.get(0);
 #ifdef DEBUG
@@ -188,10 +187,10 @@ namespace lmlCommand
 #endif
     runner.getVariable(id)->setFixedFloat(id, param.getFixedFloat(1, runner));
 
-    return true;
+    return LML_CMDRTN_NEXT;
   }
 
-  bool actionRef(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
+  LML_CMDRTN actionRef(LMLParameter &param, LMLObject &obj, LiveMLRunner &runner)
   {
 #ifdef DEBUG
     printf("actionRef: %s\n", param.getText(0, runner));
@@ -207,7 +206,7 @@ namespace lmlCommand
       obj.setAction(ap);
     }
 
-    return true;
+    return LML_CMDRTN_STOP | LML_CMDRTN_NEXT | LML_CMDRTN_REACTION;
   }
 }
 
@@ -229,10 +228,10 @@ LiveMLRunner::LiveMLRunner(LiveMLParser *p)
   _scene.activeActionStack.list.allocator(&_actionAllocator);
   _scene.activeEventStack.list.allocator(&_actionAllocator);
 
-  registerCommand("action", NULL, lmlCommand::action_close);
   registerCommand("print", lmlCommand::print);
   registerCommand("active", lmlCommand::active);
-  registerCommand("event", lmlCommand::event);
+  registerCommand("action", lmlCommand::action, lmlCommand::action_close);
+  registerCommand("event", NULL, lmlCommand::event_close);
   registerCommand("repeat", lmlCommand::repeat, lmlCommand::repeat_close);
   registerCommand("continue", lmlCommand::rcontinue);
   registerCommand("break", lmlCommand::rbreak);
@@ -252,8 +251,8 @@ LiveMLRunner::~LiveMLRunner()
 
 bool LiveMLRunner::registerCommand(
   const char* name,
-  bool (*func)(LMLParameter&, LMLObject&, LiveMLRunner&),
-  bool (*closeFunc)(LMLParameter&, LMLObject&, LiveMLRunner&)
+  LML_CMDRTN (*func)(LMLParameter&, LMLObject&, LiveMLRunner&),
+  LML_CMDRTN (*closeFunc)(LMLParameter&, LMLObject&, LiveMLRunner&)
 )
 {
   tag_type id = parser->getTagTypeId(name);
@@ -265,75 +264,99 @@ bool LiveMLRunner::registerCommand(
   return true;
 }
 
-bool LiveMLRunner::runCommand(LMLObject &obj, Tag *tag)
+LML_CMDRTN LiveMLRunner::runCommand(LMLObject &obj, Tag *tag)
 {
-  Tag **activeTag = &obj.activeActionStack.current()->tag;
-  *activeTag = tag;
+  LML_CMDRTN (*func)(LMLParameter&, LMLObject&, LiveMLRunner&) = NULL;
+  TagType *rt = tag->type;
 
-  setCurrentObject(&obj);
-
-  while (tag != NULL)
+  // close a tag
+  if (rt == NULL)
   {
-    bool (*func)(LMLParameter&, LMLObject&, LiveMLRunner&) = NULL;
-    TagType *rt = tag->type;
-
-    if (rt == NULL)
-    {
-      if (tag->param.getTag(0)->type != NULL)
-      {
-        rt = tag->param.getTag(0)->type;
-        func = _commandFuncs[rt->id].closeFunc;
-      }
-    }
-    else
-    {
-      func = _commandFuncs[rt->id].func;
-    }
+    rt = tag->param.getTag(0)->type;
+    func = _commandFuncs[rt->id].closeFunc;
+#ifdef DEBUG
+    printf("close tag: %s\n", rt->getName().c_str());
+#endif
+  }
+  else
+  {
+    func = _commandFuncs[rt->id].func;
+  }
 
-    if (func != NULL)
-    {
+  LML_CMDRTN result = LML_CMDRTN_NEXT;
+  if (func != NULL)
+  {
 #ifdef DEBUG
-      printf("execute command: %s\n", rt->getName().c_str());
+    printf("execute command: %s\n", rt->getName().c_str());
 #endif
-      bool result = (*func)((LMLParameter&)tag->param, obj, *this);
-      text_decode_clear();
+    result = (*func)((LMLParameter&)tag->param, obj, *this);
+    text_decode_clear();
 #ifdef DEBUG
-      printf("finish command: %s\n", rt->getName().c_str());
+    printf("finish command: %s\n", rt->getName().c_str());
 #endif
-      if (result == false) return false;
-      activeTag = &obj.activeActionStack.current()->tag;
-    }
+  }
 
-    tag = *activeTag = (Tag*)((list_item<Tag>*)*activeTag)->next;
+  return result;
+}
+
+LML_CMDRTN LiveMLRunner::runAction(LMLObject &obj, ActiveAction &aa)
+{
+  LML_CMDRTN result = LML_CMDRTN_NULL;
+
+  while (aa.tag != NULL)
+  {
+    result = runCommand(obj, aa.tag);
+
+    if (result & LML_CMDRTN_NEXT)
+    {
+      aa.tag = list_next(aa.tag);
+    }
+    if (result & LML_CMDRTN_STOP)
+    {
+      break;
+    }
   }
 
-  return true;
+  return result;
 }
 
-bool LiveMLRunner::runObjectEvent(LMLObject &obj)
+bool LiveMLRunner::runActiveEvents(LMLObject &obj)
 {
   bindGlobalEvent(obj);
 
   while (obj.activeEventStack.has())
   {
-    runCommand(obj, obj.activeEventStack.current()->tag);
+    ActiveAction *aa = obj.activeEventStack.current();
+    LML_CMDRTN result = runAction(obj, *aa);
+    if (result & LML_CMDRTN_REACTION)
+    {
+      continue;
+    }
   }
 
   return obj.activeEventStack.has();
 }
 
-bool LiveMLRunner::runObjectAction(LMLObject &obj)
+bool LiveMLRunner::runActiveActions(LMLObject &obj)
 {
-  if (!obj.activeActionStack.has()) return false;
+  while (obj.activeActionStack.has())
+  {
+    ActiveAction *aa = obj.activeActionStack.current();
+    LML_CMDRTN result = runAction(obj, *aa);
 
-  runCommand(obj, obj.activeActionStack.current()->tag);
+    if ((result & LML_CMDRTN_STOP) && !(result & LML_CMDRTN_REACTION))
+    {
+      break;
+    }
+  }
 
-  return true;
+  return obj.activeActionStack.has();
 }
 
 bool LiveMLRunner::runObject(LMLObject &obj)
 {
-  return runObjectEvent(obj) | runObjectAction(obj);
+  setCurrentObject(&obj);
+  return runActiveEvents(obj) | runActiveActions(obj);
 }
 
 LMLSceneObject* LiveMLRunner::setScene(const char *name)
@@ -362,7 +385,8 @@ LMLActorObject* LiveMLRunner::addActor(const char *name)
   obj->activeActionStack.list.allocator(&_actionAllocator);
   obj->activeEventStack.list.allocator(&_actionAllocator);
   obj->repeatCounter.allocator(&_repeatAllocator);
-  obj->setAction((size_t)0);
+  obj->setAction((action_type)0);
+  obj->addEvent((event_type)0);
 #ifdef DEBUG
     printf("add actor: end\n");
 #endif
@@ -385,7 +409,7 @@ bool LiveMLRunner::run(void)
 #ifdef DEBUG
     printf("execute actor: start\n");
 #endif
-    LMLActorObject *actorNext = (LMLActorObject*)((list_item<LMLActorObject>*)actor)->next;
+    LMLActorObject *actorNext = list_next(actor);
     running |= runObject((LMLObject&)*actor);
     actor = actorNext;
 #ifdef DEBUG
index f6f06ec..671d7a4 100644 (file)
 #include "list.hpp"
 #include "bool_list.hpp"
 
+typedef int LML_CMDRTN;
+enum
+{
+  LML_CMDRTN_NULL = 0x00,
+  LML_CMDRTN_STOP = 0x01,
+  LML_CMDRTN_NEXT = 0x02,
+  LML_CMDRTN_REACTION = 0x04,
+};
+
 class LiveMLRunner;
 
 class LMLParameter : public Parameter
@@ -135,12 +144,12 @@ class ActiveStack
 public:
   list<T> list;
 
-  ActiveAction* current(void)
+  T* current(void)
   {
     return list.back();
   }
 
-  ActiveAction* add(void)
+  T* add(void)
   {
     return list.push_back();
   }
@@ -272,11 +281,10 @@ public:
     EventParser *event = parser->eventContainer[type];
     if (event == NULL) return false;
 
-    addEvent(event);
 #ifdef DEBUG
     printf("LMLObject::setEvent(%d) end.\n", type);
 #endif
-    return true;
+    return addEvent(event);
   }
 };
 
@@ -292,8 +300,8 @@ class LiveMLRunner
 {
   struct FuncPtr
   {
-    bool (*func)(LMLParameter&, LMLObject&, LiveMLRunner&);
-    bool (*closeFunc)(LMLParameter&, LMLObject&, LiveMLRunner&);
+    LML_CMDRTN (*func)(LMLParameter&, LMLObject&, LiveMLRunner&);
+    LML_CMDRTN (*closeFunc)(LMLParameter&, LMLObject&, LiveMLRunner&);
   };
 
   struct OnEvent
@@ -322,8 +330,8 @@ public:
 
   bool registerCommand(
     const char*,
-    bool (*)(LMLParameter&, LMLObject&, LiveMLRunner&),
-    bool (*)(LMLParameter&, LMLObject&, LiveMLRunner&) = NULL
+    LML_CMDRTN (*)(LMLParameter&, LMLObject&, LiveMLRunner&),
+    LML_CMDRTN (*)(LMLParameter&, LMLObject&, LiveMLRunner&) = NULL
   );
 
   void setMaxActors(size_t);
@@ -339,9 +347,10 @@ public:
   void onEvent(event_type id) { _onEventList.push_back()->id = id; }
   void bindGlobalEvent(LMLObject&);
 
-  bool runCommand(LMLObject&, Tag*);
-  bool runObjectEvent(LMLObject&);
-  bool runObjectAction(LMLObject&);
+  LML_CMDRTN runCommand(LMLObject&, Tag*);
+  LML_CMDRTN runAction(LMLObject&, ActiveAction&);
+  bool runActiveEvents(LMLObject&);
+  bool runActiveActions(LMLObject&);
   bool runObject(LMLObject&);
   bool run(void);
   LMLVariable* getVariable(variable_size);
index b04b9a8..666b0bd 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 
-<liveml xmlns="http://tsuioku-denrai.xrea.jp/common/liveml">
+<liveml xmlns="http://t-denrai.net/common/liveml">
   <scene name="scene1">
     <event type="init">
       <load name="image1" type="image">path/to</load>
   </action>
 
   <actor name="actor">
+    <event type="init">
+      <print>call[init]</print>
+    </event>
     <action name="action">
-      <val name="a">1</val>
-      <val name="m.a">10</val>
-      <repeat count="$m.a">
-        <print>actor roop</print>
+      <val name="a">10</val>
+      <repeat count="$a">
+        <print>roop[actor]</print>
         <wait>1</wait>
       </repeat>
     </action>