Commit 9c567060 authored by Kenton Varda's avatar Kenton Varda

Replace JsonCodec STL maps with KJ maps.

parent 842e0d59
This diff is collapsed.
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include "any.h" #include "any.h"
#include <kj/string.h> #include <kj/string.h>
#include <kj/string-tree.h> #include <kj/string-tree.h>
#include <kj/hash.h>
namespace capnp { namespace capnp {
......
...@@ -25,6 +25,12 @@ ...@@ -25,6 +25,12 @@
namespace capnp { namespace capnp {
namespace schema {
uint KJ_HASHCODE(Type::Which w) { return kj::hashCode(static_cast<uint16_t>(w)); }
// TODO(cleanup): Cap'n Proto does not declare stringifiers nor hashers for `Which` enums, unlike
// all other enums. Fix that and remove this.
}
namespace _ { // private namespace _ { // private
// Null schemas generated using the below schema file with: // Null schemas generated using the below schema file with:
...@@ -868,7 +874,7 @@ bool Type::operator==(const Type& other) const { ...@@ -868,7 +874,7 @@ bool Type::operator==(const Type& other) const {
KJ_UNREACHABLE; KJ_UNREACHABLE;
} }
size_t Type::hashCode() const { uint Type::hashCode() const {
switch (baseType) { switch (baseType) {
case schema::Type::VOID: case schema::Type::VOID:
case schema::Type::BOOL: case schema::Type::BOOL:
...@@ -884,12 +890,12 @@ size_t Type::hashCode() const { ...@@ -884,12 +890,12 @@ size_t Type::hashCode() const {
case schema::Type::FLOAT64: case schema::Type::FLOAT64:
case schema::Type::TEXT: case schema::Type::TEXT:
case schema::Type::DATA: case schema::Type::DATA:
return (static_cast<size_t>(baseType) << 3) + listDepth; return kj::hashCode(baseType, listDepth);
case schema::Type::STRUCT: case schema::Type::STRUCT:
case schema::Type::ENUM: case schema::Type::ENUM:
case schema::Type::INTERFACE: case schema::Type::INTERFACE:
return reinterpret_cast<size_t>(schema) + listDepth; return kj::hashCode(schema, listDepth);
case schema::Type::LIST: case schema::Type::LIST:
KJ_UNREACHABLE; KJ_UNREACHABLE;
...@@ -897,9 +903,9 @@ size_t Type::hashCode() const { ...@@ -897,9 +903,9 @@ size_t Type::hashCode() const {
case schema::Type::ANY_POINTER: { case schema::Type::ANY_POINTER: {
// Trying to comply with strict aliasing rules. Hopefully the compiler realizes that // Trying to comply with strict aliasing rules. Hopefully the compiler realizes that
// both branches compile to the same instructions and can optimize it away. // both branches compile to the same instructions and can optimize it away.
size_t val = scopeId != 0 || isImplicitParam ? uint16_t val = scopeId != 0 || isImplicitParam ?
paramIndex : static_cast<uint16_t>(anyPointerKind); paramIndex : static_cast<uint16_t>(anyPointerKind);
return (val << 1 | isImplicitParam) ^ scopeId; return kj::hashCode(val, isImplicitParam, scopeId);
} }
} }
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#endif #endif
#include <capnp/schema.capnp.h> #include <capnp/schema.capnp.h>
#include <kj/hash.h>
#include <kj/windows-sanity.h> // work-around macro conflict with `VOID` #include <kj/windows-sanity.h> // work-around macro conflict with `VOID`
namespace capnp { namespace capnp {
...@@ -129,6 +130,8 @@ public: ...@@ -129,6 +130,8 @@ public:
// you want to check if two Schemas represent the same type (but possibly different versions of // you want to check if two Schemas represent the same type (but possibly different versions of
// it), compare their IDs instead. // it), compare their IDs instead.
inline uint hashCode() const { return kj::hashCode(raw); }
template <typename T> template <typename T>
void requireUsableAs() const; void requireUsableAs() const;
// Throws an exception if a value with this Schema cannot safely be cast to a native value of // Throws an exception if a value with this Schema cannot safely be cast to a native value of
...@@ -302,6 +305,7 @@ public: ...@@ -302,6 +305,7 @@ public:
inline bool operator==(const Field& other) const; inline bool operator==(const Field& other) const;
inline bool operator!=(const Field& other) const { return !(*this == other); } inline bool operator!=(const Field& other) const { return !(*this == other); }
inline uint hashCode() const;
private: private:
StructSchema parent; StructSchema parent;
...@@ -400,6 +404,7 @@ public: ...@@ -400,6 +404,7 @@ public:
inline bool operator==(const Enumerant& other) const; inline bool operator==(const Enumerant& other) const;
inline bool operator!=(const Enumerant& other) const { return !(*this == other); } inline bool operator!=(const Enumerant& other) const { return !(*this == other); }
inline uint hashCode() const;
private: private:
EnumSchema parent; EnumSchema parent;
...@@ -492,6 +497,7 @@ public: ...@@ -492,6 +497,7 @@ public:
inline bool operator==(const Method& other) const; inline bool operator==(const Method& other) const;
inline bool operator!=(const Method& other) const { return !(*this == other); } inline bool operator!=(const Method& other) const { return !(*this == other); }
inline uint hashCode() const;
private: private:
InterfaceSchema parent; InterfaceSchema parent;
...@@ -644,7 +650,7 @@ public: ...@@ -644,7 +650,7 @@ public:
bool operator==(const Type& other) const; bool operator==(const Type& other) const;
inline bool operator!=(const Type& other) const { return !(*this == other); } inline bool operator!=(const Type& other) const { return !(*this == other); }
size_t hashCode() const; uint hashCode() const;
inline Type wrapInList(uint depth = 1) const; inline Type wrapInList(uint depth = 1) const;
// Return the Type formed by wrapping this type in List() `depth` times. // Return the Type formed by wrapping this type in List() `depth` times.
...@@ -796,6 +802,16 @@ inline bool InterfaceSchema::Method::operator==(const Method& other) const { ...@@ -796,6 +802,16 @@ inline bool InterfaceSchema::Method::operator==(const Method& other) const {
return parent == other.parent && ordinal == other.ordinal; return parent == other.parent && ordinal == other.ordinal;
} }
inline uint StructSchema::Field::hashCode() const {
return kj::hashCode(parent, index);
}
inline uint EnumSchema::Enumerant::hashCode() const {
return kj::hashCode(parent, index);
}
inline uint InterfaceSchema::Method::hashCode() const {
return kj::hashCode(parent, index);
}
inline ListSchema ListSchema::of(StructSchema elementType) { inline ListSchema ListSchema::of(StructSchema elementType) {
return ListSchema(Type(elementType)); return ListSchema(Type(elementType));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment