C++静态类成员变量和成员函数
静态类的功能:
一次构造,共享一个内存空间,且在多个其他类调用不用重复构造
头文件 calculateBufferSize.h
#ifndef CALCULATE_BUFFER_SIZE
#define CALCULATE_BUFFER_SIZE
#include <mutex>
#include <memory>
#include <iostream>
#include <glm/glm.hpp>
class CalculateBufferSize {
public:
typedef std::shared_ptr<CalculateBufferSize> Ptr;
CalculateBufferSize(){
image_number = 20;
image_width = 1920;
image_height = 1080;
// 计算空间大小bytes,申请内存空间
image_size = image_width * image_height * 3ULL; // 单张彩色图大小
mask_size = image_width * image_height * 1ULL; // 单张mask图大小
mask_dt_size = image_width * image_height * sizeof(float); // 单张mask图大小
all_images_size = image_size * image_number;
all_masks_size = mask_size * image_number;
all_masks_dt_size = mask_dt_size * image_number;
buffer_size = all_images_size + all_masks_size + all_masks_dt_size * 2;
real_KRt44_size = sizeof(glm::mat4);
all_real_KRt44_size = real_KRt44_size * image_number;
real_Rt44_size = sizeof(glm::mat4);
all_real_Rt44_size = real_Rt44_size * image_number;
mp_real_K44_size = sizeof(glm::mat4);
all_mp_real_K44_size = mp_real_K44_size * image_number;
buffer_real_P_size = all_real_KRt44_size + all_real_Rt44_size + all_mp_real_K44_size;
};
~CalculateBufferSize(){};
CalculateBufferSize(CalculateBufferSize &) = delete; // 禁用拷贝构造
CalculateBufferSize& operator=(const CalculateBufferSize &) = delete; // 禁用赋值
static Ptr GetInstance() {
if(m_CalculateBufferSize == nullptr) {
std::lock_guard<std::mutex> lk(m_mutex);
if(m_CalculateBufferSize == nullptr) {
m_CalculateBufferSize = std::shared_ptr<CalculateBufferSize>(new CalculateBufferSize());
}
}
std::cout << "CalculateBufferSize类: 引用计数: " << m_CalculateBufferSize.use_count() << std::endl;
return m_CalculateBufferSize;
}
uint64_t image_number;
uint64_t image_width;
uint64_t image_height;
// 计算空间大小bytes,申请内存空间
uint64_t image_size ; // 单张彩色图大小
uint64_t mask_size; // 单张mask图大小
uint64_t mask_dt_size; // 单张mask图大小
uint64_t all_images_size;
uint64_t all_masks_size ;
uint64_t all_masks_dt_size ;
uint64_t buffer_size ;
uint64_t real_KRt44_size ;
uint64_t all_real_KRt44_size ;
uint64_t real_Rt44_size ;
uint64_t all_real_Rt44_size ;
uint64_t mp_real_K44_size;
uint64_t all_mp_real_K44_size;
uint64_t buffer_real_P_size ;
private:
static Ptr m_CalculateBufferSize;
static std::mutex m_mutex;
};
CalculateBufferSize::Ptr CalculateBufferSize::m_CalculateBufferSize = nullptr;
std::mutex CalculateBufferSize::m_mutex;
#endif
测试 main.cpp
#include <iostream>
#include "calculateBufferSize.h"
using namespace std;
void testFun(void){
auto test = CalculateBufferSize::GetInstance();
std::cout<<"test :buffer_size:"<<test->buffer_size<<std::endl;
std::cout<<"test :image_number:"<<test->image_number<<std::endl;
std::cout << "test :CalculateBufferSize内存引用计数: " << test.use_count() << std::endl;
return;
}
int main(){
std::cout<<"begin"<<std::endl;
CalculateBufferSize::Ptr test = CalculateBufferSize::GetInstance();
std::cout<<"buffer_size:"<<test->buffer_size<<std::endl;
std::cout<<"image_number:"<<test->image_number<<std::endl;
cout << "CalculateBufferSize的内存引用计数: " << test.use_count() << endl;
testFun();
cout << "CalculateBufferSize的内存引用计数: " << test.use_count() << endl;
return 0;
}
/*
CalculateBufferSize类: 引用计数: 1
buffer_size:497664000
image_number:20
CalculateBufferSize的内存引用计数: 2
CalculateBufferSize类: 引用计数: 2
test :buffer_size:497664000
test :image_number:20
test :CalculateBufferSize内存引用计数: 3
CalculateBufferSize的内存引用计数: 2
*/
cmake
cmake_minimum_required (VERSION 2.8)
project (template)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# find the required packages
find_package(GLM REQUIRED)
message(STATUS "GLM included at ${GLM_INCLUDE_DIR}")
include_directories(${CMAKE_SOURCE_DIR}/include)
set (CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE} -ldl")
add_executable(testStatic testStatic.cpp)