1 /** \file
2     \brief Implementation of the interface for parser callback objects.
3 
4 <pre>
5                __________                                 ____  ___
6     _____  __ _\______   \_____ _______  ______ __________\   \/  /
7    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     /
8   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \
9   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
10         \/                     \/           \/     \/           \_/
11                                        Copyright (C) 2016 Ingo Berg
12                                        All rights reserved.
13 
14   muParserX - A C++ math parser library with array and string support
15   Copyright (c) 2016, Ingo Berg
16   All rights reserved.
17 
18   Redistribution and use in source and binary forms, with or without
19   modification, are permitted provided that the following conditions are met:
20 
21    * Redistributions of source code must retain the above copyright notice,
22      this list of conditions and the following disclaimer.
23    * Redistributions in binary form must reproduce the above copyright notice,
24      this list of conditions and the following disclaimer in the documentation
25      and/or other materials provided with the distribution.
26 
27   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
28   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
34   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36   POSSIBILITY OF SUCH DAMAGE.
37 </pre>
38 */
39 #include "mpICallback.h"
40 #include <cassert>
41 
42 #include "mpParserBase.h"
43 
44 
45 MUP_NAMESPACE_START
46 
47   //------------------------------------------------------------------------------
ICallback(ECmdCode a_iCode,const char_type * a_szName,int a_nArgc)48   ICallback::ICallback(ECmdCode a_iCode,
49                        const char_type *a_szName,
50                        int a_nArgc)
51     :IToken(a_iCode, a_szName)
52     ,m_pParent(nullptr)
53     ,m_nArgc(a_nArgc)
54     ,m_nArgsPresent(-1)
55   {}
56 
57   //------------------------------------------------------------------------------
~ICallback()58   ICallback::~ICallback()
59   {}
60 
61   //---------------------------------------------------------------------------
AsICallback()62   ICallback* ICallback::AsICallback()
63   {
64     return this;
65   }
66 
67   //---------------------------------------------------------------------------
AsIValue()68   IValue* ICallback::AsIValue()
69   {
70     return nullptr;
71   }
72 
73   //------------------------------------------------------------------------------
74   /** \brief Returns a pointer to the parser object owning this callback.
75       \pre [assert] m_pParent must be defined
76   */
GetParent()77   ParserXBase* ICallback::GetParent()
78   {
79     assert(m_pParent);
80     return m_pParent;
81   }
82 
83   //------------------------------------------------------------------------------
SetArgc(int argc)84   void  ICallback::SetArgc(int argc)
85   {
86     m_nArgc = argc;
87   }
88 
89   //------------------------------------------------------------------------------
90   /** \brief Returns the m´number of arguments required by this callback.
91       \return Number of arguments or -1 if the number of arguments is variable.
92   */
GetArgc() const93   int ICallback::GetArgc() const
94   {
95     return m_nArgc;
96   }
97 
98   //------------------------------------------------------------------------------
99   /** \brief Assign a parser object to the callback.
100       \param a_pParent The parser that belongs to this callback object.
101 
102     The parent object can be used in order to access internals of the parser
103     from within a callback object. Thus enabling callbacks to delete
104     variables or functions if this is desired.
105   */
SetParent(parent_type * a_pParent)106   void ICallback::SetParent(parent_type *a_pParent)
107   {
108     assert(a_pParent);
109     m_pParent = a_pParent;
110   }
111 
112   //------------------------------------------------------------------------------
AsciiDump() const113   string_type ICallback::AsciiDump() const
114   {
115     stringstream_type ss;
116 
117     ss << g_sCmdCode[ GetCode() ];
118     ss << _T(" [addr=0x") << std::hex << this << std::dec;
119     ss << _T("; pos=") << GetExprPos();
120     ss << _T("; id=\"") << GetIdent() << "\"";
121     ss << _T("; argc=") << GetArgc() << " (found: " << m_nArgsPresent << ")";
122     ss << _T("]");
123 
124     return ss.str();
125   }
126 
127   //------------------------------------------------------------------------------
SetNumArgsPresent(int argc)128   void ICallback::SetNumArgsPresent(int argc)
129   {
130     m_nArgsPresent = argc;
131   }
132 
133   //------------------------------------------------------------------------------
GetArgsPresent() const134   int ICallback::GetArgsPresent() const
135   {
136     if (m_nArgc!=-1)
137       return m_nArgc;
138     else
139       return m_nArgsPresent;
140   }
141 } // namespace mu
142