1 /*
2  * FieldValue.h
3  *
4  * Copyright (C) 1999 Stephen F. White
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program (see the file "COPYING" for details); if
18  * not, write to the Free Software Foundation, Inc., 675 Mass Ave,
19  * Cambridge, MA 02139, USA.
20  */
21 
22 #pragma once
23 
24 #include "Array.h"
25 #include "MyString.h"
26 #include "Element.h"
27 
28 class Scene;
29 class Node;
30 
31 #include "Arrays.h"
32 
33 #include "MySwap.h"
34 #include "Types.h"
35 
36 #ifdef _WIN32
37 # define drand48() (rand()/(double)RAND_MAX)
38 #endif
39 
40 #define RAND() (drand48())
41 #define FLOAT_RAND() (drand48() * 10.0f)
42 #define INT_RAND() ((int)(drand48() * 10.0))
43 
44 class FieldValue {
45 public:
46                         FieldValue();
~FieldValue()47     virtual            ~FieldValue() {}
48 
getStride()49     virtual int         getStride() const { return 1; }
50 
51     // int i argument is used for MFieldValue, otherwise it is a dummy argument
52     virtual int         writeData(int filedes, int i) const = 0;
53     // write data without quotes to file
writeRaw(int filedes,int indent)54     virtual int         writeRaw(int filedes, int indent) const { return 0; }
55     virtual int         writeDequoted(int filedes, const char *string);
56     // write data in VRML syntax to file
57     virtual int         write(int filedes, int indent) const;
58     // write data in X3D/XML syntax to file
59     virtual int         writeXml(int filedes, int indent,
60                                  int containerField = -1,
61                                  bool avoidUse = false) const;
writeDataXml(int filedes,int i)62     virtual int         writeDataXml(int filedes, int i) const
63                             { return writeData(filedes, i); }
64     // write data in AC3D b syntax to file
65     virtual int         writeAc3d(int filedes, int indent) const;
66     // write data in Catt 8 syntax to file
67     virtual int         writeCattGeo(int filedes, int indent) const;
68     // write data in C++/Java syntax to file
69     virtual int         writeC(int filedes, const char* variableName,
70                                int languageFlag) const;
writeCDeclaration(int filedes,int languageFlag)71     virtual int         writeCDeclaration(int filedes, int languageFlag) const
72                            { return 0; }
73     virtual int         writeCSendEventFunction(int filedes, int languageFlag);
writeDataC(int filedes,int i,int languageFlag)74     virtual int         writeDataC(int filedes, int i, int languageFlag) const
75                            { return writeData(filedes, i); }
writeCWonderlandArt(int filedes,const char * variableName,int languageFlag)76     virtual int         writeCWonderlandArt(int filedes,
77                                             const char* variableName,
78                                             int languageFlag) const
79                            { return writeC(filedes, variableName,
80                                            languageFlag); }
81     virtual const char *getTypeC(int languageFlag) const = 0;
82     virtual const char *getDefaultC(int languageFlag) const;
isArrayInC(void)83     virtual bool        isArrayInC(void) const { return false; }
84 
write4FieldPipe(int filedes,int indent)85     virtual int         write4FieldPipe(int filedes, int indent) const
86                             { return write(filedes, indent); }
87 
88     virtual bool        readLine(int index, char *line) = 0;
89 
isMFieldValue(void)90     virtual bool        isMFieldValue(void) const { return false; }
isNode(void)91     virtual bool        isNode(void) const { return false; }
92 
isNullNode(void)93     virtual bool        isNullNode(void) const { return false; }
isUseNode(void)94     virtual bool        isUseNode(void) const { return false; }
95 
isNull(void)96     virtual bool        isNull(void) const { return false; }
97 
getDefName(void)98     virtual const char *getDefName(void) const { return ""; }
99 
100     virtual bool        checkInput(char *line);
101     virtual int         getNumbersPerType(void) const = 0;
needCheckFloat(void)102     virtual bool        needCheckFloat(void) const { return false; }
103 
104     virtual int         getType() const = 0;
105     virtual const char *getTypeName() const = 0;
106     virtual MyString    getString(int index = -1, int stride = -1) const
107                            { return "";};
108 
109     virtual bool        equals(const FieldValue *value) const = 0;
equalsAngle(const FieldValue * value,double angleUnit)110     virtual bool        equalsAngle(const FieldValue *value, double angleUnit)
111                             const { return false; }
112     virtual FieldValue *addNode(Node *node, int index = -1) const;
113     virtual FieldValue *removeNode(Node *node, int index = -1) const;
114 
clamp(const FieldValue * min,const FieldValue * max)115     virtual void        clamp(const FieldValue *min, const FieldValue *max) {}
116 
flip(int index)117     virtual void        flip(int index) {}
118 
ref()119     void                ref()
120                            {
121 #ifdef HAVE_NULL_COMPARE
122                            if (this != NULL)
123 #endif
124                            m_refs++;
125                            }
unref()126     void                unref()
127                            {
128 #ifdef HVAVE_NULL_COMPARE
129                            if ((this != NULL) && (--m_refs == 0))
130 #else
131                            if (--m_refs == 0)
132 #endif
133                                delete this;
134                            }
getRefs()135     int                 getRefs() { return m_refs; }
136 
137     virtual FieldValue *copy() = 0;
138 
fixAngle(double angleUnit)139     virtual void        fixAngle(double angleUnit) {}
140 
141     virtual MyString    getEcmaScriptComment(MyString name, int flags)
142                               const = 0;
143 
144     const char         *getEcmaScriptIndent(int  flags);
145 
146     virtual bool        supportAnimation(bool x3d) const = 0;
supportInteraction(void)147     virtual bool        supportInteraction(void) const { return false; }
makeEmpty(void)148     virtual void        makeEmpty(void) {}
149 
isX3DType()150     virtual bool        isX3DType() { return false; }
151 
152     bool                writeType(int languageFlag) const;
153 
isDefaultValue(void)154     bool                isDefaultValue(void) { return m_isDefaultValue; }
setIsDefaultValue(void)155     void                setIsDefaultValue(void) { m_isDefaultValue = true; }
removeIsDefaultValue(void)156     void                removeIsDefaultValue(void) { m_isDefaultValue = false; }
157 
getContainerField(void)158     virtual int         getContainerField(void) { return -1; }
setContainerField(int f)159     virtual void        setContainerField(int f) {}
160 
161     //only for testing
162     virtual FieldValue *getRandom(Scene *scene, int nodeType = -1) = 0;
163 private:
164     int                 m_refs;
165     bool                m_isDefaultValue;
166 };
167 
168 class MFieldValue : public FieldValue
169 {
170 public:
171     virtual int         getSFSize() const = 0;
172     virtual FieldValue *getSFValue(int index) const = 0;
173     virtual void        setSFValue(int index, FieldValue *value) = 0;
isMFNode(void)174     virtual bool        isMFNode(void) const { return false; }
isMFieldValue(void)175     virtual bool        isMFieldValue(void) const { return true; }
writeBrackets(void)176     virtual bool        writeBrackets(void) const { return(getSFSize() != 1); }
177     virtual int         write(int filedes, int indent,
178                               bool writeBrackets) const;
write(int filedes,int indent)179     virtual int         write(int filedes, int indent) const
180                            { return write(filedes, indent, writeBrackets()); }
write4FieldPipe(int filedes,int indent)181     virtual int         write4FieldPipe(int filedes, int indent) const
182                             { return write(filedes, indent, false); }
183     virtual int         writeXml(int filedes, int indent,
184                                  int containerField = -1,
185                                  bool avoidUse = false) const;
186     virtual int         writeC(int filedes, const char* variableName,
187                                int languageFlag) const;
188     virtual int         writeCSendEventFunction(int filedes, int languageFlag);
isArrayInC(void)189     virtual bool        isArrayInC(void) const { return true; }
190 
191     int                 writeJavaLongArray(int filedes, int languageFlag,
192                                            const char* variableName,
193                                            int offset, int length,
194                                            bool wonderlandArt,
195                                            const char *sceneUrl) const;
isNull(void)196     virtual bool        isNull(void) const { return getSFSize() > 0; }
197 
198     virtual void        removeSFValue(long index) = 0;
makeEmpty(void)199     virtual void        makeEmpty(void)
200                            {
201                            for (int i = getSFSize() - 1; i >= 0; i--)
202                                removeSFValue(i);
203                            }
204     void                getDiff(IntArray *newIndices, IntArray *oldIndices,
205                                 MFieldValue *old);
supportAnimation(bool x3d)206     virtual bool        supportAnimation(bool x3d) const {return false; }
207 };
208 
209 extern FieldValue *rewriteField(FieldValue *value,
210                                 const char *oldBase, const char *newBase,
211                                 int writeFlags);
212