generated-header-support.h 13.3 KB
Newer Older
Kenton Varda's avatar
Kenton Varda committed
1 2
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
3
//
Kenton Varda's avatar
Kenton Varda committed
4 5 6 7 8 9
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
10
//
Kenton Varda's avatar
Kenton Varda committed
11 12
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
13
//
Kenton Varda's avatar
Kenton Varda committed
14 15 16 17 18 19 20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
21

Harris Hancock's avatar
Harris Hancock committed
22
// This file is included from all generated headers.
23

24
#pragma once
25

26
#if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
27 28 29
#pragma GCC system_header
#endif

30
#include "raw-schema.h"
31
#include "layout.h"
32
#include "list.h"
33
#include "orphan.h"
34
#include "pointer-helpers.h"
35
#include "any.h"
36
#include <kj/string.h>
37
#include <kj/string-tree.h>
38
#include <kj/hash.h>
39

40
namespace capnp {
41

42 43
class MessageBuilder;  // So that it can be declared a friend.

44
template <typename T, Kind k = CAPNP_KIND(T)>
45
struct ToDynamic_;   // Defined in dynamic.h, needs to be declared as everyone's friend.
Kenton Varda's avatar
Kenton Varda committed
46

Kenton Varda's avatar
Kenton Varda committed
47
struct DynamicStruct;  // So that it can be declared a friend.
48

49 50
struct Capability;  // To declare brandBindingFor<Capability>()

51
namespace _ {  // private
52

53 54
#if !CAPNP_LITE

55 56 57 58 59
template <typename T, typename CapnpPrivate = typename T::_capnpPrivate, bool = false>
inline const RawSchema& rawSchema() {
  return *CapnpPrivate::schema;
}
template <typename T, uint64_t id = schemas::EnumInfo<T>::typeId>
60
inline const RawSchema& rawSchema() {
61
  return *schemas::EnumInfo<T>::schema;
62 63
}

64
template <typename T, typename CapnpPrivate = typename T::_capnpPrivate>
65
inline const RawBrandedSchema& rawBrandedSchema() {
66
  return *CapnpPrivate::brand();
67 68 69 70 71 72 73 74 75 76 77 78 79 80
}
template <typename T, uint64_t id = schemas::EnumInfo<T>::typeId>
inline const RawBrandedSchema& rawBrandedSchema() {
  return schemas::EnumInfo<T>::schema->defaultBrand;
}

template <typename TypeTag, typename... Params>
struct ChooseBrand;
// If all of `Params` are `AnyPointer`, return the type's default brand. Otherwise, return a
// specific brand instance. TypeTag is the _capnpPrivate struct for the type in question.

template <typename TypeTag>
struct ChooseBrand<TypeTag> {
  // All params were AnyPointer. No specific brand needed.
81
  static constexpr _::RawBrandedSchema const* brand() { return &TypeTag::schema->defaultBrand; }
82 83 84 85 86 87 88 89 90
};

template <typename TypeTag, typename... Rest>
struct ChooseBrand<TypeTag, AnyPointer, Rest...>: public ChooseBrand<TypeTag, Rest...> {};
// The first parameter is AnyPointer, so recurse to check the rest.

template <typename TypeTag, typename First, typename... Rest>
struct ChooseBrand<TypeTag, First, Rest...> {
  // At least one parameter is not AnyPointer, so use the specificBrand constant.
91
  static constexpr _::RawBrandedSchema const* brand() { return &TypeTag::specificBrand; }
92 93 94 95 96 97 98 99 100
};

template <typename T, Kind k = kind<T>()>
struct BrandBindingFor_;

#define HANDLE_TYPE(Type, which) \
  template <> \
  struct BrandBindingFor_<Type, Kind::PRIMITIVE> { \
    static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) { \
101
      return { which, listDepth, nullptr }; \
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    } \
  }
HANDLE_TYPE(Void, 0);
HANDLE_TYPE(bool, 1);
HANDLE_TYPE(int8_t, 2);
HANDLE_TYPE(int16_t, 3);
HANDLE_TYPE(int32_t, 4);
HANDLE_TYPE(int64_t, 5);
HANDLE_TYPE(uint8_t, 6);
HANDLE_TYPE(uint16_t, 7);
HANDLE_TYPE(uint32_t, 8);
HANDLE_TYPE(uint64_t, 9);
HANDLE_TYPE(float, 10);
HANDLE_TYPE(double, 11);
#undef HANDLE_TYPE

template <>
struct BrandBindingFor_<Text, Kind::BLOB> {
  static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
    return { 12, listDepth, nullptr };
  }
};

