edelib 2.0.0

edelib/EdbusContainer.h

00001 /*
00002  * $Id: EdbusContainer.h 2839 2009-09-28 11:36:20Z karijes $
00003  *
00004  * D-BUS stuff
00005  * Copyright (c) 2008 edelib authors
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 #ifndef __EDELIB_EDBUSCONTAINER_H__
00022 #define __EDELIB_EDBUSCONTAINER_H__
00023 
00024 #include "List.h"
00025 
00026 EDELIB_NS_BEGIN
00027 
00028 #ifndef SKIP_DOCS
00029 template <typename T>
00030 struct EdbusContainerImpl {
00031         list<T> lst;
00032         unsigned int ref;
00033 };
00034 #endif
00035 
00051 template <typename T>
00052 class EdbusContainer {
00053 public:
00057         typedef typename list<T>::iterator iterator;
00058 
00062         typedef typename list<T>::const_iterator const_iterator;
00063 
00064 #ifndef SKIP_DOCS
00065         typedef EdbusContainerImpl<T> EdbusContainerPrivate;
00066 #endif
00067 
00068 protected:
00072         EdbusContainerPrivate* impl;
00073 
00077         void dispose(void) {
00078                 if(!impl)
00079                         return;
00080 
00081                 delete impl;
00082                 impl = 0;
00083         }
00084 
00090         void unhook(void) {
00091                 E_ASSERT(impl != NULL);
00092 
00093                 if(impl->ref == 1)
00094                         return;
00095 
00096                 EdbusContainerPrivate* new_one = new EdbusContainerPrivate;
00097                 new_one->ref = 1;
00098 
00099                 /* 
00100                  * Copy the content
00101                  *
00102                  * edelib::list does not have implemented copy operator
00103                  * and that is the way I like
00104                  */
00105                 if(impl->lst.size() > 0) {
00106                         iterator it = impl->lst.begin(), it_end = impl->lst.end();
00107 
00108                         while(it != it_end) {
00109                                 new_one->lst.push_back(*it);
00110                                 ++it;
00111                         }
00112                 }
00113 
00114                 impl->ref--;
00115                 impl = new_one;
00116         }
00117 
00121         EdbusContainer() : impl(0) {
00122                 impl = new EdbusContainerPrivate;
00123                 impl->ref = 1;
00124         };
00125 
00129         EdbusContainer(const EdbusContainer& other) {
00130                 if(this == &other)
00131                         return;
00132 
00133                 impl = other.impl;
00134                 other.impl->ref++;
00135         }
00136 
00141         ~EdbusContainer() {
00142                 impl->ref--;
00143 
00144                 if(impl->ref == 0)
00145                         dispose();
00146         }
00147 
00151         EdbusContainer& operator=(const EdbusContainer& other) {
00152                 other.impl->ref++;
00153                 impl->ref--;
00154 
00155                 if(impl->ref == 0)
00156                         dispose();
00157 
00158                 impl = other.impl;
00159                 return *this;
00160         }
00161 };
00162 
00163 EDELIB_NS_END
00164 #endif