feat: CPlusPlus Studio

This commit is contained in:
XMuli 2022-12-17 19:48:56 +08:00
parent 07d3684eed
commit fa8e6f3c8b
No known key found for this signature in database
GPG Key ID: 9554B5DD5B8E986A
8 changed files with 503 additions and 0 deletions

31
Studio/Studio.sln Normal file
View File

@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.32802.440
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Studio", "Studio\Studio.vcxproj", "{11117C51-BEAF-4F63-B386-CDB0760BCDE8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{11117C51-BEAF-4F63-B386-CDB0760BCDE8}.Debug|x64.ActiveCfg = Debug|x64
{11117C51-BEAF-4F63-B386-CDB0760BCDE8}.Debug|x64.Build.0 = Debug|x64
{11117C51-BEAF-4F63-B386-CDB0760BCDE8}.Debug|x86.ActiveCfg = Debug|Win32
{11117C51-BEAF-4F63-B386-CDB0760BCDE8}.Debug|x86.Build.0 = Debug|Win32
{11117C51-BEAF-4F63-B386-CDB0760BCDE8}.Release|x64.ActiveCfg = Release|x64
{11117C51-BEAF-4F63-B386-CDB0760BCDE8}.Release|x64.Build.0 = Release|x64
{11117C51-BEAF-4F63-B386-CDB0760BCDE8}.Release|x86.ActiveCfg = Release|Win32
{11117C51-BEAF-4F63-B386-CDB0760BCDE8}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BBED567D-5B88-44EF-BE70-53896DD79856}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,37 @@
/*******************************************************************
* Copyright (c) 2022~2023 XMuli All rights reserved.
* GitHub: https://github.com/XMuli
* Description: ÀàÖÐ×Ö½ÚÔÆëÅÐÏ
******************************************************************/
// NOTE:
#pragma once
class A
{
private:
int m_a;
char m_c1;
char m_c2;
static int g_sta;
}; // 8
class B
{
public:
void fun() {};
private:
char m_c1;
int m_a;
char m_c2;
}; // 12
class C
{
public:
virtual void fun() {};
private:
int m_a;
}; // 8

View File

@ -0,0 +1 @@
#include "SharedPtr.h"

117
Studio/Studio/SharedPtr.h Normal file
View File

@ -0,0 +1,117 @@
/*******************************************************************
* Copyright (c) 2022~2023 XMuli All rights reserved.
* GitHub: https://github.com/XMuli
* Description: C++ shared_ptr 线访线
* Note: 使, _ptr_refCount_pMutex release()
* Ref: https://juejin.cn/post/7111726931301072910#heading-8
* https://cloud.tencent.com/developer/article/1688444
* https://blog.csdn.net/Z_Stand/article/details/98512756
******************************************************************/
#pragma once
#include <iostream>
#include <mutex>
using namespace std;
template <typename T>
class SharedPtr
{
public:
SharedPtr() : _ptr(nullptr), _refCount(nullptr), _pMutex(nullptr) { cout << "default constructor" << endl; };
SharedPtr(T* obj) : _ptr(obj), _refCount(new int(1)), _pMutex(new mutex) { cout << "no default constructor" << endl; };
SharedPtr(const SharedPtr<T>& obj) // 其 _refCount 可以通过另外一个指针来修改,指向的是同一个地址
: _ptr(obj._ptr)
, _refCount(obj._refCount)
, _pMutex(obj._pMutex)
{
cout << "copy constructor" << endl;
addRefCount();
};
SharedPtr<T>& operator=(const SharedPtr<T>& obj)
{
cout << "copy assignment constructor" << endl;
if (&obj != this) {
if (_ptr != obj._ptr) {
release(); // 先释放旧的资源
_ptr = obj._ptr;
_refCount = obj._refCount;
_pMutex = obj._pMutex;
addRefCount(); // 再技计数 +1
}
}
return *this;
}
//SharedPtr(SharedPtr<T>&& obj) noexcept;
//SharedPtr<T>& operator=(SharedPtr<T>&& obj)noexcept;
~SharedPtr() { cout << "destructor" << endl; release(); }
T& operator*() { return *_ptr; }
T* operator->() { return _ptr; }
int useCount() { return *_refCount; }
T* get() { return _ptr; }
private:
void addRefCount()
{
cout << "addRefCount" << endl;
_pMutex->lock();
++*_refCount;
_pMutex->unlock();
}
void release()
{
cout << "release" << endl;
bool bDelMutex = false;
_pMutex->lock();
if (_ptr && --*_refCount == 0) { // 先校验是否存在,及计数为 0 才释放
delete _ptr;
delete _refCount;
_ptr = nullptr;
_refCount = nullptr;
bDelMutex = true;
}
_pMutex->unlock();
if (bDelMutex)
delete _pMutex;
}
private: // 需在构造函数中初始化
T* _ptr;
int* _refCount;
mutex* _pMutex; // 计数自增非原子操作,加锁解决多线程
};
int main()
{
SharedPtr<int> sp1(new int(10));
SharedPtr<int> sp2(sp1);
*sp2 = 20;
//sp1 与 sp2 在管理这部分资源,引用计数为 2
cout << sp1.useCount() << " *ptr:" << *sp1 << endl; //2 20
cout << sp2.useCount() << " *ptr:" << *sp2 << endl; //2 20
SharedPtr<int> sp3(new int(30));
sp2 = sp3; //sp3 赋值给它,释放管理的旧资源,引用计数-1
cout << sp1.useCount() << " *ptr:" << *sp1 << endl; //1 20
cout << sp2.useCount() << " *ptr:" << *sp2 << endl; //2 30
cout << sp3.useCount() << " *ptr:" << *sp3 << endl; //2 30
sp1 = sp3;
cout << sp1.useCount() << " *ptr:" << *sp1 << endl; //3 30
cout << sp2.useCount() << " *ptr:" << *sp2 << endl; //3 30
cout << sp3.useCount() << " *ptr:" << *sp3 << endl; //3 30
std::cout << "Hello World!\n";
return 0;
}

