Skip to content

Commit

Permalink
Make maps fields work on installed C++ protobufs.
Browse files Browse the repository at this point in the history
Change-Id: Id937e25bbb35968ee76c92bd4a8ce6247408c443
  • Loading branch information
liujisi committed Feb 25, 2015
1 parent 581a526 commit df184fb
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 25 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Expand Up @@ -310,10 +310,12 @@ EXTRA_DIST = $(@DIST_LANG@_EXTRA_DIST) \
LICENSE \
CONTRIBUTORS.txt \
CHANGES.txt \
config.h.include \
editors/README.txt \
editors/proto.vim \
editors/protobuf-mode.el \
vsprojects/config.h \
vsprojects/google/protobuf/stubs/pbconfig.h \
vsprojects/extract_includes.bat \
vsprojects/libprotobuf.vcproj \
vsprojects/libprotobuf-lite.vcproj \
Expand Down
7 changes: 7 additions & 0 deletions config.h.include
@@ -0,0 +1,7 @@
HASH_MAP_CLASS
HASH_MAP_H
HASH_NAMESPACE
HASH_SET_CLASS
HASH_SET_H
HAVE_HASH_MAP
HAVE_HASH_SET
25 changes: 21 additions & 4 deletions src/Makefile.am
Expand Up @@ -31,12 +31,24 @@ nobase_dist_proto_DATA = google/protobuf/descriptor.proto \
clean-local:
rm -f *.loT

CLEANFILES = $(protoc_outputs) unittest_proto_middleman \
public_config = google/protobuf/stubs/pbconfig.h

CLEANFILES = $(protoc_outputs) $(public_config) unittest_proto_middleman \
testzip.jar testzip.list testzip.proto testzip.zip

MAINTAINERCLEANFILES = \
Makefile.in

# Generate and distribute a minimum config.h file to make hash_map work.
# The autoheader config has too much info, which might conflict with other
# macros applications might include. Thus, we create a stubs/pbconfig.h, that
# only #defines what we really need, and prefix it with GOOGLE_PROTOBUF_ to
# avoid conflicts.
$(public_config): $(top_builddir)/config.h $(top_srcdir)/config.h.include
echo "// Note: Google Protobuf internal only. Do NOT include." > $@
cat $(top_builddir)/config.h | grep -f $(top_srcdir)/config.h.include | \
sed 's,#define , #define GOOGLE_PROTOBUF_,' >> $@

nobase_include_HEADERS = \
google/protobuf/stubs/atomic_sequence_num.h \
google/protobuf/stubs/atomicops.h \
Expand All @@ -54,11 +66,12 @@ nobase_include_HEADERS = \
google/protobuf/stubs/atomicops_internals_x86_gcc.h \
google/protobuf/stubs/atomicops_internals_x86_msvc.h \
google/protobuf/stubs/casts.h \
google/protobuf/stubs/singleton.h \
google/protobuf/stubs/common.h \
google/protobuf/stubs/fastmem.h \
google/protobuf/stubs/hash.h \
google/protobuf/stubs/once.h \
google/protobuf/stubs/platform_macros.h \
google/protobuf/stubs/singleton.h \
google/protobuf/stubs/stl_util.h \
google/protobuf/stubs/template_util.h \
google/protobuf/stubs/type_traits.h \
Expand Down Expand Up @@ -111,6 +124,9 @@ nobase_include_HEADERS = \
google/protobuf/compiler/python/python_generator.h \
google/protobuf/compiler/ruby/ruby_generator.h

nobase_nodist_include_HEADERS = \
$(public_config)

lib_LTLIBRARIES = libprotobuf-lite.la libprotobuf.la libprotoc.la

libprotobuf_lite_la_LIBADD = $(PTHREAD_LIBS)
Expand All @@ -135,7 +151,8 @@ libprotobuf_lite_la_SOURCES = \
google/protobuf/io/coded_stream.cc \
google/protobuf/io/coded_stream_inl.h \
google/protobuf/io/zero_copy_stream.cc \
google/protobuf/io/zero_copy_stream_impl_lite.cc
google/protobuf/io/zero_copy_stream_impl_lite.cc \
$(public_config)