template <>
struct BrandBindingFor_<Data, Kind::BLOB> {
  static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
    return { 13, listDepth, nullptr };
  }
};

template <typename T>
struct BrandBindingFor_<List<T>, Kind::LIST> {
  static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
    return BrandBindingFor_<T>::get(listDepth + 1);
  }
};

template <typename T>
struct BrandBindingFor_<T, Kind::ENUM> {
  static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
    return { 15, listDepth, nullptr };
  }
};

template <typename T>
struct BrandBindingFor_<T, Kind::STRUCT> {
  static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
149
    return { 16, listDepth, T::_capnpPrivate::brand() };
150 151 152 153 154 155
  }
};

template <typename T>
struct BrandBindingFor_<T, Kind::INTERFACE> {
  static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
156
    return { 17, listDepth, T::_capnpPrivate::brand() };
157 158 159 160 161 162 163 164 165 166
  }
};

template <>
struct BrandBindingFor_<AnyPointer, Kind::OTHER> {
  static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
    return { 18, listDepth, 0, 0 };
  }
};

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
template <>
struct BrandBindingFor_<AnyStruct, Kind::OTHER> {
  static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
    return { 18, listDepth, 0, 1 };
  }
};

template <>
struct BrandBindingFor_<AnyList, Kind::OTHER> {
  static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
    return { 18, listDepth, 0, 2 };
  }
};

template <>
struct BrandBindingFor_<Capability, Kind::OTHER> {
  static constexpr RawBrandedSchema::Binding get(uint16_t listDepth) {
    return { 18, listDepth, 0, 3 };
  }
};

188 189 190
template <typename T>
constexpr RawBrandedSchema::Binding brandBindingFor() {
  return BrandBindingFor_<T>::get(0);
191 192 193
}

kj::StringTree structString(StructReader reader, const RawBrandedSchema& schema);
194
kj::String enumString(uint16_t value, const RawBrandedSchema& schema);
Kenton Varda's avatar
Kenton Varda committed
195
// Declared here so that we can declare inline stringify methods on generated types.
196 197 198
// Defined in stringify.c++, which depends on dynamic.c++, which is allowed not to be linked in.

template <typename T>
199
inline kj::StringTree structString(StructReader reader) {
200
  return structString(reader, rawBrandedSchema<T>());
Kenton Varda's avatar
Kenton Varda committed
201
}
202 203 204 205
template <typename T>
inline kj::String enumString(T value) {
  return enumString(static_cast<uint16_t>(value), rawBrandedSchema<T>());
}
Kenton Varda's avatar
Kenton Varda committed
206

207 208
#endif  // !CAPNP_LITE

209
// TODO(cleanup):  Unify ConstStruct and ConstList.
210 211 212 213 214 215 216 217
template <typename T>
class ConstStruct {
public:
  ConstStruct() = delete;
  KJ_DISALLOW_COPY(ConstStruct);
  inline explicit constexpr ConstStruct(const word* ptr): ptr(ptr) {}

  inline typename T::Reader get() const {
218
    return AnyPointer::Reader(PointerReader::getRootUnchecked(ptr)).getAs<T>();
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  }

  inline operator typename T::Reader() const { return get(); }
  inline typename T::Reader operator*() const { return get(); }
  inline TemporaryPointer<typename T::Reader> operator->() const { return get(); }

private:
  const word* ptr;
};

template <typename T>
class ConstList {
public:
  ConstList() = delete;
  KJ_DISALLOW_COPY(ConstList);
  inline explicit constexpr ConstList(const word* ptr): ptr(ptr) {}

