include/cinder/DataSource.h
Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2010, The Barbarian Group
00003  All rights reserved.
00004 
00005  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
00006  the following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and
00009     the following disclaimer.
00010     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
00011     the following disclaimer in the documentation and/or other materials provided with the distribution.
00012 
00013  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00014  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00015  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00016  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00017  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00018  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00019  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00020  POSSIBILITY OF SUCH DAMAGE.
00021 */
00022 
00023 #pragma once 
00024 
00025 #include "cinder/Cinder.h"
00026 #include "cinder/Url.h"
00027 #include "cinder/Buffer.h"
00028 #include "cinder/Stream.h"
00029 #include "cinder/Filesystem.h"
00030 
00031 namespace cinder {
00032 
00033 typedef std::shared_ptr<class DataSource>   DataSourceRef;
00034 
00035 class DataSource { 
00036   public:
00037     virtual bool    isFilePath() = 0;
00038     virtual bool    isUrl() = 0;
00039     
00040     const fs::path&     getFilePath();
00041     const Url&          getUrl();
00042     const std::string&  getFilePathHint();
00043 
00044     Buffer&             getBuffer();
00045     virtual IStreamRef  createStream() = 0;
00046 
00047   protected:
00048     DataSource( const fs::path &aFilePath, const Url &aUrl )
00049         : mFilePath( aFilePath ), mUrl( aUrl ) 
00050     {}
00051     virtual ~DataSource() {}
00052     
00053     virtual void    createBuffer() = 0;
00054     
00055     void    setFilePathHint( const std::string &aFilePathHint );
00056     
00057     Buffer              mBuffer;
00058     fs::path            mFilePath;
00059     std::string         mFilePathHint;
00060     Url                 mUrl;
00061 };
00062 
00063 
00064 typedef std::shared_ptr<class DataSourcePath>   DataSourcePathRef;
00065 
00066 class DataSourcePath : public DataSource {
00067   public:
00068     static DataSourcePathRef    create( const fs::path &path );
00069 
00070     virtual bool    isFilePath() { return true; }
00071     virtual bool    isUrl() { return false; }
00072 
00073     virtual IStreamRef  createStream();
00074 
00075   protected:
00076     explicit DataSourcePath( const fs::path &path );
00077     
00078     virtual void    createBuffer();
00079     
00080     IStreamFileRef  mStream;    
00081 };
00082 
00083 DataSourceRef   loadFile( const fs::path &path );
00084 
00085 typedef std::shared_ptr<class DataSourceUrl>    DataSourceUrlRef;
00086 
00087 class DataSourceUrl : public DataSource {
00088   public:
00089     static DataSourceUrlRef create( const Url &Url );
00090 
00091     virtual bool    isFilePath() { return false; }
00092     virtual bool    isUrl() { return true; }
00093 
00094     virtual IStreamRef  createStream();
00095 
00096   protected:
00097     DataSourceUrl( const Url &Url );
00098     
00099     virtual void    createBuffer();
00100 
00101     IStreamUrlRef   mStream;
00102 };
00103 
00104 DataSourceRef           loadUrl( const Url &Url );
00105 inline DataSourceRef    loadUrl( const std::string &urlString ) { return loadUrl( Url( urlString ) ); }
00106 
00107 typedef std::shared_ptr<class DataSourceBuffer> DataSourceBufferRef;
00108 
00109 class DataSourceBuffer : public DataSource {
00110   public:
00111     static DataSourceBufferRef      create( Buffer buffer, const std::string &filePathHint = "" );
00112 
00113     virtual bool    isFilePath() { return false; }
00114     virtual bool    isUrl() { return false; }
00115 
00116     virtual IStreamRef  createStream();
00117     
00118   protected:
00119     DataSourceBuffer( Buffer aBuffer );
00120     
00121     virtual void    createBuffer();
00122     
00123     IStreamMemRef   mStream;
00124 };
00125 
00126 } // namespace cinder