编写二进制文件模板功能

writing Binary File template functions

本文关键字:功能 二进制文件      更新时间:2023-10-16

我做了一些功能来编写和读取class到二进制文件。

如果参数是类,我只想打电话给第一个writeElement函数。

然后,我想在一个函数中具有int, double, size_t的其他功能。必须有一种更好的方法来做到这一点,我不必为所有不同类型创建一个新功能。

template <class T>
inline void writeElement(ostream& out, T target) { target.write(out); }
inline void writeElement(ostream& out, int target) { out.write((char*)&target, sizeof(int)); }
inline void writeElement(ostream& out, double target) { out.write((char*)&target, sizeof(double)); }
inline void writeElement(ostream& out, size_t target) { out.write((char*)&target, sizeof(size_t)); }
inline void writeElement(ostream& out, const string str)
{
    size_t size = str.size();
    writeElement(out, size);
    out.write(&str[0], size);
}
template <class T>
inline void writeElement(ostream& out, vector<T> vector)
{
    size_t size = vector.size();
    writeElement(out, size);
    for (auto &element : vector)
    {
        writeElement(out, element);
    }
}

class Header
{
public:
    string sig;
    double  version;
public:
    void read(istream& in)
    {
        readElement(in, sig);
        readElement(in, version);
    }
    void write(ostream& out)
    {
        writeElement(out, sig);
        writeElement(out, version);
    }
};

我想有很多方法可以做到这一点。

我在设置 constexpr value

的自定义类型特征的帮助下提出了Sfinae的使用
template <typename>
struct foo { static constexpr std::size_t value { 0U }; };
template <>
struct foo<int> { static constexpr std::size_t value { 1U }; };
template <>
struct foo<double> { static constexpr std::size_t value { 1U }; };
template <>
struct foo<std::size_t> { static constexpr std::size_t value { 1U }; };
template <>
struct foo<std::string> { static constexpr std::size_t value { 2U }; };

因此,您可以使用foo<T>::value

的值启用不同的写功能
template <typename T>
inline typename std::enable_if<0U == foo<T>::value>::type
   writeElement (std::ostream & out, T const & t)
 { std::cout << "generic case" << std::endl; }
template <typename T>
inline typename std::enable_if<1U == foo<T>::value>::type
   writeElement (std::ostream & out, T const & t)
 { std::cout << "int, double, size_t case" << std::endl; }
template <typename T>
inline typename std::enable_if<2U == foo<T>::value>::type
   writeElement (std::ostream & out, T const & t)
 { std::cout << "string case" << std::endl; }
template <typename T>
inline void writeElement (std::ostream & out, std::vector<T> const & v)
 { std::cout << "vector case" << std::endl; }

以下是一个完整的工作示例

#include <set>
#include <vector>
#include <iostream>
#include <type_traits>
template <typename>
struct foo { static constexpr std::size_t value { 0U }; };
template <>
struct foo<int> { static constexpr std::size_t value { 1U }; };
template <>
struct foo<double> { static constexpr std::size_t value { 1U }; };
template <>
struct foo<std::size_t> { static constexpr std::size_t value { 1U }; };
template <>
struct foo<std::string> { static constexpr std::size_t value { 2U }; };
template <typename T>
inline typename std::enable_if<0U == foo<T>::value>::type
   writeElement (std::ostream & out, T const & t)
 { std::cout << "generic case" << std::endl; }
template <typename T>
inline typename std::enable_if<1U == foo<T>::value>::type
   writeElement (std::ostream & out, T const & t)
 { std::cout << "int, double, size_t case" << std::endl; }
template <typename T>
inline typename std::enable_if<2U == foo<T>::value>::type
   writeElement (std::ostream & out, T const & t)
 { std::cout << "string case" << std::endl; }
template <typename T>
inline void writeElement (std::ostream & out, std::vector<T> const & v)
 { std::cout << "vector case" << std::endl; }
int main ()
 {
   writeElement(std::cout, std::set<int>{});
   writeElement(std::cout, 0);
   writeElement(std::cout, 0.0);
   writeElement(std::cout, std::size_t{});
   writeElement(std::cout, std::string{"0"});
   writeElement(std::cout, std::vector<int>{});
 }

然后我想在int, double, size_t中具有其他功能 一个功能。必须有一种更好的方法来做到这一点,我不应该 必须为所有不同类型创建一个新功能。

为此,您可能希望使用"特质" class-template ,并且您有一个函数为您完成工作。

//Primary templates for every other object
template<typename T, typename = void>
struct WriteHelper{
     static void write(ostream& out, T target){
         target.write(out);
     }
};
//Specialization for integral types
template<typename T>
struct WriteHelper<T, std::enable_if_t<std::is_arithmetic<T>::value >>{
     static void write(ostream& out, T target){
         out.write((char*)&target, sizeof(T));
     }
};
//Specialization for std::string
template<typename T>
struct WriteHelper<T, std::enable_if_t<std::is_same<std::string, T>::value >>{
     static void write(ostream& out, T target){
         size_t size = str.size();
         writeElement(out, size);
         out.write(&str[0], size);
     }
};

并使用为:

template<typename T, typename X = std::decay_t<T>>
inline void writeElement(ostream& out, T&& target){
      WriteHelper<X>::write(out, std::forward<T>(target));
}

只需将Sfinae用于班级,本机和其他特殊定义类型,例如:

    template <typename T, typename std::enable_if < std::is_class<T>::value, void >::type* = nullptr >
inline void writeElement( T  )
{
    std::cout << "Class write" << std::endl;
}
    template <typename T, typename std::enable_if<!std::is_class<T>::value, void>::type* = nullptr>
inline void writeElement( T  )
{
    std::cout << "Native type" << std::endl;
}
template <typename T>
inline void writeElement( const std::vector<T>& )
{
    std::cout << "vector" << std::endl;
}
inline void writeElement( std::string )
{
    std::cout << "string" << std::endl;
}
class A{};
int main()
{
    writeElement( A{} );
    writeElement( 1 );
    writeElement( std::vector<int>{1,2,3,4} );
    writeElement( std::string{"Hallo"});
}

因此,每个"本机类型"都可以使用一个模板函数来处理。您还应该去看指针,因为您无法以这种方式处理指针,因为您不能用指针本身将长度延长。

也使用sfinae并在

中分裂
  1. 具有void write(ostream&)方法的类:

    template <typename T>
    typename std::enable_if<std::is_same<decltype(&T::write), void(T::*)(ostream&)>::value, void>::type
    writeElement(ostream& out, T& target)
    {
        target.write(out);
    }
    

    注意最好通过引用通过类,以防止复制。

  2. 算术类型(intdoublesize_t等(:

    template <typename T>
    typename std::enable_if<std::is_arithmetic<T>::value, void>::type
    writeElement(ostream& out, T target)
    {
        out.write((char*)&target, sizeof(T));
    }
    

    std::is_scalar如果要包括枚举。

  3. std::stringstd::vector<T>

    inline void writeElement(ostream& out, const string& str)
    {
        const size_t size = str.size();
        writeElement(out, size);
        out.write(str.data(), size); // NOTE: Accessing &str[0] if the `.size() == 0` is *Undefined behavior*, use `std::string::data()` insetad
    }
    template <class T>
    inline void writeElement(ostream& out, const vector<T>& vector)
    {
        const size_t size = vector.size();
        writeElement(out, size);
        for (const auto &element : vector)
        {
            writeElement(out, element);
        }
    }
    

    NOTE 访问string S &str[0]如果其尺寸为零,则导致不确定的行为