  inline typename List<T>::Reader get() const {
237
    return AnyPointer::Reader(PointerReader::getRootUnchecked(ptr)).getAs<List<T>>();
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
  }

  inline operator typename List<T>::Reader() const { return get(); }
  inline typename List<T>::Reader operator*() const { return get(); }
  inline TemporaryPointer<typename List<T>::Reader> operator->() const { return get(); }

private:
  const word* ptr;
};

template <size_t size>
class ConstText {
public:
  ConstText() = delete;
  KJ_DISALLOW_COPY(ConstText);
  inline explicit constexpr ConstText(const word* ptr): ptr(ptr) {}

  inline Text::Reader get() const {
    return Text::Reader(reinterpret_cast<const char*>(ptr), size);
  }

  inline operator Text::Reader() const { return get(); }
  inline Text::Reader operator*() const { return get(); }
  inline TemporaryPointer<Text::Reader> operator->() const { return get(); }

263 264 265 266
  inline kj::StringPtr toString() const {
    return get();
  }

267 268 269 270
private:
  const word* ptr;
};

271 272 273 274 275
template <size_t size>
inline kj::StringPtr KJ_STRINGIFY(const ConstText<size>& s) {
  return s.get();
}

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
template <size_t size>
class ConstData {
public:
  ConstData() = delete;
  KJ_DISALLOW_COPY(ConstData);
  inline explicit constexpr ConstData(const word* ptr): ptr(ptr) {}

  inline Data::Reader get() const {
    return Data::Reader(reinterpret_cast<const byte*>(ptr), size);
  }

  inline operator Data::Reader() const { return get(); }
  inline Data::Reader operator*() const { return get(); }
  inline TemporaryPointer<Data::Reader> operator->() const { return get(); }

private:
  const word* ptr;
};

295 296 297 298 299
template <size_t size>
inline auto KJ_STRINGIFY(const ConstData<size>& s) -> decltype(kj::toCharSequence(s.get())) {
  return kj::toCharSequence(s.get());
}

300
}  // namespace _ (private)
301

302 303
template <typename T, typename CapnpPrivate = typename T::_capnpPrivate>
inline constexpr uint64_t typeId() { return CapnpPrivate::typeId; }
304
template <typename T, uint64_t id = schemas::EnumInfo<T>::typeId>
305
inline constexpr uint64_t typeId() { return id; }
306 307 308
// typeId<MyType>() returns the type ID as defined in the schema.  Works with structs, enums, and
// interfaces.

Kenton Varda's avatar
Kenton Varda committed
309 310 311 312 313
template <typename T>
inline constexpr uint sizeInWords() {
  // Return the size, in words, of a Struct type, if allocated free-standing (not in a list).
  // May be useful for pre-computing space needed in order to precisely allocate messages.

314
  return unbound((upgradeBound<uint>(_::structSize<T>().data) +
315
      _::structSize<T>().pointers * WORDS_PER_POINTER) / WORDS);
Kenton Varda's avatar
Kenton Varda committed
316 317
}

318
}  // namespace capnp
319

320 321 322 323 324 325 326 327 328 329 330
#if _MSC_VER
// MSVC doesn't understand floating-point constexpr yet.
//
// TODO(msvc): Remove this hack when MSVC is fixed.
#define CAPNP_NON_INT_CONSTEXPR_DECL_INIT(value)
#define CAPNP_NON_INT_CONSTEXPR_DEF_INIT(value) = value
#else
#define CAPNP_NON_INT_CONSTEXPR_DECL_INIT(value) = value
#define CAPNP_NON_INT_CONSTEXPR_DEF_INIT(value)
#endif

331 332 333 334 335 336 337 338
#if _MSC_VER
// TODO(msvc): A little hack to allow MSVC to use C++14 return type deduction in cases where the
// explicit type exposes bugs in the compiler.
#define CAPNP_AUTO_IF_MSVC(...) auto
#else
#define CAPNP_AUTO_IF_MSVC(...) __VA_ARGS__
#endif

339 340 341 342 343
#if CAPNP_LITE

