美文网首页
C++桥接模式封装C函数

C++桥接模式封装C函数

作者: FredricZhu | 来源:发表于2020-11-04 13:57 被阅读0次

程序目录如下


图片.png

代码如下,
main.cpp

#include "utils/utils.hpp"

using namespace utils;

int main() {
    utils::FileSharedPtr ptr("1.log", "r");    
    std::string s;
    int n;
    while((n=ptr.Read(s, utils::BUFFER_SIZE)) != utils::F_EOF) {
        std::cout << s;
    }
    std::cout << std::endl;
    system("pause");
    return 0;
}

utils.hpp

#ifndef UTILS_HPP
#define UTILS_HPP

#include "file_shared_ptr.hpp"




namespace utils {
    static const int BUFFER_SIZE = 50;
    static const int F_EOF = 0;
};

#endif

file_shared_ptr.hpp

#ifndef FILE_SHARED_PTR_HPP
#define FILE_SHARED_PTR_HPP

#include <boost/shared_ptr.hpp>
#include <boost/format.hpp>
#include <iostream>
#include <windows.h>
using namespace std;
using namespace boost;

namespace utils {
    class FileSharedPtr {
        private:
            class impl; // 前向声明
            boost::shared_ptr<impl> pimpl;
        public:
            FileSharedPtr(std::string name, std::string mode);
            int Read(std::string& s, size_t size);
    };
};

#endif

file_shared_ptr.cpp

#include "file_shared_ptr.hpp"


namespace utils {
    class FileSharedPtr::impl {
        private:
            impl(const impl &){}
            impl& operator=(const impl&){return *this;}
            FILE* f;
        
        public:
            impl(std::string name, std::string mode) {
                f = fopen(name.c_str(), mode.c_str());
            }

            ~impl() {
                int result = fclose(f);
                std::cout << "invoke FileSharedPtr::impl 析构函数, result= " << result << std::endl;
            }

            int Read(std::string& s, size_t size) {
                char data[size+1];
                int res = fread(data, 1, size, f);
                data[size] = '\0';
                s = boost::str(boost::format(data));
                return res; 
            }
    };

    FileSharedPtr::FileSharedPtr(std::string name, std::string mode): pimpl(new FileSharedPtr::impl(name,mode)) {}
    int FileSharedPtr::Read(std::string& s, size_t size) {
        return pimpl->Read(s, size);
    }
};

程序输出如下,
还是最后一句析构是精华


图片.png

相关文章

  • C++桥接模式封装C函数

    程序目录如下 代码如下,main.cpp utils.hpp file_shared_ptr.hpp file_s...

  • 桥接模式C++

    合成/聚合复用原则 合成/聚合复用原则(CARP):尽量使用合成/聚合,尽量不用使用类继承(这是一种强耦合)。优先...

  • c++桥接模式

    1.桥接模式简介    桥接(Bridge)模式的定义如下:将抽象与实现分离,使它们可以独立变化。它是用组合关系代...

  • 【C++设计模式】桥接模式

  • 桥接模式 C++实现

    桥接模式:将抽象部分与实现部分分离,使它们都可以独立的变化。主要解决:在有多种可能会变化的情况下,用继承会造成类爆...

  • 第一周(Geek Band)

    C++实现数据和函数的封装 C++面向对象(ObjectOriented) 基本格式 Inline内联函数关键字 ...

  • 【C++ 设计模式】7.桥接模式

    桥接模式:一种结构型设计模式 应用场景:一个对象由多部分属性组成,而对象与属性之间的关系,有些为has a,有些为...

  • 桥接模式(Bridge Pattern)

    桥接模式,桥梁模式 意图:将抽象和实现解耦,让它们可以独立变化。 桥接模式基于类的最小设计原则,通过使用封装、聚合...

  • 深刻剖析之c++博客文章

    三大特性 封装、继承、多态 多态 C++ 虚函数表解析C++多态的实现原理 介绍了类的多态(虚函数和动态/迟绑定)...

  • C++知识点

    C++基本方法: C++ memcpy C++基本特性: C++引用(vs指针) C++指针 C++封装: 将...

网友评论

      本文标题:C++桥接模式封装C函数

      本文链接:https://www.haomeiwen.com/subject/blrgvktx.html