Created
April 9, 2017 10:21
-
-
Save chayleaf/4c0458b1b7226ad166569f079b33a4d3 to your computer and use it in GitHub Desktop.
boost::any helper
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
| #ifndef ANY_CAST_H | |
| #define ANY_CAST_H | |
| #include "stdc++.h" | |
| #include <boost/any.hpp> | |
| using namespace std; | |
| #define bac boost::any_cast | |
| class cast { | |
| private: | |
| boost::any v; | |
| public: | |
| cast(const boost::any &a) { | |
| v = a; | |
| } | |
| operator int() { | |
| if (v.type == typeid(int32_t)) | |
| return bac<int32_t>(v); | |
| else if (v.type == typeid(int64_t)) | |
| return bac<int64_t>(v); | |
| else if (v.type == typeid(long)) | |
| return bac<long>(v); | |
| else if (v.type == typeid(short)) | |
| return bac<short>(v); | |
| else | |
| return 0; | |
| } | |
| operator float() { | |
| if (v.type == typeid(double)) | |
| return bac<double>(v); | |
| else if (v.type == typeid(float)) | |
| return bac<float>(v); | |
| else | |
| return 0; | |
| } | |
| template<typename T> | |
| operator T() { | |
| return bac<T>(v); | |
| } | |
| }; | |
| #undef bac | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment