Property.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
00004  *       mailto: <tevs@mpi-sb.mpg.de>                                      *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  ***************************************************************************/
00012 
00013 //----------------------------------------------------------------------------------
00014 // Includes
00015 //----------------------------------------------------------------------------------
00016 #include "Property.h"
00017 #include "Exception.h"
00018 #include "Log.h"
00019 
00020 namespace nrEngine {
00021 
00022         //----------------------------------------------------------------------------------
00023         Property::Property() :mUserData(NULL)
00024         {
00025 
00026         }
00027 
00028         //----------------------------------------------------------------------------------
00029         Property::Property(const std::string& name) : mName(name), mFullName(name), mUserData(NULL)
00030         {
00031 
00032         }
00033 
00034         //----------------------------------------------------------------------------------
00035         Property::Property (const std::string& name, const boost::any& value) : mName(name), mFullName(name), mValue(value), mUserData(NULL)
00036         {
00037 
00038         }
00039 
00040         //----------------------------------------------------------------------------------
00041         Property::Property(const Property& p):mName(p.getName()), mFullName(p.getFullName()), mValue(p.getValue()), mUserData(p.mUserData)
00042         {
00043 
00044         }
00045 
00046         //----------------------------------------------------------------------------------
00047         Property& Property::operator=(const boost::any& val)
00048         {
00049                 mValue = val;
00050                 return *this;
00051         }
00052 
00053         //----------------------------------------------------------------------------------
00054         bool Property::operator==(const Property& p)
00055         {
00056                 return (p.getFullName() == mFullName && p.getValue().type() == mValue.type());
00057         }
00058 
00059         
00060         //----------------------------------------------------------------------------------
00061         PropertyList& PropertyList::operator, (const Property& p)
00062         {
00063                 push_back(p);
00064                 return *this;
00065         }
00066 
00067         //----------------------------------------------------------------------------------
00068         PropertyList& PropertyList::operator, ( Property& p)
00069         {
00070                 p = front();
00071                 pop_front();
00072                 return *this;
00073         }
00074 
00075         //----------------------------------------------------------------------------------
00076         bool PropertyList::exists(const std::string& name) const
00077         {
00078                 // search for the key
00079                 const_iterator it = begin();
00080                 for (; it != end(); it++)
00081                         if (it->getName() == name) return true;
00082                 return false;
00083         }
00084 
00085         //----------------------------------------------------------------------------------
00086         /*const Property& PropertyList::operator[](const std::string& name)
00087         {
00088                 // search for the key
00089                 for (const_iterator it = begin(); it != end(); it++) if (it->getName() == name) return *it;
00090 
00091                 // we could not find it, so create it and return reference
00092                 push_back(Property(name));
00093                 for (const_iterator it = begin(); it != end(); it++) if (it->getName() == name) return *it;
00094                 
00095                 // ok it should be there, so exception
00096                 NR_EXCEPT(UNKNOWN_ERROR, "This is a BUG! Contact Author!", "PropertyList::operator[]");
00097         }*/
00098         
00099         //----------------------------------------------------------------------------------
00100         Property& PropertyList::operator[](const std::string& name)
00101         {
00102                 // search for the key
00103                 for (iterator it = begin(); it != end(); it++) if (it->getName() == name) return *it;
00104 
00105                 // we could not find it, so create it and return reference
00106                 Property p(name);
00107                 push_back(p);
00108 
00109                 // some debug info
00110                 NR_Log(Log::LOG_ENGINE, Log::LL_DEBUG, "Property: Initialize new property '%s'", name.c_str());
00111 
00112                 return back();
00113         }
00114 
00115         //----------------------------------------------------------------------------------
00116         const Property& PropertyList::operator[](const std::string& name) const
00117         {
00118                 // search for the key
00119                 for (const_iterator it = begin(); it != end(); it++) if (it->getName() == name) return *it;
00120 
00121                 // we could not find it, so error, because we can not add new elements to const
00122                 NR_EXCEPT(UNKNOWN_ERROR, "const PropertyList can not add new elements", "PropertyList::operator[]");
00123         }
00124 
00125 };
00126 

Generated on Wed Sep 12 23:19:42 2007 for nrEngine by  doxygen 1.5.1