View File

@ -0,0 +1,108 @@
/*******************************************************************
* Copyright (c) 2022~2023 XMuli All rights reserved.
* GitHub: https://github.com/XMuli
* Description: C++
* Refhttps://en.cppreference.com/w/cpp/language/rule_of_three https://en.wikipedia.org/wiki/Special_member_functions
******************************************************************/
#pragma once
#include <iostream>
#include <utility>
using namespace std;
class A
{
public:
A() : m_ptr(nullptr) {
std::cout << "default constructor" << endl;
}
A(const char* s) : m_ptr(nullptr) {
std::cout << "no-default-val constructor" << endl;
if (s) {
auto n = std::strlen(s) + 1;
m_ptr = new char[n];
std::memcpy(m_ptr, s, n);
}
}
A(const A& other)
: m_ptr(nullptr) {
std::cout << "copy constructor" << endl;
if (other.m_ptr) {
auto n = std::strlen(other.m_ptr) + 1;
m_ptr = new char[n];
std::memcpy(m_ptr, other.m_ptr, n);
}
}
A& operator = (const A& other) {
std::cout << "copy assignment constructor" << endl;
if (this != &other) {
delete[] m_ptr; // Free the existing resource.
if (other.m_ptr != nullptr) {
auto n = std::strlen(other.m_ptr) + 1;
m_ptr = new char[n];
std::memcpy(m_ptr, other.m_ptr, n);
}
}
return *this;
}
A(A&& other) noexcept // 行参无 const
: m_ptr(nullptr) {
std::cout << "move constructor" << endl;
if (other.m_ptr)
m_ptr = std::move(other.m_ptr);
other.m_ptr = nullptr;
}
A& operator=(A&& other) noexcept { // 行参无 const
std::cout << "move assignment constructor" << endl;
if (this != &other) {
delete[] m_ptr; // Free the existing resource.
if (other.m_ptr)
m_ptr = std::move(other.m_ptr);
other.m_ptr = nullptr;
}
return *this;
}
~A() {
std::cout << "destructor" << endl;
if (m_ptr)
delete[] m_ptr;
}
private:
char* m_ptr;
};
A fn() {
A t("A fun()");
return t;
}
//int main()
//{
// A a1("a1"); // default constructor
// A a2(a1); // copy constructor
// A a3 = a1; // copy constructor
// a1 = a3; // copy assignment constructor
//
// std::cout << "----------------------------\n\n";
//
// //fn(); // function returning a A object
// A a5 = std::move(a1); // move constructor
// A a6; // default constructor
// a6 = std::move(a1); // move assignment constructor
// std::cout << "Hello World!\n";
//
// return 0;
//}

20
Studio/Studio/Studio.cpp Normal file
View File

@ -0,0 +1,20 @@
// Studio.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
//int main()
//{
// std::cout << "Hello World!\n";
//}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file

View File

@ -0,0 +1,153 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{11117c51-beaf-4f63-b386-cdb0760bcde8}</ProjectGuid>
<RootNamespace>Studio</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="SharedPtr.cpp" />
<ClCompile Include="Studio.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="MemoryAlignment.h" />
<ClInclude Include="SharedPtr.h" />
<ClInclude Include="SpecialMembers.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Studio.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SharedPtr.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="MemoryAlignment.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="SpecialMembers.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="SharedPtr.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>