Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

value.h

Go to the documentation of this file.
00001 // value.h
00002 //
00003 // Copyright (C) 2002, Chris Laurel <claurel@shatters.net>
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 
00010 #ifndef CELSCRIPT_VALUE_H_
00011 #define CELSCRIPT_VALUE_H_
00012 
00013 #include <celscript/celx.h>
00014 #include <string>
00015 #include <iostream>
00016 #include <celscript/type.h>
00017 
00018 
00019 namespace celx
00020 {
00021 
00022 class Function;
00023 
00024 class Value
00025 {
00026  public:
00027     Value();
00028     Value(const Value&);
00029     Value(double);
00030     Value(const std::string&);
00031     Value(bool);
00032     Value(Function*);
00033 
00034     ~Value();
00035 
00036     Value& operator=(const Value&);
00037 
00038     inline Type getType() const;
00039     inline bool numberValue(double&) const;
00040     inline bool booleanValue(bool&) const;
00041     inline bool stringValue(std::string&) const;
00042     inline bool functionValue(Function*&) const;
00043 
00044     bool toBoolean() const;
00045     double toNumber() const;
00046     std::string toString() const;
00047 
00048     void output(std::ostream&) const;
00049 
00050     bool operator==(const Value&) const;
00051     bool operator!=(const Value&) const;
00052 
00053  private:
00054     Type type;
00055     union
00056     {
00057         bool boolVal;
00058         double numVal;
00059         const std::string* strVal;
00060         Function* funcVal;
00061     } val;
00062 };
00063 
00064 Type Value::getType() const
00065 {
00066     return type;
00067 }
00068 
00069 bool Value::booleanValue(bool& x) const
00070 {
00071     if (type != BooleanType)
00072     {
00073         return false;
00074     }
00075     else
00076     {
00077         x = val.boolVal;
00078         return true;
00079     }
00080 }
00081 
00082 bool Value::numberValue(double& x) const
00083 {
00084     if (type != NumberType)
00085     {
00086         return false;
00087     }
00088     else
00089     {
00090         x = val.numVal;
00091         return true;
00092     }
00093 }
00094 
00095 bool Value::stringValue(std::string& x) const
00096 {
00097     if (type != StringType)
00098     {
00099         return false;
00100     }
00101     else
00102     {
00103         x = *val.strVal;
00104         return true;
00105     }
00106 }
00107 
00108 bool Value::functionValue(Function*& f) const
00109 {
00110     if (type != FunctionType)
00111     {
00112         return false;
00113     }
00114     else
00115     {
00116         f = val.funcVal;
00117         return true;
00118     }
00119 }
00120 
00121 } // namespace celx
00122 
00123 std::ostream& operator<<(std::ostream&, const celx::Value&);
00124 
00125 #endif // CELSCRIPT_VALUE_H_

Generated on Sat Jan 14 22:30:32 2006 for Celestia by  doxygen 1.4.1