Skip to content
Permalink
Browse files

variant constructor parameters

  • Loading branch information
megagrump committed Nov 28, 2018
1 parent d239d66 commit 2196850dc4a8db934fdc4d4891d104210c933332
Showing with 63 additions and 50 deletions.
  1. +10 −5 BlobReader.moon
  2. +7 −4 BlobWriter.moon
  3. +16 −15 doc/classes/BlobReader.html
  4. +13 −12 doc/classes/BlobWriter.html
  5. +1 −1 doc/index.html
  6. +3 −3 doc/ldoc.css
  7. +3 −3 ldoc.css
  8. +3 −1 lua/BlobReader.lua
  9. +3 −1 lua/BlobWriter.lua
  10. +4 −5 test/test.moon
@@ -10,16 +10,18 @@ class BlobReader
--- Creates a new BlobReader instance.
--
-- @tparam[opt] string|cdata data Source data
-- @tparam[opt] string byteOrder The byte order of the data (required when data is a `cdata` pointer)
-- @tparam[opt] number|string sizeOrByteOrder Size of `data` (required when data is a `cdata` pointer) or
-- the byte order of the data
--
-- Use `le` or `<` for little endian; `be` or `>` for big endian; `host`, `=` or `nil` to use the
-- **Byte order**: Use `le` or `<` for little endian; `be` or `>` for big endian; `host`, `=` or `nil` to use the
-- host's native byte order (default)
-- @tparam[opt] number size When data is of type `cdata`, you need to pass the size manually
--
-- @tparam[opt] number size Size of `data` (required when `data` is a `cdata` pointer)
-- @treturn BlobReader A new BlobReader instance.
-- @usage reader = BlobReader(data)
-- @usage reader = BlobReader(data, '>')
-- @usage reader = BlobReader(cdata, nil, 1000)
new: (data, byteOrder, size) =>
-- @usage reader = BlobReader(cdata, 1000)
new: (data, sizeOrByteOrder, size) =>
@_native = ffi.new[[
union {
int8_t s8[8];
@@ -34,6 +36,9 @@ class BlobReader
double f64;
}
]]

byteOrder = type(sizeOrByteOrder) == 'string' and sizeOrByteOrder or nil
size = type(sizeOrByteOrder) == 'number' and sizeOrByteOrder or size
@reset(data, size)
@setByteOrder(byteOrder)

@@ -9,17 +9,18 @@ local _tags, _getTag, _taggedReaders, _taggedWriters, _packMap, _unpackMap, _arr
class BlobWriter
--- Creates a new BlobWriter instance.
--
-- @tparam[opt] string byteOrder Byte order
-- @tparam[opt] number|string sizeOrByteOrder Size or byte order
--
-- Use `le` or `<` for little endian; `be` or `>` for big endian; `host`, `=` or `nil` to use the
-- **Byte order**: Use `le` or `<` for little endian; `be` or `>` for big endian; `host`, `=` or `nil` to use the
-- host's native byteOrder (default)
--
-- @tparam[opt] number size The initial size of the blob in bytes. Default is 1024. Will grow automatically when needed.
-- @tparam[opt] number size The initial size of the blob in bytes. Default is 1024. Will grow automatically when
-- required.
-- @treturn BlobWriter A new BlobWriter instance.
-- @usage writer = BlobWriter!
-- @usage writer = BlobWriter('<', 1000)
-- @see clear
new: (byteOrder, size) =>
new: (sizeOrByteOrder, size) =>
@_native = ffi.new[[
union {
int8_t s8[8];
@@ -35,6 +36,8 @@ class BlobWriter
}
]]

