diff options
author | yang-g <yangg@google.com> | 2015-07-15 00:50:15 -0700 |
---|---|---|
committer | yang-g <yangg@google.com> | 2015-07-15 00:55:30 -0700 |
commit | 811536efce1ba1203c4cb469e455a41fdd207e86 (patch) | |
tree | 6a8f916b5c82ca1351526116d92dfea2094cd816 /src/cpp/common | |
parent | 1cbeeb508b033c84f73ec79756bb4f7c0227a0d0 (diff) |
auth context iterator
Diffstat (limited to 'src/cpp/common')
-rw-r--r-- | src/cpp/common/secure_auth_context.cc | 63 | ||||
-rw-r--r-- | src/cpp/common/secure_auth_context.h | 4 |
2 files changed, 67 insertions, 0 deletions
diff --git a/src/cpp/common/secure_auth_context.cc b/src/cpp/common/secure_auth_context.cc index 4513723653..53b940f108 100644 --- a/src/cpp/common/secure_auth_context.cc +++ b/src/cpp/common/secure_auth_context.cc @@ -77,4 +77,67 @@ std::vector<grpc::string> SecureAuthContext::FindPropertyValues( return values; } +AuthContext::PropertyIterator::PropertyIterator() + : property_(nullptr), ctx_(nullptr), index_(0), name_(nullptr) {} + +AuthContext::PropertyIterator::PropertyIterator( + const grpc_auth_property* property, const grpc_auth_property_iterator* iter) + : property_(property), + ctx_(iter->ctx), + index_(iter->index), + name_(iter->name) {} + +AuthContext::PropertyIterator::~PropertyIterator() {} + +AuthContext::PropertyIterator& AuthContext::PropertyIterator::operator++() { + grpc_auth_property_iterator iter = {ctx_, index_, name_}; + property_ = grpc_auth_property_iterator_next(&iter); + ctx_ = iter.ctx; + index_ = iter.index; + name_ = iter.name; + return *this; +} + +AuthContext::PropertyIterator AuthContext::PropertyIterator::operator++(int) { + PropertyIterator tmp(*this); + operator++(); + return tmp; +} + +bool AuthContext::PropertyIterator::operator==( + const AuthContext::PropertyIterator& rhs) const { + if (property_ == nullptr || rhs.property_ == nullptr) { + return property_ == rhs.property_; + } else { + return index_ == rhs.index_; + } +} + +bool AuthContext::PropertyIterator::operator!=( + const AuthContext::PropertyIterator& rhs) const { + return !operator==(rhs); +} + +const AuthContext::Property AuthContext::PropertyIterator::operator*() { + return std::make_pair<grpc::string, grpc::string>( + grpc::string(property_->name), + grpc::string(property_->value, property_->value_length)); +} + +SecureAuthContext::const_iterator SecureAuthContext::begin() const { + if (ctx_) { + grpc_auth_property_iterator iter = + grpc_auth_context_property_iterator(ctx_); + const grpc_auth_property* property = + grpc_auth_property_iterator_next(&iter); + return AuthContext::PropertyIterator(property, &iter); + } else { + return end(); + } +} + +SecureAuthContext::const_iterator SecureAuthContext::end() const { + return AuthContext::PropertyIterator(); +} + } // namespace grpc diff --git a/src/cpp/common/secure_auth_context.h b/src/cpp/common/secure_auth_context.h index bba46803cd..9ea4eb1b5c 100644 --- a/src/cpp/common/secure_auth_context.h +++ b/src/cpp/common/secure_auth_context.h @@ -53,6 +53,10 @@ class SecureAuthContext GRPC_FINAL : public AuthContext { std::vector<grpc::string> FindPropertyValues(const grpc::string& name) const GRPC_OVERRIDE; + const_iterator begin() const GRPC_OVERRIDE; + + const_iterator end() const GRPC_OVERRIDE; + private: grpc_auth_context* ctx_; }; |