"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/src/include/Diluculum/LuaFunction.hpp" (12 May 2020, 9511 Bytes) of package /windows/www/highlight-3.57-x64.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /******************************************************************************\
    2 * LuaFunction.hpp                                                              *
    3 * A C++ equivalent of a Lua function.                                          *
    4 *                                                                              *
    5 *                                                                              *
    6 * Copyright (C) 2005-2013 by Leandro Motta Barros.                             *
    7 *                                                                              *
    8 * Permission is hereby granted, free of charge, to any person obtaining a copy *
    9 * of this software and associated documentation files (the "Software"), to     *
   10 * deal in the Software without restriction, including without limitation the   *
   11 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or  *
   12 * sell copies of the Software, and to permit persons to whom the Software is   *
   13 * furnished to do so, subject to the following conditions:                     *
   14 *                                                                              *
   15 * The above copyright notice and this permission notice shall be included in   *
   16 * all copies or substantial portions of the Software.                          *
   17 *                                                                              *
   18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR   *
   19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,     *
   20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE *
   21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER       *
   22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      *
   23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS *
   24 * IN THE SOFTWARE.                                                             *
   25 \******************************************************************************/
   26 
   27 #ifndef _DILUCULUM_LUA_FUNCTION_HPP_
   28 #define _DILUCULUM_LUA_FUNCTION_HPP_
   29 
   30 #include <string>
   31 #include <boost/scoped_array.hpp>
   32 #include <lua.hpp>
   33 #include <Diluculum/Types.hpp>
   34 
   35 
   36 namespace Diluculum
   37 {
   38    /** A C++ equivalent of a Lua function. This can store both C functions
   39     *  exported (or exportable) to Lua and pure Lua functions. In the first
   40     *  case, it stores a \c lua_CFunction. In the second case it stores data
   41     *  that would normally be in the Lua-space as a function implemented in Lua
   42     *  (one can think of it as a buffer that stores Lua bytecode that a Lua
   43     *  interpreter can understand as a function implemented in Lua). This is
   44     *  used as the value of a \c LuaValue when it holds a Lua function.
   45     *  @note A \c LuaFunction does not have any reference to a Lua
   46     *        interpreter. Thus, it does not make sense to call a \c LuaFunction
   47     *        object directly. To call a \c LuaFunction, use \c LuaState::call().
   48     */
   49    class LuaFunction
   50    {
   51       public:
   52 
   53          /** Constructs a \c LuaFunction from Lua source code (a Lua chunk). The
   54           *  default value (a "dummy" chunk, that doesn't return anything) can
   55           *  be useful for moments in which a default constructable \c
   56           *  LuaFunction is desired.
   57           *  @param luaChunk The Lua source of the chunk.
   58           *  @note The chunk can be seen as a vararg function. So, if one wants,
   59           *        for instance, a chunk corresponding to a function that
   60           *        returns its first parameter doubled, the following chunk can
   61           *        be used: <tt>"local p = {...}; return p[1]*2"</tt>.
   62           */
   63          LuaFunction (const::std::string& luaChunk = "");
   64 
   65          /** Constructs a \c LuaFunction from Lua bytecode. Typically, this
   66           *  constructor is not called directly by end users. Instead, end users
   67           *  use <tt>LuaState::operator[]</tt> to get a \c LuaVariable
   68           *  containing a \c LuaFunction value, which can be accessed via
   69           *  <tt>LuaVariable::value()</tt>.
   70           *  @param data A pointer to the beginning of the memory area
   71           *         containing Lua bytecode.
   72           *  @param size The number of bytecode bytes to read.
   73           */
   74          LuaFunction (const void* data, size_t size);
   75 
   76          /** Constructs a \c LuaFunction from a \c lua_CFunction (a Lua function
   77           * implemented in C).
   78           */
   79          LuaFunction (lua_CFunction func);
   80 
   81          /** The copy constructor. The newly constructed \c LuaFunction will
   82           *  have its own block of memory, with the same contents as the \c
   83           *  other. In other words, this constructor allocates and copies
   84           *  memory.
   85           */
   86          LuaFunction (const LuaFunction& other);
   87 
   88          /// Destroys the \c LuaFunction, freeing all the resources owned by it.
   89          ~LuaFunction() { destroyObjectAtData(); }
   90 
   91          /** Assigns a \c LuaFunction to this one. The memory currently
   92           *  allocated for \c this will be freed, new memory will be allocated,
   93           *  and the data stored in \c rhs will be copied to \c this.
   94           */
   95          const LuaFunction& operator= (const LuaFunction& rhs);
   96 
   97          /**
   98           * Checks if this \c LuaFunction holds a C function (instead of a
   99           * "pure" Lua function).
  100           */
  101          bool isCFunction() const { return functionType_ == LUA_C_FUNCTION; }
  102 
  103          /**
  104           * Returns the value of this \c LuaFunction as a C function.
  105           * @note Will \c assert() if this \c LuaFunction does not hold a C
  106           *       function.
  107           */
  108          lua_CFunction getCFunction() const;
  109 
  110          /** Returns the size, in bytes, of the data stored in this
  111           *  \c LuaFunction.
  112           */
  113          size_t getSize() const { return size_; }
  114 
  115          /// Returns a pointer to the data stored in this \c LuaFunction.
  116          void* getData() {
  117             assert(functionType_ == LUA_LUA_FUNCTION
  118                    && "Called LuaFunction::getData() for a non-Lua function.");
  119 
  120             return data_.typeLuaFunction;
  121          }
  122 
  123          /** Returns a \c const pointer to the data stored in this
  124           *  \c LuaFunction.
  125           */
  126          const void* getData() const {
  127             assert(functionType_ == LUA_LUA_FUNCTION
  128                    && "Called LuaFunction::getData() for a non-Lua function.");
  129 
  130             return data_.typeLuaFunction;
  131          }
  132 
  133          /// Sets the data stored in this \c LuaFunction.
  134          void setData(void* data, size_t size);
  135 
  136          /// Gets the "reader flag".
  137          bool getReaderFlag() const { return readerFlag_; }
  138 
  139          /// Sets the "reader flag" to a given value.
  140          void setReaderFlag(bool f) { readerFlag_ = f; }
  141 
  142          /** The "greater than" operator for \c LuaFunction.
  143           *  @note Given two <tt>LuaFunction</tt>s, the decision on which one is
  144           *        greater is somewhat arbitrary. Here, the function with larger
  145           *        size is considered greater. If both are equal, the decision
  146           *        is based on the contents of the stored data.
  147           */
  148          bool operator> (const LuaFunction& rhs) const;
  149 
  150          /** The "less than" operator for \c LuaFunction.
  151           *  @note Given two <tt>LuaFunction</tt>s, the decision on which one is
  152           *        lesser is somewhat arbitrary. The criterion is similar to the
  153           *        described for the "greater than" operator.
  154           */
  155          bool operator< (const LuaFunction& rhs) const;
  156 
  157          /** The "equal to" operator for \c LuaFunction.
  158           *  @note Two <tt>LuaFunctions</tt>s are considered equal if the data
  159           *        they store have the same size and the same contents.
  160           *  @todo In Lua, a function is considered equal only to itself. Things
  161           *        are different here. Does this have a reason to not be like in
  162           *        Lua?
  163           */
  164          bool operator== (const LuaFunction& rhs) const;
  165 
  166          /// The "different than" operator for \c LuaFunction.
  167          bool operator!= (const LuaFunction& rhs) const;
  168 
  169       private:
  170 
  171          /** Destroys the object allocated at the \c data_ member, freeing its
  172           *  resources.
  173           */
  174          void destroyObjectAtData();
  175 
  176          /** The possible types of functions than possibly be stored in a
  177           *  \c LuaFunction.
  178           */
  179          enum TypeOfFunction
  180          {
  181             /// A Lua function implemented in C.
  182             LUA_C_FUNCTION,
  183 
  184             /// A Lua function implemented in Lua.
  185             LUA_LUA_FUNCTION
  186          };
  187 
  188          /// The type of function stored in this \c LuaFunction.
  189          TypeOfFunction functionType_;
  190 
  191          /// The number of bytes stored "in" \c data_.
  192          size_t size_;
  193 
  194          union PossibleTypes
  195          {
  196                lua_CFunction typeCFunction;
  197                char* typeLuaFunction;
  198          };
  199 
  200          /** This stores the actual data of this \c LuaFunction.
  201           *  <p>Implementation details: The values are allocated here using
  202           *  placement new, with destructors explicitly called whenever
  203           *  necessary.
  204           */
  205          PossibleTypes data_;
  206 
  207          /** A flag used when reading the bytecode data, in calls to \c
  208           *  lua_load() and its \c lua_Reader.
  209           */
  210          bool readerFlag_;
  211    };
  212 
  213 } // namespace Diluculum
  214 
  215 #endif // _DILUCULUM_LUA_FUNCTION_HPP_