1 #ifndef CoCoA_OpenMath_H
2 #define CoCoA_OpenMath_H
3 
4 //   Copyright (c)  2005  John Abbott
5 
6 //   This file is part of the source of CoCoALib, the CoCoA Library.
7 
8 //   CoCoALib is free software: you can redistribute it and/or modify
9 //   it under the terms of the GNU General Public License as published by
10 //   the Free Software Foundation, either version 3 of the License, or
11 //   (at your option) any later version.
12 
13 //   CoCoALib is distributed in the hope that it will be useful,
14 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //   GNU General Public License for more details.
17 
18 //   You should have received a copy of the GNU General Public License
19 //   along with CoCoALib.  If not, see <http://www.gnu.org/licenses/>.
20 
21 
22 #include "CoCoA/SmartPtrIRC.H"
23 
24 #include <iosfwd>
25 // using std::ostream;
26 #include <string>
27 // using std::string
28 
29 
30 namespace CoCoA
31 {
32 
33   class BigInt;      // fwd decl, defined in BigInt.H
34   class MachineInt;  // fwd decl, defined in MachineInt.H
35 
36   class OpenMathSymbol
37   {
38   public:
39     OpenMathSymbol(); // fills symbol with a default "unset" value
40     OpenMathSymbol(const char* const cd, const char* const name);
41     OpenMathSymbol(const std::string& cd, const std::string& name);
42     // Default copy ctor, assignment and dtor are fine.
43 
44     void myOutputSelf(std::ostream& out) const;
45 
46   private: // data members
47     std::string myCD;
48     std::string myName;
49     friend const std::string& CD(const OpenMathSymbol& s);   // accessor
50     friend const std::string& name(const OpenMathSymbol& s); // accessor
51   };
52 
53   std::ostream& operator<<(std::ostream& out, const OpenMathSymbol& oms);
54 
55   //---------------------------------------------------------------------------
56   // Output functions etc.
57 
58   class OpenMathOutputBase; // fwd decl, defined below.
59   class OpenMathOutput
60   {
61   public:
OpenMathOutput(OpenMathOutputBase * ptr)62     explicit OpenMathOutput(OpenMathOutputBase* ptr): mySmartPtr(ptr) {}
63     // assignment disabled because SmartPtrIRC has no assignment
64     OpenMathOutputBase* operator->() const { return mySmartPtr.operator->(); }  ///< Allow const member fns to be called.
65   private: // data members
66     SmartPtrIRC<OpenMathOutputBase> mySmartPtr;
67   };
68 
69 
70 //  Removed next line to permit auto conversion from bool to bool3
71 //  OpenMathOutput& operator<<(OpenMathOutput& OMOut, const MachineInt& n);
72   OpenMathOutput& operator<<(OpenMathOutput& OMOut, int n);
73   OpenMathOutput& operator<<(OpenMathOutput& OMOut, unsigned int n);
74   OpenMathOutput& operator<<(OpenMathOutput& OMOut, long n);
75   OpenMathOutput& operator<<(OpenMathOutput& OMOut, unsigned long n);
76   OpenMathOutput& operator<<(OpenMathOutput& OMOut, const OpenMathSymbol& s);
77 
78 
79   class OpenMathOutputBase: protected IntrusiveReferenceCount
80   {
81     friend class SmartPtrIRC<OpenMathOutputBase>; // morally "friend OpenMathOutput", so it can alter reference count
82   protected:
83     OpenMathOutputBase(); // just sets ref count to zero.
84     virtual ~OpenMathOutputBase();
85   public:
86     virtual void mySend(const MachineInt& n) = 0;
87     virtual void mySend(const BigInt& N) = 0;
88     virtual void mySend(const OpenMathSymbol& s) = 0;
mySendSymbol(const char * const cd,const char * const name)89     virtual void mySendSymbol(const char* const cd, const char* const name) { mySend(OpenMathSymbol(cd,name)); };
90     virtual void mySendApplyStart() = 0;
91     virtual void mySendApplyEnd() = 0;
92     virtual void mySendObjectStart() = 0;
93     virtual void mySendObjectEnd() = 0;
94 
95     //void OutputAttribute() = 0;//??????
96   };
97 
98 
99   //---------------------------------------------------------------------------
100   // Input functions etc.
101 
102   enum OpenMathTag { OpenMathObj, OpenMathApply, OpenMathInt, OpenMathSym, OpenMathAttr, OpenMathEOF };
103 
104   class OpenMathInputBase;  // fwd decl, defined below.
105   class OpenMathInput
106   {
107   public:
OpenMathInput(OpenMathInputBase * ptr)108     explicit OpenMathInput(OpenMathInputBase* ptr): mySmartPtr(ptr) {}
109     // assignment disabled because SmartPtrIRC has no assignment
110     OpenMathInputBase* operator->() const { return mySmartPtr.operator->(); }  ///< Allow const member fns to be called.
111   private: // data members
112     SmartPtrIRC<OpenMathInputBase> mySmartPtr;
113   };
114 
115   class OpenMathInputBase: protected IntrusiveReferenceCount
116   {
117     friend class SmartPtrIRC<OpenMathInputBase>; // morally "friend OpenMathInput", so it can alter reference count
118   protected:
119     OpenMathInputBase(); // just sets ref count to zero.
120     virtual ~OpenMathInputBase();
121   public:
122     virtual void advance() = 0;
123     virtual OpenMathTag myCurrTag() = 0;
124     virtual long NumDescendants() const = 0;
125     virtual bool myRecv(long & n) = 0;
126 //????    virtual bool myRecv(unsigned long & n) = 0; // UNSIGNED SHORT INTEGER
127     virtual bool myRecv(BigInt& N) = 0;
128     virtual bool myRecv(OpenMathSymbol& s) = 0; // result is false if node is of wrong type
129 
130 //       virtual void InputApplyStart() = 0;
131 //       virtual void InputApplyEnd() = 0;
132 //       virtual void InputObjectStart() = 0;
133 //       virtual void InputObjectEnd() = 0;
134 
135     //void InputAttribute() = 0;//??????
136   };
137 
138   OpenMathInput& operator>>(OpenMathInput& OMIn, long& n);
139   OpenMathInput& operator>>(OpenMathInput& OMIn, OpenMathSymbol& s); //????
140 
141 } // end of namespace CoCoA
142 
143 
144 // RCS header/log in the next few lines
145 // $Header: /Volumes/Home_1/cocoa/cvs-repository/CoCoALib-0.99/include/CoCoA/OpenMath.H,v 1.10 2012/05/04 16:59:43 abbott Exp $
146 // $Log: OpenMath.H,v $
147 // Revision 1.10  2012/05/04 16:59:43  abbott
148 // For future compatibility with auto conversion from bool to 3-way bool,
149 // removed sending of MachineInt; instead added operators for sending
150 // (unsigned) int and (unsigned) long [but no other integral types for now].
151 //
152 // Revision 1.9  2012/02/02 15:55:48  abbott
153 // Replaced include of MachineInt by fwd decl.
154 //
155 // Revision 1.8  2011/11/09 13:47:56  bigatti
156 // -- renamed MachineInteger --> MachineInt
157 //
158 // Revision 1.7  2011/08/14 15:52:18  abbott
159 // Changed ZZ into BigInt (phase 1: just the library sources).
160 //
161 // Revision 1.6  2011/08/12 15:48:31  abbott
162 // Added virtual mem fns for BigInt type.
163 //
164 // Revision 1.5  2011/03/11 14:49:08  abbott
165 // Changed size_t into long.
166 //
167 // Revision 1.4  2010/03/18 13:53:48  abbott
168 // Minor rationalization to OpenMath implementation: moved op<<(OMOut,ZZ) to ZZ files.
169 //
170 // Revision 1.3  2008/12/16 21:10:32  abbott
171 // Replaced the various output fns for different sort of machine integers by a
172 // single one for MachineInt.
173 //
174 // Revision 1.2  2007/10/30 17:14:12  abbott
175 // Changed licence from GPL-2 only to GPL-3 or later.
176 // New version for such an important change.
177 //
178 // Revision 1.1.1.1  2007/03/09 15:16:11  abbott
179 // Imported files
180 //
181 // Revision 1.6  2006/12/06 17:14:12  cocoa
182 // -- removed #include "config.H"
183 //
184 // Revision 1.5  2006/11/27 13:04:35  cocoa
185 // Added explicit OpenMath output operators for all integer types.
186 //
187 // Revision 1.4  2006/11/23 17:10:52  cocoa
188 // -- changed: OpenMathOutput and OpenMathInput are now a class (instead of typedef)
189 //
190 // Revision 1.3  2006/11/02 13:25:44  cocoa
191 // Simplification of header files: the OpenMath classes have been renamed.
192 // Many minor consequential changes.
193 //
194 // Revision 1.2  2006/10/06 14:04:15  cocoa
195 // Corrected position of #ifndef in header files.
196 // Separated CoCoA_ASSERT into assert.H from config.H;
197 // many minor consequential changes (have to #include assert.H).
198 // A little tidying of #include directives (esp. in Max's code).
199 //
200 
201 
202 #endif
203