X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fnetradiant.git;a=blobdiff_plain;f=libs%2Fstringio.h;h=47772ee47845d139e9bac1e625811a893249bbc1;hp=0eb5d337269f161cfb14a258b1dbf0c8928723c9;hb=42856811f2b174e37a761bfcb9fbeb8c6af2f558;hpb=2a1cfc426e60b77c7b212d827e2592de01041266 diff --git a/libs/stringio.h b/libs/stringio.h index 0eb5d337..47772ee4 100644 --- a/libs/stringio.h +++ b/libs/stringio.h @@ -29,6 +29,7 @@ #include "iscriplib.h" #include "string/string.h" #include "generic/callback.h" +#include "property.h" inline float string_read_float( const char* string ){ return static_cast( atof( string ) ); @@ -287,183 +288,78 @@ inline TextOutputStreamType& ostream_write( TextOutputStreamType& outputStream, } - - -inline void CopiedString_importString( CopiedString& self, const char* string ){ - self = string; -} -typedef ReferenceCaller CopiedStringImportStringCaller; -inline void CopiedString_exportString( const CopiedString& self, const ImportExportCallback::Import_t& importer ){ - importer( self.c_str() ); -} -typedef ConstReferenceCaller::Import_t&), CopiedString_exportString> CopiedStringExportStringCaller; - -inline void Bool_importString( bool& self, const char* string ){ - self = string_equal( string, "true" ); -} -typedef ReferenceCaller BoolImportStringCaller; -inline void Bool_exportString( const bool& self, const ImportExportCallback::Import_t& importer ){ - importer( self ? "true" : "false" ); -} -typedef ConstReferenceCaller::Import_t&), Bool_exportString> BoolExportStringCaller; - -inline void Int_importString( int& self, const char* string ){ - if ( !string_parse_int( string, self ) ) { - self = 0; - } -} -typedef ReferenceCaller IntImportStringCaller; -inline void Int_exportString( const int& self, const ImportExportCallback::Import_t& importer ){ - char buffer[16]; - sprintf( buffer, "%d", self ); - importer( buffer ); -} -typedef ConstReferenceCaller::Import_t&), Int_exportString> IntExportStringCaller; - -inline void Size_importString( std::size_t& self, const char* string ){ - int i; - if ( string_parse_int( string, i ) && i >= 0 ) { - self = i; - } - else - { - self = 0; +template<> +struct PropertyImpl { + static void Export(const bool &self, const Callback &returnz) { + returnz(self ? "true" : "false"); } -} -typedef ReferenceCaller SizeImportStringCaller; -inline void Size_exportString( const std::size_t& self, const ImportExportCallback::Import_t& importer ){ - char buffer[16]; - sprintf( buffer, "%u", Unsigned( self ) ); - importer( buffer ); -} -typedef ConstReferenceCaller::Import_t&), Size_exportString> SizeExportStringCaller; -inline void Float_importString( float& self, const char* string ){ - if ( !string_parse_float( string, self ) ) { - self = 0; - } -} -typedef ReferenceCaller FloatImportStringCaller; -inline void Float_exportString( const float& self, const ImportExportCallback::Import_t& importer ){ - char buffer[16]; - sprintf( buffer, "%g", self ); - importer( buffer ); -} -typedef ConstReferenceCaller::Import_t&), Float_exportString> FloatExportStringCaller; + static void Import(bool &self, const char *value) { + self = string_equal(value, "true"); + } +}; -inline void Vector3_importString( Vector3& self, const char* string ){ - if ( !string_parse_vector3( string, self ) ) { - self = Vector3( 0, 0, 0 ); +template<> +struct PropertyImpl { + static void Export(const int &self, const Callback &returnz) { + char buffer[16]; + sprintf(buffer, "%d", self); + returnz(buffer); } -} -typedef ReferenceCaller Vector3ImportStringCaller; -inline void Vector3_exportString( const Vector3& self, const ImportExportCallback::Import_t& importer ){ - char buffer[64]; - sprintf( buffer, "%g %g %g", self[0], self[1], self[2] ); - importer( buffer ); -} -typedef ConstReferenceCaller::Import_t&), Vector3_exportString> Vector3ExportStringCaller; - - -template -class ImportConvert1 -{ -public: -static void thunk( void* environment, FirstArgument firstArgument ){ - Caller::thunk( environment, FirstConversion( firstArgument ) ); -} + static void Import(int &self, const char *value) { + if (!string_parse_int(value, self)) { + self = 0; + } + } }; +template<> +struct PropertyImpl { + static void Export(const std::size_t &self, const Callback &returnz) { + char buffer[16]; + sprintf(buffer, "%u", Unsigned(self)); + returnz(buffer); + } -class BoolFromString -{ -bool m_value; -public: -BoolFromString( const char* string ){ - Bool_importString( m_value, string ); -} -operator bool() const -{ - return m_value; -} + static void Import(std::size_t &self, const char *value) { + int i; + if (string_parse_int(value, i) && i >= 0) { + self = i; + } else { + self = 0; + } + } }; -inline void Bool_toString( const ImportExportCallback::Import_t& self, bool value ){ - Bool_exportString( value, self ); -} -typedef ConstReferenceCaller::Import_t, void(bool), Bool_toString> BoolToString; - - -template -inline ImportExportCallback::Import_t makeBoolStringImportCallback( const Caller& caller ){ - return ImportExportCallback::Import_t( caller.getEnvironment(), ImportConvert1::Import_t, 0>, Caller, BoolFromString>::thunk ); -} - -template -inline ImportExportCallback::Export_t makeBoolStringExportCallback( const Caller& caller ){ - return ImportExportCallback::Export_t( caller.getEnvironment(), ImportConvert1::Export_t, 0>, Caller, BoolToString>::thunk ); -} - +template<> +struct PropertyImpl { + static void Export(const float &self, const Callback &returnz) { + char buffer[16]; + sprintf(buffer, "%g", self); + returnz(buffer); + } -class IntFromString -{ -int m_value; -public: -IntFromString( const char* string ){ - Int_importString( m_value, string ); -} -operator int() const -{ - return m_value; -} + static void Import(float &self, const char *value) { + if (!string_parse_float(value, self)) { + self = 0; + } + } }; -inline void Int_toString( const ImportExportCallback::Import_t& self, int value ){ - Int_exportString( value, self ); -} -typedef ConstReferenceCaller::Import_t, void(int), Int_toString> IntToString; - - -template -inline ImportExportCallback::Import_t makeIntStringImportCallback( const Caller& caller ){ - return ImportExportCallback::Import_t( caller.getEnvironment(), ImportConvert1::Import_t, 0>, Caller, IntFromString>::thunk ); -} - -template -inline ImportExportCallback::Export_t makeIntStringExportCallback( const Caller& caller ){ - return ImportExportCallback::Export_t( caller.getEnvironment(), ImportConvert1::Export_t, 0>, Caller, IntToString>::thunk ); -} - - +template<> +struct PropertyImpl { + static void Export(const Vector3 &self, const Callback &returnz) { + char buffer[64]; + sprintf(buffer, "%g %g %g", self[0], self[1], self[2]); + returnz(buffer); + } -class SizeFromString -{ -std::size_t m_value; -public: -SizeFromString( const char* string ){ - Size_importString( m_value, string ); -} -operator std::size_t() const -{ - return m_value; -} + static void Import(Vector3 &self, const char *value) { + if (!string_parse_vector3(value, self)) { + self = Vector3(0, 0, 0); + } + } }; -inline void Size_toString( const ImportExportCallback::Import_t& self, std::size_t value ){ - Size_exportString( value, self ); -} -typedef ConstReferenceCaller::Import_t, void(std::size_t), Size_toString> SizeToString; - - -template -inline ImportExportCallback::Import_t makeSizeStringImportCallback( const Caller& caller ){ - return ImportExportCallback::Import_t( caller.getEnvironment(), ImportConvert1::Import_t, 0>, Caller, SizeFromString>::thunk ); -} - -template -inline ImportExportCallback::Export_t makeSizeStringExportCallback( const Caller& caller ){ - return ImportExportCallback::Export_t( caller.getEnvironment(), ImportConvert1::Export_t, 0>, Caller, SizeToString>::thunk ); -} - #endif