byteOrder = type(sizeOrByteOrder) == 'string' and sizeOrByteOrder or nil
size = type(sizeOrByteOrder) == 'number' and sizeOrByteOrder or size
@_length, @_size = 0, 0
@setByteOrder(byteOrder)
@_allocate(size or 1024)
@@ -56,7 +56,7 @@ <h1>Module <code>BlobReader</code></h1>
<h2><a href="#Functions">Functions</a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#new">new ([data[, byteOrder[, size]]])</a></td>
<td class="name" nowrap><a href="#new">new ([data[, sizeOrByteOrder[, size]]])</a></td>
<td class="summary">Creates a new BlobReader instance.</td>
</tr>
<tr>
@@ -178,7 +178,7 @@ <h2 class="section-header "><a name="Functions"></a>Functions</h2>
<dl class="function">
<dt>
<a name = "new"></a>
<strong>new ([data[, byteOrder[, size]]])</strong>
<strong>new ([data[, sizeOrByteOrder[, size]]])</strong>
</dt>
<dd>
Creates a new BlobReader instance.
@@ -188,21 +188,22 @@ <h3>Parameters:</h3>
<ul>
<li><span class="parameter">data</span>
<span class="types"><a class="type" href="../classes/BlobReader.html#string">string</a> or <span class="type">cdata</span></span>
<em>optional</em>
Source data
(<em>optional</em>)
</li>
<li><span class="parameter">byteOrder</span>
<span class="types"><a class="type" href="../classes/BlobReader.html#string">string</a></span>
The byte order of the data (required when data is a <code>cdata</code> pointer)</p>
<li><span class="parameter">sizeOrByteOrder</span>
<span class="types"><a class="type" href="../classes/BlobReader.html#number">number</a> or <a class="type" href="../classes/BlobReader.html#string">string</a></span>
<em>optional</em>
Size of <code>data</code> (required when data is a <code>cdata</code> pointer) or
the byte order of the data</p>

<p> Use <code>le</code> or <code>&lt;</code> for little endian; <code>be</code> or <code>&gt;</code> for big endian; <code>host</code>, <code>=</code> or <code>nil</code> to use the
<p> <strong>Byte order</strong>: Use <code>le</code> or <code>&lt;</code> for little endian; <code>be</code> or <code>&gt;</code> for big endian; <code>host</code>, <code>=</code> or <code>nil</code> to use the
host's native byte order (default)
(<em>optional</em>)
</li>
<li><span class="parameter">size</span>
<span class="types"><a class="type" href="../classes/BlobReader.html#number">number</a></span>
When data is of type <code>cdata</code>, you need to pass the size manually
(<em>optional</em>)
<em>optional</em>
Size of <code>data</code> (required when <code>data</code> is a <code>cdata</code> pointer)
</li>
</ul>

@@ -219,7 +220,7 @@ <h3>Usage:</h3>
<ul>
<li><pre class="example">reader = BlobReader(data)</pre></li>
<li><pre class="example">reader = BlobReader(data, <span class="string">'&gt;'</span>)</pre></li>
<li><pre class="example">reader = BlobReader(cdata, <span class="keyword">nil</span>, <span class="number">1000</span>)</pre></li>
<li><pre class="example">reader = BlobReader(cdata, <span class="number">1000</span>)</pre></li>
</ul>

</dd>
@@ -333,8 +334,8 @@ <h3>Parameters:</h3>
<ul>
<li><span class="parameter">result</span>
<span class="types"><a class="type" href="../classes/BlobReader.html#table">table</a></span>
<em>optional</em>
Table to put values in
(<em>optional</em>)
</li>
</ul>

@@ -703,8 +704,8 @@ <h3>Parameters:</h3>
</li>
<li><span class="parameter">result</span>
<span class="types"><a class="type" href="../classes/BlobReader.html#table">table</a></span>
<em>optional</em>
Table to put the values in
(<em>optional</em>)
</li>
</ul>

@@ -829,8 +830,8 @@ <h3>Parameters:</h3>
</li>
<li><span class="parameter">size</span>
<span class="types"><a class="type" href="../classes/BlobReader.html#number">number</a></span>
<em>optional</em>
The length of the data (only required when <code>data</code> is a <code>cdata</code> pointer)
(<em>optional</em>)
</li>
</ul>

@@ -902,7 +903,7 @@ <h3>Returns:</h3>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.6</a></i>
<i style="float:right;">Last updated 2018-11-13 11:50:22 </i>
<i style="float:right;">Last updated 2018-11-28 13:45:43 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
@@ -56,7 +56,7 @@ <h1>Module <code>BlobWriter</code></h1>
<h2><a href="#Functions">Functions</a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#new">new ([byteOrder[, size]])</a></td>
<td class="name" nowrap><a href="#new">new ([sizeOrByteOrder[, size]])</a></td>
<td class="summary">Creates a new BlobWriter instance.</td>
</tr>
<tr>
@@ -182,26 +182,27 @@ <h2 class="section-header "><a name="Functions"></a>Functions</h2>
<dl class="function">
<dt>
<a name = "new"></a>
<strong>new ([byteOrder[, size]])</strong>
<strong>new ([sizeOrByteOrder[, size]])</strong>
</dt>
<dd>
Creates a new BlobWriter instance.


<h3>Parameters:</h3>
<ul>
<li><span class="parameter">byteOrder</span>
<span class="types"><a class="type" href="../classes/BlobWriter.html#string">string</a></span>
Byte order</p>
<li><span class="parameter">sizeOrByteOrder</span>
<span class="types"><a class="type" href="../classes/BlobWriter.html#number">number</a> or <a class="type" href="../classes/BlobWriter.html#string">string</a></span>
<em>optional</em>
Size or byte order</p>

<p> Use <code>le</code> or <code>&lt;</code> for little endian; <code>be</code> or <code>&gt;</code> for big endian; <code>host</code>, <code>=</code> or <code>nil</code> to use the
<p> <strong>Byte order</strong>: Use <code>le</code> or <code>&lt;</code> for little endian; <code>be</code> or <code>&gt;</code> for big endian; <code>host</code>, <code>=</code> or <code>nil</code> to use the
host's native byteOrder (default)
(<em>optional</em>)
</li>
<li><span class="parameter">size</span>
<span class="types"><a class="type" href="../classes/BlobWriter.html#number">number</a></span>
The initial size of the blob in bytes. Default is 1024. Will grow automatically when needed.
(<em>optional</em>)
<em>optional</em>
The initial size of the blob in bytes. Default is 1024. Will grow automatically when
required.
</li>
</ul>

@@ -730,8 +731,8 @@ <h3>Parameters:</h3>
</li>
<li><span class="parameter">length</span>
<span class="types"><a class="type" href="../classes/BlobWriter.html#number">number</a></span>
<em>optional</em>
Length of data (not required when data is a string)
(<em>optional</em>)
</li>
</ul>

@@ -940,9 +941,9 @@ <h3>Parameters:</h3>
<ul>
<li><span class="parameter">size</span>
<span class="types"><a class="type" href="../classes/BlobWriter.html#number">number</a></span>
<em>optional</em>
Set the writer buffer size to this value. If <code>nil</code>, the currently allocated buffer
is reused.
(<em>optional</em>)
</li>
</ul>

@@ -1108,7 +1109,7 @@ <h3>Returns:</h3>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.6</a></i>
<i style="float:right;">Last updated 2018-11-13 11:50:22 </i>
<i style="float:right;">Last updated 2018-11-28 13:45:43 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
@@ -57,7 +57,7 @@ <h2>Classes</h2>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.6</a></i>
<i style="float:right;">Last updated 2018-11-13 11:50:22 </i>
<i style="float:right;">Last updated 2018-11-28 13:45:43 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
@@ -70,19 +70,19 @@ body {
background-color: #ffffff; margin: 0px;
}