#define CAPNP_DECLARE_SCHEMA(id) \
    extern ::capnp::word const* const bp_##id

Kenton Varda's avatar
Kenton Varda committed
344
#define CAPNP_DECLARE_ENUM(type, id) \
345 346 347
    inline ::kj::String KJ_STRINGIFY(type##_##id value) { \
      return ::kj::str(static_cast<uint16_t>(value)); \
    } \
348
    template <> struct EnumInfo<type##_##id> { \
349 350
      struct IsEnum; \
      static constexpr uint64_t typeId = 0x##id; \
351
      static inline ::capnp::word const* encodedSchema() { return bp_##id; } \
352
    }
353 354 355 356 357

#if _MSC_VER
// TODO(msvc): MSVC dosen't expect constexprs to have definitions.
#define CAPNP_DEFINE_ENUM(type, id)
#else
358 359
#define CAPNP_DEFINE_ENUM(type, id) \
    constexpr uint64_t EnumInfo<type>::typeId
360
#endif
361

362
#define CAPNP_DECLARE_STRUCT_HEADER(id, dataWordSize_, pointerCount_) \
363
      struct IsStruct; \
364
      static constexpr uint64_t typeId = 0x##id; \
365 366
      static constexpr uint16_t dataWordSize = dataWordSize_; \
      static constexpr uint16_t pointerCount = pointerCount_; \
367
      static inline ::capnp::word const* encodedSchema() { return ::capnp::schemas::bp_##id; }
368 369 370 371 372 373 374 375

#else  // CAPNP_LITE

#define CAPNP_DECLARE_SCHEMA(id) \
    extern ::capnp::word const* const bp_##id; \
    extern const ::capnp::_::RawSchema s_##id

#define CAPNP_DECLARE_ENUM(type, id) \
376 377 378
    inline ::kj::String KJ_STRINGIFY(type##_##id value) { \
      return ::capnp::_::enumString(value); \
    } \
379 380 381
    template <> struct EnumInfo<type##_##id> { \
      struct IsEnum; \
      static constexpr uint64_t typeId = 0x##id; \
382
      static inline ::capnp::word const* encodedSchema() { return bp_##id; } \
383
      static constexpr ::capnp::_::RawSchema const* schema = &s_##id; \
384
    }
385
#define CAPNP_DEFINE_ENUM(type, id) \
386 387
    constexpr uint64_t EnumInfo<type>::typeId; \
    constexpr ::capnp::_::RawSchema const* EnumInfo<type>::schema
388

389
#define CAPNP_DECLARE_STRUCT_HEADER(id, dataWordSize_, pointerCount_) \
390
      struct IsStruct; \
391 392
      static constexpr uint64_t typeId = 0x##id; \
      static constexpr ::capnp::Kind kind = ::capnp::Kind::STRUCT; \
393 394
      static constexpr uint16_t dataWordSize = dataWordSize_; \
      static constexpr uint16_t pointerCount = pointerCount_; \
395
      static inline ::capnp::word const* encodedSchema() { return ::capnp::schemas::bp_##id; } \
396
      static constexpr ::capnp::_::RawSchema const* schema = &::capnp::schemas::s_##id;
397

398
#define CAPNP_DECLARE_INTERFACE_HEADER(id) \
399
      struct IsInterface; \
400 401
      static constexpr uint64_t typeId = 0x##id; \
      static constexpr ::capnp::Kind kind = ::capnp::Kind::INTERFACE; \
402
      static inline ::capnp::word const* encodedSchema() { return ::capnp::schemas::bp_##id; } \
403
      static constexpr ::capnp::_::RawSchema const* schema = &::capnp::schemas::s_##id;
404

405
#endif  // CAPNP_LITE, else
406 407 408 409 410

namespace capnp {
namespace schemas {
CAPNP_DECLARE_SCHEMA(995f9a3377c0b16e);
// HACK: Forward-declare the RawSchema for StreamResult, from stream.capnp. This allows capnp
Kenton Varda's avatar
Kenton Varda committed
411
//   files which declare streaming methods to avoid including stream.capnp.h.
412 413
}
}