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

gcvalue.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2002,2003 Koblenz University
00006    Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: gcvalue.cpp,v 1.5 2004/12/19 14:08:03 rollmark Exp $
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; version 2 of the License.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 #include "gcvalue.h"
00023 #include <sstream>
00024 
00025 using namespace zeitgeist;
00026 using namespace std;
00027 
00028 GCValue::GCValue()
00029 {
00030     mValue = Qnil;
00031 }
00032 
00033 GCValue::GCValue(const GCValue& value)
00034 {
00035     mValue = Qnil;
00036     Set(value.mValue);
00037 }
00038 
00039 GCValue::GCValue(VALUE v)
00040 {
00041     mValue = Qnil;
00042     Set(v);
00043 }
00044 
00045 GCValue::GCValue(bool b)
00046 {
00047     mValue = Qnil;
00048     Set(b ? Qtrue:Qfalse);
00049 }
00050 
00051 GCValue::GCValue(const string& str)
00052 {
00053     mValue = Qnil;
00054     Set(rb_str_new2(str.c_str()));
00055 }
00056 
00057 GCValue::GCValue(const char* str)
00058 {
00059     mValue = Qnil;
00060     Set(rb_str_new2(str));
00061 }
00062 
00063 GCValue::GCValue(float f)
00064 {
00065     mValue = Qnil;
00066     Set(rb_float_new(f));
00067 }
00068 
00069 GCValue::GCValue(int i)
00070 {
00071     mValue = Qnil;
00072     Set(rb_int_new(i));
00073 }
00074 
00075 GCValue::~GCValue()
00076 {
00077     GC_Unregister();
00078 }
00079 
00080 void GCValue::operator = (const GCValue& value)
00081 {
00082     mValue = Qnil;
00083     Set(value.mValue);
00084 }
00085 
00086 void GCValue::operator = (const VALUE& value)
00087 {
00088     mValue = Qnil;
00089     Set(value);
00090 }
00091 
00092 VALUE GCValue::Get()
00093 {
00094     return mValue;
00095 }
00096 
00097 void GCValue::Set(VALUE v)
00098 {
00099     GC_Unregister();
00100     mValue = v;
00101     GC_Register();
00102 }
00103 
00104 bool GCValue::IsNil()
00105 {
00106     return NIL_P(mValue);
00107 }
00108 
00109 bool GCValue::GetInt(int& value)
00110 {
00111     if (IsNil())
00112         {
00113             return false;
00114         }
00115 
00116     value = NUM2INT(mValue);
00117     return true;
00118 }
00119 
00120 bool GCValue::GetFloat(float& value)
00121 {
00122     if (IsNil())
00123         {
00124             return false;
00125         }
00126 
00127     value = (float)NUM2DBL(mValue);
00128     return true;
00129 }
00130 
00131 bool GCValue::GetBool(bool& value)
00132 {
00133     if (IsNil())
00134         {
00135             return false;
00136         }
00137 
00138     switch(TYPE(mValue))
00139         {
00140         case T_TRUE :
00141             value = true;
00142             return true;
00143 
00144         case T_FALSE :
00145             value = false;
00146             return true;
00147 
00148         default:
00149             return false;
00150         }
00151 }
00152 
00153 bool GCValue::GetString(std::string& value)
00154 {
00155     if (IsNil())
00156         {
00157             return false;
00158         }
00159 
00160     switch(TYPE(mValue))
00161         {
00162         case T_STRING:
00163             {
00164                 value = STR2CSTR(mValue);
00165                 return true;
00166             }
00167 
00168         case T_FLOAT:
00169             {
00170                 stringstream ss;
00171                 ss << (float)NUM2DBL(mValue);
00172                 value = ss.str();
00173                 return true;
00174             }
00175 
00176         case T_FIXNUM:
00177             {
00178                 stringstream ss;
00179                 ss << NUM2INT(mValue);
00180                 value = ss.str();
00181                 return true;
00182             }
00183 
00184         default:
00185             break;
00186         }
00187 
00188     return false;
00189 }
00190 
00191 void GCValue::GC_Unregister()
00192 {
00193     if (! IsNil())
00194         {
00195             rb_gc_unregister_address(&mValue);
00196         }
00197 }
00198 
00199 void GCValue::GC_Register()
00200 {
00201     if (! IsNil())
00202         {
00203             rb_gc_register_address(&mValue);
00204         }
00205 }

Generated on Thu Apr 6 15:25:38 2006 for rcssserver3d by  doxygen 1.4.4