From 43561467ef75e49e0ee517f714b1ac9ad637fede Mon Sep 17 00:00:00 2001 From: Andy Pack Date: Fri, 21 Apr 2023 17:50:56 +0100 Subject: [PATCH] initial skeleton --- .gitattributes | 2 + .github/workflows/ci.yml | 13 ++ .gitignore | 279 +++++++++++++++++++++++++++ CMakeLists.txt | 26 +++ README.md | 2 + cbuild | 2 + include/cppScratch/dal/dal.hpp | 11 ++ include/cppScratch/test.hpp | 12 ++ libs/dal/CMakeLists.txt | 24 +++ libs/dal/include/dal/secret.hpp | 5 + libs/dal/src/lib.cpp | 85 ++++++++ libs/dal/src/secret.cpp | 8 + libs/library/CMakeLists.txt | 15 ++ libs/library/include/library/lib.hpp | 3 + libs/library/src/lib.cpp | 8 + src/CMakeLists.txt | 11 ++ src/main.cpp | 10 + 17 files changed, 516 insertions(+) create mode 100644 .gitattributes create mode 100644 .github/workflows/ci.yml create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 README.md create mode 100755 cbuild create mode 100644 include/cppScratch/dal/dal.hpp create mode 100644 include/cppScratch/test.hpp create mode 100644 libs/dal/CMakeLists.txt create mode 100644 libs/dal/include/dal/secret.hpp create mode 100644 libs/dal/src/lib.cpp create mode 100644 libs/dal/src/secret.cpp create mode 100644 libs/library/CMakeLists.txt create mode 100644 libs/library/include/library/lib.hpp create mode 100644 libs/library/src/lib.cpp create mode 100644 src/CMakeLists.txt create mode 100644 src/main.cpp diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..85f4db7 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,13 @@ +name: ci + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Build + run: mkdir build && ../cbuild \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5257758 --- /dev/null +++ b/.gitignore @@ -0,0 +1,279 @@ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Bb]uild/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..e553a34 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,26 @@ +# CMakeList.txt : CMake project for ImageMan, include source and define +# project specific logic here. +# +cmake_minimum_required(VERSION 3.19) + +if(${CMAKE_VERSION} VERSION_LESS 3.19) + cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) +endif() + +include(FetchContent) + +project(cppScratch + VERSION + 1.0 + DESCRIPTION + "C++ scratchpad" + LANGUAGES + CXX) + +# set(CMAKE_CXX_STANDARD 17) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) + +add_subdirectory(libs/library) +add_subdirectory(libs/dal) +add_subdirectory(src) \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..2664bd3 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# cppScratch + Testing c++ \ No newline at end of file diff --git a/cbuild b/cbuild new file mode 100755 index 0000000..909003a --- /dev/null +++ b/cbuild @@ -0,0 +1,2 @@ +cmake .. -DCMAKE_BUILD_TYPE=Debug +cmake --build . \ No newline at end of file diff --git a/include/cppScratch/dal/dal.hpp b/include/cppScratch/dal/dal.hpp new file mode 100644 index 0000000..1e28757 --- /dev/null +++ b/include/cppScratch/dal/dal.hpp @@ -0,0 +1,11 @@ +// ImageMan.h : Include file for standard system include files, +// or project specific include files. + +#pragma once + +#include +#include + +void dal_func(); + +// TODO: Reference additional headers your program requires here. diff --git a/include/cppScratch/test.hpp b/include/cppScratch/test.hpp new file mode 100644 index 0000000..74e0038 --- /dev/null +++ b/include/cppScratch/test.hpp @@ -0,0 +1,12 @@ +// ImageMan.h : Include file for standard system include files, +// or project specific include files. + +#pragma once + +#include +#include +// #include + +void example_func(); + +// TODO: Reference additional headers your program requires here. diff --git a/libs/dal/CMakeLists.txt b/libs/dal/CMakeLists.txt new file mode 100644 index 0000000..ed5a3e6 --- /dev/null +++ b/libs/dal/CMakeLists.txt @@ -0,0 +1,24 @@ +set(LIB_NAME "dal") + +add_library(${LIB_NAME} src/lib.cpp src/secret.cpp) + +FetchContent_Declare( + libpqxx + GIT_REPOSITORY https://github.com/jtv/libpqxx.git + GIT_TAG 7.7.5 +) + +FetchContent_MakeAvailable(libpqxx) + +# We need this directory, and users of our library will need it too +target_include_directories(${LIB_NAME} PUBLIC ../../include PRIVATE ./include) +target_link_libraries(${LIB_NAME} PUBLIC pqxx) + +# All users of this library will need at least C++11 +target_compile_features(${LIB_NAME} PUBLIC cxx_std_17) + +# IDEs should put the headers in a nice place +source_group( + TREE "${PROJECT_SOURCE_DIR}/include" + PREFIX "Header Files" + FILES ${HEADER_LIST}) \ No newline at end of file diff --git a/libs/dal/include/dal/secret.hpp b/libs/dal/include/dal/secret.hpp new file mode 100644 index 0000000..17bc0b5 --- /dev/null +++ b/libs/dal/include/dal/secret.hpp @@ -0,0 +1,5 @@ +#pragma once + +#include + +void dal_secret(); \ No newline at end of file diff --git a/libs/dal/src/lib.cpp b/libs/dal/src/lib.cpp new file mode 100644 index 0000000..67b6bfe --- /dev/null +++ b/libs/dal/src/lib.cpp @@ -0,0 +1,85 @@ +#include "cppScratch/dal/dal.hpp" +#include "dal/secret.hpp" + +using namespace std; + +void dal_func() { + cout << "hello from dal" << endl; + dal_secret(); + + try + { + // Connect to the database. You can have multiple connections open + // at the same time, even to the same database. + pqxx::connection c; + std::cout << "Connected to " << c.dbname() << '\n'; + + // Start a transaction. A connection can only have one transaction + // open at the same time, but after you finish a transaction, you + // can start a new one on the same connection. + pqxx::work tx{c}; + + // Query data of two columns, converting them to std::string and + // int respectively. Iterate the rows. + for (auto [name, salary] : tx.query( + "SELECT name, salary FROM employee ORDER BY name")) + { + std::cout << name << " earns " << salary << ".\n"; + } + + // For large amounts of data, "streaming" the results is more + // efficient. It does not work for all types of queries though. + // + // You can read fields as std::string_view here, which is not + // something you can do in most places. A string_view becomes + // meaningless when the underlying string ceases to exist. In this + // one situation, you can convert a field to string_view and it + // will be valid for just that one iteration of the loop. The next + // iteration may overwrite or deallocate its buffer space. + for (auto [name, salary] : tx.stream( + "SELECT name, salary FROM employee")) + { + std::cout << name << " earns " << salary << ".\n"; + } + + // Execute a statement, and check that it returns 0 rows of data. + // This will throw pqxx::unexpected_rows if the query returns rows. + std::cout << "Doubling all employees' salaries...\n"; + tx.exec0("UPDATE employee SET salary = salary*2"); + + // Shorthand: conveniently query a single value from the database. + int my_salary = tx.query_value( + "SELECT salary FROM employee WHERE name = 'Me'"); + std::cout << "I now earn " << my_salary << ".\n"; + + // Or, query one whole row. This function will throw an exception + // unless the result contains exactly 1 row. + auto [top_name, top_salary] = tx.query1( + R"( + SELECT name, salary + FROM employee + WHERE salary = max(salary) + LIMIT 1 + )"); + std::cout << "Top earner is " << top_name << " with a salary of " + << top_salary << ".\n"; + + // If you need to access the result metadata, not just the actual + // field values, use the "exec" functions. Most of them return + // pqxx::result objects. + pqxx::result res = tx.exec("SELECT * FROM employee"); + std::cout << "Columns:\n"; + for (pqxx::row_size_type col = 0; col < res.columns(); ++col) + std::cout << res.column_name(col) << '\n'; + + // Commit the transaction. If you don't do this, the database will + // undo any changes you made in the transaction. + std::cout << "Making changes definite: "; + tx.commit(); + std::cout << "OK.\n"; + } + catch (std::exception const &e) + { + std::cerr << "ERROR: " << e.what() << '\n'; + } +} \ No newline at end of file diff --git a/libs/dal/src/secret.cpp b/libs/dal/src/secret.cpp new file mode 100644 index 0000000..d61bad4 --- /dev/null +++ b/libs/dal/src/secret.cpp @@ -0,0 +1,8 @@ +#include "dal/secret.hpp" + +using namespace std; + +void dal_secret() +{ + cout << "secret shhh" << endl; +} \ No newline at end of file diff --git a/libs/library/CMakeLists.txt b/libs/library/CMakeLists.txt new file mode 100644 index 0000000..792a8bf --- /dev/null +++ b/libs/library/CMakeLists.txt @@ -0,0 +1,15 @@ +set(LIB_NAME "library") + +add_library(${LIB_NAME} src/lib.cpp) + +# We need this directory, and users of our library will need it too +target_include_directories(${LIB_NAME} PUBLIC ../../include PRIVATE ./include) + +# All users of this library will need at least C++11 +target_compile_features(${LIB_NAME} PUBLIC cxx_std_17) + +# IDEs should put the headers in a nice place +source_group( + TREE "${PROJECT_SOURCE_DIR}/include" + PREFIX "Header Files" + FILES ${HEADER_LIST}) \ No newline at end of file diff --git a/libs/library/include/library/lib.hpp b/libs/library/include/library/lib.hpp new file mode 100644 index 0000000..cdf0cec --- /dev/null +++ b/libs/library/include/library/lib.hpp @@ -0,0 +1,3 @@ +#pragma once + +#include \ No newline at end of file diff --git a/libs/library/src/lib.cpp b/libs/library/src/lib.cpp new file mode 100644 index 0000000..84f71ad --- /dev/null +++ b/libs/library/src/lib.cpp @@ -0,0 +1,8 @@ +#include "cppScratch/test.hpp" +#include "library/lib.hpp" + +using namespace std; + +void example_func() { + cout << "hello from a func" << endl; +} \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..b105814 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,11 @@ +add_executable(cppScratch main.cpp) + +FetchContent_Declare( + json + GIT_REPOSITORY https://github.com/nlohmann/json.git + GIT_TAG v3.11.2 +) + +FetchContent_MakeAvailable(json) + +target_link_libraries(cppScratch PRIVATE nlohmann_json::nlohmann_json dal library) \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..7682156 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,10 @@ +#include "cppScratch/test.hpp" +#include "cppScratch/dal/dal.hpp" + +using namespace std; + +int main(void) { + cout << "hello world" << endl; + dal_func(); + example_func(); +} \ No newline at end of file