Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(util): Introduce map utilities 2 #118

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -667,5 +667,13 @@ header:
- 'src/zookeeper/zookeeper_session.h'
- 'src/zookeeper/zookeeper_session_mgr.cpp'
- 'src/zookeeper/zookeeper_session_mgr.h'
# Apache License, Version 2.0, Copyright 2018 Google LLC
- 'src/gutil/test/map_traits_test.cpp'
- 'src/gutil/test/map_util_test.h'
- 'src/gutil/test/map_util_unittest.cpp'
- 'src/gutil/test/no_destructor_test.cpp'
- 'src/gutil/map_traits.h'
- 'src/gutil/map_util.h'
- 'src/gutil/no_destructor.h'

comment: on-failure
23 changes: 23 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -570,3 +570,26 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

--------------------------------------------------------------------------------

src/gutil/test/map_traits_test.cpp
src/gutil/test/map_util_test.h
src/gutil/test/map_util_unittest.cpp
src/gutil/test/no_destructor_test.cpp
src/gutil/map_traits.h
src/gutil/map_util.h
src/gutil/no_destructor.h

Copyright 2018 Google LLC

Licensed 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.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ add_subdirectory(client_lib)
add_subdirectory(common)
add_subdirectory(failure_detector)
add_subdirectory(geo)
add_subdirectory(gutil)
add_subdirectory(http)
add_subdirectory(meta)
add_subdirectory(nfs)
Expand Down
9 changes: 5 additions & 4 deletions src/base/idl_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@

#include "rrdb/rrdb_types.h"
#include "utils/fmt_utils.h"
#include "gutil/map_util.h"

namespace pegasus {

inline std::string cas_check_type_to_string(dsn::apps::cas_check_type::type type)
{
auto it = dsn::apps::_cas_check_type_VALUES_TO_NAMES.find(type);
if (it == dsn::apps::_cas_check_type_VALUES_TO_NAMES.end()) {
return std::string("INVALID=") + std::to_string(int(type));
const auto *name = gutil::FindOrNull(dsn::apps::_cas_check_type_VALUES_TO_NAMES, type);
if (dsn_unlikely(name == nullptr)) {
return fmt::format("INVALID={}", type);
}
return it->second;
return *name;
}

inline bool cas_is_check_operand_needed(dsn::apps::cas_check_type::type type)
Expand Down
1 change: 1 addition & 0 deletions src/common/duplication_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class duplication_group_registry : public utils::singleton<duplication_group_reg
_group.size(),
"there might be duplicate cluster_name in configuration");

// TODO(yingchun): add InsertValuesFromMap to src/gutil/map_util.h, then use it here.
for (const auto &kv : _group) {
_distinct_cids.insert(kv.second);
}
Expand Down
21 changes: 21 additions & 0 deletions src/gutil/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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.

# TODO(yingchun): add the project after new *.cpp files have been added.
#set(MY_PROJ_NAME pgs_gutil)

add_subdirectory(test)
80 changes: 80 additions & 0 deletions src/gutil/map_traits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// Copyright 2018 Google LLC
//
// Licensed 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.
//

#pragma once

// Traits classes for performing uniform lookup on different map value types.
//
// The access is computed as follows:
//
// 1. If T has a `first` or `second` field, use them.
// 2. Otherwise if it has `key()` or `value()` methods, use them.
// 3. Otherwise the program is ill-formed.

#include <utility>

namespace gutil {
namespace subtle {
namespace internal_map_traits {
struct Rank1
{
};
struct Rank0 : Rank1
{
};

template <class V>
auto GetKey(V &&v, Rank0) -> decltype((std::forward<V>(v).first))
{
return std::forward<V>(v).first;
}
template <class V>
auto GetKey(V &&v, Rank1) -> decltype(std::forward<V>(v).key())
{
return std::forward<V>(v).key();
}

template <class V>
auto GetMapped(V &&v, Rank0) -> decltype((std::forward<V>(v).second))
{
return std::forward<V>(v).second;
}
template <class V>
auto GetMapped(V &&v, Rank1) -> decltype(std::forward<V>(v).value())
{
return std::forward<V>(v).value();
}

} // namespace internal_map_traits

// Accesses the `key_type` from a `value_type`.
template <typename V>
auto GetKey(V &&v)
-> decltype(internal_map_traits::GetKey(std::forward<V>(v), internal_map_traits::Rank0()))
{
return internal_map_traits::GetKey(std::forward<V>(v), internal_map_traits::Rank0());
}

// Accesses the `mapped_type` from a `value_type`.
template <typename V>
auto GetMapped(V &&v)
-> decltype(internal_map_traits::GetMapped(std::forward<V>(v), internal_map_traits::Rank0()))
{
return internal_map_traits::GetMapped(std::forward<V>(v), internal_map_traits::Rank0());
}

} // namespace subtle
} // namespace gutil
Loading
Loading