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 00030 namespace cinder { 00031 00032 typedef shared_ptr<class DataSource> DataSourceRef; 00033 00034 class DataSource { 00035 public: 00036 virtual bool isFilePath() = 0; 00037 virtual bool isUrl() = 0; 00038 00039 const std::string& getFilePath(); 00040 const Url& getUrl(); 00041 const std::string& getFilePathHint(); 00042 00043 Buffer& getBuffer(); 00044 virtual IStreamRef createStream() = 0; 00045 00046 protected: 00047 DataSource( const std::string &aFilePath, const Url &aUrl ) 00048 : mFilePath( aFilePath ), mUrl( aUrl ) 00049 {} 00050 virtual ~DataSource() {} 00051 00052 virtual void createBuffer() = 0; 00053 00054 void setFilePathHint( const std::string &aFilePathHint ); 00055 00056 Buffer mBuffer; 00057 std::string mFilePath, mFilePathHint; 00058 Url mUrl; 00059 }; 00060 00061 00062 typedef shared_ptr<class DataSourcePath> DataSourcePathRef; 00063 00064 class DataSourcePath : public DataSource { 00065 public: 00066 static DataSourcePathRef createRef( const std::string &path ); 00067 00068 virtual bool isFilePath() { return true; } 00069 virtual bool isUrl() { return false; } 00070 00071 virtual IStreamRef createStream(); 00072 00073 protected: 00074 DataSourcePath( const std::string &path ); 00075 00076 virtual void createBuffer(); 00077 00078 IStreamFileRef mStream; 00079 }; 00080 00081 DataSourcePathRef loadFile( const std::string &path ); 00082 00083 #if ! defined( CINDER_COCOA_TOUCH ) 00084 typedef shared_ptr<class DataSourceUrl> DataSourceUrlRef; 00085 00086 class DataSourceUrl : public DataSource { 00087 public: 00088 static DataSourceUrlRef createRef( const Url &Url ); 00089 00090 virtual bool isFilePath() { return false; } 00091 virtual bool isUrl() { return true; } 00092 00093 virtual IStreamRef createStream(); 00094 00095 protected: 00096 DataSourceUrl( const Url &Url ); 00097 00098 virtual void createBuffer(); 00099 00100 IStreamUrlRef mStream; 00101 }; 00102 00103 DataSourceUrlRef loadUrl( const Url &Url ); 00104 #endif 00105 00106 typedef shared_ptr<class DataSourceBuffer> DataSourceBufferRef; 00107 00108 class DataSourceBuffer : public DataSource { 00109 public: 00110 static DataSourceBufferRef createRef( Buffer buffer, const std::string &filePathHint = "" ); 00111 00112 virtual bool isFilePath() { return false; } 00113 virtual bool isUrl() { return false; } 00114 00115 virtual IStreamRef createStream(); 00116 00117 protected: 00118 DataSourceBuffer( Buffer aBuffer ); 00119 00120 virtual void createBuffer(); 00121 00122 IStreamMemRef mStream; 00123 }; 00124 00125 } // namespace cinder