]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Script.hh
stream-based logging classes
[chaz/yoink] / src / Moof / Script.hh
index f67c62c20d27f67a09732650d4cdb4adb70b88c4..05b9da972829b28b3cd378ad092914a809974b9c 100644 (file)
@@ -38,6 +38,7 @@
  * more consistent API.
  */
 
+#include <iostream>
 #include <list>
 #include <map>
 #include <string>
@@ -79,7 +80,7 @@ public:
                THREAD                  = LUA_TTHREAD
        };
 
-       enum Status
+       enum Result
        {
                SUCCESS                 = 0,
                YIELD                   = LUA_YIELD,
@@ -99,14 +100,14 @@ public:
 
        /**
         * This is the most prominent abstraction on top of the standard Lua API.
-        * A Value object represents a value on the stack.  More specifically, it
+        * A Slot object represents a value on the stack.  More specifically, it
         * represents a position on the stack.  The distinction is only important
-        * when values are moved around on the stack or if the Value represents a
+        * when objects are moved around on the stack or if the Slot represents a
         * negative index on the stack (the value of which will change as things are
         * pushed onto and popped from the stack).
         */
 
-       struct Value
+       struct Slot
        {
                /**
                 * You have direct access to the index of the value on the stack being
@@ -117,13 +118,13 @@ public:
 
 
                /**
-                * A default-constructed Value is invalid until a valid Value is
-                * assigned to it.  The only method that should be called on such a
-                * Value is isValid(), otherwise chaos may ensue.  In this case, the
-                * Value will be invalid even if index is manually changed to a valid
-                * index.  You have to index the script itself to get a valid Value.
+                * A default-constructed Slot is invalid until a valid Slot is assigned
+                * to it.  The only method that should be called on such a Slot is
+                * isValid(), otherwise chaos may ensue.  In this case, the Slot will be
+                * invalid even if index is manually changed to a valid index.  You have
+                * to index the script itself to get a valid Slot.
                 */
-               Value(lua_State* s = 0, int i = 0) :
+               Slot(lua_State* s = 0, int i = 0) :
                        index(i),
                        mState(s) {}
 
@@ -134,7 +135,7 @@ public:
                 * copied into the new value object.
                 */
 
-               Value(const Value& copy) :
+               Slot(const Slot& copy) :
                        index(copy.getRealIndex()),
                        mState(copy.mState) {}
 
@@ -180,42 +181,42 @@ public:
                }
 
 
-               Value& requireBoolean()
+               Slot& requireBoolean()
                {
                        if (!isBoolean()) luaL_typerror(mState, index, "boolean");
                        return *this;
                }
-               Value& requireNumber()
+               Slot& requireNumber()
                {
                        if (!isNumber()) luaL_typerror(mState, index, "number");
                        return *this;
                }
-               Value& requireString()
+               Slot& requireString()
                {
                        if (!isString()) luaL_typerror(mState, index, "string");
                        return *this;
                }
-               Value& requireTable()
+               Slot& requireTable()
                {
                        if (!isTable()) luaL_typerror(mState, index, "table");
                        return *this;
                }
-               Value& requireFunction()
+               Slot& requireFunction()
                {
                        if (!isFunction()) luaL_typerror(mState, index, "function");
                        return *this;
                }
-               Value& requireData()
+               Slot& requireData()
                {
                        if (!isData()) luaL_typerror(mState, index, "data");
                        return *this;
                }
-               Value& requireNil()
+               Slot& requireNil()
                {
                        if (!isNil()) luaL_typerror(mState, index, "nil");
                        return *this;
                }
-               Value& requireThread()
+               Slot& requireThread()
                {
                        if (!isThread()) luaL_typerror(mState, index, "thread");
                        return *this;
@@ -267,27 +268,27 @@ public:
                }
 
 
-               bool operator == (const Value& rhs) const
+               bool operator == (const Slot& rhs) const
                {
                        return (bool)lua_equal(mState, index, rhs.index);
                }
-               bool operator != (const Value& rhs) const
+               bool operator != (const Slot& rhs) const
                {
                        return !(*this == rhs);
                }
-               bool operator < (const Value& rhs) const
+               bool operator < (const Slot& rhs) const
                {
                        return (bool)lua_lessthan(mState, index, rhs.index);
                }
-               bool operator <= (const Value& rhs) const
+               bool operator <= (const Slot& rhs) const
                {
                        return *this < rhs || *this == rhs;
                }
-               bool operator > (const Value& rhs) const
+               bool operator > (const Slot& rhs) const
                {
                        return !(*this <= rhs);
                }
-               bool operator >= (const Value& rhs) const
+               bool operator >= (const Slot& rhs) const
                {
                        return !(*this < rhs);
                }
@@ -296,7 +297,7 @@ public:
                        return (bool)lua_toboolean(mState, index);
                }
 
-               Value& operator = (const Value& rhs)
+               Slot& operator = (const Slot& rhs)
                {
                        rhs.pushCopy();
                        replaceWithTop();
@@ -367,7 +368,7 @@ public:
 
                        array.clear();
 
-                       Value   value(mState, -1);
+                       Slot    value(mState, -1);
                        int             realIndex = getRealIndex();
 
                        bool done = false;
@@ -392,8 +393,8 @@ public:
 
                        dictionary.clear();
 
-                       Value   key(mState, -2);
-                       Value   value(mState, -1);
+                       Slot    key(mState, -2);
+                       Slot    value(mState, -1);
                        int             realIndex = getRealIndex();
 
                        lua_pushnil(mState);
@@ -413,7 +414,6 @@ public:
                }
 
 
