MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
module-namespace.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/modules/module-namespace/module-namespace.cc,v 1.9 2017/01/12 14:55:38 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
33 
34 #include "mathp.h"
35 #include "parser.h"
36 #include "dataman.h"
37 
39 protected:
41 
42 public:
43  TableNameSpace(const std::string& sName);
44  ~TableNameSpace(void);
45 
46  bool IsFunc(const std::string& fname) const;
47  MathParser::MathFunc_t* GetFunc(const std::string& fname) const;
49  virtual Table* GetTable(void);
50 };
51 
52 TableNameSpace::TableNameSpace(const std::string& sName)
53 : MathParser::NameSpace(sName)
54 {
55  m_pTable = new Table(false);
56 }
57 
59 {
60  delete m_pTable;
61 }
62 
63 bool
64 TableNameSpace::IsFunc(const std::string& fname) const
65 {
66  return false;
67 }
68 
70 TableNameSpace::GetFunc(const std::string& fname) const
71 {
72  return 0;
73 }
74 
77 {
79 }
80 
81 Table*
83 {
84  return m_pTable;
85 }
86 
87 struct NameSpaceDR : public DescRead {
88  bool Read(HighParser& HP);
89 };
90 
91 bool
93 {
94  if (!HP.IsArg()) {
95  silent_cerr("Parser error in NameSpaceDR::Read(), "
96  " arg expected at line "
97  << HP.GetLineData() << std::endl);
99  }
100 
101  const char *sName = HP.GetString();
102  if (!HP.GetMathParser().bNameValidate(sName)) {
103  silent_cerr("Parser error in NameSpaceDR::Read(), "
104  " invalid namespace \"" << sName << "\" at line "
105  << HP.GetLineData() << std::endl);
107  }
108 
109  MathParser::NameSpace *pNS = new TableNameSpace(sName);
110  int rc = HP.GetMathParser().RegisterNameSpace(pNS);
111  if (rc != 0) {
112  delete pNS;
113  }
114 
115  return (rc == 0);
116 }
117 
118 extern "C" int
119 module_init(const char *module_name, void *pdm, void *php)
120 {
121  MBDynParser& HP = *((MBDynParser *)php);
122 
123  NameSpaceDR *pDR = new NameSpaceDR;
124  if (pDR == 0) {
125  return false;
126  }
127  SetDescData("namespace", pDR);
128 
129  int rc = 0;
130  while (HP.IsArg()) {
131  const char *sName = HP.GetString();
132 
133  if (!HP.GetMathParser().bNameValidate(sName)) {
134  silent_cerr("Parser error in module-namespace::module_init(), "
135  " invalid namespace \"" << sName << "\" at line "
136  << HP.GetLineData() << std::endl);
138  }
139 
140  /* registers namespace */
141  MathParser::NameSpace *pNS = new TableNameSpace(sName);
142  rc = HP.GetMathParser().RegisterNameSpace(pNS);
143  if (rc != 0) {
144  delete pNS;
145  }
146  }
147 
148  return rc;
149 }
150 
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
bool Read(HighParser &HP)
int RegisterNameSpace(NameSpace *ns)
Definition: mathp.cc:4602
bool SetDescData(const std::string &name, DescRead *rf)
Definition: parser.cc:301
int module_init(const char *module_name, void *pdm, void *php)
This function registers our user defined element for the math parser.
virtual HighParser::ErrOut GetLineData(void) const
Definition: parser.cc:681
TypedValue EvalFunc(MathParser::MathFunc_t *f) const
virtual Table * GetTable(void)
MathParser::MathFunc_t * GetFunc(const std::string &fname) const
bool bNameValidate(const std::string &s) const
Definition: mathp.cc:3292
virtual MathParser & GetMathParser(void)
Definition: parser.cc:668
bool IsFunc(const std::string &fname) const
virtual std::string GetString(const std::string &sDefVal)
Definition: parser.cc:1074
virtual bool IsArg(void)
Definition: parser.cc:807
Definition: table.h:43
virtual HighParser::ErrOut GetLineData(void) const
Definition: parsinc.cc:697
TableNameSpace(const std::string &sName)