libprotobuf_la_LIBADD = $(PTHREAD_LIBS)
libprotobuf_la_LDFLAGS = -version-info 10:0:0 -export-dynamic -no-undefined
Expand Down Expand Up @@ -374,7 +391,7 @@ protoc_outputs = \
google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.cc \
google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h

BUILT_SOURCES = $(protoc_outputs)
BUILT_SOURCES = $(protoc_outputs) $(public_config)

if USE_EXTERNAL_PROTOC

Expand Down
44 changes: 23 additions & 21 deletions src/google/protobuf/stubs/hash.h
Expand Up @@ -37,21 +37,22 @@

#include <string.h>
#include <google/protobuf/stubs/common.h>
#include "config.h"
#include <google/protobuf/stubs/pbconfig.h>

#if defined(HAVE_HASH_MAP) && defined(HAVE_HASH_SET)
#include HASH_MAP_H
#include HASH_SET_H
#if defined(GOOGLE_PROTOBUF_HAVE_HASH_MAP) && \
defined(GOOGLE_PROTOBUF_HAVE_HASH_SET)
#include GOOGLE_PROTOBUF_HASH_MAP_H
#include GOOGLE_PROTOBUF_HASH_SET_H
#else
#define MISSING_HASH
#define GOOGLE_PROTOBUF_MISSING_HASH
#include <map>
#include <set>
#endif