-
                /**
                 * Copy the value and push the copy to the stack.
                 */
@@ -498,7 +498,7 @@ public:
                if (mState) destroy();
                mState = luaL_newstate();
                lua_pushlightuserdata(mState, this);
-               lua_setfield(mState, LUA_REGISTRYINDEX, "_script_obj");
+               lua_setfield(mState, LUA_REGISTRYINDEX, "Script_hh_Object");
        }
 
 
@@ -571,14 +571,14 @@ public:
        }
 
 
-       Status doString(const std::string& commands)
+       Result doString(const std::string& commands)
        {
-               return (Status)luaL_dostring(mState, commands.c_str());
+               return (Result)luaL_dostring(mState, commands.c_str());
        }
 
-       Status doFile(const std::string& file)
+       Result doFile(const std::string& file)
        {
-               return (Status)luaL_dofile(mState, file.c_str());
+               return (Result)luaL_dofile(mState, file.c_str());
        }
 
 
@@ -596,14 +596,14 @@ public:
                lua_pushthread(mState);
        }
 
-       Status resume(int nargs)
+       Result resume(int nargs)
        {
-               return (Status)lua_resume(mState, nargs);
+               return (Result)lua_resume(mState, nargs);
        }
 
-       Status getStatus() const
+       Result getStatus() const
        {
-               return (Status)lua_status(mState);
+               return (Result)lua_status(mState);
        }
 
        int yield(int results)
@@ -637,24 +637,24 @@ public:
         * Get significant values.
         */
 
-       Value getGlobalTable() const
+       Slot getGlobalTable() const
        {
-               return Value(mState, GLOBALS);
+               return Slot(mState, GLOBALS);
        }
 
-       Value getRegistryTable() const
+       Slot getRegistryTable() const
        {
-               return Value(mState, REGISTRY);
+               return Slot(mState, REGISTRY);
        }
 
-       Value getEnvironmentTable() const
+       Slot getEnvironmentTable() const
        {
-               return Value(mState, ENVIRONMENT);
+               return Slot(mState, ENVIRONMENT);
        }
 
-       Value getTop() const
+       Slot getTop() const
        {
-               return Value(mState, lua_gettop(mState));
+               return Slot(mState, lua_gettop(mState));
        }
 
        /**
@@ -692,10 +692,10 @@ public:
 
 
        /**
-        * Concatenates the top-most n values on the stack.
+        * Concatenates the top-most n slots on the stack.
         */
 
-       void concat(int n)
+       void concat(int n = 2)
        {
                lua_concat(mState, n);
        }
@@ -761,14 +761,14 @@ public:
                lua_xmove(thread.mState, mState, n);
        }
 
-       Status pushCode(const std::string& filename)
+       Result pushCode(const std::string& filename)
        {
-               return (Status)luaL_loadfile(mState, filename.c_str());
+               return (Result)luaL_loadfile(mState, filename.c_str());
        }
 
-       Status pushCode(const std::string& name, const char* buffer, size_t size)
+       Result pushCode(const std::string& name, const char* buffer, size_t size)
        {
-               return (Status)luaL_loadbuffer(mState, buffer, size, name.c_str());
+               return (Result)luaL_loadbuffer(mState, buffer, size, name.c_str());
        }
 
        void* pushNewData(size_t size)
@@ -789,9 +789,9 @@ public:
         * is any number of return values, depending on the callee).
         */
 
-       Status call(int nargs, int nresults = LUA_MULTRET)
+       Result call(int nargs, int nresults = LUA_MULTRET)
        {
-               return (Status)lua_pcall(mState, nargs, nresults, 0);
+               return (Result)lua_pcall(mState, nargs, nresults, 0);
        }
 
 
@@ -806,12 +806,12 @@ public:
 
 
        /**
-        * Index into the stack to get a Value.
+        * Index into the stack to get a Slot.
         */
 
-       Value operator [] (int index) const
+       Slot operator [] (int index) const
        {
-               return Value(mState, index);
+               return Slot(mState, index);
        }
 
 
@@ -819,7 +819,7 @@ public:
         * Getting and setting fields of a table.
         */
 
-       void get(const std::string& field,  int index = GLOBALS) const
+       void pushField(const std::string& field, int index = GLOBALS) const
        {
                lua_getfield(mState, index, field.c_str());
        }
@@ -878,7 +878,7 @@ private:
                const Function* function = (const Function*)lua_touserdata(state,
                                lua_upvalueindex(1));
 
-               lua_getfield(state, LUA_REGISTRYINDEX, "_script_obj");
+               lua_getfield(state, LUA_REGISTRYINDEX, "Script_hh_Object");
                Script* script = (Script*)lua_touserdata(state, -1);
                lua_pop(state, 1);
 
@@ -896,6 +896,34 @@ private:
 };
 
 
+inline std::ostream& operator << (std::ostream& stream,
+               const Script::Slot& slot)
+{
+       if (slot.isString())
+       {
+               std::string str;
+               slot.get(str);
+               stream << str;
+       }
+       else if (slot.isBoolean())
+       {
+               if (slot) stream << "true";
+               else      stream << "false";
+       }
+       else if (slot.isNil())
+       {
+               stream << "nil";
+       }
+       else
+       {
+               stream << slot.getTypeName()
+                          << " (" << slot.getIdentifier() << ")" << std::endl;
+       }
+
+       return stream;
+}
+
+
 } // namespace Mf
 
 #endif // _MOOF_SCRIPT_HH_
This page took 0.029211 seconds and 4 git commands to generate.