00001
00002
00003 #ifndef AITOOLS_VALUE_SINGLE_TRAITS_HPP
00004 #define AITOOLS_VALUE_SINGLE_TRAITS_HPP
00005
00006 #include <string>
00007 #include <cstring>
00008 #include <iostream>
00009 #include <boost/utility.hpp>
00010 #include <boost/type_traits.hpp>
00011 #include <boost/mpl/not.hpp>
00012
00013 namespace aitools {
00014 namespace value {
00015
00016
00017
00018
00019
00020
00021
00022
00023 template <typename T>
00024 inline std::string
00025 name_of(T dummy = T());
00026
00027 template <>
00028 inline std::string
00029 name_of(int8_t dummy)
00030 {
00031 return "int8_t";
00032 }
00033
00034 template <>
00035 inline std::string
00036 name_of(int16_t dummy)
00037 {
00038 return "int16_t";
00039 }
00040
00041 template <>
00042 inline std::string
00043 name_of(int32_t dummy)
00044 {
00045 return "int32_t";
00046 }
00047
00048 template <>
00049 inline std::string
00050 name_of(int64_t dummy)
00051 {
00052 return "int64_t";
00053 }
00054
00055 template <>
00056 inline std::string
00057 name_of(uint8_t dummy)
00058 {
00059 return "uint8_t";
00060 }
00061
00062 template <>
00063 inline std::string
00064 name_of(uint16_t dummy)
00065 {
00066 return "uint16_t";
00067 }
00068
00069 template <>
00070 inline std::string
00071 name_of(uint32_t dummy)
00072 {
00073 return "uint32_t";
00074 }
00075
00076 template <>
00077 inline std::string
00078 name_of(uint64_t dummy)
00079 {
00080 return "uint64_t";
00081 }
00082
00083 template <>
00084 inline std::string
00085 name_of(float dummy)
00086 {
00087 return "float";
00088 }
00089
00090 template <>
00091 inline std::string
00092 name_of(double dummy)
00093 {
00094 return "double";
00095 }
00096
00097
00098
00099
00100
00101 template <typename T>
00102 inline bool
00103 print_to_os(T value, std::ostream& os)
00104 {
00105 return os << value;
00106 }
00107
00108
00109
00110 inline bool
00111 print_to_os(int8_t value, std::ostream& os)
00112 {
00113 return os << static_cast<int16_t>(value);
00114 }
00115
00116
00117
00118 inline bool
00119 print_to_os(uint8_t value, std::ostream& os)
00120 {
00121 return os << static_cast<uint16_t>(value);
00122 }
00123
00124 template <typename T>
00125 struct value_traits
00126 {
00127 typedef typename boost::enable_if<
00128 boost::is_arithmetic<T>, T>::type value_type;
00129 typedef uint16_t io_size_type;
00130
00131 static size_t
00132 size_of(const value_type& value)
00133 {
00134 return sizeof(value);
00135 }
00136
00137 static char*
00138 copy_to(const value_type& value, char* buffer)
00139 {
00140 std::memcpy(buffer, &value, sizeof(value));
00141 return buffer + sizeof(value);
00142 }
00143
00144 static const char*
00145 copy_from(value_type& value, const char* buffer)
00146 {
00147 std::memcpy(&value, buffer, sizeof(value));
00148 return buffer + sizeof(value);
00149 }
00150
00151 static bool
00152 write_to(const value_type& value, FILE* file)
00153 {
00154 return std::fwrite(&value, sizeof(value), 1, file) == 1;
00155 }
00156
00157 static bool
00158 read_from(value_type& value, FILE* file)
00159 {
00160 return std::fread(&value, sizeof(value), 1, file) == 1;
00161 }
00162
00163 static bool
00164 print_to(const value_type& value, std::ostream& os)
00165 {
00166 return print_to_os(value, os);
00167 }
00168
00169 static bool
00170 println_to(const value_type& value, std::ostream& os)
00171 {
00172 print_to(value, os);
00173 return os << '\n';
00174 }
00175
00176 static bool
00177 parse_from(value_type& value, std::istream& is)
00178 {
00179 return is >> value;
00180 }
00181
00182 static std::string
00183 type_name()
00184 {
00185 return name_of<value_type>();
00186 }
00187 };
00188
00189 }
00190
00191 using value::value_traits;
00192
00193 }
00194
00195 #endif // AITOOLS_VALUE_SINGLE_TRAITS_HPP