Skip to content

Commit

Permalink
Don't put unsanctioned code in namespace std
Browse files Browse the repository at this point in the history
Enable the clang-tidy check cert-dcl58-cpp, which prohibits code in
namespace std (except where explicitly permitted).

We had some such code in the form of a tuple hash implementation.  Move
it to namespace cata instead.
  • Loading branch information
jbytheway authored and kevingranade committed Aug 10, 2019
1 parent acaf1de commit 91012d1
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 34 deletions.
1 change: 0 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ readability-*,\
-bugprone-undefined-memory-manipulation,\
-bugprone-unused-return-value,\
-bugprone-use-after-move,\
-cert-dcl58-cpp,\
-cert-dcl59-cpp,\
-cert-env33-c,\
-cert-err34-c,\
Expand Down
8 changes: 5 additions & 3 deletions src/effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,11 @@ void weed_msg( player &p )
}
}

static void extract_effect( JsonObject &j,
std::unordered_map<std::tuple<std::string, bool, std::string, std::string>, double> &data,
const std::string &mod_type, std::string data_key, std::string type_key, std::string arg_key )
static void extract_effect(
JsonObject &j,
std::unordered_map<std::tuple<std::string, bool, std::string, std::string>, double,
cata::tuple_hash> &data,
const std::string &mod_type, std::string data_key, std::string type_key, std::string arg_key )
{
double val = 0;
double reduced_val = 0;
Expand Down
4 changes: 3 additions & 1 deletion src/effect.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ class effect_type
std::string remove_memorial_log;

/** Key tuple order is:("base_mods"/"scaling_mods", reduced: bool, type of mod: "STR", desired argument: "tick") */
std::unordered_map<std::tuple<std::string, bool, std::string, std::string>, double> mod_data;
std::unordered_map <
std::tuple<std::string, bool, std::string, std::string>, double, cata::tuple_hash
> mod_data;
};

class effect
Expand Down
3 changes: 2 additions & 1 deletion src/mutation.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,9 @@ struct mutation_branch {
std::map<body_part, int> encumbrance_covered;
// Body parts that now need OVERSIZE gear
std::set<body_part> restricts_gear;
// Mutation stat mods
/** Key pair is <active: bool, mod type: "STR"> */
std::unordered_map<std::pair<bool, std::string>, int> mods; // Mutation stat mods
std::unordered_map<std::pair<bool, std::string>, int, cata::tuple_hash> mods;
std::map<body_part, resistances> armor;
std::vector<matype_id>
initial_ma_styles; // Martial art styles that can be chosen upon character generation
Expand Down
10 changes: 6 additions & 4 deletions src/mutation_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@ bool string_id<Trait_group>::is_valid() const
return trait_groups.count( *this );
}

static void extract_mod( JsonObject &j, std::unordered_map<std::pair<bool, std::string>, int> &data,
const std::string &mod_type, bool active, const std::string &type_key )
static void extract_mod(
JsonObject &j, std::unordered_map<std::pair<bool, std::string>, int, cata::tuple_hash> &data,
const std::string &mod_type, bool active, const std::string &type_key )
{
int val = j.get_int( mod_type, 0 );
if( val != 0 ) {
data[std::make_pair( active, type_key )] = val;
}
}

static void load_mutation_mods( JsonObject &jsobj, const std::string &member,
std::unordered_map<std::pair<bool, std::string>, int> &mods )
static void load_mutation_mods(
JsonObject &jsobj, const std::string &member,
std::unordered_map<std::pair<bool, std::string>, int, cata::tuple_hash> &mods )
{
if( jsobj.has_object( member ) ) {
JsonObject j = jsobj.get_object( member );
Expand Down
16 changes: 8 additions & 8 deletions src/savegame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1220,14 +1220,14 @@ struct mongroup_hash {
std::size_t operator()( const mongroup &mg ) const {
// Note: not hashing monsters or position
size_t ret = std::hash<mongroup_id>()( mg.type );
std::hash_combine( ret, mg.radius );
std::hash_combine( ret, mg.population );
std::hash_combine( ret, mg.target );
std::hash_combine( ret, mg.interest );
std::hash_combine( ret, mg.dying );
std::hash_combine( ret, mg.horde );
std::hash_combine( ret, mg.horde_behaviour );
std::hash_combine( ret, mg.diffuse );
cata::hash_combine( ret, mg.radius );
cata::hash_combine( ret, mg.population );
cata::hash_combine( ret, mg.target );
cata::hash_combine( ret, mg.interest );
cata::hash_combine( ret, mg.dying );
cata::hash_combine( ret, mg.horde );
cata::hash_combine( ret, mg.horde_behaviour );
cata::hash_combine( ret, mg.diffuse );
return ret;
}
};
Expand Down
29 changes: 13 additions & 16 deletions src/tuple_hash.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once
#ifndef TUPLE_HASH_H
#define TUPLE_HASH_H
#ifndef CATA_TUPLE_HASH_H
#define CATA_TUPLE_HASH_H

// Support for tuple and pair hashing.
// This is taken almost directly from the boost library code.
// Function has to live in the std namespace
// so that it is picked up by argument-dependent name lookup (ADL).
namespace std
namespace cata
{
namespace
{
Expand All @@ -20,7 +20,7 @@ namespace
template <class T>
inline void hash_combine( std::size_t &seed, const T &v )
{
seed ^= hash<T>()( v ) + 0x9e3779b9 + ( seed << 6 ) + ( seed >> 2 );
seed ^= std::hash<T>()( v ) + 0x9e3779b9 + ( seed << 6 ) + ( seed >> 2 );
}

// Recursive template code derived from Matthieu M.
Expand All @@ -29,38 +29,35 @@ struct HashValueImpl
{
static void apply( size_t &seed, const Tuple &tuple ) {
HashValueImpl < Tuple, Index - 1 >::apply( seed, tuple );
hash_combine( seed, get<Index>( tuple ) );
hash_combine( seed, std::get<Index>( tuple ) );
}
};

template <class Tuple>
struct HashValueImpl<Tuple, 0> {
static void apply( size_t &seed, const Tuple &tuple ) {
hash_combine( seed, get<0>( tuple ) );
hash_combine( seed, std::get<0>( tuple ) );
}
};
} // namespace

template <typename ... TT>
struct hash<std::tuple<TT...>> {
size_t
operator()( const std::tuple<TT...> &tt ) const {
struct tuple_hash {
template <typename ... TT>
std::size_t operator()( const std::tuple<TT...> &tt ) const {
size_t seed = 0;
HashValueImpl<std::tuple<TT...> >::apply( seed, tt );
return seed;
}

};

template <class A, class B>
struct hash<std::pair<A, B>> {
template <class A, class B>
std::size_t operator()( const std::pair<A, B> &v ) const {
std::size_t seed = 0;
hash_combine( seed, v.first );
hash_combine( seed, v.second );
return seed;
}
};
} // namespace std

#endif
} // namespace cata

#endif // CATA_TUPLE_HASH_H

0 comments on commit 91012d1

Please sign in to comment.