adaptive_protocol_type.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright (c) 2015 Baidu, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef BRPC_ADAPTIVE_PROTOCOL_TYPE_H
  15. #define BRPC_ADAPTIVE_PROTOCOL_TYPE_H
  16. // To brpc developers: This is a header included by user, don't depend
  17. // on internal structures, use opaque pointers instead.
  18. #include "butil/strings/string_piece.h"
  19. #include "brpc/options.pb.h"
  20. namespace brpc {
  21. // NOTE: impl. are in brpc/protocol.cpp
  22. // Convert a case-insensitive string to corresponding ProtocolType which is
  23. // defined in protocol/brpc/options.proto
  24. // Returns: PROTOCOL_UNKNOWN on error.
  25. ProtocolType StringToProtocolType(const butil::StringPiece& type,
  26. bool print_log_on_unknown);
  27. inline ProtocolType StringToProtocolType(const butil::StringPiece& type)
  28. { return StringToProtocolType(type, true); }
  29. // Convert a ProtocolType to a c-style string.
  30. const char* ProtocolTypeToString(ProtocolType);
  31. // Assignable by both ProtocolType and names.
  32. class AdaptiveProtocolType {
  33. public:
  34. AdaptiveProtocolType() : _type(PROTOCOL_UNKNOWN) {}
  35. AdaptiveProtocolType(ProtocolType type) : _type(type) {}
  36. ~AdaptiveProtocolType() {}
  37. void operator=(ProtocolType type) { _type = type; }
  38. void operator=(const butil::StringPiece& name)
  39. { _type = StringToProtocolType(name); };
  40. operator ProtocolType() const { return _type; }
  41. const char* name() const { return ProtocolTypeToString(_type); }
  42. private:
  43. ProtocolType _type;
  44. };
  45. } // namespace brpc
  46. #endif // BRPC_ADAPTIVE_PROTOCOL_TYPE_H