├── Ex02_Factory_Method ├── src │ ├── 2_factory_method_intermediate │ │ ├── factory │ │ ├── factory.h │ │ ├── main.cc │ │ ├── factory.cc │ │ ├── course.h │ │ └── makefile │ ├── 4_factory_method_advanced_template │ │ ├── factory-advanced │ │ ├── net.cc │ │ ├── java.cc │ │ ├── cpp.cc │ │ ├── course.h │ │ ├── net.h │ │ ├── cpp.h │ │ ├── java.h │ │ ├── main.cc │ │ ├── factory.cc │ │ ├── factory.h │ │ └── makefile │ ├── 3_factory_method_advanced │ │ ├── java.cc │ │ ├── cpp.cc │ │ ├── course.h │ │ ├── cpp.h │ │ ├── java.h │ │ ├── main.cc │ │ ├── factory.cc │ │ ├── factory.h │ │ └── makefile │ ├── 5_generic_factory_method │ │ ├── java.cc │ │ ├── cpp.cc │ │ ├── course.h │ │ ├── cpp.h │ │ ├── java.h │ │ ├── main.cc │ │ ├── factory.h │ │ └── makefile │ ├── 6_generic_factory_constructor │ │ ├── java.cc │ │ ├── cpp.cc │ │ ├── course.h │ │ ├── cpp.h │ │ ├── java.h │ │ ├── main.cc │ │ ├── factory.h │ │ └── makefile │ └── 1_factory_method_simple │ │ └── factory_method.cc ├── Factory_Method.odp ├── Factory_Method.pdf └── Factory_Method.pptx ├── Ex13_Threads ├── src │ ├── thread │ ├── multithread │ ├── lambdathread │ ├── sync │ │ ├── 1_mutex │ │ │ ├── atomic │ │ │ ├── guard │ │ │ ├── mutex │ │ │ ├── atomic.cc │ │ │ ├── mutex.cc │ │ │ └── guard.cc │ │ ├── 0_nosync │ │ │ ├── nosync │ │ │ └── nosync.cc │ │ ├── 3_deadlock │ │ │ ├── deadlock │ │ │ ├── deadlock.cc │ │ │ └── deadlock_fixed.cc │ │ └── 2_exception_lock │ │ │ ├── unsafe.cc │ │ │ ├── auto_safe.cc │ │ │ └── safe.cc │ ├── thread.cc │ ├── lambdathread.cc │ └── multithreads.cc ├── Ex13_Threads.odp └── Ex13_Threads.pdf ├── Ex05_Prototype ├── Prototype.odp ├── Prototype.pdf └── src │ ├── 0_prototype_example │ ├── prototype.h │ └── prototype.cc │ ├── 2_document_manager_prototype_factory │ ├── document.h │ └── document.cc │ ├── 3_document_manager_prototype_factory_template │ ├── document.h │ ├── document.cc │ └── makefile │ └── 1_document_manager │ └── docmanager.cc ├── Ex06_Pointers_I ├── figs │ ├── memory.png │ ├── pointers.png │ ├── Pointers_Address.png │ ├── Pointers_Address.xcf │ ├── memory.svg │ └── pointers.svg ├── Ex6_Pointers-I.odp └── Ex6_Pointers-I.pdf ├── Ex11_Classes_I ├── Ex11_Classes.odp ├── Ex11_Classes.pdf └── src │ ├── implicit_constructor.cc │ ├── explicit_constructor.cc │ ├── bad_class.h │ └── good_class.h ├── Ex12_Parallelisation ├── src │ ├── array │ ├── hello │ ├── hello-cc │ ├── sum_bad │ ├── sum_good │ ├── multithread │ ├── thread.cc │ ├── sum_bad.cc │ ├── sum_good.cc │ ├── multithreads.cc │ ├── hello.cc │ └── add_array.cc ├── Ex12_Parallelisation.odp └── Ex12_Parallelisation.pdf ├── Ex15_Functors ├── Ex15_Functors.odp ├── Ex15_Functors.pdf └── src │ ├── nested_functions │ ├── nested_functor.cc │ └── nested_complete_solution.cc │ ├── accessors │ ├── getset_overload.cc │ ├── getset.cc │ ├── functor_accesor.cc │ └── templatised_functor_accesor.cc │ ├── functor.cc │ └── function │ └── function.cc ├── Ex22_C++_Quiz ├── Ex22_C++_Quiz.odp ├── Ex22_C++_Quiz.pdf └── src │ ├── rvalue.cc │ ├── pointer.cc │ ├── template_function_call.cc │ ├── lvalue.cc │ ├── namespace.cc │ ├── template_specialisation.cc │ └── template_member_function.cc ├── Ex07_Pointers_II ├── Ex7_Pointers-II.odp └── Ex7_Pointers-II.pdf ├── Ex08_Pointers_III ├── Ex8_Pointers-III.odp └── Ex8_Pointers-III.pdf ├── Ex17_RTTI_Traits ├── Ex17_RTTI_Traits.odp ├── Ex17_RTTI_Traits.pdf └── src │ ├── accumator.cc │ ├── typeid.cc │ ├── dynamic_cast.cc │ ├── accumator_specialisation.cc │ ├── accumator_class.cc │ ├── accumator_trait_policy.cc │ └── .clang-format ├── Ex03_Smart_Pointers ├── Ex3_Smart_Pointers.odp └── Ex3_Smart_Pointers.pdf ├── Ex04_Lamda_Functions ├── Ex4_Lamda_Functions.odp └── Ex4_Lamda_Functions.pdf ├── Ex09_Exception_Handling_I ├── Ex9_Exception_Handling_I.odp ├── Ex9_Exception_Handling_I.pdf └── src │ ├── vector_error.cc │ ├── vector_exception.cc │ └── exception.cc ├── Ex10_Exception_Handling_II ├── Ex10_Exception_Handling_II.odp ├── Ex10_Exception_Handling_II.pdf └── src │ ├── multiple_noexception.cc │ └── multiple_exception.cc ├── Ex16_Rvalue_Move_Semantics ├── Ex16_Rvalue_Move_Semantics.odp ├── Ex16_Rvalue_Move_Semantics.pdf └── src │ ├── address.cc │ ├── copyconstructor_bad.cc │ ├── copyconstructor.cc │ ├── myvector.cc │ ├── myvector_move.cc │ ├── myvector_mult_bad.cc │ └── myvector_mult_good.cc ├── Ex20_Metaclasses_Reflection ├── CPP_Metaclasses_Reflection.pdf ├── Ex20_Metaclasses_Reflection.odp ├── Ex20_Metaclasses_Reflection.pdf └── src │ └── class_def.h ├── Ex21_Name_Lookup_Interface ├── Ex21_Name_Lookup_Interface.odp ├── Ex21_Name_Lookup_Interface.pdf └── src │ └── base_derived.cc ├── Ex01_Design_Pattern_Abstract_Factory ├── Ex1_Abstract_Factory.odp └── src │ ├── bad_code │ └── bad.cc │ ├── abstract_factory │ ├── abstract_factory.cc │ └── valgrind.txt │ └── abstract_factory_good │ ├── abstract_factory_good.cc │ └── valgrind.txt ├── Ex14_Memory_Models_Compiler_Optimisation ├── Figs │ ├── store_buffer.png │ ├── Transformation_code.png │ └── Transformation_Code.svg ├── Ex14_Memory_Models_Compiler_Optimisation.odp ├── Ex14_Memory_Models_Compiler_Optimisation.pdf └── src │ ├── vectorize.cc │ ├── nosync.cc │ ├── mutex.cc │ └── dekker.cc ├── Ex19_Lazy_Evaluation_Reference_Counting ├── Ex19_Lazy_Evaluation_Reference_Counting.odp └── Ex19_Lazy_Evaluation_Reference_Counting.pdf ├── Ex18_Tuple_Variadic_Templates_Metaprogramming ├── Ex18_Variadic_Templates_Metaprogramming.odp ├── Ex18_Variadic_Templates_Metaprogramming.pdf └── src │ ├── dot_product_simple.cc │ ├── template_metaprogram_n3.cc │ ├── variadic_function.cc │ ├── variadic_template.cc │ ├── dot_product_metaprogram.cc │ └── tuple.cc ├── .gitignore ├── LICENSE └── README.md /Ex02_Factory_Method/src/2_factory_method_intermediate/factory: -------------------------------------------------------------------------------- 1 | bin/release/factory -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/4_factory_method_advanced_template/factory-advanced: -------------------------------------------------------------------------------- 1 | bin/release/factory-advanced -------------------------------------------------------------------------------- /Ex13_Threads/src/thread: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex13_Threads/src/thread -------------------------------------------------------------------------------- /Ex05_Prototype/Prototype.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex05_Prototype/Prototype.odp -------------------------------------------------------------------------------- /Ex05_Prototype/Prototype.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex05_Prototype/Prototype.pdf -------------------------------------------------------------------------------- /Ex13_Threads/src/multithread: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex13_Threads/src/multithread -------------------------------------------------------------------------------- /Ex06_Pointers_I/figs/memory.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex06_Pointers_I/figs/memory.png -------------------------------------------------------------------------------- /Ex11_Classes_I/Ex11_Classes.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex11_Classes_I/Ex11_Classes.odp -------------------------------------------------------------------------------- /Ex11_Classes_I/Ex11_Classes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex11_Classes_I/Ex11_Classes.pdf -------------------------------------------------------------------------------- /Ex12_Parallelisation/src/array: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex12_Parallelisation/src/array -------------------------------------------------------------------------------- /Ex12_Parallelisation/src/hello: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex12_Parallelisation/src/hello -------------------------------------------------------------------------------- /Ex13_Threads/Ex13_Threads.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex13_Threads/Ex13_Threads.odp -------------------------------------------------------------------------------- /Ex13_Threads/Ex13_Threads.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex13_Threads/Ex13_Threads.pdf -------------------------------------------------------------------------------- /Ex13_Threads/src/lambdathread: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex13_Threads/src/lambdathread -------------------------------------------------------------------------------- /Ex15_Functors/Ex15_Functors.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex15_Functors/Ex15_Functors.odp -------------------------------------------------------------------------------- /Ex15_Functors/Ex15_Functors.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex15_Functors/Ex15_Functors.pdf -------------------------------------------------------------------------------- /Ex22_C++_Quiz/Ex22_C++_Quiz.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex22_C++_Quiz/Ex22_C++_Quiz.odp -------------------------------------------------------------------------------- /Ex22_C++_Quiz/Ex22_C++_Quiz.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex22_C++_Quiz/Ex22_C++_Quiz.pdf -------------------------------------------------------------------------------- /Ex06_Pointers_I/figs/pointers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex06_Pointers_I/figs/pointers.png -------------------------------------------------------------------------------- /Ex12_Parallelisation/src/hello-cc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex12_Parallelisation/src/hello-cc -------------------------------------------------------------------------------- /Ex12_Parallelisation/src/sum_bad: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex12_Parallelisation/src/sum_bad -------------------------------------------------------------------------------- /Ex12_Parallelisation/src/sum_good: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex12_Parallelisation/src/sum_good -------------------------------------------------------------------------------- /Ex06_Pointers_I/Ex6_Pointers-I.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex06_Pointers_I/Ex6_Pointers-I.odp -------------------------------------------------------------------------------- /Ex06_Pointers_I/Ex6_Pointers-I.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex06_Pointers_I/Ex6_Pointers-I.pdf -------------------------------------------------------------------------------- /Ex07_Pointers_II/Ex7_Pointers-II.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex07_Pointers_II/Ex7_Pointers-II.odp -------------------------------------------------------------------------------- /Ex07_Pointers_II/Ex7_Pointers-II.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex07_Pointers_II/Ex7_Pointers-II.pdf -------------------------------------------------------------------------------- /Ex12_Parallelisation/src/multithread: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex12_Parallelisation/src/multithread -------------------------------------------------------------------------------- /Ex13_Threads/src/sync/1_mutex/atomic: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex13_Threads/src/sync/1_mutex/atomic -------------------------------------------------------------------------------- /Ex13_Threads/src/sync/1_mutex/guard: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex13_Threads/src/sync/1_mutex/guard -------------------------------------------------------------------------------- /Ex13_Threads/src/sync/1_mutex/mutex: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex13_Threads/src/sync/1_mutex/mutex -------------------------------------------------------------------------------- /Ex02_Factory_Method/Factory_Method.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex02_Factory_Method/Factory_Method.odp -------------------------------------------------------------------------------- /Ex02_Factory_Method/Factory_Method.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex02_Factory_Method/Factory_Method.pdf -------------------------------------------------------------------------------- /Ex08_Pointers_III/Ex8_Pointers-III.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex08_Pointers_III/Ex8_Pointers-III.odp -------------------------------------------------------------------------------- /Ex08_Pointers_III/Ex8_Pointers-III.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex08_Pointers_III/Ex8_Pointers-III.pdf -------------------------------------------------------------------------------- /Ex13_Threads/src/sync/0_nosync/nosync: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex13_Threads/src/sync/0_nosync/nosync -------------------------------------------------------------------------------- /Ex17_RTTI_Traits/Ex17_RTTI_Traits.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex17_RTTI_Traits/Ex17_RTTI_Traits.odp -------------------------------------------------------------------------------- /Ex17_RTTI_Traits/Ex17_RTTI_Traits.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex17_RTTI_Traits/Ex17_RTTI_Traits.pdf -------------------------------------------------------------------------------- /Ex02_Factory_Method/Factory_Method.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex02_Factory_Method/Factory_Method.pptx -------------------------------------------------------------------------------- /Ex06_Pointers_I/figs/Pointers_Address.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex06_Pointers_I/figs/Pointers_Address.png -------------------------------------------------------------------------------- /Ex06_Pointers_I/figs/Pointers_Address.xcf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex06_Pointers_I/figs/Pointers_Address.xcf -------------------------------------------------------------------------------- /Ex13_Threads/src/sync/3_deadlock/deadlock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex13_Threads/src/sync/3_deadlock/deadlock -------------------------------------------------------------------------------- /Ex22_C++_Quiz/src/rvalue.cc: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int&& x = int(); 4 | x = 3; 5 | std::cout << x << std::endl; 6 | } 7 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/3_factory_method_advanced/java.cc: -------------------------------------------------------------------------------- 1 | #include "java.h" 2 | #include "factory.h" 3 | 4 | REGISTER_CLASS("Java", Java); 5 | 6 | -------------------------------------------------------------------------------- /Ex03_Smart_Pointers/Ex3_Smart_Pointers.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex03_Smart_Pointers/Ex3_Smart_Pointers.odp -------------------------------------------------------------------------------- /Ex03_Smart_Pointers/Ex3_Smart_Pointers.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex03_Smart_Pointers/Ex3_Smart_Pointers.pdf -------------------------------------------------------------------------------- /Ex04_Lamda_Functions/Ex4_Lamda_Functions.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex04_Lamda_Functions/Ex4_Lamda_Functions.odp -------------------------------------------------------------------------------- /Ex04_Lamda_Functions/Ex4_Lamda_Functions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex04_Lamda_Functions/Ex4_Lamda_Functions.pdf -------------------------------------------------------------------------------- /Ex12_Parallelisation/Ex12_Parallelisation.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex12_Parallelisation/Ex12_Parallelisation.odp -------------------------------------------------------------------------------- /Ex12_Parallelisation/Ex12_Parallelisation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex12_Parallelisation/Ex12_Parallelisation.pdf -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/3_factory_method_advanced/cpp.cc: -------------------------------------------------------------------------------- 1 | #include "cpp.h" 2 | #include "factory.h" 3 | 4 | // Registering new course 5 | REGISTER_CLASS("C++", Cpp); 6 | -------------------------------------------------------------------------------- /Ex09_Exception_Handling_I/Ex9_Exception_Handling_I.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex09_Exception_Handling_I/Ex9_Exception_Handling_I.odp -------------------------------------------------------------------------------- /Ex09_Exception_Handling_I/Ex9_Exception_Handling_I.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex09_Exception_Handling_I/Ex9_Exception_Handling_I.pdf -------------------------------------------------------------------------------- /Ex10_Exception_Handling_II/Ex10_Exception_Handling_II.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex10_Exception_Handling_II/Ex10_Exception_Handling_II.odp -------------------------------------------------------------------------------- /Ex10_Exception_Handling_II/Ex10_Exception_Handling_II.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex10_Exception_Handling_II/Ex10_Exception_Handling_II.pdf -------------------------------------------------------------------------------- /Ex16_Rvalue_Move_Semantics/Ex16_Rvalue_Move_Semantics.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex16_Rvalue_Move_Semantics/Ex16_Rvalue_Move_Semantics.odp -------------------------------------------------------------------------------- /Ex16_Rvalue_Move_Semantics/Ex16_Rvalue_Move_Semantics.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex16_Rvalue_Move_Semantics/Ex16_Rvalue_Move_Semantics.pdf -------------------------------------------------------------------------------- /Ex20_Metaclasses_Reflection/CPP_Metaclasses_Reflection.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex20_Metaclasses_Reflection/CPP_Metaclasses_Reflection.pdf -------------------------------------------------------------------------------- /Ex21_Name_Lookup_Interface/Ex21_Name_Lookup_Interface.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex21_Name_Lookup_Interface/Ex21_Name_Lookup_Interface.odp -------------------------------------------------------------------------------- /Ex21_Name_Lookup_Interface/Ex21_Name_Lookup_Interface.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex21_Name_Lookup_Interface/Ex21_Name_Lookup_Interface.pdf -------------------------------------------------------------------------------- /Ex01_Design_Pattern_Abstract_Factory/Ex1_Abstract_Factory.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex01_Design_Pattern_Abstract_Factory/Ex1_Abstract_Factory.odp -------------------------------------------------------------------------------- /Ex20_Metaclasses_Reflection/Ex20_Metaclasses_Reflection.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex20_Metaclasses_Reflection/Ex20_Metaclasses_Reflection.odp -------------------------------------------------------------------------------- /Ex20_Metaclasses_Reflection/Ex20_Metaclasses_Reflection.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex20_Metaclasses_Reflection/Ex20_Metaclasses_Reflection.pdf -------------------------------------------------------------------------------- /Ex14_Memory_Models_Compiler_Optimisation/Figs/store_buffer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex14_Memory_Models_Compiler_Optimisation/Figs/store_buffer.png -------------------------------------------------------------------------------- /Ex22_C++_Quiz/src/pointer.cc: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | char* ptr; 4 | char str[] = "abcdefg"; 5 | ptr = str; 6 | ptr += 5; 7 | std::cout << ptr << std::endl; 8 | } 9 | -------------------------------------------------------------------------------- /Ex22_C++_Quiz/src/template_function_call.cc: -------------------------------------------------------------------------------- 1 | #include 2 | template 3 | void fun() { 4 | i = 20; 5 | std::cout << i << std::endl; 6 | } 7 | int main() { fun<10>(); } 8 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/4_factory_method_advanced_template/net.cc: -------------------------------------------------------------------------------- 1 | #include "net.h" 2 | #include "factory.h" 3 | 4 | //REGISTER_CLASS("Java", Java); 5 | static Registrar registrar("Net"); 6 | -------------------------------------------------------------------------------- /Ex14_Memory_Models_Compiler_Optimisation/Figs/Transformation_code.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex14_Memory_Models_Compiler_Optimisation/Figs/Transformation_code.png -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/5_generic_factory_method/java.cc: -------------------------------------------------------------------------------- 1 | #include "java.h" 2 | #include "factory.h" 3 | 4 | //REGISTER_CLASS("Java", Java); 5 | static Register registry("Java"); 6 | 7 | 8 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/4_factory_method_advanced_template/java.cc: -------------------------------------------------------------------------------- 1 | #include "java.h" 2 | #include "factory.h" 3 | 4 | //REGISTER_CLASS("Java", Java); 5 | static Registrar registrar("Java"); 6 | 7 | 8 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/6_generic_factory_constructor/java.cc: -------------------------------------------------------------------------------- 1 | #include "java.h" 2 | #include "factory.h" 3 | 4 | //REGISTER_CLASS("Java", Java); 5 | static Register registry("Java"); 6 | 7 | 8 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/5_generic_factory_method/cpp.cc: -------------------------------------------------------------------------------- 1 | #include "cpp.h" 2 | #include "factory.h" 3 | 4 | // Registering new course 5 | //REGISTER_CLASS("C++", Cpp); 6 | static Register registry("C++"); 7 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/4_factory_method_advanced_template/cpp.cc: -------------------------------------------------------------------------------- 1 | #include "cpp.h" 2 | #include "factory.h" 3 | 4 | // Registering new course 5 | //REGISTER_CLASS("C++", Cpp); 6 | static Registrar registrar("C++"); 7 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/6_generic_factory_constructor/cpp.cc: -------------------------------------------------------------------------------- 1 | #include "cpp.h" 2 | #include "factory.h" 3 | 4 | // Registering new course 5 | //REGISTER_CLASS("C++", Cpp); 6 | static Register registry("C++"); 7 | -------------------------------------------------------------------------------- /Ex19_Lazy_Evaluation_Reference_Counting/Ex19_Lazy_Evaluation_Reference_Counting.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex19_Lazy_Evaluation_Reference_Counting/Ex19_Lazy_Evaluation_Reference_Counting.odp -------------------------------------------------------------------------------- /Ex19_Lazy_Evaluation_Reference_Counting/Ex19_Lazy_Evaluation_Reference_Counting.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex19_Lazy_Evaluation_Reference_Counting/Ex19_Lazy_Evaluation_Reference_Counting.pdf -------------------------------------------------------------------------------- /Ex14_Memory_Models_Compiler_Optimisation/Ex14_Memory_Models_Compiler_Optimisation.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex14_Memory_Models_Compiler_Optimisation/Ex14_Memory_Models_Compiler_Optimisation.odp -------------------------------------------------------------------------------- /Ex14_Memory_Models_Compiler_Optimisation/Ex14_Memory_Models_Compiler_Optimisation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex14_Memory_Models_Compiler_Optimisation/Ex14_Memory_Models_Compiler_Optimisation.pdf -------------------------------------------------------------------------------- /Ex18_Tuple_Variadic_Templates_Metaprogramming/Ex18_Variadic_Templates_Metaprogramming.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex18_Tuple_Variadic_Templates_Metaprogramming/Ex18_Variadic_Templates_Metaprogramming.odp -------------------------------------------------------------------------------- /Ex18_Tuple_Variadic_Templates_Metaprogramming/Ex18_Variadic_Templates_Metaprogramming.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kks32/cpp-software-development/HEAD/Ex18_Tuple_Variadic_Templates_Metaprogramming/Ex18_Variadic_Templates_Metaprogramming.pdf -------------------------------------------------------------------------------- /Ex22_C++_Quiz/src/lvalue.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int& fun() { 4 | static int x = 10; // Remove static and see what the result is 5 | return x; 6 | } 7 | int main() { 8 | fun() = 30; 9 | std::cout << fun() << std::endl; 10 | } 11 | -------------------------------------------------------------------------------- /Ex22_C++_Quiz/src/namespace.cc: -------------------------------------------------------------------------------- 1 | #include 2 | namespace foo { 3 | int x; 4 | } 5 | namespace foo { 6 | void bar() { 7 | x++; 8 | std::cout << x << std::endl; 9 | } 10 | } 11 | int main() { 12 | foo::x = 0; 13 | foo::bar(); 14 | } 15 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/3_factory_method_advanced/course.h: -------------------------------------------------------------------------------- 1 | #ifndef _COURSE_H_ 2 | #define _COURSE_H_ 3 | 4 | //Abstract Base Class 5 | class Course 6 | { 7 | public: 8 | virtual void info() = 0; 9 | virtual void schedule() = 0; 10 | }; 11 | 12 | #endif // _COURSE_H_ 13 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/5_generic_factory_method/course.h: -------------------------------------------------------------------------------- 1 | #ifndef _COURSE_H_ 2 | #define _COURSE_H_ 3 | 4 | //Abstract Base Class 5 | class Course 6 | { 7 | public: 8 | virtual void info() = 0; 9 | virtual void schedule() = 0; 10 | }; 11 | 12 | #endif // _COURSE_H_ 13 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/6_generic_factory_constructor/course.h: -------------------------------------------------------------------------------- 1 | #ifndef _COURSE_H_ 2 | #define _COURSE_H_ 3 | 4 | //Abstract Base Class 5 | class Course 6 | { 7 | public: 8 | virtual void info() = 0; 9 | virtual void schedule() = 0; 10 | }; 11 | 12 | #endif // _COURSE_H_ 13 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/4_factory_method_advanced_template/course.h: -------------------------------------------------------------------------------- 1 | #ifndef _COURSE_H_ 2 | #define _COURSE_H_ 3 | 4 | //Abstract Base Class 5 | class Course 6 | { 7 | public: 8 | virtual void info() = 0; 9 | virtual void schedule() = 0; 10 | }; 11 | 12 | #endif // _COURSE_H_ 13 | -------------------------------------------------------------------------------- /Ex14_Memory_Models_Compiler_Optimisation/src/vectorize.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int SIZE = 100000; 4 | int main() { 5 | 6 | int a[SIZE], b[SIZE], c[SIZE]; 7 | 8 | for (int i=0; i 2 | #include 3 | #include 4 | 5 | void execute(const std::string& name) { 6 | std::cout << "Hello Concurent World, and hello " << name << std::endl; 7 | } 8 | 9 | int main() { 10 | std::string your_name = "Krishna!"; 11 | std::thread worker_thread(execute, your_name); 12 | worker_thread.join(); 13 | } 14 | -------------------------------------------------------------------------------- /Ex12_Parallelisation/src/thread.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void execute(const std::string& name) { 6 | std::cout << "Hello Concurent World, and hello " << name << std::endl; 7 | } 8 | 9 | int main() { 10 | std::string your_name = "Krishna!"; 11 | std::thread worker_thread(execute, your_name); 12 | 13 | worker_thread.join(); 14 | } 15 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/2_factory_method_intermediate/factory.h: -------------------------------------------------------------------------------- 1 | #ifndef _FACTORY_H_ 2 | #define _FACTORY_H_ 3 | 4 | #include 5 | #include "course.h" 6 | 7 | // Abstract Factory 8 | class MyCourseFactory 9 | { 10 | public: 11 | virtual ~MyCourseFactory(){} 12 | //Factory Method 13 | static std::shared_ptr generatecourse(std::string& coursename); 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /Ex22_C++_Quiz/src/template_specialisation.cc: -------------------------------------------------------------------------------- 1 | #include 2 | template 3 | T max(T& a, T& b) { 4 | std::cout << "generic"; 5 | return (a > b) ? a : b; 6 | } 7 | template <> 8 | int max(int& a, int& b) { 9 | std::cout << "specialised"; 10 | return (a > b) ? a : b; 11 | } 12 | int main() { 13 | int a = 10, b = 20; 14 | std::cout << max(a, b) << std::endl; 15 | } 16 | -------------------------------------------------------------------------------- /Ex15_Functors/src/nested_functions/nested_functor.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int f(int i) { 4 | int j = i * 2; 5 | class g_ { 6 | public: 7 | g_(int& j) : j_(j) {} 8 | // access j via a reference 9 | int operator()(int k) { return j_ + k; } 10 | 11 | private: 12 | int& j_; 13 | } g{j}; 14 | j += 4; 15 | return g(3); 16 | } 17 | 18 | int main() { std::cout << "Value of f(5) is : " << f(5) << std::endl; } 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gch* 2 | *.release_time* 3 | bin* 4 | build* 5 | _site 6 | .DS_Store 7 | *.sublime-project 8 | *.sublime-workspace 9 | codekit-config.json 10 | node_modules 11 | Gemfile.lock 12 | *~* 13 | *#* 14 | Makefile 15 | *.out* 16 | .deps/* 17 | config.log 18 | config.status 19 | autom4te.cache/* 20 | configure.scan 21 | .DS_Store* 22 | *.o 23 | *~ 24 | *.hpp~ 25 | *.ipp~ 26 | *.ipp# 27 | *.hpp# 28 | *.orig 29 | *.tar.gz 30 | *.bkp 31 | *.log 32 | *.exe 33 | -------------------------------------------------------------------------------- /Ex22_C++_Quiz/src/template_member_function.cc: -------------------------------------------------------------------------------- 1 | #include 2 | struct Base { 3 | int x; 4 | template 5 | void print() { 6 | std::cout << "Base class : " << Trange + x + 1 << std::endl; 7 | } 8 | }; 9 | struct Derived : public Base { 10 | template 11 | void print() { 12 | std::cout << "Derived class : " << Trange + x + 2 << std::endl; 13 | } 14 | }; 15 | int main() { 16 | Base* p = new Derived; 17 | p->x = 1; 18 | p->print<5>(); 19 | } 20 | -------------------------------------------------------------------------------- /Ex09_Exception_Handling_I/src/vector_error.cc: -------------------------------------------------------------------------------- 1 | // vector::at 2 | #include 3 | #include 4 | 5 | int main () 6 | { 7 | std::vector myvector (10); // 10 zero-initialized ints 8 | 9 | // assign some values: 10 | for (unsigned i=0; i 2 | 3 | class f { 4 | int retval; // f's "return value" 5 | int j; 6 | int g(int k) { return j + k; }; 7 | 8 | public: 9 | f(int i) : j(i * 2) { // original function, now a constructor 10 | j += 4; 11 | retval = g(3); 12 | } 13 | operator int() const { // returning the result 14 | return retval; 15 | } 16 | }; 17 | 18 | int main() { std::cout << "Value of f(5) is : " << f(5) << std::endl; } 19 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/3_factory_method_advanced/cpp.h: -------------------------------------------------------------------------------- 1 | #ifndef _CPP_H_ 2 | #define _CPP_H_ 3 | 4 | #include "course.h" 5 | #include 6 | 7 | class Cpp : public Course 8 | { 9 | public: 10 | Cpp(){}; 11 | virtual ~Cpp(){}; 12 | 13 | virtual void info() { 14 | std::cout << "C++ course" << std::endl; 15 | } 16 | 17 | 18 | virtual void schedule() { 19 | std::cout << "C++ schedule" << std::endl; 20 | } 21 | }; 22 | #endif // _CPP_H_ 23 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/4_factory_method_advanced_template/net.h: -------------------------------------------------------------------------------- 1 | #ifndef _NET_H_ 2 | #define _NET_H_ 3 | 4 | #include "course.h" 5 | #include 6 | 7 | class Net : public Course 8 | { 9 | public: 10 | Net(){}; 11 | virtual ~Net(){}; 12 | 13 | virtual void info() { 14 | std::cout << "Net course" << std::endl; 15 | } 16 | 17 | virtual void schedule() { 18 | std::cout << "Net schedule" << std::endl; 19 | } 20 | }; 21 | #endif // _Net_H_ 22 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/5_generic_factory_method/cpp.h: -------------------------------------------------------------------------------- 1 | #ifndef _CPP_H_ 2 | #define _CPP_H_ 3 | 4 | #include "course.h" 5 | #include 6 | 7 | class Cpp : public Course 8 | { 9 | public: 10 | Cpp(){}; 11 | virtual ~Cpp(){}; 12 | 13 | virtual void info() { 14 | std::cout << "C++ course" << std::endl; 15 | } 16 | 17 | 18 | virtual void schedule() { 19 | std::cout << "C++ schedule" << std::endl; 20 | } 21 | }; 22 | #endif // _CPP_H_ 23 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/3_factory_method_advanced/java.h: -------------------------------------------------------------------------------- 1 | #ifndef _JAVA_H_ 2 | #define _JAVA_H_ 3 | 4 | #include "course.h" 5 | #include 6 | 7 | class Java : public Course 8 | { 9 | public: 10 | Java(){}; 11 | virtual ~Java(){}; 12 | 13 | virtual void info() { 14 | std::cout << "Java course" << std::endl; 15 | } 16 | 17 | virtual void schedule() { 18 | std::cout << "Java schedule" << std::endl; 19 | } 20 | }; 21 | #endif // _JAVA_H_ 22 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/5_generic_factory_method/java.h: -------------------------------------------------------------------------------- 1 | #ifndef _JAVA_H_ 2 | #define _JAVA_H_ 3 | 4 | #include "course.h" 5 | #include 6 | 7 | class Java : public Course 8 | { 9 | public: 10 | Java(){}; 11 | virtual ~Java(){}; 12 | 13 | virtual void info() { 14 | std::cout << "Java course" << std::endl; 15 | } 16 | 17 | virtual void schedule() { 18 | std::cout << "Java schedule" << std::endl; 19 | } 20 | }; 21 | #endif // _JAVA_H_ 22 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/6_generic_factory_constructor/cpp.h: -------------------------------------------------------------------------------- 1 | #ifndef _CPP_H_ 2 | #define _CPP_H_ 3 | 4 | #include "course.h" 5 | #include 6 | 7 | class Cpp : public Course 8 | { 9 | public: 10 | Cpp(){}; 11 | virtual ~Cpp(){}; 12 | 13 | virtual void info() { 14 | std::cout << "C++ course" << std::endl; 15 | } 16 | 17 | 18 | virtual void schedule() { 19 | std::cout << "C++ schedule" << std::endl; 20 | } 21 | }; 22 | #endif // _CPP_H_ 23 | -------------------------------------------------------------------------------- /Ex09_Exception_Handling_I/src/vector_exception.cc: -------------------------------------------------------------------------------- 1 | // vector::at 2 | #include 3 | #include 4 | 5 | int main () 6 | { 7 | std::vector myvector (10); // 10 zero-initialized ints 8 | 9 | // assign some values: 10 | for (unsigned i=0; i 6 | 7 | class Cpp : public Course 8 | { 9 | public: 10 | Cpp(){}; 11 | virtual ~Cpp(){}; 12 | 13 | virtual void info() { 14 | std::cout << "C++ course" << std::endl; 15 | } 16 | 17 | 18 | virtual void schedule() { 19 | std::cout << "C++ schedule" << std::endl; 20 | } 21 | }; 22 | #endif // _CPP_H_ 23 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/6_generic_factory_constructor/java.h: -------------------------------------------------------------------------------- 1 | #ifndef _JAVA_H_ 2 | #define _JAVA_H_ 3 | 4 | #include "course.h" 5 | #include 6 | 7 | class Java : public Course 8 | { 9 | public: 10 | Java(){}; 11 | virtual ~Java(){}; 12 | 13 | virtual void info() { 14 | std::cout << "Java course" << std::endl; 15 | } 16 | 17 | virtual void schedule() { 18 | std::cout << "Java schedule" << std::endl; 19 | } 20 | }; 21 | #endif // _JAVA_H_ 22 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/4_factory_method_advanced_template/java.h: -------------------------------------------------------------------------------- 1 | #ifndef _JAVA_H_ 2 | #define _JAVA_H_ 3 | 4 | #include "course.h" 5 | #include 6 | 7 | class Java : public Course 8 | { 9 | public: 10 | Java(){}; 11 | virtual ~Java(){}; 12 | 13 | virtual void info() { 14 | std::cout << "Java course" << std::endl; 15 | } 16 | 17 | virtual void schedule() { 18 | std::cout << "Java schedule" << std::endl; 19 | } 20 | }; 21 | #endif // _JAVA_H_ 22 | -------------------------------------------------------------------------------- /Ex12_Parallelisation/src/sum_bad.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | int main(int argc, char* argv[]) { 7 | int i, n, chunk; 8 | float a[100], b[100], sum; 9 | chunk = 10; 10 | /* Some initializations */ 11 | n = 100; 12 | for (i = 0; i < n; i++) 13 | a[i] = b[i] = 1.0; 14 | sum = 0.0; 15 | 16 | #pragma omp parallel for default(shared) private(i) schedule(dynamic, chunk) 17 | for (i = 0; i < n; i++) 18 | sum += (a[i] * b[i]); 19 | 20 | printf("Sum = %f\n", sum); 21 | } 22 | -------------------------------------------------------------------------------- /Ex18_Tuple_Variadic_Templates_Metaprogramming/src/dot_product_simple.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | inline T dot_product(int dim, T* a, T* b) { 5 | T result = 0; 6 | for (int i = 0; i < dim; ++i) { 7 | result += a[i] * b[i]; 8 | } 9 | return result; 10 | } 11 | 12 | int main() { 13 | int a[3] = {1, 2, 3}; 14 | int b[3] = {5, 6, 7}; 15 | 16 | std::cout << "dot_product(3,a,b) = " << dot_product(3, a, b) << std::endl; 17 | std::cout << "dot_product(3,a,a) = " << dot_product(3, a, a) << std::endl; 18 | } 19 | -------------------------------------------------------------------------------- /Ex12_Parallelisation/src/sum_good.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | int main(int argc, char* argv[]) { 7 | int i, n, chunk; 8 | float a[100], b[100], sum; 9 | chunk = 10; 10 | /* Some initializations */ 11 | n = 100; 12 | for (i = 0; i < n; i++) 13 | a[i] = b[i] = 1.0; 14 | sum = 0.0; 15 | 16 | #pragma omp parallel for default(shared) private(i) schedule(dynamic, chunk) reduction(+:sum) 17 | for (i = 0; i < n; i++) 18 | sum += (a[i] * b[i]); 19 | 20 | printf("Sum = %f\n", sum); 21 | } 22 | -------------------------------------------------------------------------------- /Ex18_Tuple_Variadic_Templates_Metaprogramming/src/template_metaprogram_n3.cc: -------------------------------------------------------------------------------- 1 | #include 2 | // primary template to compute 3 to the Nth 3 | template 4 | class Pow3 { 5 | public: 6 | enum { result = 3 * Pow3::result }; 7 | 8 | // Why not ? - No longer a pure compiler time 9 | // static int const result = 3 * Pow3::result; 10 | }; 11 | 12 | // full specialization to end the recursion 13 | template <> 14 | class Pow3<0> { 15 | public: 16 | enum { result = 1 }; 17 | }; 18 | 19 | int main() { std::cout << "Power 3^10 is : " << Pow3<2>::result << std::endl; } 20 | -------------------------------------------------------------------------------- /Ex18_Tuple_Variadic_Templates_Metaprogramming/src/variadic_function.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template 5 | T summation(T v) { 6 | return v; 7 | } 8 | 9 | template 10 | T summation(T first, Args... args) { 11 | return first + summation(args...); 12 | } 13 | 14 | int main() { 15 | std::cout << "Sum of integers: " << summation(1, 2, 3, 4, 5) << std::endl; 16 | std::string str1 = "C++", str2 = " is ", str3 = "great!"; 17 | std::cout << "Concatenate strings: " << summation(str1, str2, str3) 18 | << std::endl; 19 | } 20 | -------------------------------------------------------------------------------- /Ex13_Threads/src/lambdathread.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | static const int num_threads = 2; 7 | 8 | int main() { 9 | std::vector threads; 10 | // Launch a group of threads 11 | for (int i = 0; i < num_threads; ++i) { 12 | threads.push_back(std::thread([]() { 13 | std::cout << " Hello from thread " << std::this_thread::get_id() 14 | << std::endl; 15 | })); 16 | } 17 | 18 | // Join the threads with the main thread 19 | for (auto& thread : threads) { 20 | thread.join(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Ex15_Functors/src/accessors/getset_overload.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Point { 4 | double Xcoord_, Ycoord_; 5 | 6 | public: 7 | Point(); 8 | Point(double Xcoord, double Ycoord) : Xcoord_{Xcoord}, Ycoord_{Ycoord} {}; 9 | double Xcoord() const { return Xcoord_; }; 10 | void Xcoord(double newValue) { Xcoord_ = newValue; }; 11 | double Ycoord() const { return Ycoord_; }; 12 | void Ycoord(double newValue) { Ycoord_ = newValue; }; 13 | }; 14 | 15 | int main() { 16 | Point point{0., 0.}; 17 | point.Xcoord(point.Xcoord() + 10.); 18 | std::cout << "The new xcoordinate is : " << point.Xcoord() << std::endl; 19 | } 20 | -------------------------------------------------------------------------------- /Ex11_Classes_I/src/implicit_constructor.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class complex { 4 | double real, img; 5 | 6 | public: 7 | complex() : real(0), img(0) {} 8 | complex(const complex& c) { 9 | real = c.real; 10 | img = c.img; 11 | } 12 | complex(double r, double i = 0.0) { 13 | real = r; 14 | img = i; 15 | } 16 | friend void display(const complex& cx); 17 | }; 18 | void display(const complex& cx) { 19 | std::cout << " Real Part : " << cx.real << " Imag Part : " << cx.img 20 | << std::endl; 21 | } 22 | int main() { 23 | complex one(1); 24 | display(one); 25 | display(300); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Ex15_Functors/src/accessors/getset.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Point { 4 | double Xcoord_, Ycoord_; 5 | 6 | public: 7 | Point(); 8 | Point(double Xcoord, double Ycoord) : Xcoord_{Xcoord}, Ycoord_{Ycoord} {}; 9 | double getXcoord() const { return Xcoord_; }; 10 | void setXcoord(double newValue) { Xcoord_ = newValue; }; 11 | double getYcoord() const { return Ycoord_; }; 12 | void setYcoord(double newValue) { Ycoord_ = newValue; }; 13 | }; 14 | 15 | int main() { 16 | Point point{0., 0.}; 17 | point.setXcoord(point.getXcoord() + 10.); 18 | std::cout << "The new xcoordinate is : " << point.getXcoord() << std::endl; 19 | } 20 | -------------------------------------------------------------------------------- /Ex13_Threads/src/multithreads.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | static const int num_threads = 10; 7 | 8 | // This function will be called from a thread 9 | void hello_id(int id) { 10 | std::cout << std::this_thread::get_id() << std::endl; 11 | 12 | } 13 | 14 | int main() { 15 | std::vector threads; 16 | // Launch a group of threads 17 | for (int i = 0; i < num_threads; ++i) { 18 | threads.push_back(std::thread(hello_id, i)); 19 | } 20 | 21 | // Join the threads with the main thread 22 | for (auto& thread : threads) { 23 | thread.join(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/5_generic_factory_method/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "course.h" 5 | #include "factory.h" 6 | 7 | // Client-side implementation 8 | int main(int argc, char **argv) { 9 | 10 | // User input 11 | if (argc != 2) { 12 | std::cerr << "Incorrect number of arguments" << std::endl; 13 | std::cerr << "Usage: ./factory course (C++/Java)" << std::endl; 14 | std::exit(EXIT_FAILURE); 15 | } 16 | std::string coursename = argv[1]; 17 | 18 | auto mycourse = Factory::instance()->create(coursename); 19 | 20 | mycourse->info(); 21 | mycourse->schedule(); 22 | } 23 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/6_generic_factory_constructor/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "course.h" 5 | #include "factory.h" 6 | 7 | // Client-side implementation 8 | int main(int argc, char **argv) { 9 | 10 | // User input 11 | if (argc != 2) { 12 | std::cerr << "Incorrect number of arguments" << std::endl; 13 | std::cerr << "Usage: ./factory course (C++/Java)" << std::endl; 14 | std::exit(EXIT_FAILURE); 15 | } 16 | std::string coursename = argv[1]; 17 | 18 | auto mycourse = Factory::instance()->create(coursename); 19 | 20 | mycourse->info(); 21 | mycourse->schedule(); 22 | } 23 | -------------------------------------------------------------------------------- /Ex11_Classes_I/src/explicit_constructor.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class complex { 4 | double real, img; 5 | 6 | public: 7 | complex() : real(0), img(0) {} 8 | complex(const complex& c) { 9 | real = c.real; 10 | img = c.img; 11 | } 12 | explicit complex(double r, double i = 0.0) { 13 | real = r; 14 | img = i; 15 | } 16 | friend void display(const complex& cx); 17 | }; 18 | void display(const complex& cx) { 19 | std::cout << " Real Part : " << cx.real 20 | << " Imag Part : " << cx.img << std::endl; 21 | } 22 | int main() { 23 | complex one(1); 24 | display(one); 25 | display(complex {300}); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Ex15_Functors/src/accessors/functor_accesor.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Coordinate { 4 | double coord_; 5 | 6 | public: 7 | Coordinate(); 8 | Coordinate(double coord) : coord_{coord} {}; 9 | double operator()() const { return coord_; } 10 | void operator()(double coord) { coord_ = coord; } 11 | }; 12 | 13 | class Point { 14 | public: 15 | Coordinate Xcoord, Ycoord; 16 | Point(); 17 | Point(double X, double Y) : Xcoord{X}, Ycoord{Y} {}; 18 | 19 | }; 20 | 21 | int main() { 22 | Point point{0., 0.}; 23 | point.Xcoord(point.Xcoord() + 10.); 24 | std::cout << "The new xcoordinate is : " << point.Xcoord() << std::endl; 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /Ex12_Parallelisation/src/multithreads.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | static const int num_threads = 10; 6 | 7 | //This function will be called from a thread 8 | void call_from_thread(int tid) { 9 | printf("Launched by thread %d \n", tid); 10 | } 11 | 12 | int main() { 13 | std::thread t[num_threads]; 14 | 15 | //Launch a group of threads 16 | for (int i = 0; i < num_threads; ++i) { 17 | t[i] = std::thread(call_from_thread, i); 18 | } 19 | 20 | printf("Launched from the main\n"); 21 | 22 | //Join the threads with the main thread 23 | for (int i = 0; i < num_threads; ++i) { 24 | t[i].join(); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Ex18_Tuple_Variadic_Templates_Metaprogramming/src/variadic_template.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Calculate the number of items in the container 5 | 6 | template 7 | struct count; 8 | 9 | // Basis case with zero arguments 10 | template <> 11 | struct count<> { 12 | static const int value = 0; 13 | }; 14 | 15 | // Recursive unpacking 16 | template 17 | struct count { 18 | static const int value = 1 + count::value; 19 | }; 20 | 21 | int main() { 22 | std::cout << "Size of the variadic Templates : " 23 | << count>::value << std::endl; 24 | } 25 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/3_factory_method_advanced/main.cc: -------------------------------------------------------------------------------- 1 | #include "factory.h" 2 | 3 | #include 4 | #include 5 | 6 | // Client-side implementation 7 | int main(int argc, char** argv) 8 | { 9 | 10 | // User input 11 | if (argc != 2) { 12 | std::cerr << "Incorrect number of arguments" << std::endl; 13 | std::cerr << "Usage: ./factory course (C++/Java)" << std::endl; 14 | std::exit(EXIT_FAILURE); 15 | } 16 | std::string coursename = argv[1]; 17 | 18 | auto mycourse = MyCourseFactory::Instance()->Create(coursename); 19 | 20 | mycourse->info(); 21 | mycourse->schedule(); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/4_factory_method_advanced_template/main.cc: -------------------------------------------------------------------------------- 1 | #include "factory.h" 2 | 3 | #include 4 | #include 5 | 6 | // Client-side implementation 7 | int main(int argc, char** argv) 8 | { 9 | 10 | // User input 11 | if (argc != 2) { 12 | std::cerr << "Incorrect number of arguments" << std::endl; 13 | std::cerr << "Usage: ./factory course (C++/Java)" << std::endl; 14 | std::exit(EXIT_FAILURE); 15 | } 16 | std::string coursename = argv[1]; 17 | 18 | auto mycourse = MyCourseFactory::Instance()->Create(coursename); 19 | 20 | mycourse->info(); 21 | mycourse->schedule(); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/2_factory_method_intermediate/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "course.h" 6 | #include "factory.h" 7 | 8 | 9 | // Client side implementation 10 | int main(int argc, char* argv[]) { 11 | 12 | // User input 13 | if (argc != 2) { 14 | std::cerr << "Incorrect number of arguments" << std::endl; 15 | std::cerr << "Usage: ./factory course (C++/Java)" << std::endl; 16 | std::exit(EXIT_FAILURE); 17 | } 18 | std::string coursename = argv[1]; 19 | 20 | auto mycourse = MyCourseFactory::generatecourse(coursename); 21 | mycourse->info(); 22 | mycourse->schedule(); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Ex15_Functors/src/accessors/templatised_functor_accesor.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | class Coordinate { 5 | Tcoord coord_; 6 | 7 | public: 8 | Coordinate(); 9 | explicit Coordinate(Tcoord coord) : coord_{coord} {}; 10 | Tcoord operator()() const { return coord_; } 11 | void operator()(Tcoord coord) { coord_ = coord; } 12 | }; 13 | 14 | class Point { 15 | public: 16 | Coordinate Xcoord, Ycoord; 17 | Point(); 18 | Point(double X, double Y) : Xcoord{X}, Ycoord{Y} {}; 19 | 20 | }; 21 | 22 | int main() { 23 | Point point{0., 0.}; 24 | point.Xcoord(point.Xcoord() + 10.); 25 | std::cout << "The new xcoordinate is : " << point.Xcoord() << std::endl; 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /Ex12_Parallelisation/src/hello.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char* argv[]) { 5 | int nthreads, tid; 6 | 7 | // Fork a team of threads giving them their own copies of variables 8 | #pragma omp parallel private(nthreads, tid) 9 | { 10 | // Obtain thread number 11 | tid = omp_get_thread_num(); 12 | 13 | // Using printf instead of cout 14 | printf("Hello World from thread = %d\n", tid); 15 | 16 | // Only master thread does this 17 | if (tid == 0) { 18 | // get the number of threads 19 | nthreads = omp_get_num_threads(); 20 | printf("Number of threads = %d\n", nthreads); 21 | } 22 | 23 | } // All threads join master thread and disband 24 | } 25 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/2_factory_method_intermediate/factory.cc: -------------------------------------------------------------------------------- 1 | #include "factory.h" 2 | 3 | //Factory method generates a product 4 | std::shared_ptr MyCourseFactory::generatecourse(std::string& coursename) { 5 | 6 | Course* courseptr = NULL; 7 | 8 | if (coursename == "C++") { 9 | courseptr = new Cpp; 10 | } 11 | else if (coursename == "Java") { 12 | courseptr = new Java; 13 | } 14 | 15 | // Evaluating if the course exists 16 | if(courseptr != nullptr) { 17 | return std::shared_ptr(courseptr); 18 | } 19 | else { 20 | std::cerr << "Invalid course name. Program terminating" << std::endl; 21 | std::exit(EXIT_FAILURE); 22 | } 23 | } 24 | 25 | 26 | -------------------------------------------------------------------------------- /Ex13_Threads/src/sync/3_deadlock/deadlock.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | class Account { 7 | std::mutex mutex; 8 | double i; 9 | public: 10 | Account() : i(0) {} 11 | void credit(double x){ 12 | std::lock_guard lock(mutex); 13 | i += x; 14 | } 15 | void debit(double x){ 16 | std::lock_guard lock(mutex); 17 | i -= x; 18 | } 19 | void both(double x, double y){ 20 | std::lock_guard lock(mutex); 21 | credit(x); 22 | debit(y); 23 | } 24 | 25 | double print() { return i;} 26 | }; 27 | int main(){ 28 | Account savings; 29 | savings.both(32, 23); 30 | std::cout << "Balance is : " << savings.print() << std::endl; 31 | } 32 | -------------------------------------------------------------------------------- /Ex16_Rvalue_Move_Semantics/src/address.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Object { 4 | int x; 5 | int getInt() { return x; } 6 | 7 | int&& getRvalueInt() { return std::move(x); } 8 | }; 9 | 10 | void printAddress(const int& v) { // const ref to allow binding to rvalues 11 | std::cout << reinterpret_cast(&v) << std::endl; 12 | } 13 | 14 | int main() { 15 | Object obj; 16 | std::cout << "Address of getint :"; 17 | printAddress(obj.getInt()); 18 | std::cout << "Address of object `x':"; 19 | printAddress(obj.x); 20 | 21 | // On the other hand, 22 | 23 | std::cout << "Address of rvalueint :"; 24 | printAddress(obj.getRvalueInt()); 25 | std::cout << "Address of object `x':"; 26 | printAddress(obj.x); 27 | } 28 | -------------------------------------------------------------------------------- /Ex09_Exception_Handling_I/src/exception.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double division(double a, double b) 4 | { 5 | if( b == 0. ) 6 | { 7 | throw "\nDivision by zero condition!"; 8 | } 9 | return (a/b); 10 | } 11 | 12 | int main () 13 | { 14 | double x, y; 15 | double z = 0.; 16 | std::cout << "\nEnter the value of the numerator: "; 17 | std::cin >> x; 18 | std::cout << "\nEnter the value of the denominator: "; 19 | std::cin >> y; 20 | 21 | try { 22 | z = division(x, y); 23 | std::cout << "The result is: " << z << std::endl; 24 | }catch (const char* msg) { 25 | std::cerr << msg << std::endl; 26 | } 27 | 28 | std::cout << "\nThe code continues after excpetion" << std::endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Ex13_Threads/src/sync/0_nosync/nosync.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | class Counter { 6 | int value_; 7 | 8 | public: 9 | 10 | Counter() : value_(0){} 11 | 12 | void increment() { ++value_; } 13 | int value() { return value_; } 14 | void value(int& newvalue) { value_ = newvalue; } 15 | }; 16 | 17 | int main() { 18 | Counter counter; 19 | 20 | std::vector threads; 21 | for (int i = 0; i < 500; ++i) { 22 | threads.push_back(std::thread([&counter]() { 23 | for (int i = 0; i < 1000; ++i) { 24 | counter.increment(); 25 | } 26 | })); 27 | } 28 | 29 | for (auto& thread : threads) { 30 | thread.join(); 31 | } 32 | 33 | std::cout << counter.value() << std::endl; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Ex13_Threads/src/sync/3_deadlock/deadlock_fixed.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | class Account { 7 | std::recursive_mutex mutex; 8 | double i; 9 | public: 10 | Account() : i(0) {} 11 | void credit(double x){ 12 | std::lock_guard lock(mutex); 13 | i += x; 14 | } 15 | void debit(double x){ 16 | std::lock_guard lock(mutex); 17 | i -= x; 18 | } 19 | void both(double x, double y){ 20 | std::lock_guard lock(mutex); 21 | credit(x); 22 | debit(y); 23 | } 24 | 25 | double print() { return i;} 26 | }; 27 | 28 | int main(){ 29 | Account savings; 30 | savings.both(32, 23); 31 | std::cout << "Balance is : " << savings.print() << std::endl; 32 | } 33 | -------------------------------------------------------------------------------- /Ex14_Memory_Models_Compiler_Optimisation/src/nosync.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | volatile int counter = 0; 6 | pthread_mutex_t myMutex; 7 | 8 | void* mutex_testing(int* param) { 9 | int i; 10 | for (i = 0; i < 5; i++) { 11 | ++counter; 12 | usleep(1); // simulate some process 13 | printf("thread %d counter = %d\n", (int)param, counter); 14 | } 15 | } 16 | 17 | int main() { 18 | int one = 1, two = 2, three = 3; 19 | pthread_t thread1, thread2, thread3; 20 | pthread_create(&thread1, 0, mutex_testing, (void*)one); 21 | pthread_create(&thread2, 0, mutex_testing, (void*)two); 22 | pthread_create(&thread3, 0, mutex_testing, (void*)three); 23 | pthread_join(thread1, 0); 24 | pthread_join(thread2, 0); 25 | pthread_join(thread3, 0); 26 | } 27 | -------------------------------------------------------------------------------- /Ex17_RTTI_Traits/src/accumator.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | T accum(T const* beg, T const* end) { 5 | T total = T(); // assume T() actually creates a zero value 6 | while (beg != end) { 7 | total += *beg; 8 | ++beg; 9 | } 10 | return total; 11 | } 12 | 13 | int main() { 14 | // create array of 5 integer values 15 | int num[] = {1, 2, 3, 4, 5}; 16 | // print average value 17 | std::cout << "the average value of the integer values is " 18 | << accum(&num[0], &num[5]) / 5 << '\n'; 19 | 20 | // create array of character values 21 | char name[] = "templates"; 22 | int length = sizeof(name) - 1; 23 | std::cout << "the average value of the characters in" << name << " is " 24 | << accum(&name[0], &name[length]) / length << '\n'; 25 | } 26 | -------------------------------------------------------------------------------- /Ex05_Prototype/src/0_prototype_example/prototype.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef PROTOTYPE_H 3 | #define PROTOTYPE_H 4 | 5 | #include 6 | 7 | class Prototype 8 | { 9 | public: 10 | virtual ~Prototype(); 11 | virtual Prototype* Clone() const = 0; 12 | }; 13 | 14 | class MySQLManager: public Prototype 15 | { 16 | std::string m_host,m_login,m_pass; 17 | std::string m_base,m_table; 18 | public: 19 | MySQLManager(const std::string& host="",const std::string& login="",const std::string& pass=""); 20 | 21 | // const at the end of the function prevents 22 | // changing the member variables 23 | MySQLManager* Clone() const; 24 | 25 | void Display() const; 26 | void Set(const std::string& base="",const std::string& table=""); 27 | }; 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /Ex13_Threads/src/sync/1_mutex/atomic.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | class Counter { 7 | std::atomic value_; 8 | 9 | public: 10 | 11 | Counter() : value_(0){} 12 | 13 | void increment() { 14 | ++value_; 15 | } 16 | int value() { return value_; } 17 | void value(int& newvalue) { value_ = newvalue; } 18 | }; 19 | 20 | int main() { 21 | Counter counter; 22 | 23 | std::vector threads; 24 | for (int i = 0; i < 500; ++i) { 25 | threads.push_back(std::thread([&counter]() { 26 | for (int i = 0; i < 1000; ++i) { 27 | counter.increment(); 28 | } 29 | })); 30 | } 31 | 32 | for (auto& thread : threads) { 33 | thread.join(); 34 | } 35 | 36 | std::cout << counter.value() << std::endl; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Ex16_Rvalue_Move_Semantics/src/copyconstructor_bad.cc: -------------------------------------------------------------------------------- 1 | // This is an example with default copy constructor 2 | #include 3 | #include 4 | 5 | class Widget { 6 | int x = 10; 7 | int* ptr; 8 | 9 | public: 10 | Widget() { ptr = &x; }; 11 | int xvalue() { return *ptr; }; 12 | void xvalue(const int newx) { x = newx; }; 13 | }; 14 | 15 | int main() { 16 | Widget w1; 17 | std::cout << "Widget W1 has a value of : " << w1.xvalue() << std::endl; 18 | 19 | Widget w2{w1}; // default copy constructor is called 20 | // Widget w2 = w1; // default assignment operator 21 | 22 | std::cout << "Widget W2 has a value of : " << w2.xvalue() << std::endl; 23 | 24 | w1.xvalue(5); // Value of w1.x is changed to 5 25 | std::cout << "Widget W1 has a value of : " << w1.xvalue() << std::endl; 26 | std::cout << "Widget W2 has a value of : " << w2.xvalue() << std::endl; 27 | } 28 | -------------------------------------------------------------------------------- /Ex13_Threads/src/sync/1_mutex/mutex.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | class Counter { 7 | int value_; 8 | std::mutex mutex; 9 | 10 | public: 11 | 12 | Counter() : value_(0){} 13 | 14 | void increment() { 15 | mutex.lock(); 16 | ++value_; 17 | mutex.unlock(); 18 | } 19 | int value() { return value_; } 20 | void value(int& newvalue) { value_ = newvalue; } 21 | }; 22 | 23 | int main() { 24 | Counter counter; 25 | 26 | std::vector threads; 27 | for (int i = 0; i < 500; ++i) { 28 | threads.push_back(std::thread([&counter]() { 29 | for (int i = 0; i < 1000; ++i) { 30 | counter.increment(); 31 | } 32 | })); 33 | } 34 | 35 | for (auto& thread : threads) { 36 | thread.join(); 37 | } 38 | 39 | std::cout << counter.value() << std::endl; 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Ex13_Threads/src/sync/1_mutex/guard.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | class Counter { 7 | int value_; 8 | std::mutex mutex; 9 | 10 | public: 11 | 12 | Counter() : value_(0){} 13 | 14 | void increment() { 15 | std::lock_guard guard(mutex); 16 | ++value_; 17 | 18 | } 19 | int value() { return value_; } 20 | void value(int& newvalue) { value_ = newvalue; } 21 | }; 22 | 23 | int main() { 24 | Counter counter; 25 | 26 | std::vector threads; 27 | for (int i = 0; i < 500; ++i) { 28 | threads.push_back(std::thread([&counter]() { 29 | for (int i = 0; i < 1000; ++i) { 30 | counter.increment(); 31 | } 32 | })); 33 | } 34 | 35 | for (auto& thread : threads) { 36 | thread.join(); 37 | } 38 | 39 | std::cout << counter.value() << std::endl; 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Ex18_Tuple_Variadic_Templates_Metaprogramming/src/dot_product_metaprogram.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // primary template 4 | template 5 | class DotProduct { 6 | public: 7 | static T result(T* a, T* b) { 8 | return *a * *b + DotProduct::result(a + 1, b + 1); 9 | } 10 | }; 11 | 12 | // partial specialization as end criteria 13 | template 14 | class DotProduct<1, T> { 15 | public: 16 | static T result(T* a, T* b) { return *a * *b; } 17 | }; 18 | 19 | // convenience function 20 | template 21 | inline T dot_product(T* a, T* b) { 22 | return DotProduct::result(a, b); 23 | } 24 | 25 | int main() { 26 | int a[3] = {1, 2, 3}; 27 | int b[3] = {5, 6, 7}; 28 | 29 | std::cout << "dot_product<3>(a,b) = " << dot_product<3>(a, b) << std::endl; 30 | std::cout << "dot_product<3>(a,a) = " << dot_product<3>(a, a) << std::endl; 31 | } 32 | -------------------------------------------------------------------------------- /Ex21_Name_Lookup_Interface/src/base_derived.cc: -------------------------------------------------------------------------------- 1 | // Determine what's the output 2 | 3 | #include 4 | #include 5 | 6 | class Base { 7 | public: 8 | Base() {} 9 | ~Base() { std::cout << "Base class destructor called" << std::endl; } 10 | virtual void foo(int i = 10) { 11 | std::cout << "Base class foo called with i: " << i << std::endl; 12 | } 13 | }; 14 | 15 | class Derived : public Base { 16 | public: 17 | Derived() {} 18 | ~Derived() { std::cout << "Derived class destructor called" << std::endl; } 19 | virtual void foo(int k = 5) { 20 | std::cout << "Derived class foo called with k: " << k << std::endl; 21 | } 22 | }; 23 | 24 | int main() { 25 | Base* bptr = new Derived(); // Convert to Derived ptr and see 26 | 27 | std::cout << "Type id: " << typeid(*bptr).name() << std::endl; 28 | 29 | bptr->foo(); // Pass an integer and check. 30 | 31 | delete bptr; 32 | } 33 | -------------------------------------------------------------------------------- /Ex11_Classes_I/src/bad_class.h: -------------------------------------------------------------------------------- 1 | // Example from Herb Sutter's Exception C++ 2 | 3 | #ifndef _COMPLEX_H_ 4 | #define _COMPLEX_H_ 5 | 6 | class Complex { 7 | public: 8 | // Constructor 9 | Complex(double real, double imaginary = 0) 10 | : _real(real), _imaginary(imaginary) {} 11 | 12 | // Addition operator 13 | void operator+(Complex other) { 14 | _real = _real + other._real; 15 | _imaginary = _imaginary + other._imaginary; 16 | } 17 | 18 | // << operator 19 | void operator<<(ostream os) { 20 | os << "(" << _real << "," << _imaginary << ")"; 21 | } 22 | 23 | // Pre-Increament Operator 24 | Complex operator++() { 25 | ++_real; 26 | return *this; 27 | } 28 | 29 | // Post-increament operator 30 | Complex operator++(int) { 31 | Complex temp = *this; 32 | ++_real; 33 | return temp; 34 | } 35 | 36 | private: 37 | double _real, _imaginary; 38 | }; 39 | 40 | #endif // _COMPLEX_H_ 41 | -------------------------------------------------------------------------------- /Ex14_Memory_Models_Compiler_Optimisation/src/mutex.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | volatile int counter = 0; 6 | pthread_mutex_t myMutex; 7 | 8 | void* mutex_testing(int* param) { 9 | int i; 10 | for (i = 0; i < 5; i++) { 11 | pthread_mutex_lock(&myMutex); 12 | counter++; 13 | usleep(1); // simulate some process 14 | printf("thread %d counter = %d\n", (int)param, counter); 15 | pthread_mutex_unlock(&myMutex); 16 | } 17 | } 18 | 19 | int main() { 20 | int one = 1, two = 2, three = 3; 21 | pthread_t thread1, thread2, thread3; 22 | pthread_mutex_init(&myMutex, 0); 23 | pthread_create(&thread1, 0, mutex_testing, (void*)one); 24 | pthread_create(&thread2, 0, mutex_testing, (void*)two); 25 | pthread_create(&thread3, 0, mutex_testing, (void*)three); 26 | pthread_join(thread1, 0); 27 | pthread_join(thread2, 0); 28 | pthread_join(thread3, 0); 29 | pthread_mutex_destroy(&myMutex); 30 | } 31 | -------------------------------------------------------------------------------- /Ex15_Functors/src/functor.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // this is a functor 6 | class Multiplier { 7 | double x_; 8 | 9 | public: 10 | Multiplier(double x) : x_{x} {} 11 | double operator()(double y) { return x_ * y; } 12 | }; 13 | 14 | int main() { 15 | Multiplier doubler{2}; 16 | double x = 20; 17 | std::cout << "Value " << x << " doubled is: " << doubler(x) << std::endl; 18 | 19 | 20 | std::vector in{1, 2, 3, 4, 5}; 21 | std::vector out; 22 | out.resize(in.size()); 23 | 24 | // Pass a functor to std::transform, which calls the functor on every element 25 | // in the input sequence, and stores the result to the output sequence 26 | 27 | for (auto& elout : in) std::cout << "Original Value :" << elout << std::endl; 28 | 29 | std::transform(in.begin(), in.end(), out.begin(), Multiplier(5)); 30 | 31 | for (auto& elout : out) std::cout << "Modified Value :" << elout << std::endl; 32 | } 33 | -------------------------------------------------------------------------------- /Ex17_RTTI_Traits/src/typeid.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include //for 'typeid' to work 3 | 4 | class Person { 5 | public: 6 | // ... Person members ... 7 | virtual ~Person() {} 8 | }; 9 | 10 | class Employee : public Person { 11 | // ... Employee members ... 12 | }; 13 | 14 | int main() { 15 | Person person; 16 | Employee employee; 17 | Person* ptr = &employee; 18 | // The string returned by typeid::name is implementation-defined 19 | 20 | // Person (statically known at compile-time) 21 | std::cout << typeid(person).name() << std::endl; 22 | 23 | // Employee (statically known at compile-time) 24 | std::cout << typeid(employee).name() << std::endl; 25 | 26 | // Person * (statically known at compile-time) 27 | std::cout << typeid(ptr).name() << std::endl; 28 | 29 | // Employee (looked up dynamically at run-time 30 | // because it is the dereference of a 31 | // pointer to a polymorphic class) 32 | std::cout << typeid(*ptr).name() << std::endl; 33 | } 34 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/2_factory_method_intermediate/course.h: -------------------------------------------------------------------------------- 1 | #ifndef _COURSE_H_ 2 | #define _COURSE_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | // Abstract Product: Course 9 | class Course 10 | { 11 | public: 12 | virtual ~Course(){} 13 | 14 | virtual void info() = 0; 15 | virtual void schedule() = 0; 16 | }; 17 | 18 | // Concrete Product: C++ 19 | class Cpp : public Course 20 | { 21 | public: 22 | virtual ~Cpp(){} 23 | void info(){ 24 | std::cout << "This is a C++ Course" << std::endl; 25 | } 26 | 27 | void schedule(){ 28 | std::cout << "This is the C++ course schedule" << std::endl; 29 | } 30 | }; 31 | 32 | // Concrete Product: Java 33 | class Java : public Course 34 | { 35 | public: 36 | virtual ~Java(){} 37 | void info(){ 38 | std::cout << "This is a Java Course" << std::endl; 39 | } 40 | void schedule(){ 41 | std::cout << "This is the Java course schedule" << std::endl; 42 | } 43 | 44 | }; 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /Ex17_RTTI_Traits/src/dynamic_cast.cc: -------------------------------------------------------------------------------- 1 | #include 2 | struct BaseA { 3 | virtual void print() { std::cout << "Base Class A" << std::endl; } 4 | }; 5 | 6 | struct DerivedB : BaseA { 7 | 8 | virtual void print() { std::cout << "Derived Class B" << std::endl; } 9 | }; 10 | 11 | struct DerivedC : BaseA { 12 | 13 | virtual void print() { std::cout << "Derived Class C" << std::endl; } 14 | }; 15 | 16 | void dynamic_cast_function(BaseA* arg) { 17 | 18 | DerivedB* bp = dynamic_cast(arg); 19 | 20 | DerivedC* cp = dynamic_cast(arg); 21 | 22 | if (bp) 23 | bp->print(); 24 | 25 | else if (cp) 26 | cp->print(); 27 | 28 | else 29 | arg->print(); 30 | }; 31 | 32 | int main() { 33 | 34 | BaseA aobj; 35 | 36 | DerivedC cobj; 37 | 38 | BaseA* ptr = &aobj; 39 | 40 | BaseA* ptr2 = &cobj; // BaseA* ptr 41 | 42 | // auto* ptr2 = &cobj; //BaseA* ptr 43 | 44 | // decltype(ptr) ptr2 = &cobj; //BaseA* ptr 45 | 46 | dynamic_cast_function(ptr); 47 | 48 | dynamic_cast_function(ptr2); 49 | } 50 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/4_factory_method_advanced_template/factory.cc: -------------------------------------------------------------------------------- 1 | #include "factory.h" 2 | 3 | // Abstract Course Factory 4 | 5 | MyCourseFactory * MyCourseFactory::Instance() 6 | { 7 | static MyCourseFactory factory; 8 | return &factory; 9 | } 10 | 11 | 12 | void MyCourseFactory::RegisterFactoryFunction(std::string name, std::function classFactoryFunction) 13 | { 14 | // register the class factory function 15 | factoryFunctionRegistry[name] = classFactoryFunction; 16 | } 17 | 18 | 19 | std::shared_ptr MyCourseFactory::Create(std::string name) 20 | { 21 | Course * instance = nullptr; 22 | 23 | // find name in the registry and call factory method. 24 | auto it = factoryFunctionRegistry.find(name); 25 | if(it != factoryFunctionRegistry.end()) 26 | instance = it->second(); 27 | 28 | // wrap instance in a shared ptr and return 29 | if(instance != nullptr) 30 | return std::shared_ptr(instance); 31 | else 32 | return nullptr; 33 | } 34 | -------------------------------------------------------------------------------- /Ex16_Rvalue_Move_Semantics/src/copyconstructor.cc: -------------------------------------------------------------------------------- 1 | // This is an example with user-defined copy constructor 2 | #include 3 | #include 4 | 5 | class Widget { 6 | int x = 10; 7 | int* ptr; 8 | 9 | public: 10 | Widget() { ptr = &x; }; 11 | // user-defined copy constructor 12 | Widget(const Widget& w) : x{w.x} { ptr = &x; }; 13 | // user-defined assignment operator 14 | Widget& operator=(const Widget& w) { 15 | x = w.x; 16 | ptr = &x; 17 | return *this; 18 | }; 19 | int xvalue() { return *ptr; }; 20 | void xvalue(const int newx) { x = newx; }; 21 | }; 22 | 23 | int main() { 24 | Widget w1; 25 | std::cout << "Widget W1 has a value of : " << w1.xvalue() << std::endl; 26 | 27 | Widget w2{w1}; // user-defined copy constructor 28 | // Widget w2 = w1; // user-defined assignment operator 29 | 30 | std::cout << "Widget W2 has a value of : " << w2.xvalue() << std::endl; 31 | 32 | w1.xvalue(5); // Value of w1.x is changed to 5 33 | std::cout << "Widget W1 has a value of : " << w1.xvalue() << std::endl; 34 | std::cout << "Widget W2 has a value of : " << w2.xvalue() << std::endl; 35 | } 36 | -------------------------------------------------------------------------------- /Ex12_Parallelisation/src/add_array.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | int main(int argc, char** argv) { 9 | 10 | int N; 11 | if (argc != 2) { 12 | std::cerr << "Incorrect arguments: usage ./array ARRAY_SIZE" << std::endl; 13 | std::exit(EXIT_FAILURE); 14 | } 15 | else { 16 | N = atoi (argv[1]); 17 | } 18 | 19 | int nthreads, tid, i, chunk; 20 | chunk = 10; 21 | 22 | float a[N], b[N], c[N]; 23 | 24 | /* Some initializations */ 25 | for (i = 0; i < N; i++) a[i] = b[i] = i * 1.0; 26 | 27 | #pragma omp parallel shared(a, b, c, nthreads, chunk) private(i, tid) 28 | { 29 | tid = omp_get_thread_num(); 30 | if (tid == 0) { 31 | nthreads = omp_get_num_threads(); 32 | printf("Number of threads = %d\n", nthreads); 33 | } 34 | printf("Thread %d starting...\n", tid); 35 | 36 | #pragma omp for schedule(dynamic, chunk) //change dynamic to static 37 | for (i = 0; i < N; i++) { 38 | c[i] = a[i] + b[i]; 39 | printf("Thread %d: c[%d]= %f\n", tid, i, c[i]); 40 | } 41 | 42 | } /* end of parallel section */ 43 | } 44 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Krishna Kumar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /Ex11_Classes_I/src/good_class.h: -------------------------------------------------------------------------------- 1 | // Example from Herb Sutter's Exception C++ 2 | #ifndef _COMPLEX_H_ 3 | #define _COMPLEX_H_ 4 | 5 | class Complex { 6 | public: 7 | // Constructor 8 | explicit Complex(double real, double imaginary = 0) 9 | : real_(real), imaginary_(imaginary) {} 10 | 11 | // Assignment operator 12 | Complex& operator+=(const Complex& other) { 13 | real_ += other.real_; 14 | imaginary_ += other.imaginary_; 15 | return *this; 16 | } 17 | 18 | // Preincreament operator 19 | Complex& operator++() { 20 | ++real_; 21 | return *this; 22 | } 23 | 24 | // Post-increament operator 25 | const Complex operator++(int) { 26 | Complex temp(*this); 27 | ++*this; 28 | return temp; 29 | } 30 | 31 | // Print function 32 | ostream& Print(ostream& os) const { 33 | return os << "(" << real_ << "," << imaginary_ << ")"; 34 | } 35 | 36 | private: 37 | double real_, imaginary_; 38 | }; 39 | 40 | // Addition operator 41 | const Complex operator+(const Complex& lhs, const Complex& rhs) { 42 | Complex ret(lhs); 43 | ret += rhs; 44 | return ret; 45 | } 46 | 47 | // Output stream 48 | ostream& operator<<(ostream& os, const Complex& c) { return c.Print(os); } 49 | 50 | #endif // _COMPLEX_H_ 51 | -------------------------------------------------------------------------------- /Ex13_Threads/src/sync/2_exception_lock/unsafe.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | class Counter { 7 | int value_; 8 | std::mutex mutex; 9 | void cincrement() { ++value_; } 10 | void cdecrement() { 11 | if(value_ == 0) { 12 | throw "Value cannot be less than 0"; 13 | } 14 | --value_; 15 | } 16 | 17 | public: 18 | 19 | Counter() : value_(10){} 20 | 21 | 22 | void increment() { 23 | mutex.lock(); 24 | cincrement(); 25 | mutex.unlock(); 26 | } 27 | 28 | void decrement() { 29 | mutex.lock(); 30 | cdecrement(); 31 | mutex.unlock(); 32 | } 33 | int value() { return value_; } 34 | void value(int& newvalue) { value_ = newvalue; } 35 | }; 36 | 37 | int main() { 38 | Counter counter; 39 | 40 | std::cout << counter.value() << std::endl; 41 | 42 | std::vector threads; 43 | for (int i = 0; i < 10; ++i) { 44 | threads.push_back(std::thread([&counter]() { 45 | counter.decrement(); 46 | printf("%d \n", counter.value()); 47 | })); 48 | } 49 | 50 | for (auto& thread : threads) { 51 | thread.join(); 52 | } 53 | 54 | counter.increment(); 55 | std::cout << "After 1 increment: " << counter.value() << std::endl; 56 | } 57 | -------------------------------------------------------------------------------- /Ex13_Threads/src/sync/2_exception_lock/auto_safe.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | class Counter { 7 | int value_; 8 | std::mutex mutex; 9 | void cincrement() { ++value_; } 10 | void cdecrement() { 11 | if(value_ == 0) { 12 | throw "Value cannot be less than 0"; 13 | } 14 | --value_; 15 | } 16 | 17 | public: 18 | 19 | Counter() : value_(10){} 20 | 21 | 22 | void increment() { 23 | std::lock_guard guard(mutex); 24 | cincrement(); 25 | } 26 | 27 | void decrement() { 28 | std::lock_guard guard(mutex); 29 | cdecrement(); 30 | } 31 | int value() { return value_; } 32 | void value(int& newvalue) { value_ = newvalue; } 33 | }; 34 | 35 | int main() { 36 | Counter counter; 37 | 38 | std::cout << counter.value() << std::endl; 39 | 40 | std::vector threads; 41 | for (int i = 0; i < 10; ++i) { 42 | threads.push_back(std::thread([&counter]() { 43 | counter.decrement(); 44 | printf("%d \n", counter.value()); 45 | })); 46 | } 47 | 48 | for (auto& thread : threads) { 49 | thread.join(); 50 | } 51 | 52 | counter.increment(); 53 | std::cout << "After 1 increment: " << counter.value() << std::endl; 54 | } 55 | -------------------------------------------------------------------------------- /Ex05_Prototype/src/2_document_manager_prototype_factory/document.h: -------------------------------------------------------------------------------- 1 | #ifndef _DOCUMENT_H_ 2 | #define _DOCUMENT_H_ 3 | 4 | #include 5 | #include 6 | 7 | // Prototype class 8 | template class Prototype 9 | { 10 | public: 11 | virtual ~Prototype(){} 12 | virtual T* Clone() const =0 ; 13 | }; 14 | 15 | // Interface to document 16 | class Document : public Prototype 17 | { 18 | public: 19 | virtual void view_document()=0; 20 | }; 21 | 22 | class XmlDoc : public Document 23 | { 24 | public: 25 | Document* Clone() const; 26 | void view_document(); 27 | 28 | }; 29 | 30 | 31 | class PlainDoc : public Document 32 | { 33 | public: 34 | Document* Clone() const ; 35 | void view_document(); 36 | }; 37 | 38 | class SpreadSheet : public Document 39 | { 40 | public: 41 | Document* Clone() const ; 42 | void view_document(); 43 | }; 44 | 45 | // Document Factory 46 | class DocumentFactory 47 | { 48 | public: 49 | static std::map m_map; 50 | 51 | public: 52 | // Function that combines 'key' <=> prototype 53 | static void Register(const std::string& key,Document* obj); 54 | 55 | // This creates the objects 56 | Document* Create(const std::string& key) const; 57 | }; 58 | #endif // _DOCUMENT_H_ 59 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/3_factory_method_advanced/factory.cc: -------------------------------------------------------------------------------- 1 | #include "factory.h" 2 | 3 | 4 | 5 | Registrar::Registrar(std::string name, std::function classFactoryFunction) 6 | { 7 | // register the class factory function 8 | MyCourseFactory::Instance()->RegisterFactoryFunction(name, classFactoryFunction); 9 | } 10 | 11 | // Abstract Course Factory 12 | 13 | MyCourseFactory * MyCourseFactory::Instance() 14 | { 15 | static MyCourseFactory factory; 16 | return &factory; 17 | } 18 | 19 | 20 | void MyCourseFactory::RegisterFactoryFunction(std::string name, std::function classFactoryFunction) 21 | { 22 | // register the class factory function 23 | factoryFunctionRegistry[name] = classFactoryFunction; 24 | } 25 | 26 | 27 | std::shared_ptr MyCourseFactory::Create(std::string name) 28 | { 29 | Course * instance = nullptr; 30 | 31 | // find name in the registry and call factory method. 32 | auto it = factoryFunctionRegistry.find(name); 33 | if(it != factoryFunctionRegistry.end()) 34 | instance = it->second(); 35 | 36 | // wrap instance in a shared ptr and return 37 | if(instance != nullptr) 38 | return std::shared_ptr(instance); 39 | else 40 | return nullptr; 41 | } 42 | -------------------------------------------------------------------------------- /Ex10_Exception_Handling_II/src/multiple_noexception.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double calculator(const double N1, const double N2, const char p); 4 | 5 | int main() { 6 | double operand1, operand2, result; 7 | char operator_; 8 | 9 | std::cout << "This program allows you to perform a division of two numbers" << std::endl; 10 | std::cout << "To proceed, enter" << std::endl; 11 | std::cout << "First Number: "; std::cin >> operand1; 12 | std::cout << "An Operator: "; std::cin >> operator_; 13 | std::cout << "Second Number: "; std::cin >> operand2; 14 | 15 | 16 | result = calculator(operand1, operand2, operator_); 17 | 18 | std::cout << operand1 << " " << operator_ << " " 19 | << operand2 << " = " << result; 20 | 21 | std::cout << std::endl; 22 | return 0; 23 | } 24 | 25 | double calculator(const double oper1, const double oper2, const char symbol) { 26 | double value; 27 | 28 | switch(symbol) { 29 | case '+': 30 | value = oper1 + oper2; 31 | break; 32 | 33 | case '-': 34 | value = oper1 - oper2; 35 | break; 36 | 37 | case '*': 38 | value = oper1 * oper2; 39 | break; 40 | 41 | case '/': 42 | value = oper1 / oper2; 43 | break; 44 | } 45 | 46 | return value; 47 | } 48 | -------------------------------------------------------------------------------- /Ex17_RTTI_Traits/src/accumator_specialisation.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | class AccumulationTraits; 5 | 6 | template <> 7 | class AccumulationTraits { 8 | public: 9 | typedef int AccT; 10 | static AccT const zero = 0; 11 | }; 12 | 13 | template <> 14 | class AccumulationTraits { 15 | public: 16 | typedef int AccT; 17 | static AccT const zero = 0; 18 | }; 19 | 20 | template 21 | inline typename AccumulationTraits::AccT accum(T const* beg, T const* end) { 22 | // return type is traits of the element type 23 | typedef typename AccumulationTraits::AccT AccT; 24 | 25 | AccT total = AccumulationTraits::zero; 26 | while (beg != end) { 27 | total += *beg; 28 | ++beg; 29 | } 30 | return total; 31 | } 32 | 33 | int main() { 34 | // create array of 5 integer values 35 | int num[] = {1, 2, 3, 4, 5}; 36 | // print average value 37 | std::cout << "the average value of the integer values is " 38 | << accum(&num[0], &num[5]) / 5 << std::endl; 39 | 40 | // create array of character values 41 | char name[] = "templates"; 42 | int length = sizeof(name) - 1; 43 | std::cout << "the average value of the characters in" << name << " is " 44 | << accum(&name[0], &name[length]) / length << std::endl; 45 | } 46 | -------------------------------------------------------------------------------- /Ex05_Prototype/src/3_document_manager_prototype_factory_template/document.h: -------------------------------------------------------------------------------- 1 | #ifndef _DOCUMENT_H_ 2 | #define _DOCUMENT_H_ 3 | 4 | #include 5 | #include 6 | 7 | // Prototype class 8 | template class Prototype 9 | { 10 | public: 11 | virtual ~Prototype(){} 12 | virtual T* Clone() const =0 ; 13 | }; 14 | 15 | // Interface to document 16 | class Document : public Prototype 17 | { 18 | public: 19 | virtual void view_document()=0; 20 | virtual ~Document(){}; 21 | }; 22 | 23 | class XmlDoc : public Document 24 | { 25 | public: 26 | Document* Clone() const; 27 | void view_document(); 28 | virtual ~XmlDoc(){}; 29 | 30 | }; 31 | 32 | 33 | class PlainDoc : public Document 34 | { 35 | public: 36 | Document* Clone() const ; 37 | void view_document(); 38 | virtual ~PlainDoc(){}; 39 | }; 40 | 41 | class SpreadSheet : public Document 42 | { 43 | public: 44 | Document* Clone() const ; 45 | void view_document(); 46 | virtual ~SpreadSheet(){}; 47 | }; 48 | 49 | 50 | // Document Factory 51 | template class Factory 52 | { 53 | static std::map m_map; 54 | public: 55 | static void Register(Key key,Object* obj); 56 | Object* Create(const Key& key); 57 | }; 58 | 59 | 60 | #endif // _DOCUMENT_H_ 61 | -------------------------------------------------------------------------------- /Ex17_RTTI_Traits/src/accumator_class.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | class AccumulationTraits; 5 | 6 | template <> 7 | class AccumulationTraits { 8 | public: 9 | typedef int AccT; 10 | static AccT const zero = 0; 11 | }; 12 | 13 | template <> 14 | class AccumulationTraits { 15 | public: 16 | typedef int AccT; 17 | static AccT const zero = 0; 18 | }; 19 | 20 | template > 21 | class Accum { 22 | public: 23 | static typename AT::AccT accum(T const* beg, T const* end) { 24 | typename AT::AccT total = AT::zero; 25 | while (beg != end) { 26 | total += *beg; 27 | ++beg; 28 | } 29 | return total; 30 | } 31 | }; 32 | 33 | int main() { 34 | // create array of 5 integer values 35 | int num[] = {1, 2, 3, 4, 5}; 36 | // print average value 37 | std::cout << "the average value of the integer values is " 38 | << Accum >::accum(&num[0], &num[5]) / 5 39 | << std::endl; 40 | 41 | // create array of character values 42 | char name[] = "templates"; 43 | int length = sizeof(name) - 1; 44 | std::cout << "the average value of the characters in" << name << " is " 45 | << Accum::accum(&name[0], &name[length]) / length 46 | << std::endl; 47 | } 48 | -------------------------------------------------------------------------------- /Ex13_Threads/src/sync/2_exception_lock/safe.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | class Counter { 7 | int value_; 8 | std::mutex mutex; 9 | void cincrement() { ++value_; } 10 | void cdecrement() { 11 | if(value_ == 0) { 12 | throw "Value cannot be less than 0"; 13 | } 14 | --value_; 15 | } 16 | 17 | public: 18 | 19 | Counter() : value_(10){} 20 | 21 | 22 | void increment() { 23 | mutex.lock(); 24 | cincrement(); 25 | mutex.unlock(); 26 | } 27 | 28 | void decrement() { 29 | mutex.lock(); 30 | try { 31 | cdecrement(); 32 | } catch (std::string e){ 33 | mutex.unlock(); 34 | throw e; 35 | } 36 | mutex.unlock(); 37 | } 38 | int value() { return value_; } 39 | void value(int& newvalue) { value_ = newvalue; } 40 | }; 41 | 42 | int main() { 43 | Counter counter; 44 | 45 | std::cout << counter.value() << std::endl; 46 | 47 | std::vector threads; 48 | for (int i = 0; i < 10; ++i) { 49 | threads.push_back(std::thread([&counter]() { 50 | counter.decrement(); 51 | printf("%d \n", counter.value()); 52 | })); 53 | } 54 | 55 | for (auto& thread : threads) { 56 | thread.join(); 57 | } 58 | 59 | counter.increment(); 60 | std::cout << "After 1 increment: " << counter.value() << std::endl; 61 | } 62 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/3_factory_method_advanced/factory.h: -------------------------------------------------------------------------------- 1 | #ifndef _FACTORY_H_ 2 | #define _FACTORY_H_ 3 | 4 | #include "course.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | //using namespace std; 12 | 13 | // A helper class to register a factory function 14 | class Registrar { 15 | public: 16 | Registrar(std::string className, std::function classFactoryFunction); 17 | }; 18 | 19 | // A preprocessor define used by derived classes 20 | #define REGISTER_CLASS(NAME, TYPE) static Registrar registrar(NAME, [](void) -> Course * { return new TYPE();}); 21 | 22 | // The factory - implements singleton pattern! 23 | class MyCourseFactory 24 | { 25 | public: 26 | // Get the single instance of the factory 27 | static MyCourseFactory * Instance(); 28 | 29 | // register a factory function to create an instance of className 30 | void RegisterFactoryFunction(std::string name, std::function classFactoryFunction); 31 | 32 | // create an instance of a registered class 33 | std::shared_ptr Create(std::string name); 34 | 35 | private: 36 | // a private ctor 37 | MyCourseFactory(){} 38 | 39 | // the registry of factory functions 40 | std::map> factoryFunctionRegistry; 41 | 42 | }; 43 | 44 | #endif // _FACTORY_H_ 45 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/4_factory_method_advanced_template/factory.h: -------------------------------------------------------------------------------- 1 | #ifndef _FACTORY_H_ 2 | #define _FACTORY_H_ 3 | 4 | #include "course.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | //using namespace std; 12 | 13 | // The factory - implements singleton pattern! 14 | class MyCourseFactory 15 | { 16 | public: 17 | // Get the single instance of the factory 18 | static MyCourseFactory* Instance(); 19 | 20 | // register a factory function to create an instance of className 21 | void RegisterFactoryFunction(std::string name, std::function classFactoryFunction); 22 | 23 | // create an instance of a registered class 24 | std::shared_ptr Create(std::string name); 25 | 26 | private: 27 | // a private ctor 28 | MyCourseFactory(){} 29 | 30 | // the registry of factory functions 31 | std::map> factoryFunctionRegistry; 32 | 33 | }; 34 | 35 | 36 | // A helper class to register a factory function 37 | template 38 | class Registrar { 39 | public: 40 | Registrar(std::string className) 41 | { 42 | // register the class factory function 43 | MyCourseFactory::Instance()->RegisterFactoryFunction(className, 44 | [](void) -> Course* { return new T();}); 45 | } 46 | }; 47 | 48 | 49 | 50 | #endif // _FACTORY_H_ 51 | -------------------------------------------------------------------------------- /Ex15_Functors/src/function/function.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Foo { 5 | Foo(int num) : num_(num) {} 6 | void print_add(int i) const { std::cout << num_ + i << '\n'; } 7 | int num_; 8 | }; 9 | 10 | void print_num(int i) { std::cout << i << '\n'; } 11 | 12 | struct PrintNum { 13 | void operator()(int i) const { std::cout << i << '\n'; } 14 | }; 15 | 16 | int main() { 17 | // store a free function 18 | std::function f_display = print_num; 19 | f_display(-9); 20 | 21 | // store a lambda 22 | std::function f_display_42 = []() { print_num(42); }; 23 | f_display_42(); 24 | 25 | // store the result of a call to std::bind 26 | std::function f_display_31337 = std::bind(print_num, 31337); 27 | f_display_31337(); 28 | 29 | // store a call to a member function 30 | std::function f_add_display = &Foo::print_add; 31 | const Foo foo(314159); 32 | f_add_display(foo, 1); 33 | 34 | // store a call to a member function and object 35 | using std::placeholders::_1; 36 | std::function f_add_display2 = std::bind(&Foo::print_add, foo, _1); 37 | f_add_display2(2); 38 | 39 | // store a call to a member function and object ptr 40 | std::function f_add_display3 = 41 | std::bind(&Foo::print_add, &foo, _1); 42 | f_add_display3(3); 43 | 44 | // store a call to a function object 45 | std::function f_display_obj = PrintNum(); 46 | f_display_obj(18); 47 | } 48 | -------------------------------------------------------------------------------- /Ex01_Design_Pattern_Abstract_Factory/src/bad_code/bad.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #define _SINGAPORE_ 3 | 4 | // finance 5 | class Finance 6 | { 7 | public: 8 | virtual void compute() = 0; 9 | }; 10 | 11 | // Europe Shipping Fee 12 | class EuropeShippingFee : public Finance 13 | { 14 | public: 15 | void compute(){ 16 | std::cout << "Europe ShippingFee: $4.99" << std::endl; 17 | } 18 | }; 19 | 20 | // Singapore Shipping Fee 21 | class SingaporeShippingFee : public Finance 22 | { 23 | public: 24 | void compute(){ 25 | std::cout << "Singapore ShippingFee $39.99" << std::endl; 26 | } 27 | }; 28 | 29 | 30 | // Europe Tax Fee 31 | class EuropeTax : public Finance 32 | { 33 | public: 34 | void compute(){ 35 | std::cout << "Europe Tax: $49.99" << std::endl; 36 | } 37 | }; 38 | 39 | // Singapore Tax Fee 40 | class SingaporeTax : public Finance 41 | { 42 | public: 43 | void compute (){ 44 | std::cout << "Singapore Tax $19.99" << std::endl; 45 | } 46 | }; 47 | 48 | 49 | void display_finance() { 50 | #ifdef _SINGAPORE_ 51 | Finance* fcountry[] = { new SingaporeShippingFee, 52 | new SingaporeTax }; 53 | #else //Europe 54 | Finance* fcountry[] = { new EuropeShippingFee, 55 | new EuropeTax}; 56 | #endif 57 | fcountry[0]->compute(); //Shipping fee 58 | fcountry[1]->compute(); //Tax 59 | } 60 | 61 | 62 | // Client side implementation 63 | int main() { 64 | display_finance(); 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Ex17_RTTI_Traits/src/accumator_trait_policy.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Trait 4 | template 5 | class AccumulationTraits; 6 | 7 | template <> 8 | class AccumulationTraits { 9 | public: 10 | typedef int AccT; 11 | static AccT const zero = 0; 12 | }; 13 | 14 | template <> 15 | class AccumulationTraits { 16 | public: 17 | typedef int AccT; 18 | static AccT const zero = 0; 19 | }; 20 | 21 | 22 | // Summation 23 | class SumPolicy { 24 | public: 25 | template 26 | static void accumulate(T1& total, T2 const& value) { 27 | total += value; 28 | } 29 | }; 30 | 31 | // Multiplication 32 | class MultPolicy { 33 | public: 34 | template 35 | static void accumulate(T1& total, T2 const& value) { 36 | total *= value; 37 | } 38 | }; 39 | 40 | 41 | 42 | // Accumulator 43 | template > 45 | class Accum { 46 | public: 47 | typedef typename Traits::AccT AccT; 48 | static AccT accum(T const* beg, T const* end) { 49 | AccT total = Traits::zero; 50 | while (beg != end) { 51 | Policy::accumulate(total, *beg); 52 | ++beg; 53 | } 54 | return total; 55 | } 56 | }; 57 | int main() { 58 | // create array of 5 integer values 59 | int num[] = {1, 2, 3, 4, 5}; 60 | 61 | // print product of all values 62 | std::cout << "the product of the integer values is " 63 | << Accum::accum(&num[0], &num[5]) << '\n'; 64 | } 65 | -------------------------------------------------------------------------------- /Ex17_RTTI_Traits/src/.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | # BasedOnStyle: CUED GeoMechanics 3 | AccessModifierOffset: -1 4 | ConstructorInitializerIndentWidth: 4 5 | AlignEscapedNewlinesLeft: true 6 | AlignTrailingComments: true 7 | AllowAllParametersOfDeclarationOnNextLine: true 8 | AllowShortIfStatementsOnASingleLine: true 9 | AllowShortLoopsOnASingleLine: true 10 | AlwaysBreakTemplateDeclarations: true 11 | AlwaysBreakBeforeMultilineStrings: true 12 | BreakBeforeBinaryOperators: false 13 | BreakBeforeTernaryOperators: true 14 | BreakConstructorInitializersBeforeComma: false 15 | BinPackParameters: true 16 | ColumnLimit: 80 17 | ConstructorInitializerAllOnOneLineOrOnePerLine: true 18 | DerivePointerBinding: false 19 | ExperimentalAutoDetectBinPacking: false 20 | IndentCaseLabels: true 21 | MaxEmptyLinesToKeep: 1 22 | NamespaceIndentation: None 23 | ObjCSpaceBeforeProtocolList: false 24 | PenaltyBreakBeforeFirstCallParameter: 1 25 | PenaltyBreakComment: 60 26 | PenaltyBreakString: 1000 27 | PenaltyBreakFirstLessLess: 120 28 | PenaltyExcessCharacter: 1000000 29 | PenaltyReturnTypeOnItsOwnLine: 200 30 | PointerBindsToType: true 31 | SpacesBeforeTrailingComments: 2 32 | Cpp11BracedListStyle: true 33 | Standard: Auto 34 | IndentWidth: 2 35 | TabWidth: 8 36 | UseTab: Never 37 | BreakBeforeBraces: Attach 38 | IndentFunctionDeclarationAfterType: true 39 | SpacesInParentheses: false 40 | SpacesInAngles: false 41 | SpaceInEmptyParentheses: false 42 | SpacesInCStyleCastParentheses: false 43 | SpaceAfterControlStatementKeyword: true 44 | SpaceBeforeAssignmentOperators: true 45 | ContinuationIndentWidth: 4 46 | ... 47 | 48 | -------------------------------------------------------------------------------- /Ex16_Rvalue_Move_Semantics/src/myvector.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Class MyVector 5 | template 6 | class Myvector { 7 | public: 8 | // Allocate i elements, initialised as 0, 1, ..., 1-1, n 9 | explicit Myvector(int i) : size{i} { 10 | elems = new T[i](); 11 | for (int j = 0; j < i; ++j) { 12 | elems[j] = j; 13 | } 14 | } 15 | // Destructor 16 | ~Myvector() { delete[] elems; } 17 | 18 | // Copy constructor 19 | Myvector(const Myvector& newvector) : size{newvector.size} { 20 | elems = new T[newvector.size]; 21 | for (int i = 0; i < newvector.get_size(); ++i) 22 | elems[i] = newvector.elems[i]; 23 | } 24 | 25 | // Assignment operator 26 | Myvector& operator=(const Myvector& newvector) { 27 | size = newvector.size; 28 | elems = new T[newvector.size]; 29 | for (int i = 0; i < newvector.get_size(); ++i) 30 | elems[i] = newvector.elems[i]; 31 | return *this; 32 | } 33 | 34 | T& operator[](int index) const { return elems[index]; } 35 | int get_size() const { return size; } 36 | T* begin() { return elems; } // begin iterator 37 | T* end() { return elems + size; } // end iterator 38 | 39 | private: 40 | T* elems; 41 | int size; 42 | }; 43 | 44 | int main() { 45 | Myvector v{10}; 46 | std::cout << "Elements in v" << std::endl; 47 | for (const auto& x : v) std::cout << x << std::endl; 48 | Myvector v2{v}; 49 | std::cout << "Elements in v2" << std::endl; 50 | for (const auto& x : v2) std::cout << x << std::endl; 51 | std::cout << "How about the size of v now:" << v.get_size() << std::endl; 52 | } 53 | -------------------------------------------------------------------------------- /Ex05_Prototype/src/0_prototype_example/prototype.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "prototype.h" 4 | 5 | Prototype::~Prototype() 6 | { 7 | 8 | } 9 | 10 | MySQLManager::MySQLManager(const std::string& host,const std::string& login,const std::string& pass): m_host(host),m_login(login),m_pass(pass) { 11 | // we connect to the MySQL database. 12 | // this takes time because you need to connect and identify. 13 | // Heavy. 14 | } 15 | 16 | MySQLManager* MySQLManager::Clone() const { 17 | // copy constructor 18 | return (new MySQLManager(*this)); 19 | } 20 | 21 | void MySQLManager::Set(const std::string& base,const std::string& table) { 22 | m_base=base; 23 | m_table=table; 24 | // we connect to the m_base basis for studying m_table 25 | } 26 | 27 | void MySQLManager::Display() const { 28 | std::cout< manager2(manager1.Clone()); 42 | // and here manager2 is already connected to localhost with id Krishna.Pas need to reauthenticate. 43 | manager2->Display(); 44 | manager2->Set("fakedb2","table2"); 45 | manager2->Display(); 46 | 47 | // so on 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/5_generic_factory_method/factory.h: -------------------------------------------------------------------------------- 1 | #ifndef _FACTORY_H_ 2 | #define _FACTORY_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | //! Singleton factory implementation 10 | template 11 | class Factory { 12 | public: 13 | // Get the single instance of the factory 14 | static Factory* instance() { 15 | static Factory factory; 16 | return &factory; 17 | } 18 | 19 | // register a factory function to create an instance of className 20 | void register_factory(Key key, 21 | std::function class_factory_fn) { 22 | // register the class factory function 23 | registry[key] = class_factory_fn; 24 | } 25 | 26 | // create an instance of a registered class 27 | std::shared_ptr create(Key key) { 28 | BaseClass* instance = nullptr; 29 | // find name in the registry and call factory method. 30 | auto it = registry.find(key); 31 | if (it != registry.end()) { 32 | instance = it->second(); 33 | return std::shared_ptr(instance); 34 | } else 35 | return nullptr; 36 | } 37 | 38 | private: 39 | // Private constructor 40 | Factory() {} 41 | 42 | // Register of factory functions 43 | std::map> registry; 44 | }; 45 | 46 | // A helper class to register a factory function 47 | template 48 | class Register { 49 | public: 50 | Register(Key key) { 51 | // register the class factory function 52 | Factory::instance()->register_factory( 53 | key, [](void) -> BaseClass* { return new DerivedClass(); }); 54 | } 55 | }; 56 | 57 | #endif // _FACTORY_H_ 58 | -------------------------------------------------------------------------------- /Ex18_Tuple_Variadic_Templates_Metaprogramming/src/tuple.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | // Store account name, account no, balance 7 | using accounts = std::tuple; 8 | 9 | void print_account_info(const accounts& account) { 10 | std::cout << "( " << std::get<0>(account) << ", " << std::get<1>(account) 11 | << ", " << std::get<2>(account) << " )" << std::endl; 12 | } 13 | 14 | int main() { 15 | // Using the constructor to declare and initialize a tuple 16 | accounts alice{"Alice", 42, 748.90}; 17 | 18 | // Compare using the helper function to declare and initialize a tuple 19 | // aka Packing values in a tuple 20 | auto bob = std::make_tuple("Bob", 10, -15.12); 21 | 22 | // Making a copy of a tuple 23 | accounts alice2(alice); 24 | 25 | // Size of a tuple 26 | std::cout << "The size of tuple alice is: " 27 | << std::tuple_size::value << std::endl; 28 | 29 | // Accessing element type 30 | std::tuple_element<0, decltype(bob)>::type first = std::get<0>(bob); 31 | std::cout << "The first element in tuple bob is: " << first << std::endl; 32 | 33 | // Unpacking a tuple 34 | int accountno; 35 | double balance; 36 | std::tie(std::ignore, accountno, balance) = alice2; 37 | std::cout << "Account no of alice2 is: " << accountno 38 | << " and her balance is: " << balance << std::endl; 39 | 40 | std::vector vaccounts; 41 | std::string str = "Eve"; 42 | 43 | vaccounts.push_back(alice); 44 | vaccounts.push_back(bob); 45 | vaccounts.push_back(std::forward_as_tuple(str + " Smith", 23, 344.12)); 46 | 47 | std::cout << "The tuples in the vector are" << std::endl; 48 | for (auto account : vaccounts) print_account_info(account); 49 | } 50 | -------------------------------------------------------------------------------- /Ex10_Exception_Handling_II/src/multiple_exception.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double calculator(const double N1, const double N2, const char p); 4 | 5 | int main() { 6 | double operand1, operand2, result; 7 | char operator_; 8 | 9 | std::cout << "This program allows you to perform a division of two numbers" << std::endl; 10 | 11 | try { 12 | 13 | std::cout << "To proceed, enter" << std::endl; 14 | std::cout << "First Number: "; std::cin >> operand1; 15 | std::cout << "An Operator: "; std::cin >> operator_; 16 | std::cout << "Second Number: "; std::cin >> operand2; 17 | 18 | // Make sure the user typed a valid operator 19 | if(operator_ != '+' && operator_ != '-' && 20 | operator_ != '*' && operator_ != '/') 21 | throw operator_; 22 | 23 | // Find out if the denominator is 0 24 | if(operator_ == '/') 25 | if(operand2 == 0) 26 | throw 0; 27 | 28 | result = calculator(operand1, operand2, operator_); 29 | 30 | // Move this outside the try block and see what happens 31 | std::cout << operand1 << " " << operator_ << " " 32 | << operand2 << " = " << result; 33 | 34 | std::cout << std::endl; 35 | } 36 | catch(const char n) { 37 | std::cout << "Operation Error: " << n << " is not a valid operator" << std::endl; 38 | } 39 | catch(const int p) { 40 | std::cout << "Bad Operation: Division by " << p << " not allowed" << std::endl; 41 | } 42 | 43 | return 0; 44 | } 45 | 46 | double calculator(const double oper1, const double oper2, const char symbol) { 47 | double value; 48 | 49 | switch(symbol) { 50 | case '+': 51 | value = oper1 + oper2; 52 | break; 53 | 54 | case '-': 55 | value = oper1 - oper2; 56 | break; 57 | 58 | case '*': 59 | value = oper1 * oper2; 60 | break; 61 | 62 | case '/': 63 | value = oper1 / oper2; 64 | break; 65 | } 66 | 67 | return value; 68 | } 69 | -------------------------------------------------------------------------------- /Ex20_Metaclasses_Reflection/src/class_def.h: -------------------------------------------------------------------------------- 1 | #ifndef CLASS_MOP_ 2 | #define CLASS_MOP_ 3 | 4 | class ClassDef { // typedefs Container, Iterator for attributes 5 | public: 6 | ClassDef(ClassDef const* base, const string& name_) 7 | : baseClass(base), name(name_), definitionFix(false) { 8 | baseInit(); 9 | effectiveAttributes.insert(effectiveAttributes.end(), ownAttributes.begin(), 10 | ownAttributes.end()); 11 | } 12 | 13 | template 14 | ClassDef(ClassDef const* base, const string& name_, iterator attribBegin, 15 | iterator attribEnd) 16 | : baseClass(base), 17 | name(name_), 18 | ownAttributes(attribBegin, attribEnd), 19 | definitionFix(false) { 20 | baseInit(); 21 | effectiveAttributes.insert(effectiveAttributes.end(), ownAttributes.begin(), 22 | ownAttributes.end()); 23 | } 24 | 25 | string getName() const; 26 | Object* newObject() const { 27 | definitionFix = true; 28 | return new Object(this); 29 | } 30 | 31 | AttrIterator attribBegin() const; 32 | AttrIterator attribEnd() const; 33 | Attribute const& getAttribute(size_t idx) const; 34 | void addAttribute(const Attribute&); 35 | size_t getAttributeCount() const; 36 | 37 | size_t findAttribute(string const& name) const { 38 | // this does a reverse search to find the most derived 39 | AttributeContainer::const_reverse_iterator i; 40 | 41 | for (i = effectiveAttributes.rbegin(); i != effectiveAttributes.rend(); 42 | ++i) { 43 | if (i->getName() == name) { 44 | return distance(i, effectiveAttributes.rend()) - 1; 45 | } 46 | } 47 | return getAttributeCount(); 48 | } 49 | 50 | private: 51 | void baseInit() { 52 | if (baseClass) { 53 | baseClass->definitionFix = true; 54 | copy(baseClass->attribBegin(), baseClass->attribEnd(), 55 | back_inserter(effectiveAttributes)); 56 | } 57 | } 58 | 59 | ClassDef const* const baseClass; 60 | string name; 61 | AttributeContainer ownAttributes, effectiveAttributes; 62 | mutable bool definitionFix; 63 | }; 64 | 65 | #endif // Class MOP 66 | -------------------------------------------------------------------------------- /Ex05_Prototype/src/3_document_manager_prototype_factory_template/document.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "document.h" 6 | 7 | using namespace std; 8 | 9 | Document* XmlDoc::Clone() const { 10 | return new XmlDoc(*this); 11 | } 12 | 13 | void XmlDoc::view_document() { 14 | std::cout<<"A new XML document"< std::map Factory::m_map=std::map(); 34 | 35 | template void Factory::Register(Key key,Object* obj) { 36 | if(m_map.find(key)==m_map.end()) { 37 | m_map[key]=obj; 38 | } 39 | } 40 | 41 | template Object* Factory::Create (const Key& key) { 42 | Object* tmp=0; 43 | typename std::map::iterator it=m_map.find(key); 44 | 45 | if(it!=m_map.end()) { 46 | tmp=((*it).second)->Clone(); 47 | } 48 | 49 | return tmp; 50 | } 51 | 52 | int main(int argc, char** argv) { 53 | // Document factory 54 | Factory documentfactory; 55 | 56 | // Register the types 57 | Factory::Register("XML",new XmlDoc); 58 | Factory::Register("DOC",new PlainDoc); 59 | Factory::Register("C++",new C++); 60 | 61 | // User input 62 | if (argc != 2) { 63 | std::cerr << "Incorrect number of arguments" << std::endl; 64 | std::cerr << "Usage: ./document-generator (XML/DOC/XLS)" << std::endl; 65 | std::exit(EXIT_FAILURE); 66 | } 67 | std::string documenttype = argv[1]; 68 | 69 | 70 | // Objects are created via the factory 71 | 72 | Document* doc = documentfactory.Create(documenttype); 73 | 74 | doc->view_document(); 75 | 76 | delete doc; 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /Ex05_Prototype/src/2_document_manager_prototype_factory/document.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "document.h" 6 | 7 | Document* XmlDoc::Clone() const { 8 | return new XmlDoc(*this); 9 | } 10 | 11 | void XmlDoc::view_document() { 12 | std::cout<<"A new XML document"< DocumentFactory::m_map= std::map(); 32 | 33 | void DocumentFactory::Register(const std::string& key,Document* obj) { 34 | // if the key is not already present 35 | if(m_map.find(key)==m_map.end()) { 36 | // is added in the object map 37 | m_map[key]=obj; 38 | } 39 | 40 | } 41 | 42 | Document* DocumentFactory::Create(const std::string& key) const { 43 | Document* tmp=0; 44 | std::map::const_iterator it=m_map.find(key); 45 | 46 | // If the iterator is not map.end (), it means that the key has been found 47 | if(it!=m_map.end()) { 48 | tmp=((*it).second)->Clone(); 49 | } 50 | 51 | // could launch an exeption if the key was not found 52 | 53 | return tmp; 54 | } 55 | 56 | int main(int argc, char** argv) { 57 | // Documet factory 58 | DocumentFactory fac; 59 | 60 | // Register the types 61 | DocumentFactory::Register("XmlDoc",new XmlDoc); 62 | DocumentFactory::Register("PlainDoc",new PlainDoc); 63 | DocumentFactory::Register("SpreadSheet",new SpreadSheet); 64 | 65 | // Objects are created via the factory 66 | Document* doc = fac.Create("PlainDoc"); 67 | Document* xml = fac.Create("XmlDoc"); 68 | Document* xls =fac.Create("SpreadSheet"); 69 | 70 | doc->view_document(); 71 | xml->view_document(); 72 | xls->view_document(); 73 | 74 | delete doc; 75 | delete xml; 76 | delete xls; 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /Ex16_Rvalue_Move_Semantics/src/myvector_move.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Class MyVector 5 | template 6 | class Myvector { 7 | public: 8 | // Allocate i elements, initialised as 0, 1, ..., 1-1, n 9 | explicit Myvector(int i) : size{i} { 10 | elems = new T[i](); 11 | for (int j = 0; j < i; ++j) { 12 | elems[j] = j; 13 | } 14 | } 15 | // Destructor 16 | ~Myvector() { delete[] elems; } 17 | 18 | // Copy constructor 19 | Myvector(const Myvector& newvector) : size{newvector.size} { 20 | elems = new T[newvector.size]; 21 | for (int i = 0; i < newvector.get_size(); ++i) 22 | elems[i] = newvector.elems[i]; 23 | } 24 | 25 | // Assignment & move operator 26 | Myvector& operator=(Myvector newvector) { 27 | swap(newvector); 28 | return *this; 29 | } 30 | 31 | // Swap the vectors 32 | void swap(Myvector& newvector) { 33 | std::swap(elems, newvector.elems); 34 | std::swap(size, newvector.size); 35 | // Inefficient way of doing this would be 36 | // Myvector temp{newvector}; // unnecessary temp 37 | // newvector = *this; 38 | //*this = temp; 39 | } 40 | 41 | // Move construct 42 | Myvector(Myvector&& newvector) : size{0}, elems{nullptr} { 43 | swap(newvector); 44 | } 45 | 46 | // Alternative move construct 47 | // Myvector(Myvector&& newvector) 48 | // : size{newvector.size}, elems{newvector.elems} { 49 | // newvector.size = 0; 50 | // newvector.elems = nullptr; // no elements 51 | // } 52 | 53 | T& operator[](int index) const { return elems[index]; } 54 | int get_size() const { return size; } 55 | T* begin() { return elems; } // begin iterator 56 | T* end() { return elems + size; } // end iterator 57 | 58 | private: 59 | T* elems; 60 | int size; 61 | }; 62 | 63 | int main() { 64 | Myvector v{10}; 65 | std::cout << "Elements in v" << std::endl; 66 | for (const auto& x : v) std::cout << x << std::endl; 67 | Myvector v2 = std::move(v); // try just passing 'v' as well v2 = v; 68 | std::cout << "Elements in v2" << std::endl; 69 | for (const auto& x : v2) std::cout << x << std::endl; 70 | std::cout << "How about the size of v :" << v.get_size() << std::endl; 71 | } 72 | -------------------------------------------------------------------------------- /Ex05_Prototype/src/1_document_manager/docmanager.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | const int N = 4; // # of document types + 1 7 | 8 | // Prototype 9 | class Document 10 | { 11 | public: 12 | virtual Document* clone() const = 0; 13 | virtual void store() const = 0; 14 | virtual ~Document() { } 15 | }; 16 | 17 | 18 | // Concrete prototypes : xmlDoc, plainDoc, spreadsheetDoc 19 | class xmlDoc : public Document 20 | { 21 | public: 22 | Document* clone() const { return new xmlDoc; } 23 | void store() const { std::cout << "xmlDoc\n"; } 24 | }; 25 | 26 | class plainDoc : public Document 27 | { 28 | public: 29 | Document* clone() const { return new plainDoc; } 30 | void store() const { std::cout << "plainDoc\n"; } 31 | }; 32 | 33 | class spreadsheetDoc : public Document 34 | { 35 | public: 36 | Document* clone() const { return new spreadsheetDoc; } 37 | void store() const { std::cout << "spreadsheetDoc\n"; } 38 | }; 39 | 40 | // makeDocument() calls Concrete Portotype's clone() method 41 | // inherited from Prototype 42 | class DocumentManager { 43 | public: 44 | static Document* makeDocument( int choice ); 45 | ~DocumentManager(){} 46 | 47 | private: 48 | static Document* mDocTypes[N]; 49 | }; 50 | 51 | Document* DocumentManager::mDocTypes[] = 52 | { 53 | 0, new xmlDoc, new plainDoc, new spreadsheetDoc 54 | }; 55 | 56 | Document* DocumentManager::makeDocument( int choice ) 57 | { 58 | return mDocTypes[choice]->clone(); 59 | } 60 | 61 | // for_each op () 62 | struct Destruct 63 | { 64 | void operator()(Document *a) const { 65 | delete a; 66 | } 67 | }; 68 | 69 | // Client 70 | int main(int argc, char** argv) { 71 | std::vector docs(N); 72 | int choice; 73 | std::cout << "quit(0), xml(1), plain(2), spreadsheet(3): " << std::endl; 74 | while(true) { 75 | std::cout << "Type in your choice (0-3)\n"; 76 | std::cin >> choice; 77 | if(choice <= 0 || choice >= N) 78 | break; 79 | docs[choice] = DocumentManager::makeDocument( choice ); 80 | } 81 | 82 | for(int i = 1; i < docs.size(); ++i) 83 | if(docs[i]) docs[i]->store(); 84 | 85 | Destruct d; 86 | // this calls Destruct::operator() 87 | std::for_each(docs.begin(), docs.end(), d); 88 | 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /Ex16_Rvalue_Move_Semantics/src/myvector_mult_bad.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // Class MyVector 6 | template 7 | class Myvector { 8 | public: 9 | // Allocate i elements, initialised as 0, 1, ..., 1-1, n 10 | explicit Myvector(int i) : size{i} { 11 | elems = new T[i](); 12 | for (int j = 0; j < i; ++j) { 13 | elems[j] = j; 14 | } 15 | } 16 | // Destructor 17 | ~Myvector() { delete[] elems; } 18 | 19 | // Copy constructor 20 | Myvector(const Myvector& newvector) : size{newvector.size} { 21 | elems = new T[newvector.size]; 22 | for (int i = 0; i < newvector.get_size(); ++i) 23 | elems[i] = newvector.elems[i]; 24 | } 25 | 26 | // Assignment operator 27 | Myvector& operator=(const Myvector& newvector) { 28 | size = newvector.size; 29 | elems = new T[newvector.size]; 30 | for (int i = 0; i < newvector.get_size(); ++i) 31 | elems[i] = newvector.elems[i]; 32 | return *this; 33 | } 34 | 35 | T& operator[](int index) const { return elems[index]; } 36 | int get_size() const { return size; } 37 | T* begin() { return elems; } // begin iterator 38 | T* end() { return elems + size; } // end iterator 39 | 40 | private: 41 | T* elems; 42 | int size; 43 | }; 44 | 45 | template 46 | Myvector operator*(const Myvector& a, const Myvector& b) { 47 | if (a.get_size() != b.get_size()) std::exit(EXIT_FAILURE); 48 | Myvector res(a.get_size()); 49 | for (int i = 0; i != a.get_size(); ++i) res[i] = a[i] * b[i]; 50 | std::cout << "Address of res: " << &res << std::endl; 51 | return res; 52 | } 53 | 54 | template 55 | void make_vector(const Myvector& a, const Myvector& b, 56 | std::vector> &nv) { 57 | if (a.get_size() != b.get_size()) std::exit(EXIT_FAILURE); 58 | 59 | Myvector res(a.get_size()); 60 | for (int i = 0; i != a.get_size(); ++i) res[i] = a[i] * b[i]; 61 | nv.push_back(res); 62 | } 63 | 64 | int main() { 65 | Myvector v{100000}; 66 | Myvector v2{v}; 67 | Myvector v3{v}; 68 | v3 = v * v2; 69 | std::cout << "Address of v3: " << &v3 << std::endl; 70 | // std::vector> vec; 71 | // for (int i = 0; i < 1000; ++i) make_vector(v, v2, vec); 72 | // std::cout << "Size of vector - vec : " << vec.size() << std::endl; 73 | } 74 | -------------------------------------------------------------------------------- /Ex16_Rvalue_Move_Semantics/src/myvector_mult_good.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // Class MyVector 6 | template 7 | class Myvector { 8 | public: 9 | // Allocate i elements, initialised as 0, 1, ..., 1-1, n 10 | explicit Myvector(int i) : size{i} { 11 | elems = new T[i](); 12 | for (int j = 0; j < i; ++j) { 13 | elems[j] = j; 14 | } 15 | } 16 | // Destructor 17 | ~Myvector() { delete[] elems; } 18 | 19 | // Copy constructor 20 | Myvector(const Myvector& newvector) : size{newvector.size} { 21 | elems = new T[newvector.size]; 22 | for (int i = 0; i < newvector.get_size(); ++i) 23 | elems[i] = newvector.elems[i]; 24 | } 25 | 26 | // Assignment operator 27 | Myvector& operator=(const Myvector& newvector) { 28 | size = newvector.size; 29 | elems = new T[newvector.size]; 30 | for (int i = 0; i < newvector.get_size(); ++i) 31 | elems[i] = newvector.elems[i]; 32 | return *this; 33 | } 34 | 35 | // Move construct 36 | Myvector(Myvector&& newvector) 37 | : size{newvector.size}, elems{newvector.elems} { 38 | newvector.size = 0; 39 | newvector.elems = nullptr; // no elements 40 | } 41 | 42 | T& operator[](int index) const { return elems[index]; } 43 | int get_size() const { return size; } 44 | T* begin() { return elems; } // begin iterator 45 | T* end() { return elems + size; } // end iterator 46 | 47 | private: 48 | T* elems; 49 | int size; 50 | }; 51 | 52 | template 53 | Myvector operator*(const Myvector& a, const Myvector& b) { 54 | if (a.get_size() != b.get_size()) { 55 | std::cout << "Vectors are not of the same size" << std::endl; 56 | std::exit(EXIT_FAILURE); 57 | } 58 | Myvector res(a.get_size()); 59 | for (int i = 0; i != a.get_size(); ++i) res[i] = a[i] * b[i]; 60 | 61 | return std::move(res); // return statements are usually an rvalue 62 | } 63 | 64 | template 65 | void make_vector(const Myvector& a, const Myvector& b, 66 | std::vector>& nv) { 67 | if (a.get_size() != b.get_size()) std::exit(EXIT_FAILURE); 68 | 69 | Myvector res(a.get_size()); 70 | for (int i = 0; i != a.get_size(); ++i) res[i] = a[i] * b[i]; 71 | nv.push_back(std::move(res)); 72 | } 73 | 74 | int main() { 75 | Myvector v{100000}; 76 | Myvector v2{v}; 77 | Myvector v3{v*v2}; 78 | // std::vector> vec; 79 | // for (int i = 0; i < 1000; ++i) make_vector(v, v2, vec); 80 | // std::cout << "Size of vector - vec : " << vec.size() << std::endl; 81 | } 82 | -------------------------------------------------------------------------------- /Ex01_Design_Pattern_Abstract_Factory/src/abstract_factory/abstract_factory.cc: -------------------------------------------------------------------------------- 1 | #include 2 | // Using macros is bad! really bad. 3 | #define _SINGAPORE_ 4 | 5 | // Abstract Product: ShippingFee 6 | class ShippingFee 7 | { 8 | public: 9 | virtual void compute() = 0; 10 | }; 11 | 12 | // Concrete Product: Europe Shipping Fee 13 | class EuropeShippingFee : public ShippingFee 14 | { 15 | public: 16 | void compute(){ 17 | std::cout << "Europe ShippingFee: $4.99" << std::endl; 18 | } 19 | }; 20 | 21 | // Concrete Product: Singapore Shipping Fee 22 | class SingaporeShippingFee : public ShippingFee 23 | { 24 | public: 25 | void compute(){ 26 | std::cout << "Singapore ShippingFee $39.99" << std::endl; 27 | } 28 | }; 29 | 30 | // Abstract Product: Tax 31 | class Tax 32 | { 33 | public: 34 | virtual void compute() = 0; 35 | }; 36 | 37 | // Concrete Product: Europe Tax Fee 38 | class EuropeTax : public Tax 39 | { 40 | public: 41 | void compute(){ 42 | std::cout << "Europe Tax: $49.99" << std::endl; 43 | } 44 | }; 45 | 46 | // Concrete Product: Singapore Tax Fee 47 | class SingaporeTax : public Tax { 48 | public: 49 | void compute(){ 50 | std::cout << "Singapore Tax $19.99" << std::endl; 51 | } 52 | }; 53 | 54 | // Abstract Factory 55 | class FinanceToolFactory 56 | { 57 | public: 58 | virtual ShippingFee* calculateShippingFee() = 0; 59 | virtual Tax* calculateTax() = 0; 60 | }; 61 | 62 | // Concrete factory: country - Europe 63 | class EuropeFinanceFactory : public FinanceToolFactory 64 | { 65 | public: 66 | ShippingFee* calculateShippingFee(){ 67 | return new EuropeShippingFee; 68 | } 69 | Tax* calculateTax(){ 70 | return new EuropeTax; 71 | } 72 | }; 73 | 74 | 75 | // Concrete factory: country - Singapore 76 | class SingaporeFinanceFactory : public FinanceToolFactory 77 | { 78 | public: 79 | ShippingFee* calculateShippingFee(){ 80 | return new SingaporeShippingFee; 81 | } 82 | Tax* calculateTax(){ 83 | return new SingaporeTax; 84 | } 85 | }; 86 | 87 | 88 | // Client side implementation 89 | int main() { 90 | FinanceToolFactory* finance_factory; 91 | ShippingFee *ship_fee; 92 | Tax *tax; 93 | 94 | #ifdef _SINGAPORE_ 95 | finance_factory = new SingaporeFinanceFactory; 96 | ship_fee = finance_factory->calculateShippingFee(); 97 | ship_fee -> compute(); 98 | tax = finance_factory->calculateTax(); 99 | tax -> compute(); 100 | #else 101 | finance_factory = new EuropeFinanceFactory; 102 | ship_fee = finance_factory->calculateShippingFee(); 103 | ship_fee -> compute(); 104 | tax = finance_factory->calculateTax(); 105 | tax -> compute(); 106 | #endif 107 | return 0; 108 | } 109 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/1_factory_method_simple/factory_method.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | // Abstract Product: Course 6 | class Course 7 | { 8 | public: 9 | virtual ~Course(){} 10 | 11 | virtual void info() = 0; 12 | virtual void schedule() = 0; 13 | }; 14 | 15 | // Concrete Product: C++ 16 | class Cpp : public Course 17 | { 18 | public: 19 | virtual ~Cpp(){} 20 | void info(){ 21 | std::cout << "This is a C++ Course" << std::endl; 22 | } 23 | 24 | void schedule(){ 25 | std::cout << "This is the C++ course schedule" << std::endl; 26 | } 27 | }; 28 | 29 | // Concrete Product: Java 30 | class Java : public Course 31 | { 32 | public: 33 | virtual ~Java(){} 34 | void info(){ 35 | std::cout << "This is a Java Course" << std::endl; 36 | } 37 | void schedule(){ 38 | std::cout << "This is the Java course schedule" << std::endl; 39 | } 40 | 41 | }; 42 | 43 | 44 | // Abstract Factory 45 | class CourseFactory 46 | { 47 | public: 48 | virtual ~CourseFactory(){} 49 | virtual Course* generatecourse() = 0; 50 | //Factory Method 51 | static CourseFactory* create_course(std::string& coursename); 52 | }; 53 | 54 | 55 | // Concrete factory: C++ 56 | class CppFactory : public CourseFactory 57 | { 58 | public: 59 | virtual ~CppFactory(){} 60 | Course* generatecourse(){ 61 | return new Cpp; 62 | } 63 | }; 64 | 65 | // Concrete factory: Java 66 | class JavaFactory : public CourseFactory 67 | { 68 | public: 69 | virtual ~JavaFactory(){} 70 | Course* generatecourse(){ 71 | return new Java; 72 | } 73 | }; 74 | 75 | //Factory method generates a product 76 | CourseFactory* CourseFactory::create_course(std::string& coursename) { 77 | 78 | CourseFactory* course = NULL; 79 | 80 | if (coursename == "C++") { 81 | course = new CppFactory; 82 | } 83 | else if (coursename == "Java") { 84 | course = new JavaFactory; 85 | } 86 | else { 87 | std::cerr << "Invalid course name. Program terminating" << std::endl; 88 | std::exit(EXIT_FAILURE); 89 | } 90 | return course; 91 | } 92 | 93 | // Client side implementation 94 | int main(int argc, char* argv[]) { 95 | 96 | // User input 97 | if (argc != 2) { 98 | std::cerr << "Incorrect number of arguments" << std::endl; 99 | std::cerr << "Usage: ./factory course (C++/Java)" << std::endl; 100 | std::exit(EXIT_FAILURE); 101 | } 102 | std::string coursename = argv[1]; 103 | 104 | CourseFactory* newcourse = CourseFactory::create_course(coursename); 105 | Course* course = newcourse->generatecourse(); 106 | course->info(); 107 | course->schedule(); 108 | 109 | delete course; 110 | delete newcourse; 111 | 112 | return 0; 113 | } 114 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Large scale C++ Software development tutorials 2 | ======================== 3 | > Krishna Kumar 4 | 5 | ### [C++ Style guide](http://kks32.user.srcf.net/cpp-style-guide/cppguide.xml) 6 | 7 | # Course 8 | 9 | [Ex01: Design pattern: Abstract factory](https://github.com/kks32/cpp-software-development/tree/master/Ex01_Design_Pattern_Abstract_Factory) 10 | 11 | [Ex02: Design pattern: Factory method](https://github.com/kks32/cpp-software-development/tree/master/Ex02_Factory_Method) 12 | 13 | [Ex03: Smart pointers](https://github.com/kks32/cpp-software-development/tree/master/Ex03_Smart_Pointers) 14 | 15 | [Ex04: Lambda functions](https://github.com/kks32/cpp-software-development/tree/master/Ex04_Lamda_Functions) 16 | 17 | [Ex05: Design pattern: Prototype](https://github.com/kks32/cpp-software-development/tree/master/Ex05_Prototype) 18 | 19 | [Ex06: Pointers - (Part 1/3)](https://github.com/kks32/cpp-software-development/tree/master/Ex06_Pointers_I) 20 | 21 | [Ex07: Pointers - (Part 2/3)](https://github.com/kks32/cpp-software-development/tree/master/Ex07_Pointers_II) 22 | 23 | [Ex08: Pointers - (Part 3/3)](https://github.com/kks32/cpp-software-development/tree/master/Ex08_Pointers_III) 24 | 25 | [Ex09: Exception handling (part 1/2)](https://github.com/kks32/cpp-software-development/tree/master/Ex09_Exception_Handling_I) 26 | 27 | [Ex10: Exception handling (part 2/2)](https://github.com/kks32/cpp-software-development/tree/master/Ex10_Exception_Handling_II) 28 | 29 | [Ex11: Classes](https://github.com/kks32/cpp-software-development/tree/master/Ex11_Classes_I) 30 | 31 | [Ex12: Shared memory parallelisation (OpenMP)](https://github.com/kks32/cpp-software-development/tree/master/Ex12_Parallelisation) 32 | 33 | [Ex13: C++11 Threads: Atomic, Locks & Mutex](https://github.com/kks32/cpp-software-development/tree/master/Ex13_Threads) 34 | 35 | [Ex14: Memory models: Locks & Mutex (pthreads)](https://github.com/kks32/cpp-software-development/tree/master/Ex14_Memory_Models_Compiler_Optimisation) 36 | 37 | [Ex15: Functors and Accessors](https://github.com/kks32/cpp-software-development/tree/master/Ex15_Functors) 38 | 39 | [Ex16: Universal references, Rvalues and Move semantics](https://github.com/kks32/cpp-software-development/tree/master/Ex16_Rvalue_Move_Semantics) 40 | 41 | [Ex17: RTTI, Traits and Policy](https://github.com/kks32/cpp-software-development/tree/master/Ex17_RTTI_Traits) 42 | 43 | [Ex18: Tuples, Variadic templates and template metaprogramming](https://github.com/kks32/cpp-software-development/tree/master/Ex18_Tuple_Variadic_Templates_Metaprogramming) 44 | 45 | [Ex18: Lazy evaluation and Reference counting](https://github.com/kks32/cpp-software-development/tree/master/Ex19_Lazy_Evaluation_Reference_Counting) 46 | 47 | [Ex20: Metaclasses and Reflection](https://github.com/kks32/cpp-software-development/tree/master/Ex20_Metaclasses_Reflection) 48 | 49 | [Ex21: Name Lookup and Interface](https://github.com/kks32/cpp-software-development/tree/master/Ex21_Name_Lookup_Interface) 50 | 51 | [Ex22: C++ Quiz](https://github.com/kks32/cpp-software-development/tree/master/Ex22_C++_Quiz) 52 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/6_generic_factory_constructor/factory.h: -------------------------------------------------------------------------------- 1 | #ifndef _FACTORY_H_ 2 | #define _FACTORY_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | //! \brief Singleton factory implementation 11 | //! \tparam Tbaseclass Base class 12 | //! \tparam Targs variadic template arguments 13 | template 14 | class Factory { 15 | public: 16 | //! Get the single instance of the factory 17 | static Factory* instance() { 18 | static Factory factory; 19 | return &factory; 20 | } 21 | 22 | //! Register a factory function to create an instance of classname 23 | //! \param[in] key Register key 24 | //! \tparam Tderivedclass Derived class 25 | template 26 | void register_factory(const std::string& key) { 27 | registry[key].reset(new Creator); 28 | } 29 | 30 | //! Create an instance of a registered class 31 | //! \param[in] key key to item in registry 32 | //! \param[in] args Variadic template arguments 33 | //! \retval shared_ptr Shared pointer to a base class 34 | std::shared_ptr create(const std::string& key, Targs&&... args) { 35 | if (!this->check(key)) 36 | throw std::runtime_error("Invalid key: " + key + 37 | ", not found in the factory register!"); 38 | return registry.at(key)->create(std::forward(args)...); 39 | } 40 | 41 | //! Check if an element is registered 42 | //! \param[in] key Key to be checked in registry 43 | //! \retval status Return if key is in registry or not 44 | bool check(const std::string& key) const { 45 | bool status = false; 46 | for (const auto& keyvalue : registry) 47 | if (keyvalue.first == key) status = true; 48 | return status; 49 | } 50 | 51 | private: 52 | // Private constructor 53 | Factory() = default; 54 | 55 | //! A base class creator struct 56 | struct CreatorBase { 57 | //! A virtual create function 58 | virtual std::shared_ptr create(Targs&&...) = 0; 59 | }; 60 | 61 | //! Creator class 62 | //! \tparam Tderivedclass Derived class 63 | template 64 | struct Creator : public CreatorBase { 65 | //! Create instance of object 66 | std::shared_ptr create(Targs&&... args) override { 67 | return std::make_shared(std::forward(args)...); 68 | } 69 | }; 70 | // Register of factory functions 71 | std::map> registry; 72 | }; 73 | 74 | //! A helper class to register a factory function 75 | //! \tparam Tbaseclass Base class 76 | //! \tparam Tderivedclass Derived class 77 | //! \tparam Targs variadic template arguments 78 | template 79 | class Register { 80 | public: 81 | //! Register with a given key 82 | //! \param[in] key Key to item in registry 83 | explicit Register(const std::string& key) { 84 | // register the class factory function 85 | Factory::instance() 86 | ->template register_factory(key); 87 | } 88 | }; 89 | 90 | #endif // _FACTORY_H_ 91 | -------------------------------------------------------------------------------- /Ex14_Memory_Models_Compiler_Optimisation/src/dekker.cc: -------------------------------------------------------------------------------- 1 | // Dekker's algorithm, implemented on pthreads 2 | // http://jakob.engbloms.se/wp-content/uploads/2008/01/dekker.c 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define PRINT_PROGRESS 10 | 11 | static volatile int flag1 = 0; 12 | static volatile int flag2 = 0; 13 | static volatile int turn = 1; 14 | static volatile int gSharedCounter = 0; 15 | int gLoopCount; 16 | int gOnePercent; 17 | 18 | void dekker1() { 19 | flag1 = 1; 20 | turn = 2; 21 | while ((flag2 == 1) && (turn == 2)); 22 | // Critical section 23 | gSharedCounter++; 24 | // Let the other task run 25 | flag1 = 0; 26 | } 27 | 28 | void dekker2(void) { 29 | flag2 = 1; 30 | turn = 1; 31 | while ((flag1 == 1) && (turn == 1)); 32 | // critical section 33 | gSharedCounter++; 34 | // leave critical section 35 | flag2 = 0; 36 | } 37 | 38 | // 39 | // Tasks, as a level of indirection 40 | // 41 | void* task1(void* arg) { 42 | int i, j; 43 | printf("Starting task1\n"); 44 | // Do the dekker very many times 45 | #ifdef PRINT_PROGRESS 46 | for (i = 0; i < 100; i++) { 47 | printf("[One] at %d%%\n", i); 48 | for (j = gOnePercent; j > 0; j--) { 49 | dekker1(); 50 | } 51 | } 52 | #else 53 | // Simple basic loop 54 | for (i = gLoopCount; i > 0; i--) { 55 | dekker1(); 56 | } 57 | #endif 58 | } 59 | 60 | void* task2(void* arg) { 61 | int i, j; 62 | printf("Starting task2\n"); 63 | #ifdef PRINT_PROGRESS 64 | for (i = 0; i < 100; i++) { 65 | printf("[Two] at %d%%\n", i); 66 | for (j = gOnePercent; j > 0; j--) { 67 | dekker2(); 68 | } 69 | } 70 | #else 71 | for (i = gLoopCount; i > 0; i--) { 72 | dekker2(); 73 | } 74 | #endif 75 | } 76 | 77 | int main(int argc, char** argv) { 78 | int loopCount = 0; 79 | pthread_t dekker_thread_1; 80 | pthread_t dekker_thread_2; 81 | void* returnCode; 82 | int result; 83 | int expected_sum; 84 | 85 | /* Check arguments to program*/ 86 | if (argc != 2) { 87 | fprintf(stderr, "USAGE: %s \n", argv[0]); 88 | exit(1); 89 | } 90 | 91 | /* Parse argument */ 92 | loopCount = atoi(argv[1]); /* Don't bother with format checking */ 93 | gLoopCount = loopCount; 94 | gOnePercent = loopCount / 100; 95 | expected_sum = 2 * loopCount; 96 | 97 | /* Start the threads */ 98 | result = pthread_create(&dekker_thread_1, NULL, task1, NULL); 99 | result = pthread_create(&dekker_thread_2, NULL, task2, NULL); 100 | 101 | /* Wait for the threads to end */ 102 | result = pthread_join(dekker_thread_1, &returnCode); 103 | result = pthread_join(dekker_thread_2, &returnCode); 104 | printf("Both threads terminated\n"); 105 | 106 | /* Check result */ 107 | if (gSharedCounter != expected_sum) { 108 | printf("[-] Dekker did not work, sum %d rather than %d.\n", gSharedCounter, 109 | expected_sum); 110 | printf(" %d missed updates due to memory consistency races.\n", 111 | (expected_sum - gSharedCounter)); 112 | return 1; 113 | } else { 114 | printf("[+] Dekker worked.\n"); 115 | return 0; 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /Ex01_Design_Pattern_Abstract_Factory/src/abstract_factory_good/abstract_factory_good.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | // Abstract Product: ShippingFee 6 | class ShippingFee 7 | { 8 | public: 9 | virtual void compute() = 0; 10 | virtual ~ShippingFee(){} 11 | }; 12 | 13 | // Concrete Product: Europe Shipping Fee 14 | class EuropeShippingFee : public ShippingFee 15 | { 16 | public: 17 | virtual ~EuropeShippingFee(){} 18 | void compute(){ 19 | std::cout << "Europe ShippingFee: $4.99" << std::endl; 20 | } 21 | }; 22 | 23 | // Concrete Product: Singapore Shipping Fee 24 | class SingaporeShippingFee : public ShippingFee 25 | { 26 | public: 27 | virtual ~SingaporeShippingFee(){} 28 | void compute(){ 29 | std::cout << "Singapore ShippingFee $39.99" << std::endl; 30 | } 31 | }; 32 | 33 | // Abstract Product: Tax 34 | class Tax 35 | { 36 | public: 37 | virtual void compute() = 0; 38 | virtual ~Tax(){} 39 | }; 40 | 41 | // Concrete Product: Europe Tax Fee 42 | class EuropeTax : public Tax 43 | { 44 | public: 45 | virtual ~EuropeTax(){} 46 | void compute(){ 47 | std::cout << "Europe Tax: $49.99" << std::endl; 48 | } 49 | }; 50 | 51 | // Concrete Product: Singapore Tax Fee 52 | class SingaporeTax : public Tax { 53 | public: 54 | virtual ~SingaporeTax(){} 55 | void compute(){ 56 | std::cout << "Singapore Tax $19.99" << std::endl; 57 | } 58 | }; 59 | 60 | // Abstract Factory 61 | class FinanceToolFactory 62 | { 63 | public: 64 | virtual ~FinanceToolFactory(){} 65 | enum FinanceFactories { 66 | Europe, 67 | Singapore 68 | }; 69 | virtual ShippingFee* calculateShippingFee() = 0; 70 | virtual Tax* calculateTax() = 0; 71 | 72 | static FinanceToolFactory* createFactory(std::string& country); 73 | }; 74 | 75 | // Concrete factory: country - Europe 76 | class EuropeFinanceFactory : public FinanceToolFactory 77 | { 78 | public: 79 | virtual ~EuropeFinanceFactory(){} 80 | ShippingFee* calculateShippingFee (){ 81 | return new EuropeShippingFee; 82 | } 83 | Tax* calculateTax (){ 84 | return new EuropeTax; 85 | } 86 | }; 87 | 88 | // Concrete factory: country - Singapore 89 | class SingaporeFinanceFactory : public FinanceToolFactory 90 | { 91 | public: 92 | virtual ~SingaporeFinanceFactory(){} 93 | ShippingFee* calculateShippingFee(){ 94 | return new SingaporeShippingFee; 95 | } 96 | Tax* calculateTax(){ 97 | return new SingaporeTax; 98 | } 99 | }; 100 | 101 | 102 | // This section is usally a c++ file for abstract factory 103 | FinanceToolFactory* FinanceToolFactory::createFactory(std::string& country) { 104 | if (country == "Europe") { 105 | return new EuropeFinanceFactory; 106 | } 107 | else if (country == "Singapore") { 108 | return new SingaporeFinanceFactory; 109 | } 110 | else { 111 | std::cerr << "Invalid country name. Program terminating" << std::endl; 112 | std::exit(EXIT_FAILURE); 113 | } 114 | } 115 | 116 | // Client side implementation 117 | int main(int argc, char* argv[]) { 118 | 119 | // User input 120 | if (argc != 2) { 121 | std::cerr << "Incorrect number of arguments" << std::endl; 122 | std::cerr << "Usage: ./factory countryname (Europe/Singapore)" << std::endl; 123 | std::exit(EXIT_FAILURE); 124 | } 125 | std::string countryname = argv[1]; 126 | 127 | 128 | FinanceToolFactory* finance_factory = FinanceToolFactory::createFactory(countryname); 129 | ShippingFee* ship_fee = finance_factory->calculateShippingFee(); 130 | ship_fee -> compute(); 131 | delete ship_fee; 132 | Tax* tax = finance_factory->calculateTax(); 133 | tax -> compute(); 134 | delete tax; 135 | delete finance_factory; 136 | return 0; 137 | } 138 | -------------------------------------------------------------------------------- /Ex01_Design_Pattern_Abstract_Factory/src/abstract_factory_good/valgrind.txt: -------------------------------------------------------------------------------- 1 | ==8669== Memcheck, a memory error detector 2 | ==8669== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. 3 | ==8669== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info 4 | ==8669== Command: ./factory Europe 2 5 | ==8669== 6 | --8669-- Valgrind options: 7 | --8669-- --leak-check=full 8 | --8669-- -v 9 | --8669-- Contents of /proc/version: 10 | --8669-- Linux version 3.15.4-200.fc20.x86_64 (mockbuild@bkernel02.phx2.fedoraproject.org) (gcc version 4.8.3 20140624 (Red Hat 4.8.3-1) (GCC) ) #1 SMP Mon Jul 7 14:24:41 UTC 2014 11 | --8669-- Arch and hwcaps: AMD64, amd64-cx16-sse3 12 | --8669-- Page sizes: currently 4096, max supported 4096 13 | --8669-- Valgrind library directory: /usr/lib64/valgrind 14 | --8669-- Reading syms from /home/krishna/Development/cpp-tutorials/Ex1_Design_Pattern_Abstract_Factory/src/abstract_factory_good/factory 15 | --8669-- Reading syms from /usr/lib64/valgrind/memcheck-amd64-linux 16 | --8669-- object doesn't have a symbol table 17 | --8669-- object doesn't have a dynamic symbol table 18 | --8669-- Reading syms from /usr/lib64/ld-2.18.so 19 | --8669-- Scheduler: using generic scheduler lock implementation. 20 | --8669-- Reading suppressions file: /usr/lib64/valgrind/default.supp 21 | ==8669== embedded gdbserver: reading from /tmp/vgdb-pipe-from-vgdb-to-8669-by-krishna-on-kks32 22 | ==8669== embedded gdbserver: writing to /tmp/vgdb-pipe-to-vgdb-from-8669-by-krishna-on-kks32 23 | ==8669== embedded gdbserver: shared mem /tmp/vgdb-pipe-shared-mem-vgdb-8669-by-krishna-on-kks32 24 | ==8669== 25 | ==8669== TO CONTROL THIS PROCESS USING vgdb (which you probably 26 | ==8669== don't want to do, unless you know exactly what you're doing, 27 | ==8669== or are doing some strange experiment): 28 | ==8669== /usr/lib64/valgrind/../../bin/vgdb --pid=8669 ...command... 29 | ==8669== 30 | ==8669== TO DEBUG THIS PROCESS USING GDB: start GDB like this 31 | ==8669== /path/to/gdb ./factory 32 | ==8669== and then give GDB the following command 33 | ==8669== target remote | /usr/lib64/valgrind/../../bin/vgdb --pid=8669 34 | ==8669== --pid is optional if only one valgrind process is running 35 | ==8669== 36 | --8669-- REDIR: 0x3cfdc18430 (strlen) redirected to 0x38067461 (???) 37 | --8669-- Reading syms from /usr/lib64/valgrind/vgpreload_core-amd64-linux.so 38 | --8669-- object doesn't have a symbol table 39 | --8669-- Reading syms from /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so 40 | --8669-- object doesn't have a symbol table 41 | --8669-- REDIR: 0x3cfdc18380 (index) redirected to 0x4a08f60 (index) 42 | --8669-- REDIR: 0x3cfdc18400 (strcmp) redirected to 0x4a0a040 (strcmp) 43 | --8669-- Reading syms from /usr/lib64/libstdc++.so.6.0.19 44 | --8669-- object doesn't have a symbol table 45 | --8669-- Reading syms from /usr/lib64/libm-2.18.so 46 | --8669-- Reading syms from /usr/lib64/libgcc_s-4.8.3-20140624.so.1 47 | --8669-- object doesn't have a symbol table 48 | --8669-- Reading syms from /usr/lib64/libc-2.18.so 49 | --8669-- REDIR: 0x3cfe089b90 (strcasecmp) redirected to 0x4801716 (_vgnU_ifunc_wrapper) 50 | --8669-- REDIR: 0x3cfe08be80 (strncasecmp) redirected to 0x4801716 (_vgnU_ifunc_wrapper) 51 | --8669-- REDIR: 0x3cfe089360 (memcpy@GLIBC_2.2.5) redirected to 0x4801716 (_vgnU_ifunc_wrapper) 52 | --8669-- REDIR: 0x3cfe088340 (__GI_strrchr) redirected to 0x4a08d80 (__GI_strrchr) 53 | --8669-- REDIR: 0x3cfe0865f0 (strlen) redirected to 0x4a092f0 (strlen) 54 | --8669-- REDIR: 0x3cfe084b90 (strcmp) redirected to 0x4801716 (_vgnU_ifunc_wrapper) 55 | --8669-- REDIR: 0x3cfe13c200 (__strcmp_ssse3) redirected to 0x4a09fa0 (strcmp) 56 | Incorrect number of arguments--8669-- REDIR: 0x3cfe08e560 (memcpy@@GLIBC_2.14) redirected to 0x4801716 (_vgnU_ifunc_wrapper) 57 | --8669-- REDIR: 0x3cfe093f20 (__memcpy_sse2_unaligned) redirected to 0x4a0a390 (memcpy@@GLIBC_2.14) 58 | --8669-- REDIR: 0x3cfe088d90 (bcmp) redirected to 0x4801716 (_vgnU_ifunc_wrapper) 59 | --8669-- REDIR: 0x3cfe15c060 (__memcmp_sse4_1) redirected to 0x4a0b1e0 (bcmp) 60 | 61 | Usage: ./factory countryname 62 | --8669-- REDIR: 0x3cfe080330 (free) redirected to 0x4a074f0 (free) 63 | ==8669== 64 | ==8669== HEAP SUMMARY: 65 | ==8669== in use at exit: 0 bytes in 0 blocks 66 | ==8669== total heap usage: 0 allocs, 0 frees, 0 bytes allocated 67 | ==8669== 68 | ==8669== All heap blocks were freed -- no leaks are possible 69 | ==8669== 70 | ==8669== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2) 71 | --8669-- 72 | --8669-- used_suppression: 2 glibc-2.5.x-on-SUSE-10.2-(PPC)-2a /usr/lib64/valgrind/default.supp:1286 73 | ==8669== 74 | ==8669== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2) 75 | -------------------------------------------------------------------------------- /Ex01_Design_Pattern_Abstract_Factory/src/abstract_factory/valgrind.txt: -------------------------------------------------------------------------------- 1 | ==8753== Memcheck, a memory error detector 2 | ==8753== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. 3 | ==8753== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info 4 | ==8753== Command: ./factory 2 5 | ==8753== 6 | --8753-- Valgrind options: 7 | --8753-- --leak-check=full 8 | --8753-- -v 9 | --8753-- Contents of /proc/version: 10 | --8753-- Linux version 3.15.4-200.fc20.x86_64 (mockbuild@bkernel02.phx2.fedoraproject.org) (gcc version 4.8.3 20140624 (Red Hat 4.8.3-1) (GCC) ) #1 SMP Mon Jul 7 14:24:41 UTC 2014 11 | --8753-- Arch and hwcaps: AMD64, amd64-cx16-sse3 12 | --8753-- Page sizes: currently 4096, max supported 4096 13 | --8753-- Valgrind library directory: /usr/lib64/valgrind 14 | --8753-- Reading syms from /home/krishna/Development/cpp-tutorials/Ex1_Design_Pattern_Abstract_Factory/src/abstract_factory/factory 15 | --8753-- Reading syms from /usr/lib64/valgrind/memcheck-amd64-linux 16 | --8753-- object doesn't have a symbol table 17 | --8753-- object doesn't have a dynamic symbol table 18 | --8753-- Reading syms from /usr/lib64/ld-2.18.so 19 | --8753-- Scheduler: using generic scheduler lock implementation. 20 | --8753-- Reading suppressions file: /usr/lib64/valgrind/default.supp 21 | ==8753== embedded gdbserver: reading from /tmp/vgdb-pipe-from-vgdb-to-8753-by-krishna-on-kks32 22 | ==8753== embedded gdbserver: writing to /tmp/vgdb-pipe-to-vgdb-from-8753-by-krishna-on-kks32 23 | ==8753== embedded gdbserver: shared mem /tmp/vgdb-pipe-shared-mem-vgdb-8753-by-krishna-on-kks32 24 | ==8753== 25 | ==8753== TO CONTROL THIS PROCESS USING vgdb (which you probably 26 | ==8753== don't want to do, unless you know exactly what you're doing, 27 | ==8753== or are doing some strange experiment): 28 | ==8753== /usr/lib64/valgrind/../../bin/vgdb --pid=8753 ...command... 29 | ==8753== 30 | ==8753== TO DEBUG THIS PROCESS USING GDB: start GDB like this 31 | ==8753== /path/to/gdb ./factory 32 | ==8753== and then give GDB the following command 33 | ==8753== target remote | /usr/lib64/valgrind/../../bin/vgdb --pid=8753 34 | ==8753== --pid is optional if only one valgrind process is running 35 | ==8753== 36 | --8753-- REDIR: 0x3cfdc18430 (strlen) redirected to 0x38067461 (???) 37 | --8753-- Reading syms from /usr/lib64/valgrind/vgpreload_core-amd64-linux.so 38 | --8753-- object doesn't have a symbol table 39 | --8753-- Reading syms from /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so 40 | --8753-- object doesn't have a symbol table 41 | --8753-- REDIR: 0x3cfdc18380 (index) redirected to 0x4a08f60 (index) 42 | --8753-- REDIR: 0x3cfdc18400 (strcmp) redirected to 0x4a0a040 (strcmp) 43 | --8753-- Reading syms from /usr/lib64/libstdc++.so.6.0.19 44 | --8753-- object doesn't have a symbol table 45 | --8753-- Reading syms from /usr/lib64/libm-2.18.so 46 | --8753-- Reading syms from /usr/lib64/libgcc_s-4.8.3-20140624.so.1 47 | --8753-- object doesn't have a symbol table 48 | --8753-- Reading syms from /usr/lib64/libc-2.18.so 49 | --8753-- REDIR: 0x3cfe089b90 (strcasecmp) redirected to 0x4801716 (_vgnU_ifunc_wrapper) 50 | --8753-- REDIR: 0x3cfe08be80 (strncasecmp) redirected to 0x4801716 (_vgnU_ifunc_wrapper) 51 | --8753-- REDIR: 0x3cfe089360 (memcpy@GLIBC_2.2.5) redirected to 0x4801716 (_vgnU_ifunc_wrapper) 52 | --8753-- REDIR: 0x3cfe088340 (__GI_strrchr) redirected to 0x4a08d80 (__GI_strrchr) 53 | --8753-- REDIR: 0x3cfe0865f0 (strlen) redirected to 0x4a092f0 (strlen) 54 | --8753-- REDIR: 0x3cfe084b90 (strcmp) redirected to 0x4801716 (_vgnU_ifunc_wrapper) 55 | --8753-- REDIR: 0x3cfe13c200 (__strcmp_ssse3) redirected to 0x4a09fa0 (strcmp) 56 | --8753-- REDIR: 0x336585f630 (operator new(unsigned long)) redirected to 0x4a068de (operator new(unsigned long)) 57 | --8753-- REDIR: 0x3cfe08e560 (memcpy@@GLIBC_2.14) redirected to 0x4801716 (_vgnU_ifunc_wrapper) 58 | --8753-- REDIR: 0x3cfe093f20 (__memcpy_sse2_unaligned) redirected to 0x4a0a390 (memcpy@@GLIBC_2.14) 59 | --8753-- REDIR: 0x3cfe088d90 (bcmp) redirected to 0x4801716 (_vgnU_ifunc_wrapper) 60 | --8753-- REDIR: 0x3cfe15c060 (__memcmp_sse4_1) redirected to 0x4a0b1e0 (bcmp) 61 | Singapore ShippingFee $39.99 62 | Singapore Tax $19.99 63 | --8753-- REDIR: 0x3cfe080330 (free) redirected to 0x4a074f0 (free) 64 | ==8753== 65 | ==8753== HEAP SUMMARY: 66 | ==8753== in use at exit: 24 bytes in 3 blocks 67 | ==8753== total heap usage: 3 allocs, 0 frees, 24 bytes allocated 68 | ==8753== 69 | ==8753== Searching for pointers to 3 not-freed blocks 70 | ==8753== Checked 186,680 bytes 71 | ==8753== 72 | ==8753== 8 bytes in 1 blocks are definitely lost in loss record 1 of 3 73 | ==8753== at 0x4A06965: operator new(unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) 74 | ==8753== by 0x400A02: main (in /home/krishna/Development/cpp-tutorials/Ex1_Design_Pattern_Abstract_Factory/src/abstract_factory/factory) 75 | ==8753== 76 | ==8753== 8 bytes in 1 blocks are definitely lost in loss record 2 of 3 77 | ==8753== at 0x4A06965: operator new(unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) 78 | ==8753== by 0x400B84: SingaporeFinanceFactory::calculateShippingFee() (in /home/krishna/Development/cpp-tutorials/Ex1_Design_Pattern_Abstract_Factory/src/abstract_factory/factory) 79 | ==8753== by 0x400A24: main (in /home/krishna/Development/cpp-tutorials/Ex1_Design_Pattern_Abstract_Factory/src/abstract_factory/factory) 80 | ==8753== 81 | ==8753== 8 bytes in 1 blocks are definitely lost in loss record 3 of 3 82 | ==8753== at 0x4A06965: operator new(unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) 83 | ==8753== by 0x400BD6: SingaporeFinanceFactory::calculateTax() (in /home/krishna/Development/cpp-tutorials/Ex1_Design_Pattern_Abstract_Factory/src/abstract_factory/factory) 84 | ==8753== by 0x400A52: main (in /home/krishna/Development/cpp-tutorials/Ex1_Design_Pattern_Abstract_Factory/src/abstract_factory/factory) 85 | ==8753== 86 | ==8753== LEAK SUMMARY: 87 | ==8753== definitely lost: 24 bytes in 3 blocks 88 | ==8753== indirectly lost: 0 bytes in 0 blocks 89 | ==8753== possibly lost: 0 bytes in 0 blocks 90 | ==8753== still reachable: 0 bytes in 0 blocks 91 | ==8753== suppressed: 0 bytes in 0 blocks 92 | ==8753== 93 | ==8753== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 2 from 2) 94 | --8753-- 95 | --8753-- used_suppression: 2 glibc-2.5.x-on-SUSE-10.2-(PPC)-2a /usr/lib64/valgrind/default.supp:1286 96 | ==8753== 97 | ==8753== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 2 from 2) 98 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/2_factory_method_intermediate/makefile: -------------------------------------------------------------------------------- 1 | #### PROJECT SETTINGS #### 2 | # The name of the executable to be created 3 | BIN_NAME := factory 4 | # Compiler used 5 | CXX ?= g++ 6 | # Extension of source files used in the project 7 | SRC_EXT = cc 8 | # Path to the source directory, relative to the makefile 9 | SRC_PATH = . 10 | # General compiler flags 11 | COMPILE_FLAGS = -std=c++11 -Wall -Wextra -g 12 | # Additional release-specific flags 13 | RCOMPILE_FLAGS = -D NDEBUG 14 | # Additional debug-specific flags 15 | DCOMPILE_FLAGS = -D DEBUG 16 | # Add additional include paths 17 | INCLUDES = -I $(SRC_PATH)/ 18 | # General linker settings 19 | LINK_FLAGS = 20 | # Additional release-specific linker settings 21 | RLINK_FLAGS = 22 | # Additional debug-specific linker settings 23 | DLINK_FLAGS = 24 | # Destination directory, like a jail or mounted system 25 | DESTDIR = / 26 | # Install path (bin/ is appended automatically) 27 | INSTALL_PREFIX = usr/local 28 | #### END PROJECT SETTINGS #### 29 | 30 | # Generally should not need to edit below this line 31 | 32 | # Shell used in this makefile 33 | # bash is used for 'echo -en' 34 | SHELL = /bin/bash 35 | # Clear built-in rules 36 | .SUFFIXES: 37 | # Programs for installation 38 | INSTALL = install 39 | INSTALL_PROGRAM = $(INSTALL) 40 | INSTALL_DATA = $(INSTALL) -m 644 41 | 42 | # Verbose option, to output compile and link commands 43 | export V := false 44 | export CMD_PREFIX := @ 45 | ifeq ($(V),true) 46 | CMD_PREFIX := 47 | endif 48 | 49 | # Combine compiler and linker flags 50 | release: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) $(RCOMPILE_FLAGS) 51 | release: export LDFLAGS := $(LDFLAGS) $(LINK_FLAGS) $(RLINK_FLAGS) 52 | debug: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) $(DCOMPILE_FLAGS) 53 | debug: export LDFLAGS := $(LDFLAGS) $(LINK_FLAGS) $(DLINK_FLAGS) 54 | 55 | # Build and output paths 56 | release: export BUILD_PATH := build/release 57 | release: export BIN_PATH := bin/release 58 | debug: export BUILD_PATH := build/debug 59 | debug: export BIN_PATH := bin/debug 60 | install: export BIN_PATH := bin/release 61 | 62 | # Find all source files in the source directory, sorted by most 63 | # recently modified 64 | SOURCES = $(shell find $(SRC_PATH)/ -name '*.$(SRC_EXT)' -printf '%T@\t%p\n' \ 65 | | sort -k 1nr | cut -f2-) 66 | # fallback in case the above fails 67 | rwildcard = $(foreach d, $(wildcard $1*), $(call rwildcard,$d/,$2) \ 68 | $(filter $(subst *,%,$2), $d)) 69 | ifeq ($(SOURCES),) 70 | SOURCES := $(call rwildcard, $(SRC_PATH)/, *.$(SRC_EXT)) 71 | endif 72 | 73 | # Set the object file names, with the source directory stripped 74 | # from the path, and the build path prepended in its place 75 | OBJECTS = $(SOURCES:$(SRC_PATH)/%.$(SRC_EXT)=$(BUILD_PATH)/%.o) 76 | # Set the dependency files that will be used to add header dependencies 77 | DEPS = $(OBJECTS:.o=.d) 78 | 79 | # Macros for timing compilation 80 | TIME_FILE = $(dir $@).$(notdir $@)_time 81 | START_TIME = date '+%s' > $(TIME_FILE) 82 | END_TIME = read st < $(TIME_FILE) ; \ 83 | $(RM) $(TIME_FILE) ; \ 84 | st=$$((`date '+%s'` - $$st - 86400)) ; \ 85 | echo `date -u -d @$$st '+%H:%M:%S'` 86 | 87 | # Version macros 88 | # Comment/remove this section to remove versioning 89 | USE_VERSION := false 90 | # If this isn't a git repo or the repo has no tags, git describe will return non-zero 91 | ifeq ($(shell git describe > /dev/null 2>&1 ; echo $$?), 0) 92 | USE_VERSION := true 93 | VERSION := $(shell git describe --tags --long --dirty --always | \ 94 | sed 's/v\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)-\?.*-\([0-9]*\)-\(.*\)/\1 \2 \3 \4 \5/g') 95 | VERSION_MAJOR := $(word 1, $(VERSION)) 96 | VERSION_MINOR := $(word 2, $(VERSION)) 97 | VERSION_PATCH := $(word 3, $(VERSION)) 98 | VERSION_REVISION := $(word 4, $(VERSION)) 99 | VERSION_HASH := $(word 5, $(VERSION)) 100 | VERSION_STRING := \ 101 | "$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH).$(VERSION_REVISION)-$(VERSION_HASH)" 102 | override CXXFLAGS := $(CXXFLAGS) \ 103 | -D VERSION_MAJOR=$(VERSION_MAJOR) \ 104 | -D VERSION_MINOR=$(VERSION_MINOR) \ 105 | -D VERSION_PATCH=$(VERSION_PATCH) \ 106 | -D VERSION_REVISION=$(VERSION_REVISION) \ 107 | -D VERSION_HASH=\"$(VERSION_HASH)\" 108 | endif 109 | 110 | # Standard, non-optimized release build 111 | .PHONY: release 112 | release: dirs 113 | ifeq ($(USE_VERSION), true) 114 | @echo "Beginning release build v$(VERSION_STRING)" 115 | else 116 | @echo "Beginning release build" 117 | endif 118 | @$(START_TIME) 119 | @$(MAKE) all --no-print-directory 120 | @echo -n "Total build time: " 121 | @$(END_TIME) 122 | 123 | # Debug build for gdb debugging 124 | .PHONY: debug 125 | debug: dirs 126 | ifeq ($(USE_VERSION), true) 127 | @echo "Beginning debug build v$(VERSION_STRING)" 128 | else 129 | @echo "Beginning debug build" 130 | endif 131 | @$(START_TIME) 132 | @$(MAKE) all --no-print-directory 133 | @echo -n "Total build time: " 134 | @$(END_TIME) 135 | 136 | # Create the directories used in the build 137 | .PHONY: dirs 138 | dirs: 139 | @echo "Creating directories" 140 | @mkdir -p $(dir $(OBJECTS)) 141 | @mkdir -p $(BIN_PATH) 142 | 143 | # Installs to the set path 144 | .PHONY: install 145 | install: 146 | @echo "Installing to $(DESTDIR)$(INSTALL_PREFIX)/bin" 147 | @$(INSTALL_PROGRAM) $(BIN_PATH)/$(BIN_NAME) $(DESTDIR)$(INSTALL_PREFIX)/bin 148 | 149 | # Uninstalls the program 150 | .PHONY: uninstall 151 | uninstall: 152 | @echo "Removing $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BIN_NAME)" 153 | @$(RM) $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BIN_NAME) 154 | 155 | # Removes all build files 156 | .PHONY: clean 157 | clean: 158 | @echo "Deleting $(BIN_NAME) symlink" 159 | @$(RM) $(BIN_NAME) 160 | @echo "Deleting directories" 161 | @$(RM) -r build 162 | @$(RM) -r bin 163 | 164 | # Main rule, checks the executable and symlinks to the output 165 | all: $(BIN_PATH)/$(BIN_NAME) 166 | @echo "Making symlink: $(BIN_NAME) -> $<" 167 | @$(RM) $(BIN_NAME) 168 | @ln -s $(BIN_PATH)/$(BIN_NAME) $(BIN_NAME) 169 | 170 | # Link the executable 171 | $(BIN_PATH)/$(BIN_NAME): $(OBJECTS) 172 | @echo "Linking: $@" 173 | @$(START_TIME) 174 | $(CMD_PREFIX)$(CXX) $(OBJECTS) $(LDFLAGS) -o $@ 175 | @echo -en "\t Link time: " 176 | @$(END_TIME) 177 | 178 | # Add dependency files, if they exist 179 | -include $(DEPS) 180 | 181 | # Source file rules 182 | # After the first compilation they will be joined with the rules from the 183 | # dependency files to provide header dependencies 184 | $(BUILD_PATH)/%.o: $(SRC_PATH)/%.$(SRC_EXT) 185 | @echo "Compiling: $< -> $@" 186 | @$(START_TIME) 187 | $(CMD_PREFIX)$(CXX) $(CXXFLAGS) $(INCLUDES) -MP -MMD -c $< -o $@ 188 | @echo -en "\t Compile time: " 189 | @$(END_TIME) 190 | 191 | 192 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/3_factory_method_advanced/makefile: -------------------------------------------------------------------------------- 1 | #### PROJECT SETTINGS #### 2 | # The name of the executable to be created 3 | BIN_NAME := factory-advanced 4 | # Compiler used 5 | CXX ?= g++ 6 | # Extension of source files used in the project 7 | SRC_EXT = cc 8 | # Path to the source directory, relative to the makefile 9 | SRC_PATH = . 10 | # General compiler flags 11 | COMPILE_FLAGS = -std=c++11 -Wall -Wextra -g 12 | # Additional release-specific flags 13 | RCOMPILE_FLAGS = -D NDEBUG 14 | # Additional debug-specific flags 15 | DCOMPILE_FLAGS = -D DEBUG 16 | # Add additional include paths 17 | INCLUDES = -I $(SRC_PATH)/ 18 | # General linker settings 19 | LINK_FLAGS = 20 | # Additional release-specific linker settings 21 | RLINK_FLAGS = 22 | # Additional debug-specific linker settings 23 | DLINK_FLAGS = 24 | # Destination directory, like a jail or mounted system 25 | DESTDIR = / 26 | # Install path (bin/ is appended automatically) 27 | INSTALL_PREFIX = usr/local 28 | #### END PROJECT SETTINGS #### 29 | 30 | # Generally should not need to edit below this line 31 | 32 | # Shell used in this makefile 33 | # bash is used for 'echo -en' 34 | SHELL = /bin/bash 35 | # Clear built-in rules 36 | .SUFFIXES: 37 | # Programs for installation 38 | INSTALL = install 39 | INSTALL_PROGRAM = $(INSTALL) 40 | INSTALL_DATA = $(INSTALL) -m 644 41 | 42 | # Verbose option, to output compile and link commands 43 | export V := false 44 | export CMD_PREFIX := @ 45 | ifeq ($(V),true) 46 | CMD_PREFIX := 47 | endif 48 | 49 | # Combine compiler and linker flags 50 | release: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) $(RCOMPILE_FLAGS) 51 | release: export LDFLAGS := $(LDFLAGS) $(LINK_FLAGS) $(RLINK_FLAGS) 52 | debug: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) $(DCOMPILE_FLAGS) 53 | debug: export LDFLAGS := $(LDFLAGS) $(LINK_FLAGS) $(DLINK_FLAGS) 54 | 55 | # Build and output paths 56 | release: export BUILD_PATH := build/release 57 | release: export BIN_PATH := bin/release 58 | debug: export BUILD_PATH := build/debug 59 | debug: export BIN_PATH := bin/debug 60 | install: export BIN_PATH := bin/release 61 | 62 | # Find all source files in the source directory, sorted by most 63 | # recently modified 64 | SOURCES = $(shell find $(SRC_PATH)/ -name '*.$(SRC_EXT)' -printf '%T@\t%p\n' \ 65 | | sort -k 1nr | cut -f2-) 66 | # fallback in case the above fails 67 | rwildcard = $(foreach d, $(wildcard $1*), $(call rwildcard,$d/,$2) \ 68 | $(filter $(subst *,%,$2), $d)) 69 | ifeq ($(SOURCES),) 70 | SOURCES := $(call rwildcard, $(SRC_PATH)/, *.$(SRC_EXT)) 71 | endif 72 | 73 | # Set the object file names, with the source directory stripped 74 | # from the path, and the build path prepended in its place 75 | OBJECTS = $(SOURCES:$(SRC_PATH)/%.$(SRC_EXT)=$(BUILD_PATH)/%.o) 76 | # Set the dependency files that will be used to add header dependencies 77 | DEPS = $(OBJECTS:.o=.d) 78 | 79 | # Macros for timing compilation 80 | TIME_FILE = $(dir $@).$(notdir $@)_time 81 | START_TIME = date '+%s' > $(TIME_FILE) 82 | END_TIME = read st < $(TIME_FILE) ; \ 83 | $(RM) $(TIME_FILE) ; \ 84 | st=$$((`date '+%s'` - $$st - 86400)) ; \ 85 | echo `date -u -d @$$st '+%H:%M:%S'` 86 | 87 | # Version macros 88 | # Comment/remove this section to remove versioning 89 | USE_VERSION := false 90 | # If this isn't a git repo or the repo has no tags, git describe will return non-zero 91 | ifeq ($(shell git describe > /dev/null 2>&1 ; echo $$?), 0) 92 | USE_VERSION := true 93 | VERSION := $(shell git describe --tags --long --dirty --always | \ 94 | sed 's/v\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)-\?.*-\([0-9]*\)-\(.*\)/\1 \2 \3 \4 \5/g') 95 | VERSION_MAJOR := $(word 1, $(VERSION)) 96 | VERSION_MINOR := $(word 2, $(VERSION)) 97 | VERSION_PATCH := $(word 3, $(VERSION)) 98 | VERSION_REVISION := $(word 4, $(VERSION)) 99 | VERSION_HASH := $(word 5, $(VERSION)) 100 | VERSION_STRING := \ 101 | "$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH).$(VERSION_REVISION)-$(VERSION_HASH)" 102 | override CXXFLAGS := $(CXXFLAGS) \ 103 | -D VERSION_MAJOR=$(VERSION_MAJOR) \ 104 | -D VERSION_MINOR=$(VERSION_MINOR) \ 105 | -D VERSION_PATCH=$(VERSION_PATCH) \ 106 | -D VERSION_REVISION=$(VERSION_REVISION) \ 107 | -D VERSION_HASH=\"$(VERSION_HASH)\" 108 | endif 109 | 110 | # Standard, non-optimized release build 111 | .PHONY: release 112 | release: dirs 113 | ifeq ($(USE_VERSION), true) 114 | @echo "Beginning release build v$(VERSION_STRING)" 115 | else 116 | @echo "Beginning release build" 117 | endif 118 | @$(START_TIME) 119 | @$(MAKE) all --no-print-directory 120 | @echo -n "Total build time: " 121 | @$(END_TIME) 122 | 123 | # Debug build for gdb debugging 124 | .PHONY: debug 125 | debug: dirs 126 | ifeq ($(USE_VERSION), true) 127 | @echo "Beginning debug build v$(VERSION_STRING)" 128 | else 129 | @echo "Beginning debug build" 130 | endif 131 | @$(START_TIME) 132 | @$(MAKE) all --no-print-directory 133 | @echo -n "Total build time: " 134 | @$(END_TIME) 135 | 136 | # Create the directories used in the build 137 | .PHONY: dirs 138 | dirs: 139 | @echo "Creating directories" 140 | @mkdir -p $(dir $(OBJECTS)) 141 | @mkdir -p $(BIN_PATH) 142 | 143 | # Installs to the set path 144 | .PHONY: install 145 | install: 146 | @echo "Installing to $(DESTDIR)$(INSTALL_PREFIX)/bin" 147 | @$(INSTALL_PROGRAM) $(BIN_PATH)/$(BIN_NAME) $(DESTDIR)$(INSTALL_PREFIX)/bin 148 | 149 | # Uninstalls the program 150 | .PHONY: uninstall 151 | uninstall: 152 | @echo "Removing $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BIN_NAME)" 153 | @$(RM) $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BIN_NAME) 154 | 155 | # Removes all build files 156 | .PHONY: clean 157 | clean: 158 | @echo "Deleting $(BIN_NAME) symlink" 159 | @$(RM) $(BIN_NAME) 160 | @echo "Deleting directories" 161 | @$(RM) -r build 162 | @$(RM) -r bin 163 | 164 | # Main rule, checks the executable and symlinks to the output 165 | all: $(BIN_PATH)/$(BIN_NAME) 166 | @echo "Making symlink: $(BIN_NAME) -> $<" 167 | @$(RM) $(BIN_NAME) 168 | @ln -s $(BIN_PATH)/$(BIN_NAME) $(BIN_NAME) 169 | 170 | # Link the executable 171 | $(BIN_PATH)/$(BIN_NAME): $(OBJECTS) 172 | @echo "Linking: $@" 173 | @$(START_TIME) 174 | $(CMD_PREFIX)$(CXX) $(OBJECTS) $(LDFLAGS) -o $@ 175 | @echo -en "\t Link time: " 176 | @$(END_TIME) 177 | 178 | # Add dependency files, if they exist 179 | -include $(DEPS) 180 | 181 | # Source file rules 182 | # After the first compilation they will be joined with the rules from the 183 | # dependency files to provide header dependencies 184 | $(BUILD_PATH)/%.o: $(SRC_PATH)/%.$(SRC_EXT) 185 | @echo "Compiling: $< -> $@" 186 | @$(START_TIME) 187 | $(CMD_PREFIX)$(CXX) $(CXXFLAGS) $(INCLUDES) -MP -MMD -c $< -o $@ 188 | @echo -en "\t Compile time: " 189 | @$(END_TIME) 190 | 191 | 192 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/5_generic_factory_method/makefile: -------------------------------------------------------------------------------- 1 | #### PROJECT SETTINGS #### 2 | # The name of the executable to be created 3 | BIN_NAME := factory-generic 4 | # Compiler used 5 | CXX ?= g++ 6 | # Extension of source files used in the project 7 | SRC_EXT = cc 8 | # Path to the source directory, relative to the makefile 9 | SRC_PATH = . 10 | # General compiler flags 11 | COMPILE_FLAGS = -std=c++11 -Wall -Wextra -g 12 | # Additional release-specific flags 13 | RCOMPILE_FLAGS = -D NDEBUG 14 | # Additional debug-specific flags 15 | DCOMPILE_FLAGS = -D DEBUG 16 | # Add additional include paths 17 | INCLUDES = -I $(SRC_PATH)/ 18 | # General linker settings 19 | LINK_FLAGS = 20 | # Additional release-specific linker settings 21 | RLINK_FLAGS = 22 | # Additional debug-specific linker settings 23 | DLINK_FLAGS = 24 | # Destination directory, like a jail or mounted system 25 | DESTDIR = / 26 | # Install path (bin/ is appended automatically) 27 | INSTALL_PREFIX = usr/local 28 | #### END PROJECT SETTINGS #### 29 | 30 | # Generally should not need to edit below this line 31 | 32 | # Shell used in this makefile 33 | # bash is used for 'echo -en' 34 | SHELL = /bin/bash 35 | # Clear built-in rules 36 | .SUFFIXES: 37 | # Programs for installation 38 | INSTALL = install 39 | INSTALL_PROGRAM = $(INSTALL) 40 | INSTALL_DATA = $(INSTALL) -m 644 41 | 42 | # Verbose option, to output compile and link commands 43 | export V := false 44 | export CMD_PREFIX := @ 45 | ifeq ($(V),true) 46 | CMD_PREFIX := 47 | endif 48 | 49 | # Combine compiler and linker flags 50 | release: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) $(RCOMPILE_FLAGS) 51 | release: export LDFLAGS := $(LDFLAGS) $(LINK_FLAGS) $(RLINK_FLAGS) 52 | debug: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) $(DCOMPILE_FLAGS) 53 | debug: export LDFLAGS := $(LDFLAGS) $(LINK_FLAGS) $(DLINK_FLAGS) 54 | 55 | # Build and output paths 56 | release: export BUILD_PATH := build/release 57 | release: export BIN_PATH := bin/release 58 | debug: export BUILD_PATH := build/debug 59 | debug: export BIN_PATH := bin/debug 60 | install: export BIN_PATH := bin/release 61 | 62 | # Find all source files in the source directory, sorted by most 63 | # recently modified 64 | SOURCES = $(shell find $(SRC_PATH)/ -name '*.$(SRC_EXT)' -printf '%T@\t%p\n' \ 65 | | sort -k 1nr | cut -f2-) 66 | # fallback in case the above fails 67 | rwildcard = $(foreach d, $(wildcard $1*), $(call rwildcard,$d/,$2) \ 68 | $(filter $(subst *,%,$2), $d)) 69 | ifeq ($(SOURCES),) 70 | SOURCES := $(call rwildcard, $(SRC_PATH)/, *.$(SRC_EXT)) 71 | endif 72 | 73 | # Set the object file names, with the source directory stripped 74 | # from the path, and the build path prepended in its place 75 | OBJECTS = $(SOURCES:$(SRC_PATH)/%.$(SRC_EXT)=$(BUILD_PATH)/%.o) 76 | # Set the dependency files that will be used to add header dependencies 77 | DEPS = $(OBJECTS:.o=.d) 78 | 79 | # Macros for timing compilation 80 | TIME_FILE = $(dir $@).$(notdir $@)_time 81 | START_TIME = date '+%s' > $(TIME_FILE) 82 | END_TIME = read st < $(TIME_FILE) ; \ 83 | $(RM) $(TIME_FILE) ; \ 84 | st=$$((`date '+%s'` - $$st - 86400)) ; \ 85 | echo `date -u -d @$$st '+%H:%M:%S'` 86 | 87 | # Version macros 88 | # Comment/remove this section to remove versioning 89 | USE_VERSION := false 90 | # If this isn't a git repo or the repo has no tags, git describe will return non-zero 91 | ifeq ($(shell git describe > /dev/null 2>&1 ; echo $$?), 0) 92 | USE_VERSION := true 93 | VERSION := $(shell git describe --tags --long --dirty --always | \ 94 | sed 's/v\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)-\?.*-\([0-9]*\)-\(.*\)/\1 \2 \3 \4 \5/g') 95 | VERSION_MAJOR := $(word 1, $(VERSION)) 96 | VERSION_MINOR := $(word 2, $(VERSION)) 97 | VERSION_PATCH := $(word 3, $(VERSION)) 98 | VERSION_REVISION := $(word 4, $(VERSION)) 99 | VERSION_HASH := $(word 5, $(VERSION)) 100 | VERSION_STRING := \ 101 | "$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH).$(VERSION_REVISION)-$(VERSION_HASH)" 102 | override CXXFLAGS := $(CXXFLAGS) \ 103 | -D VERSION_MAJOR=$(VERSION_MAJOR) \ 104 | -D VERSION_MINOR=$(VERSION_MINOR) \ 105 | -D VERSION_PATCH=$(VERSION_PATCH) \ 106 | -D VERSION_REVISION=$(VERSION_REVISION) \ 107 | -D VERSION_HASH=\"$(VERSION_HASH)\" 108 | endif 109 | 110 | # Standard, non-optimized release build 111 | .PHONY: release 112 | release: dirs 113 | ifeq ($(USE_VERSION), true) 114 | @echo "Beginning release build v$(VERSION_STRING)" 115 | else 116 | @echo "Beginning release build" 117 | endif 118 | @$(START_TIME) 119 | @$(MAKE) all --no-print-directory 120 | @echo -n "Total build time: " 121 | @$(END_TIME) 122 | 123 | # Debug build for gdb debugging 124 | .PHONY: debug 125 | debug: dirs 126 | ifeq ($(USE_VERSION), true) 127 | @echo "Beginning debug build v$(VERSION_STRING)" 128 | else 129 | @echo "Beginning debug build" 130 | endif 131 | @$(START_TIME) 132 | @$(MAKE) all --no-print-directory 133 | @echo -n "Total build time: " 134 | @$(END_TIME) 135 | 136 | # Create the directories used in the build 137 | .PHONY: dirs 138 | dirs: 139 | @echo "Creating directories" 140 | @mkdir -p $(dir $(OBJECTS)) 141 | @mkdir -p $(BIN_PATH) 142 | 143 | # Installs to the set path 144 | .PHONY: install 145 | install: 146 | @echo "Installing to $(DESTDIR)$(INSTALL_PREFIX)/bin" 147 | @$(INSTALL_PROGRAM) $(BIN_PATH)/$(BIN_NAME) $(DESTDIR)$(INSTALL_PREFIX)/bin 148 | 149 | # Uninstalls the program 150 | .PHONY: uninstall 151 | uninstall: 152 | @echo "Removing $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BIN_NAME)" 153 | @$(RM) $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BIN_NAME) 154 | 155 | # Removes all build files 156 | .PHONY: clean 157 | clean: 158 | @echo "Deleting $(BIN_NAME) symlink" 159 | @$(RM) $(BIN_NAME) 160 | @echo "Deleting directories" 161 | @$(RM) -r build 162 | @$(RM) -r bin 163 | 164 | # Main rule, checks the executable and symlinks to the output 165 | all: $(BIN_PATH)/$(BIN_NAME) 166 | @echo "Making symlink: $(BIN_NAME) -> $<" 167 | @$(RM) $(BIN_NAME) 168 | @ln -s $(BIN_PATH)/$(BIN_NAME) $(BIN_NAME) 169 | 170 | # Link the executable 171 | $(BIN_PATH)/$(BIN_NAME): $(OBJECTS) 172 | @echo "Linking: $@" 173 | @$(START_TIME) 174 | $(CMD_PREFIX)$(CXX) $(OBJECTS) $(LDFLAGS) -o $@ 175 | @echo -en "\t Link time: " 176 | @$(END_TIME) 177 | 178 | # Add dependency files, if they exist 179 | -include $(DEPS) 180 | 181 | # Source file rules 182 | # After the first compilation they will be joined with the rules from the 183 | # dependency files to provide header dependencies 184 | $(BUILD_PATH)/%.o: $(SRC_PATH)/%.$(SRC_EXT) 185 | @echo "Compiling: $< -> $@" 186 | @$(START_TIME) 187 | $(CMD_PREFIX)$(CXX) $(CXXFLAGS) $(INCLUDES) -MP -MMD -c $< -o $@ 188 | @echo -en "\t Compile time: " 189 | @$(END_TIME) 190 | 191 | 192 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/6_generic_factory_constructor/makefile: -------------------------------------------------------------------------------- 1 | #### PROJECT SETTINGS #### 2 | # The name of the executable to be created 3 | BIN_NAME := factory-generic 4 | # Compiler used 5 | CXX ?= g++ 6 | # Extension of source files used in the project 7 | SRC_EXT = cc 8 | # Path to the source directory, relative to the makefile 9 | SRC_PATH = . 10 | # General compiler flags 11 | COMPILE_FLAGS = -std=c++11 -Wall -Wextra -g 12 | # Additional release-specific flags 13 | RCOMPILE_FLAGS = -D NDEBUG 14 | # Additional debug-specific flags 15 | DCOMPILE_FLAGS = -D DEBUG 16 | # Add additional include paths 17 | INCLUDES = -I $(SRC_PATH)/ 18 | # General linker settings 19 | LINK_FLAGS = 20 | # Additional release-specific linker settings 21 | RLINK_FLAGS = 22 | # Additional debug-specific linker settings 23 | DLINK_FLAGS = 24 | # Destination directory, like a jail or mounted system 25 | DESTDIR = / 26 | # Install path (bin/ is appended automatically) 27 | INSTALL_PREFIX = usr/local 28 | #### END PROJECT SETTINGS #### 29 | 30 | # Generally should not need to edit below this line 31 | 32 | # Shell used in this makefile 33 | # bash is used for 'echo -en' 34 | SHELL = /bin/bash 35 | # Clear built-in rules 36 | .SUFFIXES: 37 | # Programs for installation 38 | INSTALL = install 39 | INSTALL_PROGRAM = $(INSTALL) 40 | INSTALL_DATA = $(INSTALL) -m 644 41 | 42 | # Verbose option, to output compile and link commands 43 | export V := false 44 | export CMD_PREFIX := @ 45 | ifeq ($(V),true) 46 | CMD_PREFIX := 47 | endif 48 | 49 | # Combine compiler and linker flags 50 | release: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) $(RCOMPILE_FLAGS) 51 | release: export LDFLAGS := $(LDFLAGS) $(LINK_FLAGS) $(RLINK_FLAGS) 52 | debug: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) $(DCOMPILE_FLAGS) 53 | debug: export LDFLAGS := $(LDFLAGS) $(LINK_FLAGS) $(DLINK_FLAGS) 54 | 55 | # Build and output paths 56 | release: export BUILD_PATH := build/release 57 | release: export BIN_PATH := bin/release 58 | debug: export BUILD_PATH := build/debug 59 | debug: export BIN_PATH := bin/debug 60 | install: export BIN_PATH := bin/release 61 | 62 | # Find all source files in the source directory, sorted by most 63 | # recently modified 64 | SOURCES = $(shell find $(SRC_PATH)/ -name '*.$(SRC_EXT)' -printf '%T@\t%p\n' \ 65 | | sort -k 1nr | cut -f2-) 66 | # fallback in case the above fails 67 | rwildcard = $(foreach d, $(wildcard $1*), $(call rwildcard,$d/,$2) \ 68 | $(filter $(subst *,%,$2), $d)) 69 | ifeq ($(SOURCES),) 70 | SOURCES := $(call rwildcard, $(SRC_PATH)/, *.$(SRC_EXT)) 71 | endif 72 | 73 | # Set the object file names, with the source directory stripped 74 | # from the path, and the build path prepended in its place 75 | OBJECTS = $(SOURCES:$(SRC_PATH)/%.$(SRC_EXT)=$(BUILD_PATH)/%.o) 76 | # Set the dependency files that will be used to add header dependencies 77 | DEPS = $(OBJECTS:.o=.d) 78 | 79 | # Macros for timing compilation 80 | TIME_FILE = $(dir $@).$(notdir $@)_time 81 | START_TIME = date '+%s' > $(TIME_FILE) 82 | END_TIME = read st < $(TIME_FILE) ; \ 83 | $(RM) $(TIME_FILE) ; \ 84 | st=$$((`date '+%s'` - $$st - 86400)) ; \ 85 | echo `date -u -d @$$st '+%H:%M:%S'` 86 | 87 | # Version macros 88 | # Comment/remove this section to remove versioning 89 | USE_VERSION := false 90 | # If this isn't a git repo or the repo has no tags, git describe will return non-zero 91 | ifeq ($(shell git describe > /dev/null 2>&1 ; echo $$?), 0) 92 | USE_VERSION := true 93 | VERSION := $(shell git describe --tags --long --dirty --always | \ 94 | sed 's/v\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)-\?.*-\([0-9]*\)-\(.*\)/\1 \2 \3 \4 \5/g') 95 | VERSION_MAJOR := $(word 1, $(VERSION)) 96 | VERSION_MINOR := $(word 2, $(VERSION)) 97 | VERSION_PATCH := $(word 3, $(VERSION)) 98 | VERSION_REVISION := $(word 4, $(VERSION)) 99 | VERSION_HASH := $(word 5, $(VERSION)) 100 | VERSION_STRING := \ 101 | "$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH).$(VERSION_REVISION)-$(VERSION_HASH)" 102 | override CXXFLAGS := $(CXXFLAGS) \ 103 | -D VERSION_MAJOR=$(VERSION_MAJOR) \ 104 | -D VERSION_MINOR=$(VERSION_MINOR) \ 105 | -D VERSION_PATCH=$(VERSION_PATCH) \ 106 | -D VERSION_REVISION=$(VERSION_REVISION) \ 107 | -D VERSION_HASH=\"$(VERSION_HASH)\" 108 | endif 109 | 110 | # Standard, non-optimized release build 111 | .PHONY: release 112 | release: dirs 113 | ifeq ($(USE_VERSION), true) 114 | @echo "Beginning release build v$(VERSION_STRING)" 115 | else 116 | @echo "Beginning release build" 117 | endif 118 | @$(START_TIME) 119 | @$(MAKE) all --no-print-directory 120 | @echo -n "Total build time: " 121 | @$(END_TIME) 122 | 123 | # Debug build for gdb debugging 124 | .PHONY: debug 125 | debug: dirs 126 | ifeq ($(USE_VERSION), true) 127 | @echo "Beginning debug build v$(VERSION_STRING)" 128 | else 129 | @echo "Beginning debug build" 130 | endif 131 | @$(START_TIME) 132 | @$(MAKE) all --no-print-directory 133 | @echo -n "Total build time: " 134 | @$(END_TIME) 135 | 136 | # Create the directories used in the build 137 | .PHONY: dirs 138 | dirs: 139 | @echo "Creating directories" 140 | @mkdir -p $(dir $(OBJECTS)) 141 | @mkdir -p $(BIN_PATH) 142 | 143 | # Installs to the set path 144 | .PHONY: install 145 | install: 146 | @echo "Installing to $(DESTDIR)$(INSTALL_PREFIX)/bin" 147 | @$(INSTALL_PROGRAM) $(BIN_PATH)/$(BIN_NAME) $(DESTDIR)$(INSTALL_PREFIX)/bin 148 | 149 | # Uninstalls the program 150 | .PHONY: uninstall 151 | uninstall: 152 | @echo "Removing $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BIN_NAME)" 153 | @$(RM) $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BIN_NAME) 154 | 155 | # Removes all build files 156 | .PHONY: clean 157 | clean: 158 | @echo "Deleting $(BIN_NAME) symlink" 159 | @$(RM) $(BIN_NAME) 160 | @echo "Deleting directories" 161 | @$(RM) -r build 162 | @$(RM) -r bin 163 | 164 | # Main rule, checks the executable and symlinks to the output 165 | all: $(BIN_PATH)/$(BIN_NAME) 166 | @echo "Making symlink: $(BIN_NAME) -> $<" 167 | @$(RM) $(BIN_NAME) 168 | @ln -s $(BIN_PATH)/$(BIN_NAME) $(BIN_NAME) 169 | 170 | # Link the executable 171 | $(BIN_PATH)/$(BIN_NAME): $(OBJECTS) 172 | @echo "Linking: $@" 173 | @$(START_TIME) 174 | $(CMD_PREFIX)$(CXX) $(OBJECTS) $(LDFLAGS) -o $@ 175 | @echo -en "\t Link time: " 176 | @$(END_TIME) 177 | 178 | # Add dependency files, if they exist 179 | -include $(DEPS) 180 | 181 | # Source file rules 182 | # After the first compilation they will be joined with the rules from the 183 | # dependency files to provide header dependencies 184 | $(BUILD_PATH)/%.o: $(SRC_PATH)/%.$(SRC_EXT) 185 | @echo "Compiling: $< -> $@" 186 | @$(START_TIME) 187 | $(CMD_PREFIX)$(CXX) $(CXXFLAGS) $(INCLUDES) -MP -MMD -c $< -o $@ 188 | @echo -en "\t Compile time: " 189 | @$(END_TIME) 190 | 191 | 192 | -------------------------------------------------------------------------------- /Ex02_Factory_Method/src/4_factory_method_advanced_template/makefile: -------------------------------------------------------------------------------- 1 | #### PROJECT SETTINGS #### 2 | # The name of the executable to be created 3 | BIN_NAME := factory-advanced 4 | # Compiler used 5 | CXX ?= g++ 6 | # Extension of source files used in the project 7 | SRC_EXT = cc 8 | # Path to the source directory, relative to the makefile 9 | SRC_PATH = . 10 | # General compiler flags 11 | COMPILE_FLAGS = -std=c++11 -Wall -Wextra -g 12 | # Additional release-specific flags 13 | RCOMPILE_FLAGS = -D NDEBUG 14 | # Additional debug-specific flags 15 | DCOMPILE_FLAGS = -D DEBUG 16 | # Add additional include paths 17 | INCLUDES = -I $(SRC_PATH)/ 18 | # General linker settings 19 | LINK_FLAGS = 20 | # Additional release-specific linker settings 21 | RLINK_FLAGS = 22 | # Additional debug-specific linker settings 23 | DLINK_FLAGS = 24 | # Destination directory, like a jail or mounted system 25 | DESTDIR = / 26 | # Install path (bin/ is appended automatically) 27 | INSTALL_PREFIX = usr/local 28 | #### END PROJECT SETTINGS #### 29 | 30 | # Generally should not need to edit below this line 31 | 32 | # Shell used in this makefile 33 | # bash is used for 'echo -en' 34 | SHELL = /bin/bash 35 | # Clear built-in rules 36 | .SUFFIXES: 37 | # Programs for installation 38 | INSTALL = install 39 | INSTALL_PROGRAM = $(INSTALL) 40 | INSTALL_DATA = $(INSTALL) -m 644 41 | 42 | # Verbose option, to output compile and link commands 43 | export V := false 44 | export CMD_PREFIX := @ 45 | ifeq ($(V),true) 46 | CMD_PREFIX := 47 | endif 48 | 49 | # Combine compiler and linker flags 50 | release: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) $(RCOMPILE_FLAGS) 51 | release: export LDFLAGS := $(LDFLAGS) $(LINK_FLAGS) $(RLINK_FLAGS) 52 | debug: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) $(DCOMPILE_FLAGS) 53 | debug: export LDFLAGS := $(LDFLAGS) $(LINK_FLAGS) $(DLINK_FLAGS) 54 | 55 | # Build and output paths 56 | release: export BUILD_PATH := build/release 57 | release: export BIN_PATH := bin/release 58 | debug: export BUILD_PATH := build/debug 59 | debug: export BIN_PATH := bin/debug 60 | install: export BIN_PATH := bin/release 61 | 62 | # Find all source files in the source directory, sorted by most 63 | # recently modified 64 | SOURCES = $(shell find $(SRC_PATH)/ -name '*.$(SRC_EXT)' -printf '%T@\t%p\n' \ 65 | | sort -k 1nr | cut -f2-) 66 | # fallback in case the above fails 67 | rwildcard = $(foreach d, $(wildcard $1*), $(call rwildcard,$d/,$2) \ 68 | $(filter $(subst *,%,$2), $d)) 69 | ifeq ($(SOURCES),) 70 | SOURCES := $(call rwildcard, $(SRC_PATH)/, *.$(SRC_EXT)) 71 | endif 72 | 73 | # Set the object file names, with the source directory stripped 74 | # from the path, and the build path prepended in its place 75 | OBJECTS = $(SOURCES:$(SRC_PATH)/%.$(SRC_EXT)=$(BUILD_PATH)/%.o) 76 | # Set the dependency files that will be used to add header dependencies 77 | DEPS = $(OBJECTS:.o=.d) 78 | 79 | # Macros for timing compilation 80 | TIME_FILE = $(dir $@).$(notdir $@)_time 81 | START_TIME = date '+%s' > $(TIME_FILE) 82 | END_TIME = read st < $(TIME_FILE) ; \ 83 | $(RM) $(TIME_FILE) ; \ 84 | st=$$((`date '+%s'` - $$st - 86400)) ; \ 85 | echo `date -u -d @$$st '+%H:%M:%S'` 86 | 87 | # Version macros 88 | # Comment/remove this section to remove versioning 89 | USE_VERSION := false 90 | # If this isn't a git repo or the repo has no tags, git describe will return non-zero 91 | ifeq ($(shell git describe > /dev/null 2>&1 ; echo $$?), 0) 92 | USE_VERSION := true 93 | VERSION := $(shell git describe --tags --long --dirty --always | \ 94 | sed 's/v\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)-\?.*-\([0-9]*\)-\(.*\)/\1 \2 \3 \4 \5/g') 95 | VERSION_MAJOR := $(word 1, $(VERSION)) 96 | VERSION_MINOR := $(word 2, $(VERSION)) 97 | VERSION_PATCH := $(word 3, $(VERSION)) 98 | VERSION_REVISION := $(word 4, $(VERSION)) 99 | VERSION_HASH := $(word 5, $(VERSION)) 100 | VERSION_STRING := \ 101 | "$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH).$(VERSION_REVISION)-$(VERSION_HASH)" 102 | override CXXFLAGS := $(CXXFLAGS) \ 103 | -D VERSION_MAJOR=$(VERSION_MAJOR) \ 104 | -D VERSION_MINOR=$(VERSION_MINOR) \ 105 | -D VERSION_PATCH=$(VERSION_PATCH) \ 106 | -D VERSION_REVISION=$(VERSION_REVISION) \ 107 | -D VERSION_HASH=\"$(VERSION_HASH)\" 108 | endif 109 | 110 | # Standard, non-optimized release build 111 | .PHONY: release 112 | release: dirs 113 | ifeq ($(USE_VERSION), true) 114 | @echo "Beginning release build v$(VERSION_STRING)" 115 | else 116 | @echo "Beginning release build" 117 | endif 118 | @$(START_TIME) 119 | @$(MAKE) all --no-print-directory 120 | @echo -n "Total build time: " 121 | @$(END_TIME) 122 | 123 | # Debug build for gdb debugging 124 | .PHONY: debug 125 | debug: dirs 126 | ifeq ($(USE_VERSION), true) 127 | @echo "Beginning debug build v$(VERSION_STRING)" 128 | else 129 | @echo "Beginning debug build" 130 | endif 131 | @$(START_TIME) 132 | @$(MAKE) all --no-print-directory 133 | @echo -n "Total build time: " 134 | @$(END_TIME) 135 | 136 | # Create the directories used in the build 137 | .PHONY: dirs 138 | dirs: 139 | @echo "Creating directories" 140 | @mkdir -p $(dir $(OBJECTS)) 141 | @mkdir -p $(BIN_PATH) 142 | 143 | # Installs to the set path 144 | .PHONY: install 145 | install: 146 | @echo "Installing to $(DESTDIR)$(INSTALL_PREFIX)/bin" 147 | @$(INSTALL_PROGRAM) $(BIN_PATH)/$(BIN_NAME) $(DESTDIR)$(INSTALL_PREFIX)/bin 148 | 149 | # Uninstalls the program 150 | .PHONY: uninstall 151 | uninstall: 152 | @echo "Removing $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BIN_NAME)" 153 | @$(RM) $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BIN_NAME) 154 | 155 | # Removes all build files 156 | .PHONY: clean 157 | clean: 158 | @echo "Deleting $(BIN_NAME) symlink" 159 | @$(RM) $(BIN_NAME) 160 | @echo "Deleting directories" 161 | @$(RM) -r build 162 | @$(RM) -r bin 163 | 164 | # Main rule, checks the executable and symlinks to the output 165 | all: $(BIN_PATH)/$(BIN_NAME) 166 | @echo "Making symlink: $(BIN_NAME) -> $<" 167 | @$(RM) $(BIN_NAME) 168 | @ln -s $(BIN_PATH)/$(BIN_NAME) $(BIN_NAME) 169 | 170 | # Link the executable 171 | $(BIN_PATH)/$(BIN_NAME): $(OBJECTS) 172 | @echo "Linking: $@" 173 | @$(START_TIME) 174 | $(CMD_PREFIX)$(CXX) $(OBJECTS) $(LDFLAGS) -o $@ 175 | @echo -en "\t Link time: " 176 | @$(END_TIME) 177 | 178 | # Add dependency files, if they exist 179 | -include $(DEPS) 180 | 181 | # Source file rules 182 | # After the first compilation they will be joined with the rules from the 183 | # dependency files to provide header dependencies 184 | $(BUILD_PATH)/%.o: $(SRC_PATH)/%.$(SRC_EXT) 185 | @echo "Compiling: $< -> $@" 186 | @$(START_TIME) 187 | $(CMD_PREFIX)$(CXX) $(CXXFLAGS) $(INCLUDES) -MP -MMD -c $< -o $@ 188 | @echo -en "\t Compile time: " 189 | @$(END_TIME) 190 | 191 | 192 | -------------------------------------------------------------------------------- /Ex05_Prototype/src/3_document_manager_prototype_factory_template/makefile: -------------------------------------------------------------------------------- 1 | #### PROJECT SETTINGS #### 2 | # The name of the executable to be created 3 | BIN_NAME := document-generator 4 | # Compiler used 5 | CXX ?= g++ 6 | # Extension of source files used in the project 7 | SRC_EXT = cc 8 | # Path to the source directory, relative to the makefile 9 | SRC_PATH = . 10 | # General compiler flags 11 | COMPILE_FLAGS = -std=c++11 -Wall -Wextra -g 12 | # Additional release-specific flags 13 | RCOMPILE_FLAGS = -D NDEBUG 14 | # Additional debug-specific flags 15 | DCOMPILE_FLAGS = -D DEBUG 16 | # Add additional include paths 17 | INCLUDES = -I $(SRC_PATH)/ 18 | # General linker settings 19 | LINK_FLAGS = 20 | # Additional release-specific linker settings 21 | RLINK_FLAGS = 22 | # Additional debug-specific linker settings 23 | DLINK_FLAGS = 24 | # Destination directory, like a jail or mounted system 25 | DESTDIR = / 26 | # Install path (bin/ is appended automatically) 27 | INSTALL_PREFIX = usr/local 28 | #### END PROJECT SETTINGS #### 29 | 30 | # Generally should not need to edit below this line 31 | 32 | # Shell used in this makefile 33 | # bash is used for 'echo -en' 34 | SHELL = /bin/bash 35 | # Clear built-in rules 36 | .SUFFIXES: 37 | # Programs for installation 38 | INSTALL = install 39 | INSTALL_PROGRAM = $(INSTALL) 40 | INSTALL_DATA = $(INSTALL) -m 644 41 | 42 | # Verbose option, to output compile and link commands 43 | export V := false 44 | export CMD_PREFIX := @ 45 | ifeq ($(V),true) 46 | CMD_PREFIX := 47 | endif 48 | 49 | # Combine compiler and linker flags 50 | release: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) $(RCOMPILE_FLAGS) 51 | release: export LDFLAGS := $(LDFLAGS) $(LINK_FLAGS) $(RLINK_FLAGS) 52 | debug: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) $(DCOMPILE_FLAGS) 53 | debug: export LDFLAGS := $(LDFLAGS) $(LINK_FLAGS) $(DLINK_FLAGS) 54 | 55 | # Build and output paths 56 | release: export BUILD_PATH := build/release 57 | release: export BIN_PATH := bin/release 58 | debug: export BUILD_PATH := build/debug 59 | debug: export BIN_PATH := bin/debug 60 | install: export BIN_PATH := bin/release 61 | 62 | # Find all source files in the source directory, sorted by most 63 | # recently modified 64 | SOURCES = $(shell find $(SRC_PATH)/ -name '*.$(SRC_EXT)' -printf '%T@\t%p\n' \ 65 | | sort -k 1nr | cut -f2-) 66 | # fallback in case the above fails 67 | rwildcard = $(foreach d, $(wildcard $1*), $(call rwildcard,$d/,$2) \ 68 | $(filter $(subst *,%,$2), $d)) 69 | ifeq ($(SOURCES),) 70 | SOURCES := $(call rwildcard, $(SRC_PATH)/, *.$(SRC_EXT)) 71 | endif 72 | 73 | # Set the object file names, with the source directory stripped 74 | # from the path, and the build path prepended in its place 75 | OBJECTS = $(SOURCES:$(SRC_PATH)/%.$(SRC_EXT)=$(BUILD_PATH)/%.o) 76 | # Set the dependency files that will be used to add header dependencies 77 | DEPS = $(OBJECTS:.o=.d) 78 | 79 | # Macros for timing compilation 80 | TIME_FILE = $(dir $@).$(notdir $@)_time 81 | START_TIME = date '+%s' > $(TIME_FILE) 82 | END_TIME = read st < $(TIME_FILE) ; \ 83 | $(RM) $(TIME_FILE) ; \ 84 | st=$$((`date '+%s'` - $$st - 86400)) ; \ 85 | echo `date -u -d @$$st '+%H:%M:%S'` 86 | 87 | # Version macros 88 | # Comment/remove this section to remove versioning 89 | USE_VERSION := false 90 | # If this isn't a git repo or the repo has no tags, git describe will return non-zero 91 | ifeq ($(shell git describe > /dev/null 2>&1 ; echo $$?), 0) 92 | USE_VERSION := true 93 | VERSION := $(shell git describe --tags --long --dirty --always | \ 94 | sed 's/v\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)-\?.*-\([0-9]*\)-\(.*\)/\1 \2 \3 \4 \5/g') 95 | VERSION_MAJOR := $(word 1, $(VERSION)) 96 | VERSION_MINOR := $(word 2, $(VERSION)) 97 | VERSION_PATCH := $(word 3, $(VERSION)) 98 | VERSION_REVISION := $(word 4, $(VERSION)) 99 | VERSION_HASH := $(word 5, $(VERSION)) 100 | VERSION_STRING := \ 101 | "$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH).$(VERSION_REVISION)-$(VERSION_HASH)" 102 | override CXXFLAGS := $(CXXFLAGS) \ 103 | -D VERSION_MAJOR=$(VERSION_MAJOR) \ 104 | -D VERSION_MINOR=$(VERSION_MINOR) \ 105 | -D VERSION_PATCH=$(VERSION_PATCH) \ 106 | -D VERSION_REVISION=$(VERSION_REVISION) \ 107 | -D VERSION_HASH=\"$(VERSION_HASH)\" 108 | endif 109 | 110 | # Standard, non-optimized release build 111 | .PHONY: release 112 | release: dirs 113 | ifeq ($(USE_VERSION), true) 114 | @echo "Beginning release build v$(VERSION_STRING)" 115 | else 116 | @echo "Beginning release build" 117 | endif 118 | @$(START_TIME) 119 | @$(MAKE) all --no-print-directory 120 | @echo -n "Total build time: " 121 | @$(END_TIME) 122 | 123 | # Debug build for gdb debugging 124 | .PHONY: debug 125 | debug: dirs 126 | ifeq ($(USE_VERSION), true) 127 | @echo "Beginning debug build v$(VERSION_STRING)" 128 | else 129 | @echo "Beginning debug build" 130 | endif 131 | @$(START_TIME) 132 | @$(MAKE) all --no-print-directory 133 | @echo -n "Total build time: " 134 | @$(END_TIME) 135 | 136 | # Create the directories used in the build 137 | .PHONY: dirs 138 | dirs: 139 | @echo "Creating directories" 140 | @mkdir -p $(dir $(OBJECTS)) 141 | @mkdir -p $(BIN_PATH) 142 | 143 | # Installs to the set path 144 | .PHONY: install 145 | install: 146 | @echo "Installing to $(DESTDIR)$(INSTALL_PREFIX)/bin" 147 | @$(INSTALL_PROGRAM) $(BIN_PATH)/$(BIN_NAME) $(DESTDIR)$(INSTALL_PREFIX)/bin 148 | 149 | # Uninstalls the program 150 | .PHONY: uninstall 151 | uninstall: 152 | @echo "Removing $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BIN_NAME)" 153 | @$(RM) $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BIN_NAME) 154 | 155 | # Removes all build files 156 | .PHONY: clean 157 | clean: 158 | @echo "Deleting $(BIN_NAME) symlink" 159 | @$(RM) $(BIN_NAME) 160 | @echo "Deleting directories" 161 | @$(RM) -r build 162 | @$(RM) -r bin 163 | 164 | # Main rule, checks the executable and symlinks to the output 165 | all: $(BIN_PATH)/$(BIN_NAME) 166 | @echo "Making symlink: $(BIN_NAME) -> $<" 167 | @$(RM) $(BIN_NAME) 168 | @ln -s $(BIN_PATH)/$(BIN_NAME) $(BIN_NAME) 169 | 170 | # Link the executable 171 | $(BIN_PATH)/$(BIN_NAME): $(OBJECTS) 172 | @echo "Linking: $@" 173 | @$(START_TIME) 174 | $(CMD_PREFIX)$(CXX) $(OBJECTS) $(LDFLAGS) -o $@ 175 | @echo -en "\t Link time: " 176 | @$(END_TIME) 177 | 178 | # Add dependency files, if they exist 179 | -include $(DEPS) 180 | 181 | # Source file rules 182 | # After the first compilation they will be joined with the rules from the 183 | # dependency files to provide header dependencies 184 | $(BUILD_PATH)/%.o: $(SRC_PATH)/%.$(SRC_EXT) 185 | @echo "Compiling: $< -> $@" 186 | @$(START_TIME) 187 | $(CMD_PREFIX)$(CXX) $(CXXFLAGS) $(INCLUDES) -MP -MMD -c $< -o $@ 188 | @echo -en "\t Compile time: " 189 | @$(END_TIME) 190 | 191 | 192 | -------------------------------------------------------------------------------- /Ex14_Memory_Models_Compiler_Optimisation/Figs/Transformation_Code.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 21 | 23 | 43 | 47 | 51 | 55 | 56 | 58 | 59 | 61 | image/svg+xml 62 | 64 | 65 | 66 | 67 | 68 | 72 | 80 | Source 91 | 99 | Compiler 110 | 118 | Processor 129 | 137 | Cache 148 | 156 | Execution 167 | 168 | 169 | -------------------------------------------------------------------------------- /Ex06_Pointers_I/figs/memory.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 21 | 23 | 43 | 47 | 48 | 50 | 51 | 53 | image/svg+xml 54 | 56 | 57 | 58 | 59 | 60 | 64 | 71 | 78 | 85 | 92 | 103 | Code 114 | Static Data 125 | Free Storeor Heap 144 | Stack 155 | (Local variable) 167 | (Accessed via new or malloc) 179 | (Global variables) 191 | (Executable code) 203 | 204 | 205 | -------------------------------------------------------------------------------- /Ex06_Pointers_I/figs/pointers.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 19 | 21 | 24 | 28 | 32 | 33 | 35 | 39 | 43 | 47 | 48 | 57 | 66 | 67 | 87 | 91 | 95 | 96 | 98 | 99 | 101 | image/svg+xml 102 | 104 | 105 | 106 | 107 | 108 | 112 | 122 | 132 | 137 | 142 | 153 | var1*ptr 170 | Value is 12 182 | &var1ptr 199 | Address0x1000 216 | 217 | 218 | --------------------------------------------------------------------------------