Property.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2014, Institute for Artificial Intelligence,
00005  *  Universität Bremen.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the Institute for Artificial Intelligence,
00019  *     Universität Bremen, nor the names of its contributors may be
00020  *     used to endorse or promote products derived from this software
00021  *     without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *********************************************************************/
00036 
00040 #include <Property.h>
00041 
00042 
00043 namespace beliefstate {
00044   Property::Property(std::string strKey, PropertyType ptType) {
00045     m_strValue = "";
00046     m_dValue = 0.0f;
00047     
00048     this->set(ptType);
00049     this->setKey(strKey);
00050   }
00051   
00052   Property::Property(Property* prTemplate, bool bDeepCopy) {
00053     // Copy constructor
00054     this->set(prTemplate->type());
00055     this->setKey(prTemplate->key());
00056     
00057     switch(prTemplate->type()) {
00058     case String: {
00059       this->set(prTemplate->getString());
00060     } break;
00061       
00062     case Integer: {
00063       this->set(prTemplate->getInteger());
00064     } break;
00065       
00066     case Double: {
00067       this->set(prTemplate->getDouble());
00068     } break;
00069       
00070     case Boolean: {
00071       this->set(prTemplate->getBoolean());
00072     } break;
00073       
00074     case Object:
00075     case Array: {
00076       if(bDeepCopy) {
00077         for(Property* prSubProperty : prTemplate->subProperties()) {
00078           this->addSubProperty(new Property(prSubProperty));
00079         }
00080       }
00081     } break;
00082     }
00083   }
00084   
00085   Property::~Property() {
00086     for(Property* prDelete : m_lstSubProperties) {
00087       delete prDelete;
00088     }
00089     
00090     m_lstSubProperties.clear();
00091   }
00092   
00093   void Property::setKey(std::string strKey) {
00094     m_strKey = strKey;
00095   }
00096   
00097   std::string Property::key() {
00098     return m_strKey;
00099   }
00100   
00101   void Property::set(Property::PropertyType ptType) {
00102     m_ptType = ptType;
00103   }
00104   
00105   Property::PropertyType Property::type() {
00106     return m_ptType;
00107   }
00108   
00109   void Property::set(std::string strValue) {
00110     m_strValue = strValue;
00111   }
00112   
00113   std::string Property::getString() {
00114     return m_strValue;
00115   }
00116   
00117   void Property::set(int nValue) {
00118     m_dValue = nValue;
00119   }
00120   
00121   int Property::getInteger() {
00122     return (int)m_dValue;
00123   }
00124   
00125   void Property::set(double dValue) {
00126     m_dValue = dValue;
00127   }
00128   
00129   double Property::getDouble() {
00130     return m_dValue;
00131   }
00132   
00133   void Property::set(bool bValue) {
00134     m_dValue = (bValue ? 1 : 0);
00135   }
00136   
00137   bool Property::getBoolean() {
00138     return (m_dValue != 0);
00139   }
00140   
00141   void Property::addSubProperty(Property* prSubProperty) {
00142     m_lstSubProperties.push_back(prSubProperty);
00143   }
00144   
00145   std::list<Property*> Property::subProperties() {
00146     return m_lstSubProperties;
00147   }
00148   
00149   void Property::print(int nIndentationLevel, bool bPrintKey) {
00150     std::string strIndent = "";
00151     
00152     for(int nI = 0; nI < nIndentationLevel; nI++) {
00153       strIndent += " ";
00154     }
00155     
00156     std::cout << strIndent;
00157     
00158     if(bPrintKey) {
00159       std::cout << this->key() << ": ";
00160     }
00161     
00162     switch(this->type()) {
00163     case String:
00164       std::cout << "\"" << this->getString() << "\"" << std::endl;
00165       break;
00166       
00167     case Integer:
00168       std::cout << this->getInteger() << std::endl;
00169       break;
00170       
00171     case Boolean:
00172       std::cout << this->getBoolean() << std::endl;
00173       break;
00174       
00175     case Double:
00176       std::cout << this->getDouble() << std::endl;
00177       break;
00178       
00179     case Array:
00180       std::cout << std::endl;
00181       
00182       for(Property* prCurrent : m_lstSubProperties) {
00183         prCurrent->print(nIndentationLevel + 1);
00184       }
00185       break;
00186       
00187     case Object:
00188       std::cout << std::endl;
00189       
00190       for(Property* prCurrent : m_lstSubProperties) {
00191         prCurrent->print(nIndentationLevel + 1);
00192       }
00193       break;
00194     }
00195   }
00196   
00197   Property* Property::namedSubProperty(std::string strKey) {
00198     for(Property* prCurrent : m_lstSubProperties) {
00199       if(prCurrent->key() == strKey) {
00200         return prCurrent;
00201       }
00202     }
00203     
00204     return NULL;
00205   }
00206 }


beliefstate
Author(s): Jan Winkler
autogenerated on Sun Oct 5 2014 22:30:15