/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ #ifndef THRIFT_THRIFT_H_ #define THRIFT_THRIFT_H_ #include "thrift/lib/cpp/thrift_config.h" #include #include #include #include #ifdef THRIFT_HAVE_INTTYPES_H #include #endif #include #include #include #include #include #include #include #include #include "thrift/lib/cpp/TLogging.h" namespace apache { namespace thrift { struct ltstr { bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) < 0; } }; /** * Helper template class for enum<->string conversion. */ template struct TEnumTraits { /** * Finds the name of a given enum value, returning it or NULL on failure. * Specialized implementations will be emitted as part of enum codegen. * * Example specialization: * template<> * const char* TEnumTraits::findName(MyEnum value) { * return findName(_MyEnum_VALUES_TO_NAMES, value); * } * Note the use of helper function 'findName(...)', below. */ static const char* findName(T value); /** * Attempts to find a value for a given name. * Specialized implementations will be emitted as part of enum codegen. * * Example implementation: * template<> * bool TEnumTraits::findValue(const char* name, * MyEnum* outValue) { * return findValue(_MyEnum_NAMES_TO_VALUES, name, outValue); * } * Note the use of helper function 'findValue(...)', below. */ static bool findValue(const char* name, T* outValue); private: /** * Helper method used by codegen implementation of findName, Supports * use with strict and non-strict enums by way of template parameter * 'ValueType'. */ template static const char* findName(const std::map& map, T value) { auto found = map.find(value); if (found == map.end()) { return NULL; } else { return found->second; } } /** * Helper method used by codegen implementation of findValue, Supports * use with strict and non-strict enums by way of template parameter * 'ValueType'. */ template static bool findValue(const std::map& map, const char* name, T* out) { auto found = map.find(name); if (found == map.end()) { return false; } else { *out = static_cast(found->second); return true; } } }; template class TEnumIterator : public std::map::iterator { public: TEnumIterator(int n, T* enums, const char** names) : ii_(0), n_(n), enums_(enums), names_(names) { } int operator ++() { return ++ii_; } bool operator !=(const TEnumIterator& end) { assert(end.n_ == -1); return (ii_ != n_); } std::pair operator*() const { return std::make_pair(enums_[ii_], names_[ii_]); } private: int ii_; const int n_; T* enums_; const char** names_; }; template class TEnumInverseIterator : public std::map::iterator { public: TEnumInverseIterator(int n, T* enums, const char** names) : ii_(0), n_(n), enums_(enums), names_(names) { } int operator ++() { return ++ii_; } bool operator !=(const TEnumInverseIterator& end) { assert(end.n_ == -1); return (ii_ != n_); } std::pair operator*() const { return std::make_pair(names_[ii_], enums_[ii_]); } private: int ii_; const int n_; T* enums_; const char** names_; }; class TOutput { public: TOutput() : f_(&errorTimeWrapper) {} inline void setOutputFunction(void (*function)(const char *)){ f_ = function; } inline void operator()(const char *message){ f_(message); } // It is important to have a const char* overload here instead of // just the string version, otherwise errno could be corrupted // if there is some problem allocating memory when constructing // the string. void perror(const char *message, int errno_copy); inline void perror(const std::string &message, int errno_copy) { perror(message.c_str(), errno_copy); } void printf(const char *message, ...); inline static void errorTimeWrapper(const char* msg) { time_t now; char dbgtime[26]; time(&now); ctime_r(&now, dbgtime); dbgtime[24] = 0; fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg); } /** Just like strerror_r but returns a C++ string object. */ static std::string strerror_s(int errno_copy); private: void (*f_)(const char *); }; extern TOutput GlobalOutput; /** * Base class for all Thrift exceptions. * Should never be instantiated, only caught. */ class TException : public std::exception { }; /** * Base class for exceptions from the Thrift library, and occasionally * from the generated code. This class should not be thrown by user code. * Instances of this class are not meant to be serialized. */ class TLibraryException : public TException { public: TLibraryException() {} explicit TLibraryException(const std::string& message) : message_(message) {} TLibraryException(const char* message, int errnoValue); virtual ~TLibraryException() throw() {} virtual const char* what() const throw() { if (message_.empty()) { return "Default TLibraryException."; } else { return message_.c_str(); } } protected: std::string message_; }; #if T_GLOBAL_DEBUG_VIRTUAL > 1 void profile_virtual_call(const std::type_info& info); void profile_generic_protocol(const std::type_info& template_type, const std::type_info& prot_type); void profile_print_info(FILE *f); void profile_print_info(); void profile_write_pprof(FILE* gen_calls_f, FILE* virtual_calls_f); #endif }} // apache::thrift #endif // #ifndef THRIFT_THRIFT_H_