LCOV - code coverage report
Current view: top level - as2js/lib - node_param.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 27 27 100.0 %
Date: 2014-08-10 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* node_param.cpp -- written by Alexis WILKE for Made to Order Software Corp. (c) 2005-2014 */
       2             : 
       3             : /*
       4             : 
       5             : Copyright (c) 2005-2014 Made to Order Software Corp.
       6             : 
       7             : http://snapwebsites.org/project/as2js
       8             : 
       9             : Permission is hereby granted, free of charge, to any
      10             : person obtaining a copy of this software and
      11             : associated documentation files (the "Software"), to
      12             : deal in the Software without restriction, including
      13             : without limitation the rights to use, copy, modify,
      14             : merge, publish, distribute, sublicense, and/or sell
      15             : copies of the Software, and to permit persons to whom
      16             : the Software is furnished to do so, subject to the
      17             : following conditions:
      18             : 
      19             : The above copyright notice and this permission notice
      20             : shall be included in all copies or substantial
      21             : portions of the Software.
      22             : 
      23             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
      24             : ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
      25             : LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
      26             : FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
      27             : EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      28             : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
      29             : WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      30             : ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      31             : SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      32             : SOFTWARE.
      33             : 
      34             : */
      35             : 
      36             : #include    "as2js/node.h"
      37             : 
      38             : #include    "as2js/exceptions.h"
      39             : 
      40             : 
      41             : namespace as2js
      42             : {
      43             : 
      44             : 
      45             : /**********************************************************************/
      46             : /**********************************************************************/
      47             : /***  NODE PARAM  *****************************************************/
      48             : /**********************************************************************/
      49             : /**********************************************************************/
      50             : 
      51             : 
      52             : /** \brief Define the size of the parameter index and depth vectors.
      53             :  *
      54             :  * This function defines the size of the depth and index parameter
      55             :  * vectors. Until this function is called, trying to set a depth
      56             :  * or index parameter will fail.
      57             :  *
      58             :  * Also, the function cannot be called more than once and the size
      59             :  * parameter cannot be zero.
      60             :  *
      61             :  * \param[in] size  The number of parameters (size > 0 must be true).
      62             :  */
      63         174 : void Node::set_param_size(size_t size)
      64             : {
      65         174 :     if(f_type != node_t::NODE_PARAM_MATCH)
      66             :     {
      67         168 :         throw exception_internal_error("INTERNAL ERROR: set_param_depth() called with a node other than a NODE_PARAM_MATCH.");
      68             :     }
      69           6 :     if(f_param_depth.size() != 0)
      70             :     {
      71           2 :         throw exception_internal_error("INTERNAL ERROR: set_param_depth() called twice.");
      72             :     }
      73           4 :     if(size == 0)
      74             :     {
      75           2 :         throw exception_internal_error("INTERNAL ERROR: set_param_depth() was called with a size of zero.");
      76             :     }
      77           2 :     f_param_depth.resize(size);
      78           2 :     f_param_index.resize(size);
      79           2 : }
      80             : 
      81             : 
      82             : /** \brief Return the size of the parameter index and depth vectors.
      83             :  *
      84             :  * This function returns zero unless the set_param_size() was successfully
      85             :  * called with a valid size.
      86             :  *
      87             :  * \return The current size of the parameter index and depth vectors.
      88             :  */
      89      700992 : size_t Node::get_param_size() const
      90             : {
      91      700992 :     return f_param_depth.size();
      92             : }
      93             : 
      94             : 
      95             : /** \brief Get the depth at the specified index.
      96             :  *
      97             :  * This function returns the depth parameter at the specified index.
      98             :  *
      99             :  * \return The depth of the type of this parameter.
     100             :  */
     101          15 : Node::depth_t Node::get_param_depth(size_t idx) const
     102             : {
     103          15 :     return f_param_depth.at(idx);
     104             : }
     105             : 
     106             : 
     107             : /** \brief Set the depth of a parameter.
     108             :  *
     109             :  * When we search for a match of a function call, we check its parameters.
     110             :  * If a parameter has a higher class type definition, then it wins over
     111             :  * the others. This depth value represents that information.
     112             :  *
     113             :  * \param[in] j  The index of the parameter for which we define the depth.
     114             :  *               (The order is the function being called order.)
     115             :  * \param[in] depth  The new depth.
     116             :  */
     117          16 : void Node::set_param_depth(size_t j, depth_t depth)
     118             : {
     119          16 :     if(j >= f_param_depth.size())
     120             :     {
     121          11 :         throw std::out_of_range("set_param_depth() called with an index out of range");
     122             :     }
     123           5 :     f_param_depth[j] = depth;
     124           5 : }
     125             : 
     126             : 
     127             : /** \brief Get the index of the parameter.
     128             :  *
     129             :  * When a user writes a function call, he can spell out the parameter
     130             :  * names as in:
     131             :  *
     132             :  * \code
     133             :  * pos = find(size => 123, characer => 'c', haystack => str);
     134             :  * \endcode
     135             :  *
     136             :  * The parameters, in the function declaration, may not be in the
     137             :  * same order:
     138             :  *
     139             :  * \code
     140             :  * function find(haystack: string, character: string, size: number = -1);
     141             :  * \endcode
     142             :  *
     143             :  * The parameter index vector holds the indices so we can reorganize the
     144             :  * call as in:
     145             :  *
     146             :  * \code
     147             :  * pos = find(str, 'c', 123);
     148             :  * \endcode
     149             :  *
     150             :  * The really cool thing is that you could call a function with
     151             :  * multiple definitions and still get the parameters in the right
     152             :  * order even though both functions define their parameters
     153             :  * in a different order.
     154             :  *
     155             :  * \param[in] j  The index of the parameter in the function being called.
     156             :  *
     157             :  * \return The index in the function definition.
     158             :  */
     159          15 : size_t Node::get_param_index(size_t j) const
     160             : {
     161          15 :     return f_param_index.at(j);
     162             : }
     163             : 
     164             : 
     165             : /** \brief Set the parameter index.
     166             :  *
     167             :  * Save the index of the parameter in the function being called, opposed
     168             :  * to the index of the parameter in the function call.
     169             :  *
     170             :  * \param[in] idx  The index in the function call.
     171             :  * \param[in] j  The index in the function being called.
     172             :  */
     173          16 : void Node::set_param_index(size_t idx, size_t j)
     174             : {
     175          16 :     if(idx >= f_param_index.size())
     176             :     {
     177          11 :         throw std::out_of_range("set_param_index() called with an index out of range");
     178             :     }
     179           5 :     f_param_index[idx] = j;
     180           5 : }
     181             : 
     182             : 
     183             : 
     184          20 : }
     185             : // namespace as2js
     186             : 
     187             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.9