Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added Val class for generic representation of JSON, but there's some …

…problems with maps
  • Loading branch information...
commit 658fb757e3cff08e5ca1b18b1eaa4d40636dc405 1 parent d5ca8e0
@nestal nestal authored
View
15 bgrive/src/DriveModel.cc
@@ -73,9 +73,20 @@ bool DriveModel::hasChildren( const QModelIndex& parent ) const
return Res(parent)->ChildCount() > 0 ;
}
-QModelIndex DriveModel::index( int row, int column, const QModelIndex & parent ) const
+QModelIndex DriveModel::index( int row, int column, const QModelIndex& parent_idx ) const
{
- return createIndex( row, column, const_cast<Resource*>(m_drv.Child(Res(parent), row)) ) ;
+ const Resource *parent = Res(parent_idx) ;
+
+ // check out-of-bound
+ if ( parent != 0 && static_cast<unsigned>(row) >= parent->ChildCount() )
+ BOOST_THROW_EXCEPTION(
+ Exception()
+ << InvalidRow_( row )
+ << ResourceName_( parent->Title() )
+ ) ;
+
+
+ return createIndex( row, column, const_cast<Resource*>(m_drv.Child(parent, row)) ) ;
}
const Resource* DriveModel::Res( const QModelIndex& idx ) const
View
5 bgrive/src/DriveModel.hh
@@ -23,6 +23,7 @@
#include <QtCore/QAbstractItemModel>
#include "drive2/Drive.hh"
+#include "util/Exception.hh"
namespace gr {
@@ -34,6 +35,10 @@ namespace http
class DriveModel : public QAbstractItemModel
{
public :
+ typedef boost::error_info<struct InvalidRow, int> InvalidRow_ ;
+ typedef boost::error_info<struct ResourceName, std::string> ResourceName_ ;
+
+public :
DriveModel( http::Agent *agent ) ;
// QAbstractItemModel overrides
View
29 bgrive/src/main.cc
@@ -50,36 +50,9 @@ int main( int argc, char **argv )
Json cfg = Json::Parse( &file ) ;
std::string refresh_token = cfg["refresh_token"].Str() ;
- qDebug() << refresh_token.c_str() ;
-
OAuth2 token( refresh_token, client_id, client_secret ) ;
- AuthAgent agent( token, std::auto_ptr<http::Agent>( new http::CurlAgent ) ) ;
-/* Feed feed ;
- feed.Start( &agent, feed_base + "/-/folder?max-results=50&showroot=true" ) ;
-
- do
- {
- // first, get all collections from the query result
- for ( Feed::iterator i = feed.begin() ; i != feed.end() ; ++i )
- {
- Entry e( *i ) ;
- qDebug() << e.Name().c_str() ;
- }
- } while ( feed.GetNext( &agent ) ) ;
-*/
-/* http::JsonResponse jsp ;
- agent.Get( "https://www.googleapis.com/drive/v2/files", &jsp, http::Header() ) ;
- std::cout << jsp.Response() << std::endl ;
-*/
-/* Feed feed( "https://www.googleapis.com/drive/v2/files" ) ;
- while ( feed.Next(&agent) )
- {
- std::cout << feed.Content() << std::endl ;
- }
-*/
-// Drive drive ;
-// drive.Refresh( &agent ) ;
+ AuthAgent agent( token, std::auto_ptr<http::Agent>( new http::CurlAgent ) ) ;
QApplication app( argc, argv ) ;
MainWnd wnd( &agent ) ;
View
17 libgrive/CMakeLists.txt
@@ -6,7 +6,7 @@ find_package(LibGcrypt REQUIRED)
find_package(JSONC REQUIRED)
find_package(CURL REQUIRED)
find_package(EXPAT REQUIRED)
-find_package(Boost 1.40.0 COMPONENTS program_options filesystem system REQUIRED)
+find_package(Boost 1.40.0 COMPONENTS program_options filesystem unit_test_framework system REQUIRED)
find_package(BFD)
find_package(CppUnit)
find_package(Iberty)
@@ -131,3 +131,18 @@ IF ( CPPUNIT_FOUND )
)
ENDIF ( CPPUNIT_FOUND )
+
+add_executable( btest
+ test/btest/ValTest.cc
+ test/btest/UnitTest.cc )
+
+target_link_libraries( btest
+ grive
+ ${Boost_LIBRARIES}
+)
+
+if ( WIN32 )
+else ( WIN32 )
+ set_target_properties( btest
+ PROPERTIES COMPILE_FLAGS -DBOOST_TEST_DYN_LINK )
+endif (WIN32)
View
3  libgrive/src/drive2/Feed.cc
@@ -27,7 +27,8 @@
namespace gr { namespace v2 {
-Feed::Feed( const std::string& base )
+Feed::Feed( const std::string& base ) :
+ m_base( base )
{
// Next() will grab this link
m_content.Add( "nextLink", Json(base) ) ;
View
3  libgrive/src/drive2/Feed.hh
@@ -52,7 +52,8 @@ public :
Json Content() const ;
private :
- Json m_content ;
+ std::string m_base ;
+ Json m_content ;
} ;
} } // end of namespace gr::v2
View
68 libgrive/src/drive2/Val.cc
@@ -0,0 +1,68 @@
+/*
+ grive: an GPL program to sync a local directory with Google Drive
+ Copyright (C) 2013 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 "Val.hh"
+
+namespace gr {
+
+Val::Val( ) :
+ m_base( new Impl<void> )
+{
+}
+
+Val::Val( const Val& v ) :
+ m_base( v.m_base->Clone() )
+{
+}
+
+Val::~Val()
+{
+}
+
+Val::TypeEnum Val::Type() const
+{
+ return m_base->Type() ;
+}
+
+const Val& Val::operator[]( const std::string& key ) const
+{
+ static const Val null ;
+ if ( Type() == object_type )
+ {
+ const Object& obj = As<Object>() ;
+ Object::const_iterator i = obj.find(key) ;
+ return i != obj.end() ? i->second : null ;
+ }
+ else
+ return null ;
+}
+
+const Val& Val::operator[]( std::size_t index ) const
+{
+ static const Val null ;
+ return Type() == array_type ? As<Array>().at(index) : null ;
+}
+
+void Val::Add( const std::string& key, const Val& value )
+{
+ As<Object>().insert( std::make_pair(key, value) ) ;
+}
+
+} // end of namespace
View
180 libgrive/src/drive2/Val.hh
@@ -0,0 +1,180 @@
+/*
+ grive: an GPL program to sync a local directory with Google Drive
+ Copyright (C) 2013 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 <cstddef>
+#include <memory>
+#include <vector>
+#include <map>
+#include <string>
+
+namespace gr {
+
+class Val
+{
+public :
+ enum TypeEnum { null_type, bool_type, double_type, int_type, object_type, array_type, string_type } ;
+
+private :
+ template <typename T>
+ struct Type2Enum ;
+
+ typedef std::vector<Val> Array ;
+ typedef std::map<std::string, Val> Object ;
+
+public :
+ Val() ;
+ Val( const Val& v ) ;
+ ~Val() ;
+
+ template <typename T>
+ Val( const T& t ) ;
+
+ template <typename T>
+ const T& As() const ;
+
+ template <typename T>
+ T& As() ;
+
+ template <typename T>
+ bool Is() const ;
+
+ TypeEnum Type() const ;
+
+ const Val& operator[]( const std::string& key ) const ;
+ const Val& operator[]( std::size_t index ) const ;
+
+ void Add( const std::string& key, const Val& value ) ;
+
+private :
+ struct Base ;
+
+ template <typename T>
+ struct Impl ;
+
+ std::auto_ptr<Base> m_base ;
+
+private :
+ // callback functions
+ static int OnNull( void *ctx ) ;
+ static int OnBool( void *ctx, int value ) ;
+ static int OnInt( void *ctx, long long value ) ;
+ static int OnDouble( void *ctx, double value ) ;
+ static int OnStr( void *ctx, const unsigned char *str, std::size_t len ) ;
+ static int StartMap( void *ctx ) ;
+ static int EndMap( void *ctx ) ;
+ static int StartArray( void *ctx ) ;
+ static int EndArray( void *ctx ) ;
+} ;
+
+template <>
+struct Val::Type2Enum<void>
+{
+ static const TypeEnum type = null_type ;
+} ;
+
+template <>
+struct Val::Type2Enum<int>
+{
+ static const TypeEnum type = int_type ;
+} ;
+
+template <>
+struct Val::Type2Enum<bool>
+{
+ static const TypeEnum type = bool_type ;
+} ;
+
+template <>
+struct Val::Type2Enum<double>
+{
+ static const TypeEnum type = double_type ;
+} ;
+
+template <>
+struct Val::Type2Enum<std::string>
+{
+ static const TypeEnum type = string_type ;
+} ;
+
+template <>
+struct Val::Type2Enum<Val::Array>
+{
+ static const TypeEnum type = array_type ;
+} ;
+
+template <>
+struct Val::Type2Enum<Val::Object>
+{
+ static const TypeEnum type = object_type ;
+} ;
+
+struct Val::Base
+{
+ virtual ~Base() {}
+ virtual Base* Clone() const = 0 ;
+ virtual TypeEnum Type() const = 0 ;
+} ;
+
+template <typename T>
+struct Val::Impl : public Base
+{
+ T val ;
+ Impl( const T& t ) : val(t) {}
+ Impl<T>* Clone() const { return new Impl<T>(val); }
+ TypeEnum Type() const { return Type2Enum<T>::type ; }
+} ;
+
+template <>
+struct Val::Impl<void> : public Base
+{
+ Impl<void>* Clone() const { return new Impl<void>; }
+ TypeEnum Type() const { return null_type ; }
+} ;
+
+template <typename T>
+Val::Val( const T& t ) :
+ m_base( new Impl<T>(t) )
+{
+}
+
+template <typename T>
+const T& Val::As() const
+{
+ const Impl<T> *impl = &dynamic_cast<const Impl<T>&>( *m_base ) ;
+ return impl->val ;
+}
+
+template <typename T>
+T& Val::As()
+{
+ Impl<T> *impl = &dynamic_cast<Impl<T>&>( *m_base ) ;
+ return impl->val ;
+}
+
+template <typename T>
+bool Val::Is() const
+{
+ return Type() == Type2Enum<T>::type ;
+}
+
+} // end of namespace
+
View
21 libgrive/test/btest/UnitTest.cc
@@ -0,0 +1,21 @@
+/*
+ webwrite: an GPL wiki-like website with in-place editing
+ 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.
+*/
+
+#define BOOST_TEST_MAIN
+#include <boost/test/unit_test.hpp>
View
52 libgrive/test/btest/ValTest.cc
@@ -0,0 +1,52 @@
+/*
+ webwrite: an GPL wiki-like website with in-place editing
+ 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 "drive2/Val.hh"
+#include <boost/test/unit_test.hpp>
+
+using namespace gr ;
+
+namespace
+{
+ struct Fixture
+ {
+ } ;
+}
+
+BOOST_FIXTURE_TEST_SUITE( ValTest, Fixture )
+
+BOOST_AUTO_TEST_CASE( TestSimpleTypes )
+{
+ Val null ;
+ BOOST_CHECK_EQUAL( null.Type(), Val::null_type ) ;
+ BOOST_CHECK( null.Is<void>() ) ;
+
+ Val i( 100 ) ;
+ BOOST_CHECK_EQUAL( i.As<int>(), 100 ) ;
+ BOOST_CHECK_EQUAL( i.Type(), Val::int_type ) ;
+}
+
+BOOST_AUTO_TEST_CASE( TestMap )
+{
+ Val obj ;
+ obj.Add( "key", Val( std::string("value") ) ) ;
+ BOOST_CHECK_EQUAL( obj["key"].As<std::string>(), "value" ) ;
+}
+
+BOOST_AUTO_TEST_SUITE_END()
Please sign in to comment.
Something went wrong with that request. Please try again.