C++ Templates一书中的源代码,这个作者自己写了一个类似std::shared_ptr的非原子的引用计数实现。
当然没有std::shared_ptr牛笔,但是可以看出所有权转让和引用计数的原理。
CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
if(APPLE)
message(STATUS "This is Apple, do nothing.")
elseif(UNIX)
message(STATUS "This is linux, set CMAKE_PREFIX_PATH.")
set(CMAKE_PREFIX_PATH /vcpkg/ports/cppwork/vcpkg_installed/x64-linux/share)
endif(APPLE)
project(count_ptr)
add_definitions(-std=c++17)
add_definitions(-g)
find_package(ZLIB)
find_package(glog REQUIRED)
find_package(OpenCV REQUIRED )
find_package(Boost REQUIRED COMPONENTS
system
filesystem
serialization
program_options
thread
)
find_package(DataFrame REQUIRED)
if(APPLE)
MESSAGE(STATUS "This is APPLE, set INCLUDE_DIRS")
set(INCLUDE_DIRS ${Boost_INCLUDE_DIRS} /usr/local/include /usr/local/iODBC/include /opt/snowflake/snowflakeodbc/include/ ${CMAKE_CURRENT_SOURCE_DIR}/../../include/ {CMAKE_CURRENT_SOURCE_DIR}/../)
elseif(UNIX)
MESSAGE(STATUS "This is linux, set INCLUDE_DIRS")
set(INCLUDE_DIRS ${Boost_INCLUDE_DIRS} /usr/local/include ${CMAKE_CURRENT_SOURCE_DIR}/../../include/ ${CMAKE_CURRENT_SOURCE_DIR}/../)
endif(APPLE)
if(APPLE)
MESSAGE(STATUS "This is APPLE, set LINK_DIRS")
set(LINK_DIRS /usr/local/lib /usr/local/iODBC/lib /opt/snowflake/snowflakeodbc/lib/universal)
elseif(UNIX)
MESSAGE(STATUS "This is linux, set LINK_DIRS")
set(LINK_DIRS ${Boost_INCLUDE_DIRS} /usr/local/lib /vcpkg/ports/cppwork/vcpkg_installed/x64-linux/lib)
endif(APPLE)
if(APPLE)
MESSAGE(STATUS "This is APPLE, set ODBC_LIBS")
set(ODBC_LIBS iodbc iodbcinst)
elseif(UNIX)
MESSAGE(STATUS "This is linux, set LINK_DIRS")
set(ODBC_LIBS odbc odbcinst ltdl)
endif(APPLE)
include_directories(${INCLUDE_DIRS})
LINK_DIRECTORIES(${LINK_DIRS})
file( GLOB main_file_list ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
file( GLOB APP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/http/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/yaml/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/df/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/death_handler/impl/*.cpp)
add_library(${PROJECT_NAME}_lib SHARED ${APP_SOURCES})
target_link_libraries(${PROJECT_NAME}_lib ${Boost_LIBRARIES} ZLIB::ZLIB glog::glog DataFrame::DataFrame ${OpenCV_LIBS})
target_link_libraries(${PROJECT_NAME}_lib ssl crypto libgtest.a pystring libyaml-cpp.a libgmock.a ${ODBC_LIBS} libnanodbc.a pthread dl backtrace)
foreach( main_file ${main_file_list} )
file(RELATIVE_PATH filename ${CMAKE_CURRENT_SOURCE_DIR} ${main_file})
string(REPLACE ".cpp" "" file ${filename})
add_executable(${file} ${main_file})
target_link_libraries(${file} ${PROJECT_NAME}_lib)
endforeach( main_file ${main_file_list})
stdobjpolicy.hpp
#ifndef _FREDRIC_STD_OBJ_POLICY_HPP_
#define _FREDRIC_STD_OBJ_POLICY_HPP_
// 标准对象销毁器
class StandardObjectPolicy {
public:
template
void dispose(T* object) {
delete object;
}
};
#endif
stdarraypolicy.hpp
#ifndef _FREDRIC_STD_ARRAY_POLICY_HPP_
#define _FREDRIC_STD_ARRAY_POLICY_HPP_
// 标准对象销毁器
class StandardArrayPolicy {
public:
template
void dispose(T* array) {
delete [] array;
}
};
#endif
simplerefcount.hpp
#ifndef _FREDRIC_SIMPLE_REF_COUNT_HPP_
#define _FREDRIC_SIMPLE_REF_COUNT_HPP_
#include "allocator.hpp"
#include
// 简单引用计数策略类
class SimpleReferenceCount {
private:
std::size_t* counter;
public:
SimpleReferenceCount() {
counter = nullptr;
}
template
void init(T*) {
counter = alloc_counter();
*counter = 1;
}
template
void dispose(T*) {
dealloc_counter(counter);
}
template
void increment(T*) {
++*counter;
}
template
void decrement(T*) {
--*counter;
}
template
bool is_zero(T*) {
return *counter == 0;
}
};
#endif
memberrefcount.hpp
#ifndef _FREDRIC_MEMBER_REF_COUNT_HPP_
#define _FREDRIC_MEMBER_REF_COUNT_HPP_
template
class MemberReferenceCount {
public:
void init(ObjectT* object) {
object->*CountP = 1;
}
// dispose不需要做额外操作
void dispose (ObjectT*) {
}
void increment(ObjectT* object) {
++object->*CountP;
}
void decrement(ObjectT* object) {
--object->*CountP;
}
bool is_zero(ObjectT* object) {
return object->*CountP == 0;
}
};
#endif
countptr.hpp
#ifndef _FREDRIC_COUNT_PTR_HPP_
#define _FREDRIC_COUNT_PTR_HPP_
#include "simplerefcount.hpp"
#include "stdobjpolicy.hpp"
template
class CountingPtr: private CountPolicy, private ObjectPolicy {
private:
using CP = CountPolicy;
using OP = ObjectPolicy;
T* object_pointed_to;
public:
CountingPtr() {
this->object_pointed_to = nullptr;
}
explicit CountingPtr(T* p) {
this->init(p);
}
CountingPtr(CountingPtr const& cp):
CP((CP const&)cp),
OP((OP const&)cp) {
this->attach(cp);
}
~CountingPtr() {
this->detach();
}
CountingPtr& operator=(T* p) {
assert(p != this->object_pointed_to);
this->detach();
this->init(p);
return *this;
}
CountingPtr& operator=(CountingPtr const& cp) {
if(this->object_pointed_to != cp.object_pointed_to) {
this->detach();
CP::operator=((CP const&)cp);
OP::operator=((OP const&)cp);
this->attach(cp);
}
return *this;
}
T* operator->() const {
return this->object_pointed_to;
}
T& operator*() const {
return *this->object_pointed_to;
}
T* get() const {
return this->object_pointed_to;
}
private:
void init(T* p) {
if(p!=nullptr) {
CountPolicy::init(p);
}
this->object_pointed_to = p;
}
// 拷贝指针并增加引用计数
void attach(CountingPtr const& cp) {
this->object_pointed_to = cp.object_pointed_to;
if(cp.object_pointed_to != nullptr) {
CountPolicy::increment(cp.object_pointed_to);
}
}
// 减少引用计数,并销毁引用计数和对象,如果是最后一个持有引用计数的对象
void detach() {
if(this->object_pointed_to != nullptr) {
CountPolicy::decrement(this->object_pointed_to);
if(CountPolicy::is_zero(this->object_pointed_to)) {
// 销毁counter
CountPolicy::dispose(this->object_pointed_to);
// 销毁指向的对象
ObjectPolicy::dispose(this->object_pointed_to);
}
}
}
};
#endif
allocator.hpp
#ifndef _FREDRIC_ALLOCATOR_HPP_
#define _FREDRIC_ALLOCATOR_HPP_
#include
std::size_t* alloc_counter() {
return ::new std::size_t;
}
void dealloc_counter(std::size_t* ptr) {
::delete ptr;
}
#endif
main.cpp
#include "stdobjpolicy.hpp"
#include "stdarraypolicy.hpp"
#include "simplerefcount.hpp"
#include "memberrefcount.hpp"
#include "countptr.hpp"
#include
#include
class MyObject {
int count{0};
public:
using Ptr = CountingPtr>;
void do_something() {
std::cout << "Do something ..." << std::endl;
}
};
void test1();
void test2();
int main(int argc, char* argv[]) {
test1();
test2();
return EXIT_SUCCESS;
}
void test1() {
std::cout << "\ntest1()\n";
CountingPtr p0;
{
CountingPtr p1 {new int(42)};
std::cout << "*p1 = " << *p1 << std::endl;
*p1 = 17;
std::cout << "*p1 = " << *p1 << std::endl;
CountingPtr p2 = p1;
std::cout << "*p2 = " << *p2 << std::endl;
*p1 = 33;
std::cout << "*p2 = " << *p2 << std::endl;
p0 = p2;
std::cout << "*p0 = " << *p0 << std::endl;
++*p0;
++*p1;
++*p2;
std::cout << "*p0 = " << *p0 << std::endl;
std::cout << "*p1 = " << *p1 << std::endl;
std::cout << "*p2 = " << *p2 << std::endl;
}
std::cout << "After block: *p0 = " << *p0 << std::endl;
}
void test2() {
std::cout << "\ntest2()\n";
MyObject::Ptr obj_ptr {new MyObject};
obj_ptr->do_something();
CountingPtr int_arr {
new int[10]
};
auto arr = int_arr.get();
for(int i=0; i<10; ++i) {
arr[i] = i + 1;
}
for(int i=0; i<10; ++i) {
std::cout << arr[i] << " ";
}
std::cout << "\n";
}
程序输出如下,