namespace google {
namespace protobuf {

#ifdef MISSING_HASH
#ifdef GOOGLE_PROTOBUF_MISSING_HASH

// This system doesn't have hash_map or hash_set. Emulate them using map and
// set.
Expand Down Expand Up @@ -105,7 +106,7 @@ class hash_set : public std::set<Key, HashFcn> {
#elif defined(_MSC_VER) && !defined(_STLPORT_VERSION)

template <typename Key>
struct hash : public HASH_NAMESPACE::hash_compare<Key> {
struct hash : public GOOGLE_PROTOBUF_HASH_NAMESPACE::hash_compare<Key> {
};

// MSVC's hash_compare<const char*> hashes based on the string contents but
Expand All @@ -119,13 +120,13 @@ class CstringLess {

template <>
struct hash<const char*>
: public HASH_NAMESPACE::hash_compare<const char*, CstringLess> {
};
: public GOOGLE_PROTOBUF_HASH_NAMESPACE::hash_compare<
const char*, CstringLess> {};

template <typename Key, typename Data,
typename HashFcn = hash<Key>,
typename EqualKey = int >
class hash_map : public HASH_NAMESPACE::hash_map<
class hash_map : public GOOGLE_PROTOBUF_HASH_NAMESPACE::hash_map<
Key, Data, HashFcn> {
public:
hash_map(int = 0) {}
Expand All @@ -134,7 +135,7 @@ class hash_map : public HASH_NAMESPACE::hash_map<
template <typename Key,
typename HashFcn = hash<Key>,
typename EqualKey = int >
class hash_set : public HASH_NAMESPACE::hash_set<
class hash_set : public GOOGLE_PROTOBUF_HASH_NAMESPACE::hash_set<
Key, HashFcn> {
public:
hash_set(int = 0) {}
Expand All @@ -143,7 +144,7 @@ class hash_set : public HASH_NAMESPACE::hash_set<
#else

template <typename Key>
struct hash : public HASH_NAMESPACE::hash<Key> {
struct hash : public GOOGLE_PROTOBUF_HASH_NAMESPACE::hash<Key> {
};

template <typename Key>
Expand All @@ -166,25 +167,26 @@ struct hash<const char*> {
}
};

template <typename Key, typename Data,
typename HashFcn = hash<Key>,
template <typename Key, typename Data, typename HashFcn = hash<Key>,
typename EqualKey = std::equal_to<Key> >
class hash_map : public HASH_NAMESPACE::HASH_MAP_CLASS<
Key, Data, HashFcn, EqualKey> {
class hash_map
: public GOOGLE_PROTOBUF_HASH_NAMESPACE::GOOGLE_PROTOBUF_HASH_MAP_CLASS<
Key, Data, HashFcn, EqualKey> {
public:
hash_map(int = 0) {}
};

template <typename Key,
typename HashFcn = hash<Key>,
template <typename Key, typename HashFcn = hash<Key>,
typename EqualKey = std::equal_to<Key> >
class hash_set : public HASH_NAMESPACE::HASH_SET_CLASS<
Key, HashFcn, EqualKey> {
class hash_set
: public GOOGLE_PROTOBUF_HASH_NAMESPACE::GOOGLE_PROTOBUF_HASH_SET_CLASS<
Key, HashFcn, EqualKey> {
public:
hash_set(int = 0) {}
};

#endif
#undef GOOGLE_PROTOBUF_MISSING_HASH
#endif // !GOOGLE_PROTOBUF_MISSING_HASH

template <>
struct hash<string> {
Expand Down
2 changes: 2 additions & 0 deletions vsprojects/extract_includes.bat
Expand Up @@ -72,6 +72,7 @@ copy ..\src\google\protobuf\stubs\fastmem.h include\google\protobuf\stubs\fastme
copy ..\src\google\protobuf\stubs\once.h include\google\protobuf\stubs\once.h
copy ..\src\google\protobuf\stubs\platform_macros.h include\google\protobuf\stubs\platform_macros.h
copy ..\src\google\protobuf\stubs\singleton.h include\google\protobuf\stubs\singleton.h
copy ..\src\google\protobuf\stubs\hash.h include\google\protobuf\stubs\hash.h
copy ..\src\google\protobuf\stubs\stl_util.h include\google\protobuf\stubs\stl_util.h
copy ..\src\google\protobuf\stubs\template_util.h include\google\protobuf\stubs\template_util.h
copy ..\src\google\protobuf\stubs\type_traits.h include\google\protobuf\stubs\type_traits.h
Expand All @@ -80,3 +81,4 @@ copy ..\src\google\protobuf\unknown_field_set.h include\google\protobuf\unknown_
copy ..\src\google\protobuf\wire_format.h include\google\protobuf\wire_format.h
copy ..\src\google\protobuf\wire_format_lite.h include\google\protobuf\wire_format_lite.h
copy ..\src\google\protobuf\wire_format_lite_inl.h include\google\protobuf\wire_format_lite_inl.h
copy google\protobuf\stubs\pbconfig.h include\google\protobuf\stubs\pbconfig.h
29 changes: 29 additions & 0 deletions vsprojects/google/protobuf/stubs/pbconfig.h
@@ -0,0 +1,29 @@
/* protobuf config.h for MSVC. On other platforms, this is generated
* automatically by autoheader / autoconf / configure. */

/* the location of <hash_map> */
#define GOOGLE_PROTOBUF_HASH_MAP_H <hash_map>

/* the namespace of hash_map/hash_set */
// Apparently Microsoft decided to move hash_map *back* to the std namespace
// in MSVC 2010:
// http://blogs.msdn.com/vcblog/archive/2009/05/25/stl-breaking-changes-in-visual-studio-2010-beta-1.aspx
// TODO(kenton): Use unordered_map instead, which is available in MSVC 2010.
#if _MSC_VER < 1310 || _MSC_VER >= 1600
#define GOOGLE_PROTOBUF_HASH_NAMESPACE std
#else
#define GOOGLE_PROTOBUF_HASH_NAMESPACE stdext
#endif

/* the location of <hash_set> */
#define GOOGLE_PROTOBUF_HASH_SET_H <hash_set>

/* define if the compiler has hash_map */
#define GOOGLE_PROTOBUF_HAVE_HASH_MAP 1

/* define if the compiler has hash_set */
#define GOOGLE_PROTOBUF_HAVE_HASH_SET 1

/* define if you want to use zlib. See readme.txt for additional
* requirements. */
// #define HAVE_ZLIB 1
4 changes: 4 additions & 0 deletions vsprojects/libprotobuf-lite.vcproj
Expand Up @@ -163,6 +163,10 @@
RelativePath=".\config.h"
>
</File>
<File
RelativePath=".\google\protobuf\stubs\pbconfig.h"
>
</File>
<File
RelativePath="..\src\google\protobuf\extension_set.h"
>
Expand Down
4 changes: 4 additions & 0 deletions vsprojects/libprotobuf.vcproj
Expand Up @@ -163,6 +163,10 @@
RelativePath=".\config.h"
>
</File>
<File
RelativePath=".\google\protobuf\stubs\pbconfig.h"
>
</File>
<File
RelativePath="..\src\google\protobuf\descriptor.h"
>
Expand Down

0 comments on commit df184fb

Please sign in to comment.