"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/src/include/Diluculum/LuaUserData.hpp" (12 May 2020, 5764 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 * LuaUserData.hpp                                                              *
    3 * A C++ equivalent of a Lua userdata.                                          *
    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_USER_DATA_HPP_
   28 #define _DILUCULUM_LUA_USER_DATA_HPP_
   29 
   30 #include <boost/scoped_array.hpp>
   31 #include <lua.hpp>
   32 #include <Diluculum/Types.hpp>
   33 
   34 
   35 namespace Diluculum
   36 {
   37    /** A C++ equivalent of a Lua userdata. Or, perhaps more precisely: something
   38     *  that lives in the C++-space, and that stores data that would normally be
   39     *  in the Lua-space as userdata. This is used as the value of a \c LuaValue
   40     *  when it holds userdata.
   41     *  @note A \c LuaUserData has no counterpart in a Lua state. It is simply a
   42     *        block of memory that can be (via a \c LuaValue and a \c LuaState)
   43     *        stored in or read from a userdata in Lua state.
   44     */
   45    class LuaUserData
   46    {
   47       public:
   48 
   49          /** Constructs a \c LuaUserData, allocating \c size bytes of memory.
   50           *  This memory is initially filled with garbage. And this memory is
   51           *  automatically freed when the \c LuaUserData is destroyed.
   52           */
   53          explicit LuaUserData (size_t size);
   54 
   55          /** The copy constructor. The newly constructed \c LuaUserData will
   56           *  have its own block of memory, with the same contents as the
   57           *  \c other. In other words, this constructor allocates and copies
   58           *  memory.
   59           */
   60          LuaUserData (const LuaUserData& other);
   61 
   62          /** Assigns a \c LuaUserData to this one. The memory currently
   63           *  allocated for \c this will be freed, new memory will be allocated,
   64           *  and the data stored in \c rhs will be copied to \c this.
   65           */
   66          const LuaUserData& operator= (const LuaUserData& rhs);
   67 
   68          /** Returns the size, in bytes, of the data stored in this
   69           *  \c LuaUserData.
   70           */
   71          size_t getSize() const { return size_; }
   72 
   73          /// Returns a pointer to the data stored in this \c LuaUserData.
   74          void* getData() { return data_.get(); }
   75 
   76          /** Returns a \c const pointer to the data stored in this
   77           *  \c LuaUserData.
   78           */
   79          const void* getData() const { return data_.get(); }
   80 
   81          /** The "greater than" operator for \c LuaUserData.
   82           *  @note Given two <tt>LuaUserData</tt>s, the decision on which one is
   83           *        greater is somewhat arbitrary. Here, the userdata with larger
   84           *        \c size() is considered greater. If both are equal, the
   85           *        decision is based on the contents of the stored data.
   86           */
   87          bool operator> (const LuaUserData& rhs) const;
   88 
   89          /** The "less than" operator for \c LuaUserData.
   90           *  @note Given two <tt>LuaUserData</tt>s, the decision on which one is
   91           *        lesser is somewhat arbitrary. The criterion is similar to the
   92           *        described for the "greater than" operator.
   93           */
   94          bool operator< (const LuaUserData& rhs) const;
   95 
   96          /** The "equal to" operator for \c LuaUserData.
   97           *  @note Two <tt>LuaUserData</tt>s are considered equal if the data
   98           *        they store have the same size and the same contents.
   99           *  @todo In Lua, a userdata is considered equal only to itself. Things
  100           *        are different here. Does this have a reason to not be like in
  101           *        Lua?
  102           */
  103          bool operator== (const LuaUserData& rhs) const;
  104 
  105          /// The "different than" operator for \c LuaUserData.
  106          bool operator!= (const LuaUserData& rhs) const;
  107 
  108       private:
  109          /// The number of bytes stored "in" \c data_.
  110          size_t size_;
  111 
  112          /// A (smart) pointer to the data owned by this \c LuaUserData.
  113          boost::scoped_array<char> data_;
  114    };
  115 
  116 } // namespace Diluculum
  117 
  118 #endif // _DILUCULUM_LUA_USER_DATA_HPP_