inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_sg_sfs 00005 #define inlib_sg_sfs 00006 00007 #include "sf" 00008 00009 #include "../vec3f" 00010 #include "../sg/enums" //hjust,vjust 00011 #include "../rotf" 00012 #include "../img" 00013 #include "../vdata" 00014 #include "../cstr" 00015 00016 namespace inlib { 00017 namespace sg { 00018 00019 template <class T,class TT> //exa sf_vec<colorf,float> 00020 class sf_vec : public bsf<T> { 00021 public: 00022 virtual bool write(io::iwbuf& a_buffer) { 00023 const std::vector<TT>& vec = bsf<T>::m_value.vector(); 00024 return a_buffer.write_vec(vec.size(),vec_data(vec)); 00025 } 00026 virtual bool read(io::irbuf& a_buffer) { 00027 std::vector<TT>& vec = bsf<T>::m_value.vector(); 00028 return a_buffer.read_std_vec(vec); 00029 } 00030 virtual bool dump(std::ostream& a_out) { 00031 a_out << bsf<T>::m_value << std::endl; 00032 return true; 00033 } 00034 public: 00035 sf_vec(): bsf<T>(){} 00036 sf_vec(const T& a_value): bsf<T>(a_value){} 00037 virtual ~sf_vec(){} 00038 public: 00039 sf_vec(const sf_vec& a_from) 00040 : bsf<T>(a_from){} 00041 sf_vec& operator=(const sf_vec& a_from){ 00042 bsf<T>::operator=(a_from); 00043 return *this; 00044 } 00045 public: 00046 sf_vec& operator=(const T& a_value){ 00047 bsf<T>::operator=(a_value); 00048 return *this; 00049 } 00050 }; 00051 00052 template <class T> 00053 class sf_enum : public bsf<T> { 00054 public: 00055 virtual bool write(io::iwbuf& a_buffer) { 00056 return a_buffer.write((uint16)bsf<T>::m_value); 00057 } 00058 virtual bool read(io::irbuf& a_buffer) { 00059 uint16 v; 00060 if(!a_buffer.read(v)) return false; 00061 bsf<T>::m_value = (T)v; 00062 return true; 00063 } 00064 virtual bool dump(std::ostream&) { 00065 //a_out << bsf<T>::m_value << std::endl; 00066 return true; 00067 } 00068 public: 00069 sf_enum(): bsf<T>(){} 00070 sf_enum(const T& a_value): bsf<T>(a_value){} 00071 virtual ~sf_enum(){} 00072 public: 00073 sf_enum(const sf_enum& a_from): bsf<T>(a_from){} 00074 sf_enum& operator=(const sf_enum& a_from){ 00075 bsf<T>::operator=(a_from); 00076 return *this; 00077 } 00078 public: 00079 sf_enum& operator=(const T& a_value){ 00080 bsf<T>::operator=(a_value); 00081 return *this; 00082 } 00083 }; 00084 00085 class sf_string : public bsf<std::string> { 00086 public: 00087 virtual bool write(io::iwbuf& a_buffer) { 00088 return a_buffer.write_cstr(m_value.c_str()); 00089 } 00090 virtual bool read(io::irbuf& a_buffer) { 00091 char* cstr = 0; 00092 if(!a_buffer.read_cstr(cstr)) return false; 00093 m_value = cstr; 00094 str_del(cstr); 00095 return true; 00096 } 00097 virtual bool dump(std::ostream&) { 00098 //a_out << bsf<T>::m_value << std::endl; 00099 return true; 00100 } 00101 public: 00102 sf_string(): bsf<std::string>(){} 00103 sf_string(const std::string& a_value): bsf<std::string>(a_value){} 00104 virtual ~sf_string(){} 00105 public: 00106 sf_string(const sf_string& a_from): bsf<std::string>(a_from){} 00107 sf_string& operator=(const sf_string& a_from){ 00108 bsf<std::string>::operator=(a_from); 00109 return *this; 00110 } 00111 public: 00112 sf_string& operator=(const std::string& a_value){ 00113 bsf<std::string>::operator=(a_value); 00114 return *this; 00115 } 00116 }; 00117 00118 class sf_rotf : public bsf<rotf> { 00119 public: 00120 virtual bool write(io::iwbuf& a_buffer) { 00121 const std::vector<float>& vec = m_value.quat().vector(); 00122 return a_buffer.write_vec(vec.size(),vec_data(vec)); 00123 } 00124 virtual bool read(io::irbuf& a_buffer) { 00125 std::vector<float>& vec = m_value.quat().vector(); 00126 return a_buffer.read_std_vec(vec); 00127 } 00128 virtual bool dump(std::ostream&) { 00129 //a_out << bsf<T>::m_value << std::endl; 00130 return true; 00131 } 00132 public: 00133 sf_rotf(): bsf<rotf>(){} 00134 sf_rotf(const rotf& a_value): bsf<rotf>(a_value){} 00135 virtual ~sf_rotf(){} 00136 public: 00137 sf_rotf(const sf_rotf& a_from): bsf<rotf>(a_from){} 00138 sf_rotf& operator=(const sf_rotf& a_from){ 00139 bsf<rotf>::operator=(a_from); 00140 return *this; 00141 } 00142 public: 00143 sf_rotf& operator=(const rotf& a_value){ 00144 bsf<rotf>::operator=(a_value); 00145 return *this; 00146 } 00147 }; 00148 00149 template <class T> 00150 class sf_img : public bsf< img<T> > { 00151 public: 00152 virtual bool write(io::iwbuf& a_buffer) { 00153 //FIXME : have a iwbuf.write_img<T> ? 00154 img<T>& im = bsf< img<T> >::m_value; 00155 if(!a_buffer.write((uint32)im.width())) return false; 00156 if(!a_buffer.write((uint32)im.height())) return false; 00157 if(!a_buffer.write((uint32)im.bpp())) return false; 00158 return a_buffer.write_vec(im.size(),im.buffer()); 00159 } 00160 virtual bool read(io::irbuf& /*a_buffer*/) { 00161 //FIXME : have a iwbuf.write_img<T> ? 00162 /* 00163 img<T>& im = bsf< img<T> >::m_value; 00164 if(!a_buffer.read((uint32)im.width())) return false; 00165 if(!a_buffer.read((uint32)im.height())) return false; 00166 if(!a_buffer.read((uint32)im.bpp())) return false; 00167 return a_buffer.read_vec(im.size(),im.buffer()); 00168 */ 00169 return false; 00170 } 00171 virtual bool dump(std::ostream&) { 00172 //a_out << bsf<T>::m_value << std::endl; 00173 return true; 00174 } 00175 public: 00176 sf_img(): bsf< img<T> >(){} 00177 sf_img(const img<T>& a_value): bsf< img<T> >(a_value){} 00178 virtual ~sf_img(){} 00179 public: 00180 sf_img(const sf_img& a_from): bsf< img<T> >(a_from){} 00181 sf_img& operator=(const sf_img& a_from){ 00182 bsf< img<T> >::operator=(a_from); 00183 return *this; 00184 } 00185 public: 00186 sf_img& operator=(const img<T>& a_value){ 00187 bsf< img<T> >::operator=(a_value); 00188 return *this; 00189 } 00190 }; 00191 00192 }} 00193 00194 #endif