worldsim/include/ownable.h

00001 /********************************************************************************
00002  *  WorldSim -- library for robot simulations                                   *
00003  *  Copyright (C) 2008-2011 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 #ifndef OWNABLE_H
00021 #define OWNABLE_H
00022 
00023 #include "worldsimconfig.h"
00024 #include <QList>
00025 
00026 namespace farsa {
00027 
00037 class FARSA_WSIM_API Ownable
00038 {
00039 public:
00046     struct Owned {
00054         Owned(Ownable *o, bool d = true) :
00055             object(o),
00056             destroy(d)
00057         {
00058         }
00059 
00066         bool operator==(const Owned& other)
00067         {
00068             return (object == other.object);
00069         }
00070 
00074         Ownable *object;
00075 
00080         bool destroy;
00081     };
00082 
00086     typedef QList<Owned> OwnedList;
00087 
00088 public:
00092     Ownable();
00093 
00099     virtual ~Ownable();
00100 
00108     void setOwner(Ownable *owner, bool destroy = true);
00109 
00115     Ownable* owner() const
00116     {
00117         return m_owner;
00118     }
00119 
00125     const QList<Owned>& owned() const
00126     {
00127         return m_owned;
00128     }
00129 
00130 private:
00138     void addToOwned(Ownable *obj, bool destroy);
00139 
00145     void removeFromOwned(Ownable *obj);
00146 
00150     Ownable* m_owner;
00151 
00155     QList<Owned> m_owned;
00156 };
00157 
00158 }
00159 
00160 #endif