Created
March 27, 2019 20:15
-
-
Save greenbrettmichael/b67199b1f69373ddb297f8cb2959bd09 to your computer and use it in GitHub Desktop.
Beast Code Basic Template Mechanics Workshop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| std::string myEmailPW = "abcd123"; | |
| template<typename returnType, typename func> | |
| returnType resultCallback(func F) | |
| { | |
| std::string outputResult = myEmailPW; | |
| return F(outputResult); | |
| } | |
| class PassKeep | |
| { | |
| public: | |
| PassKeep(std::string securePW) : securePW(securePW) {} | |
| bool operator()(std::string inputResult) const | |
| { | |
| return inputResult.compare(securePW) == 0; | |
| } | |
| std::string securePW; | |
| }; | |
| int main() | |
| { | |
| auto thruFuncProc = [](std::string inputResult) { std::reverse(inputResult.begin(), inputResult.end()); return inputResult; }; | |
| std::cout << "Through Function Example " << resultCallback<std::string>(thruFuncProc) << std::endl; | |
| PassKeep yahooSecureLogin(myEmailPW); | |
| std::cout << "Object Compare Example " << resultCallback<bool>(yahooSecureLogin) << std::endl; | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <type_traits> | |
| #include <string> | |
| struct Cat | |
| { | |
| bool isAlive() const { return true; } | |
| bool isCute() const { return false; } | |
| }; | |
| struct Dog | |
| { | |
| bool isAlive() const { return true; } | |
| bool isCute() const { return true; } | |
| }; | |
| struct CatPerson | |
| { | |
| bool consider(bool objectiveFacts) { return !objectiveFacts; } | |
| }; | |
| struct DogPerson | |
| { | |
| bool consider(bool objectiveFacts) { return objectiveFacts; } | |
| }; | |
| template<typename T> | |
| struct AnimalReporter | |
| { | |
| AnimalReporter(T object) : object(object) {} | |
| std::string askHardHittingQuestions() | |
| { | |
| return std::string("Are you Alive? ") + (object.isAlive() ? std::string("yeah") : std::string("nah")) + | |
| std::string(" Are you Cute? ") + (object.isCute() ? std::string("yeah") : std::string("nah")); | |
| } | |
| template<typename P> | |
| bool willAdopt(P actor) { return actor.consider(object.isCute() && object.isAlive()); } | |
| T object; | |
| }; | |
| template<> | |
| struct AnimalReporter<Dog> | |
| { | |
| AnimalReporter(Dog object) : object(object) {} | |
| std::string askHardHittingQuestions() { return "Wow what a cute Dog"; } | |
| bool willPet() { return true; } | |
| Dog object; | |
| }; | |
| int main() | |
| { | |
| Cat sampleCat; Dog sampleDog; CatPerson Jack; DogPerson Scott; | |
| AnimalReporter<Cat> catFacts(sampleCat); | |
| std::cout << catFacts.askHardHittingQuestions() << std::endl; | |
| std::cout << "Jack cat adoption = " << catFacts.willAdopt(Jack) << std::endl; | |
| std::cout << "Scott cat adoption = " << catFacts.willAdopt(Scott) << std::endl; | |
| AnimalReporter<Dog> anyPerson(sampleDog); | |
| std::cout << anyPerson.askHardHittingQuestions() << std::endl; | |
| std::cout << "Would anyone pet this dog " << anyPerson.willPet() << std::endl; | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| template<typename numberType, unsigned multCount> | |
| numberType selfMult(numberType self) | |
| { | |
| numberType output = self; | |
| for (int i = 0; i < multCount; ++i) | |
| { | |
| output *= self; | |
| } | |
| return output; | |
| } | |
| template<typename numberType> | |
| numberType selfMult(numberType self) | |
| { | |
| return self * -1; | |
| } | |
| template<> | |
| std::string selfMult<std::string>(std::string self) | |
| { | |
| return self + std::string("This is a string"); | |
| } | |
| int main() | |
| { | |
| std::cout << "2^4 is " << selfMult<int, 4>(2) << std::endl; | |
| std::cout << "Negate 1 " << selfMult(1) << std::endl; | |
| std::cout << selfMult(std::string("Is this a string? ")) << std::endl; | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <type_traits> | |
| #include <string> | |
| #include <vector> | |
| template <typename T, typename = void> | |
| struct is_iterable : std::false_type {}; | |
| template <typename T> | |
| struct is_iterable<T, std::void_t<decltype(std::declval<T>().begin()), | |
| decltype(std::declval<T>().end())>> | |
| : std::true_type {}; | |
| template<typename T, typename std::enable_if<is_iterable<T>::value, T>::type* = nullptr> | |
| bool leninSort (T& container) | |
| { | |
| bool foundOutOfOrder = false; | |
| while (!foundOutOfOrder) | |
| { | |
| size_t itemCount = container.end() - container.begin(); | |
| size_t i = 0; | |
| while (i < itemCount -1) | |
| { | |
| if (*(container.begin() + i + 1) < *(container.begin() + i)) | |
| { | |
| container.erase(container.begin() + i); | |
| break; | |
| } | |
| ++i; | |
| } | |
| foundOutOfOrder = i == (itemCount -1); | |
| } | |
| return true; | |
| } | |
| template<typename T, typename std::enable_if<!is_iterable<T>::value, T>::type* = nullptr> | |
| bool leninSort(T& value) { return false; }; | |
| std::string getMsg(bool didSort) { return didSort ? "Returned Area To Order" : "Could Not Search Area"; } | |
| int main() | |
| { | |
| std::vector<double> cityArea = { 100.0, -1.5, 1000.7, 80 }; | |
| double ruralArea = 50; | |
| std::cout << "City : " << getMsg(leninSort(cityArea)) << std::endl; | |
| std::cout << "Rural : " << getMsg(leninSort(ruralArea)) << std::endl; | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <list> | |
| #include <map> | |
| #include <functional> | |
| struct A { virtual const char* print() = 0; }; | |
| struct B : public A { virtual const char* print() { return "I'm B"; } }; | |
| struct C : public A { virtual const char* print() { return "I'm C"; } }; | |
| class markupFactory | |
| { | |
| public: | |
| A* createType(std::string name) const | |
| { | |
| auto a = nameToType.find(name); | |
| if (a != nameToType.end()) | |
| { | |
| return a->second(); | |
| } | |
| return nullptr; | |
| } | |
| std::list<std::string> getNames() const | |
| { | |
| std::list<std::string> result; | |
| for (auto& item : nameToType) | |
| { | |
| result.push_back(item.first); | |
| } | |
| return result; | |
| } | |
| void registerNamedType(std::string name, std::function<A*()> typeCreator) | |
| { | |
| nameToType[name] = typeCreator; | |
| } | |
| private: | |
| std::map<std::string, std::function<A*()>> nameToType; | |
| }; | |
| int main() | |
| { | |
| markupFactory myFacts; | |
| myFacts.registerNamedType("B Type", []() { return new B; }); | |
| myFacts.registerNamedType("C Type", []() { return new C; }); | |
| A* tempB = myFacts.createType("B Type"); | |
| A* tempC = myFacts.createType("C Type"); | |
| std::cout << " Objects Made : " << tempB->print() << " " << tempC->print() << std::endl; | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <numeric> | |
| #include <type_traits> | |
| #include <vector> | |
| template<typename... numberTypes> | |
| typename std::common_type<numberTypes...>::type meanNumbersFold(numberTypes... numbers) | |
| { | |
| return (... + numbers) / sizeof...(numberTypes); | |
| } | |
| template<typename returnType, typename numberType> | |
| returnType meanNumbersRecursiveInternal(returnType sum, returnType counter, numberType number) | |
| { | |
| return (sum + number) / (counter + 1); | |
| } | |
| template<typename returnType, typename numberType, typename... numberTypes> | |
| typename returnType meanNumbersRecursiveInternal(returnType sum, returnType counter, numberType currentNumber, numberTypes... remainingNumbers) | |
| { | |
| return meanNumbersRecursiveInternal(sum + currentNumber, counter + 1, remainingNumbers...); | |
| } | |
| template<typename numberType, typename... numberTypes> | |
| typename std::common_type<numberType, numberTypes...>::type meanNumbersRecursive(numberType currentNumber, numberTypes... numbers) | |
| { | |
| using outputType = typename std::common_type<numberType, numberTypes...>::type; | |
| return meanNumbersRecursiveInternal<outputType>(currentNumber, { 1 }, numbers...); | |
| } | |
| int main() | |
| { | |
| int iNo = -1; short sNo = 2; unsigned int uNo = 3; float fNo = -2.7f; double dNo = 1.23; | |
| std::cout << "Integral Adds Fold " << meanNumbersFold(iNo, sNo, uNo) << " Recursive " << meanNumbersRecursive(iNo, sNo, uNo) << std::endl; | |
| std::cout << "Float Adds Fold" << meanNumbersFold(fNo, dNo) << " Recursive " << meanNumbersRecursive(fNo, dNo) << std::endl; | |
| std::cout << "All Adds Fold" << meanNumbersFold(iNo, sNo, uNo, fNo, dNo) << " Recursive " << meanNumbersRecursive(iNo, sNo, uNo, fNo, dNo) << std::endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment