diff --git a/libgrive/src/drive/FolderSet.cc b/libgrive/src/drive/FolderSet.cc new file mode 100644 index 0000000..d1e24c5 --- /dev/null +++ b/libgrive/src/drive/FolderSet.cc @@ -0,0 +1,89 @@ +/* + grive: an GPL program to sync a local directory with Google Drive + Copyright (C) 2012 Wan Wai Ho + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation version 2 + of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "FolderSet.hh" + +#include "util/Destroy.hh" + +#include + +namespace gr { + +using namespace details ; + +FolderSet::FolderSet( ) +{ +} + +FolderSet::FolderSet( const FolderSet& fs ) +{ + const Set& s = fs.m_set.get() ; + for ( Set::const_iterator i = s.begin() ; i != s.end() ; ++i ) + m_set.insert( new Collection( **i ) ) ; +} + +FolderSet::~FolderSet( ) +{ + // delete all pointers + const Set& s = m_set.get() ; + std::for_each( s.begin(), s.end(), Destroy() ) ; +} + +void FolderSet::Swap( FolderSet& fs ) +{ + m_set.swap( fs.m_set ) ; +} + +FolderSet& FolderSet::operator=( const FolderSet& fs ) +{ + FolderSet tmp( fs ) ; + Swap( tmp ) ; + return *this ; +} + +Collection* FolderSet::FindByHref( const std::string& href ) +{ + HrefMap& map = m_set.get() ; + HrefMap::iterator i = map.find( href ) ; + return i != map.end() ? *i : 0 ; +} + +const Collection* FolderSet::FindByHref( const std::string& href ) const +{ + const HrefMap& map = m_set.get() ; + HrefMap::const_iterator i = map.find( href ) ; + return i != map.end() ? *i : 0 ; +} + +/// Reinsert should be called when the ID/HREF were updated +bool FolderSet::ReInsert( Collection *coll ) +{ + Set& s = m_set.get() ; + Set::iterator i = s.find( coll ) ; + if ( i != s.end() ) + { + s.erase( i ) ; + m_set.insert( coll ) ; + return true ; + } + else + return false ; +} + +} // end of namespace diff --git a/libgrive/src/drive/FolderSet.hh b/libgrive/src/drive/FolderSet.hh new file mode 100644 index 0000000..1696cb2 --- /dev/null +++ b/libgrive/src/drive/FolderSet.hh @@ -0,0 +1,80 @@ +/* + grive: an GPL program to sync a local directory with Google Drive + Copyright (C) 2012 Wan Wai Ho + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation version 2 + of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#pragma once + +#include "Collection.hh" + +#include "util/FileSystem.hh" + +#include +#include +#include +#include + +namespace gr { + +namespace details +{ + using namespace boost::multi_index ; + struct ByID {} ; + struct ByHref {} ; + struct ByIdentity {} ; + + typedef multi_index_container< + Collection*, + indexed_by< + hashed_non_unique, const_mem_fun >, + hashed_non_unique, const_mem_fun >, + hashed_unique, identity > + > + > Folders ; + + typedef Folders::index::type IDMap ; + typedef Folders::index::type HrefMap ; + typedef Folders::index::type Set ; +} + +/*! \brief A simple container for storing folders + + This class stores a set of folders and provide fast search access from ID, HREF etc. + It is a wrapper around multi_index_container from Boost library. +*/ +class FolderSet +{ +public : + FolderSet( ) ; + FolderSet( const FolderSet& fs ) ; + ~FolderSet( ) ; + + void Swap( FolderSet& fs ) ; + FolderSet& operator=( const FolderSet& fs ) ; + + Collection* FindByHref( const std::string& href ) ; + const Collection* FindByHref( const std::string& href ) const ; + + bool ReInsert( Collection *coll ) ; +// void SetID( Collection *coll, const std::string& id ) ; +// void SetHref( Collection *coll, const std::string& href ) ; + +private : + details::Folders m_set ; +} ; + +} // end of namespace diff --git a/libgrive/src/drive/State.cc b/libgrive/src/drive/State.cc index bea61b3..957fb67 100644 --- a/libgrive/src/drive/State.cc +++ b/libgrive/src/drive/State.cc @@ -110,7 +110,7 @@ namespace > Folders ; typedef Folders::index::type FoldersByHref ; - typedef Folders::index::type FolderSet ; + typedef Folders::index::type FSet ; } struct State::Impl @@ -274,8 +274,8 @@ bool State::Update( const Entry& e ) if ( child != 0 ) { // since we are updating the ID and Href, we need to remove it and re-add it. - FolderSet& fs = m_impl->folders.get() ; - FolderSet::iterator c = fs.find( child ) ; + FSet& fs = m_impl->folders.get() ; + FSet::iterator c = fs.find( child ) ; if ( c != fs.end() ) fs.erase( c ) ; diff --git a/libgrive/src/drive/State.hh b/libgrive/src/drive/State.hh index 338e9f2..968d6eb 100644 --- a/libgrive/src/drive/State.hh +++ b/libgrive/src/drive/State.hh @@ -19,6 +19,7 @@ #pragma once +#include "FolderSet.hh" #include "util/FileSystem.hh" #include @@ -58,6 +59,8 @@ private : private : struct Impl ; std::auto_ptr m_impl ; + + FolderSet m_folders ; } ; } // end of namespace