added folder set

pull/40/head
Matchman Green 2012-05-18 08:52:11 +08:00
parent 9511492c65
commit 37f2066727
4 changed files with 175 additions and 3 deletions

View File

@ -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 <algorithm>
namespace gr {
using namespace details ;
FolderSet::FolderSet( )
{
}
FolderSet::FolderSet( const FolderSet& fs )
{
const Set& s = fs.m_set.get<ByIdentity>() ;
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<ByIdentity>() ;
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<ByHref>() ;
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<ByHref>() ;
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<ByIdentity>() ;
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

View File

@ -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 <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/mem_fun.hpp>
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<tag<ByHref>, const_mem_fun<Collection, std::string, &Collection::SelfHref> >,
hashed_non_unique<tag<ByID>, const_mem_fun<Collection, std::string, &Collection::ResourceID> >,
hashed_unique<tag<ByIdentity>, identity<Collection*> >
>
> Folders ;
typedef Folders::index<ByID>::type IDMap ;
typedef Folders::index<ByHref>::type HrefMap ;
typedef Folders::index<ByIdentity>::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

View File

@ -110,7 +110,7 @@ namespace
> Folders ;
typedef Folders::index<ByHref>::type FoldersByHref ;
typedef Folders::index<ByIdentity>::type FolderSet ;
typedef Folders::index<ByIdentity>::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<ByIdentity>() ;
FolderSet::iterator c = fs.find( child ) ;
FSet& fs = m_impl->folders.get<ByIdentity>() ;
FSet::iterator c = fs.find( child ) ;
if ( c != fs.end() )
fs.erase( c ) ;

View File

@ -19,6 +19,7 @@
#pragma once
#include "FolderSet.hh"
#include "util/FileSystem.hh"
#include <memory>
@ -58,6 +59,8 @@ private :
private :
struct Impl ;
std::auto_ptr<Impl> m_impl ;
FolderSet m_folders ;
} ;
} // end of namespace