code, tt, span.parameter { font-family: monospace; font-size: 1.1em; background-color: #f0f0f0; padding: 0px 4px; }
code, tt, span.parameter { font-family: monospace; font-size: 1.1em; border: solid 1px #ccc; background-color: #eee; padding: 1px 0.5em; }
span.parameter:after { content:""; }
span.types:before { content:"("; }
span.types:after { content:"):"; }
.type { font-weight: bold; font-style:italic }

body, p, td, th { font-size: .95em; line-height: 1.2em;}
body, p, td, th { font-size: .95em; line-height: 1.25em;}

p, ul { margin: 10px 0 0 0px;}

strong { font-weight: bold;}

em { font-style: italic; font-size: 0.75em; }
em { font-size: 0.75em; border: solid 1px #8ce; background-color: #7cf; padding: 1px 0.5em; }

h1 {
font-size: 1.5em;
@@ -70,19 +70,19 @@ body {
background-color: #ffffff; margin: 0px;
}

code, tt, span.parameter { font-family: monospace; font-size: 1.1em; background-color: #f0f0f0; padding: 0px 4px; }
code, tt, span.parameter { font-family: monospace; font-size: 1.1em; border: solid 1px #ccc; background-color: #eee; padding: 1px 0.5em; }
span.parameter:after { content:""; }
span.types:before { content:"("; }
span.types:after { content:"):"; }
.type { font-weight: bold; font-style:italic }

body, p, td, th { font-size: .95em; line-height: 1.2em;}
body, p, td, th { font-size: .95em; line-height: 1.25em;}

p, ul { margin: 10px 0 0 0px;}

strong { font-weight: bold;}

em { font-style: italic; font-size: 0.75em; }
em { font-size: 0.75em; border: solid 1px #8ce; background-color: #7cf; padding: 1px 0.5em; }

h1 {
font-size: 1.5em;
@@ -268,7 +268,7 @@ do
}
_base_0.__index = _base_0
_class_0 = setmetatable({
__init = function(self, data, byteOrder, size)
__init = function(self, data, sizeOrByteOrder, size)
self._native = ffi.new([[ union {
int8_t s8[8];
uint8_t u8[8];
@@ -282,6 +282,8 @@ do
double f64;
}
]])
local byteOrder = type(sizeOrByteOrder) == 'string' and sizeOrByteOrder or nil
size = type(sizeOrByteOrder) == 'number' and sizeOrByteOrder or size
self:reset(data, size)
return self:setByteOrder(byteOrder)
end,
@@ -289,7 +289,7 @@ do
}
_base_0.__index = _base_0
_class_0 = setmetatable({
__init = function(self, byteOrder, size)
__init = function(self, sizeOrByteOrder, size)
self._native = ffi.new([[ union {
int8_t s8[8];
uint8_t u8[8];
@@ -303,6 +303,8 @@ do
double f64;
}
]])
local byteOrder = type(sizeOrByteOrder) == 'string' and sizeOrByteOrder or nil
size = type(sizeOrByteOrder) == 'number' and sizeOrByteOrder or size
self._length, self._size = 0, 0
self:setByteOrder(byteOrder)
return self:_allocate(size or 1024)
@@ -13,13 +13,12 @@ equals, almost, isTrue, isError = lu.assertEquals, lu.assertAlmostEquals, lu.ass
export *

test_Error = ->
isError('Invalid data type', BlobReader, 1)
isError('Invalid data type', BlobReader, true)
isError('Invalid data type', BlobReader, {})
isError('Invalid data type', BlobReader, ->)

test_Writer_tiny = ->
b = BlobWriter(nil, 1)
b = BlobWriter(1)
b\u32(0xbeefc0de)
isTrue(b\length! <= b\size!)

@@ -39,7 +38,7 @@ test_CData = ->
data = ffi.new('uint8_t[256]')
data[i] = i for i = 0, 255

blob = BlobWriter(nil, 257)
blob = BlobWriter(257)
blob\raw(data, 256)
blob\u8(123)

@@ -50,7 +49,7 @@ test_CData = ->
equals(reader\position!, blob\length!)

data = ffi.cast('uint8_t*', data)
blob2 = BlobReader(data, nil, 256)
blob2 = BlobReader(data, 256)
equals(blob2\size!, 256)
equals(blob2\u8!, i) for i = 0, 255

@@ -591,7 +590,7 @@ test_xxxLastCheckGlobals = ->
isTrue(_globals[k] ~= nil)

test_Writer_clear = ->
b = BlobWriter(nil, 1)
b = BlobWriter(1)
b\u8(1)
b\clear!
equals(b\size!, 1)

0 comments on commit 2196850

Please sign in to comment.
You can’t perform that action at this time.