diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..1f3060a --- /dev/null +++ b/.clang-format @@ -0,0 +1,2 @@ +BasedOnStyle: LLVM +Language: Cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..378eac2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..95e31c6 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,38 @@ +cmake_minimum_required(VERSION 4.3.4) +project(ppp-cpp + DESCRIPTION "Programming Principles and Practice using C++" + HOMEPAGE_URL https://github.com/wouterbeek/Programming-_Principles_and_Practice_Using_Cpp + LANGUAGES CXX + VERSION 0.1.0 +) + +set(cppVersion 23) +set(CXX_EXTENSIONS OFF) +set(CXX_STANDARD ${cppVersion}) +set(CXX_STANDARD_REQUIRED ON) + +add_library(base INTERFACE) +add_library(${PROJECT_NAME}::base ALIAS base) +target_compile_features(base INTERFACE cxx_std_${cppVersion}) + +find_package(fltk REQUIRED) + +add_library(ppp STATIC) +add_library(${PROJECT_NAME}::ppp ALIAS ppp) +target_include_directories(ppp PUBLIC include) +target_link_libraries(ppp PUBLIC fltk::fltk ${PROJECT_NAME}::base) +target_sources(ppp + PRIVATE + src/Graph_lib/Graph.cpp + src/Graph_lib/GUI.cpp + src/Graph_lib/Simple_window.cpp + src/Graph_lib/Window.cpp +) + +add_executable(chapter12) +target_link_libraries(chapter12 PRIVATE ${PROJECT_NAME}::ppp) +target_sources(chapter12 PRIVATE tool/chapter12.cpp) + +add_executable(chapter13) +target_link_libraries(chapter13 PRIVATE ${PROJECT_NAME}::ppp) +target_sources(chapter13 PRIVATE tool/chapter13.cpp) diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..8b77dc1 --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,108 @@ +{ + "version": 11, + "cmakeMinimumRequired": { + "major": 4, + "minor": 3, + "patch": 4 + }, + "configurePresets": [ + { + "binaryDir": "${sourceDir}/build/${presetName}", + "cacheVariables": { + "CMAKE_EXPORT_COMPILE_COMMANDS": "ON" + }, + "hidden": true, + "installDir": "${sourceDir}/install/${presetName}", + "name": "base", + "toolchainFile": "${sourceDir}/build/conan_toolchain.cmake" + }, + { + "cacheVariables": { + "CMAKE_CXX_COMPILER": "g++" + }, + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Linux" + }, + "generator": "Ninja", + "hidden": true, + "inherits": "base", + "name": "linux-base" + }, + { + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + }, + "displayName": "Linux Debug", + "inherits": "linux-base", + "name": "linux-debug" + }, + { + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Release" + }, + "displayName": "Linux Release", + "inherits": "linux-base", + "name": "linux-release" + }, + { + "cacheVariables": { + "CMAKE_CXX_COMPILER": "cl.exe", + "CMAKE_PREFIX_PATH": "${sourceDir}/build" + }, + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Windows" + }, + "generator": "Ninja", + "hidden": true, + "inherits": "base", + "name": "windows-base", + "vendor": { + "microsoft.com/VisualStudioSettings/CMake/1.0": { + "hostOS": ["Windows"], + "intelliSenseMode": "windows-msvc-x64" + }, + "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": { + "hostOS": ["Windows"] + } + } + }, + { + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + }, + "displayName": "Windows Debug", + "inherits": "windows-base", + "name": "windows-debug" + }, + { + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Release" + }, + "displayName": "Windows Release", + "inherits": "windows-base", + "name": "windows-release" + } + ], + "buildPresets": [ + { + "configurePreset": "linux-debug", + "name": "linux-debug" + }, + { + "configurePreset": "linux-release", + "name": "linux-release" + }, + { + "configurePreset": "windows-debug", + "name": "windows-debug" + }, + { + "configurePreset": "windows-release", + "name": "windows-release" + } + ] +} diff --git a/GUI.cpp b/GUI.cpp deleted file mode 100644 index b0b2d81..0000000 --- a/GUI.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include "GUI.h" -#include "std_lib_facilities.h" -#include - -using namespace Graph_lib; - - -void Button::attach(Window& win) - { - pw = new Fl_Button(loc.x, loc.y, width, height, label.c_str()); - pw->callback(reinterpret_cast(do_it), &win); // pass the window - own = &win; - } - -int In_box::get_int() -{ - Fl_Input& pi = reference_to(pw); -// return atoi(pi.value()); - const char* p = pi.value(); - if (!isdigit(p[0])) return -999999; - return atoi(p); -} - -string In_box::get_string() -{ - Fl_Input& pi = reference_to(pw); - return string(pi.value()); -} - -void In_box::attach(Window& win) -{ - pw = new Fl_Input(loc.x, loc.y, width, height, label.c_str()); - own = &win; -} - -void Out_box::put(int i) -{ - Fl_Output& po = reference_to(pw); - std::stringstream ss; - ss << i; - po.value(ss.str().c_str()); -} - -void Out_box::put(const string& s) -{ - reference_to(pw).value(s.c_str()); -} - -void Out_box::attach(Window& win) -{ - pw = new Fl_Output(loc.x, loc.y, width, height, label.c_str()); - own = &win; -} - -Menu::Menu(Point xy, int w, int h, Kind kk, const string& s) -:Widget(xy,w,h,s,0), k(kk), offset(0) -{ -} - -int Menu::attach(Button& b) -{ - b.width = width; - b.height = height; - - switch(k) { - case horizontal: - b.loc = Point(loc.x+offset,loc.y); - offset+=b.width; - break; - case vertical: - b.loc = Point(loc.x,loc.y+offset); - offset+=b.height; - break; - } - selection.push_back(&b); - return int(selection.size()-1); -} - -int Menu::attach(Button* p) -{ -// owned.push_back(p); - return attach(*p); -} \ No newline at end of file diff --git a/GUI.h b/GUI.h deleted file mode 100644 index 0c7c78c..0000000 --- a/GUI.h +++ /dev/null @@ -1,133 +0,0 @@ - -// -// This is a GUI support code to the chapters 12-16 of the book -// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup -// - -#ifndef GUI_GUARD -#define GUI_GUARD - -#include "Window.h" -#include "Graph.h" - -namespace Graph_lib { - -//------------------------------------------------------------------------------ - - typedef void* Address; // Address is a synonym for void* - typedef void(*Callback)(Address, Address); // FLTK's required function type for all callbacks - -//------------------------------------------------------------------------------ - - template W& reference_to(Address pw) - // treat an address as a reference to a W - { - return *static_cast(pw); - } - -//------------------------------------------------------------------------------ - - class Widget { - // Widget is a handle to an Fl_widget - it is *not* an Fl_widget - // We try to keep our interface classes at arm's length from FLTK - public: - Widget(Point xy, int w, int h, const string& s, Callback cb) - : loc(xy), width(w), height(h), label(s), do_it(cb) - {} - - virtual void move(int dx,int dy) { hide(); pw->position(loc.x+=dx, loc.y+=dy); show(); } - virtual void hide() { pw->hide(); } - virtual void show() { pw->show(); } - virtual void attach(Window&) = 0; - - Point loc; - int width; - int height; - string label; - Callback do_it; - - virtual ~Widget() { } - - protected: - Window* own; // every Widget belongs to a Window - Fl_Widget* pw; // connection to the FLTK Widget - private: - Widget& operator=(const Widget&); // don't copy Widgets - Widget(const Widget&); - }; - -//------------------------------------------------------------------------------ - - struct Button : Widget { - Button(Point xy, int w, int h, const string& label, Callback cb) - : Widget(xy,w,h,label,cb) - {} - - void attach(Window&); - }; - -//------------------------------------------------------------------------------ - - struct In_box : Widget { - In_box(Point xy, int w, int h, const string& s) - :Widget(xy,w,h,s,0) { } - int get_int(); - string get_string(); - - void attach(Window& win); - }; - -//------------------------------------------------------------------------------ - - struct Out_box : Widget { - Out_box(Point xy, int w, int h, const string& s) - :Widget(xy,w,h,s,0) { } - void put(int); - void put(const string&); - - void attach(Window& win); - }; - -//------------------------------------------------------------------------------ - - struct Menu : Widget { - enum Kind { horizontal, vertical }; - Menu(Point xy, int w, int h, Kind kk, const string& label) - : Widget(xy,w,h,label,0), k(kk), offset(0) - {} - - Vector_ref