generated-header-support.h 9.65 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 22 23

// This file is included form all generated headers.

Kenton Varda's avatar
Kenton Varda committed
24 25
#ifndef CAPNP_GENERATED_HEADER_SUPPORT_H_
#define CAPNP_GENERATED_HEADER_SUPPORT_H_
26

27
#include "layout.h"
28
#include "list.h"
29
#include "orphan.h"
30
#include "pointer-helpers.h"
31
#include "any.h"
32
#include <kj/string.h>
33
#include <kj/string-tree.h>
34

35
namespace capnp {
36

37 38
class MessageBuilder;  // So that it can be declared a friend.

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

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

44
namespace _ {  // private
45

Kenton Varda's avatar
Kenton Varda committed
46
struct RawSchema {
47 48 49 50
  // The generated code defines a constant RawSchema for every compiled declaration.
  //
  // This is an internal structure which could change in the future.

51 52
  uint64_t id;

Kenton Varda's avatar
Kenton Varda committed
53
  const word* encodedNode;
54
  // Encoded SchemaNode, readable via readMessageUnchecked<schema::Node>(encodedNode).
55

Kenton Varda's avatar
Kenton Varda committed
56 57 58
  uint32_t encodedSize;
  // Size of encodedNode, in words.

59
  const RawSchema* const* dependencies;
60 61 62 63
  // Pointers to other types on which this one depends, sorted by ID.  The schemas in this table
  // may be uninitialized -- you must call ensureInitialized() on the one you wish to use before
  // using it.
  //
64 65
  // TODO(someday):  Make this a hashtable.

66
  const uint16_t* membersByName;
67 68 69 70 71 72
  // Indexes of members sorted by name.  Used to implement name lookup.
  // TODO(someday):  Make this a hashtable.

  uint32_t dependencyCount;
  uint32_t memberCount;
  // Sizes of above tables.
73

Kenton Varda's avatar
Kenton Varda committed
74 75 76 77
  const uint16_t* membersByDiscriminant;
  // List of all member indexes ordered by discriminant value.  Those which don't have a
  // discriminant value are listed at the end, in order by ordinal.

78 79 80 81
  const RawSchema* canCastTo;
  // Points to the RawSchema of a compiled-in type to which it is safe to cast any DynamicValue
  // with this schema.  This is null for all compiled-in types; it is only set by SchemaLoader on
  // dynamically-loaded types.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

  class Initializer {
  public:
    virtual void init(const RawSchema* schema) const = 0;
  };

  const Initializer* lazyInitializer;
  // Lazy initializer, invoked by ensureInitialized().

  inline void ensureInitialized() const {
    // Lazy initialization support.  Invoke to ensure that initialization has taken place.  This
    // is required in particular when traversing the dependency list.  RawSchemas for compiled-in
    // types are always initialized; only dynamically-loaded schemas may be lazy.

    const Initializer* i = __atomic_load_n(&lazyInitializer, __ATOMIC_ACQUIRE);
    if (i != nullptr) i->init(this);
  }
Kenton Varda's avatar
Kenton Varda committed
99 100 101
};

template <typename T>
102
struct RawSchema_;
103 104 105

template <typename T>
inline const RawSchema& rawSchema() {
106
  return RawSchema_<T>::get();
107 108
}

109 110 111 112 113 114 115
template <typename T> struct TypeId_;

extern const RawSchema NULL_INTERFACE_SCHEMA;  // defined in schema.c++
template <> struct TypeId_<Capability> { static constexpr uint64_t typeId = 0x03; };
template <> struct RawSchema_<Capability> {
  static inline const RawSchema& get() { return NULL_INTERFACE_SCHEMA; }
};
Kenton Varda's avatar
Kenton Varda committed
116

Kenton Varda's avatar
Kenton Varda committed
117
template <typename T>
118
struct UnionMemberIndex_;
Kenton Varda's avatar
Kenton Varda committed
119
template <typename T>
120
inline uint unionMemberIndex() { return UnionMemberIndex_<T>::value; }
Kenton Varda's avatar
Kenton Varda committed
121 122

template <typename T>
123
struct UnionParentType_;
Kenton Varda's avatar
Kenton Varda committed
124
template <typename T>
125
using UnionParentType = typename UnionParentType_<T>::Type;
Kenton Varda's avatar
Kenton Varda committed
126

127
kj::StringTree structString(StructReader reader, const RawSchema& schema);
Kenton Varda's avatar
Kenton Varda committed
128
// Declared here so that we can declare inline stringify methods on generated types.
129 130 131
// Defined in stringify.c++, which depends on dynamic.c++, which is allowed not to be linked in.

template <typename T>
132
inline kj::StringTree structString(StructReader reader) {
Kenton Varda's avatar
Kenton Varda committed
133 134 135
  return structString(reader, rawSchema<T>());
}

136
// TODO(cleanup):  Unify ConstStruct and ConstList.
137 138 139 140 141 142 143 144
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 {
145
    return AnyPointer::Reader(PointerReader::getRootUnchecked(ptr)).getAs<T>();
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  }

  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 {
164
    return AnyPointer::Reader(PointerReader::getRootUnchecked(ptr)).getAs<List<T>>();
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  }

  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(); }

private:
  const word* ptr;
};

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;
};

213
}  // namespace _ (private)
214 215

template <typename T>
216
inline constexpr uint64_t typeId() { return _::TypeId_<T>::typeId; }
217 218 219
// typeId<MyType>() returns the type ID as defined in the schema.  Works with structs, enums, and
// interfaces.

Kenton Varda's avatar
Kenton Varda committed
220 221 222 223 224 225 226 227 228
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.

  return (WordCount32(_::structSize<T>().data) +
      _::structSize<T>().pointers * WORDS_PER_POINTER) / WORDS;
}

229
}  // namespace capnp
230

Kenton Varda's avatar
Kenton Varda committed
231
#define CAPNP_DECLARE_ENUM(type, id) \
232 233 234 235
    template <> struct Kind_<type> { static constexpr Kind kind = Kind::ENUM; }; \
    template <> struct TypeId_<type> { static constexpr uint64_t typeId = 0x##id; }; \
    template <> struct RawSchema_<type> { \
      static inline const RawSchema& get() { return schemas::s_##id; } \
236
    }
Kenton Varda's avatar
Kenton Varda committed
237
#define CAPNP_DEFINE_ENUM(type) \
238
    constexpr Kind Kind_<type>::kind; \
239
    constexpr uint64_t TypeId_<type>::typeId
Kenton Varda's avatar
Kenton Varda committed
240

Kenton Varda's avatar
Kenton Varda committed
241
#define CAPNP_DECLARE_STRUCT(type, id, dataWordSize, pointerCount, preferredElementEncoding) \
242 243
    template <> struct Kind_<type> { static constexpr Kind kind = Kind::STRUCT; }; \
    template <> struct StructSize_<type> { \
244
      static constexpr StructSize value = StructSize( \
245
          dataWordSize * WORDS, pointerCount * POINTERS, FieldSize::preferredElementEncoding); \
246
    }; \
247 248 249
    template <> struct TypeId_<type> { static constexpr uint64_t typeId = 0x##id; }; \
    template <> struct RawSchema_<type> { \
      static inline const RawSchema& get() { return schemas::s_##id; } \
250
    }
Kenton Varda's avatar
Kenton Varda committed
251
#define CAPNP_DEFINE_STRUCT(type) \
252 253
    constexpr Kind Kind_<type>::kind; \
    constexpr StructSize StructSize_<type>::value; \
254
    constexpr uint64_t TypeId_<type>::typeId
Kenton Varda's avatar
Kenton Varda committed
255

Kenton Varda's avatar
Kenton Varda committed
256
#define CAPNP_DECLARE_UNION(type, parentType, memberIndex) \
257 258 259
    template <> struct Kind_<type> { static constexpr Kind kind = Kind::UNION; }; \
    template <> struct UnionMemberIndex_<type> { static constexpr uint value = memberIndex; }; \
    template <> struct UnionParentType_<type> { typedef parentType Type; }
Kenton Varda's avatar
Kenton Varda committed
260
#define CAPNP_DEFINE_UNION(type) \
261
    constexpr Kind Kind_<type>::kind; \
262
    constexpr uint UnionMemberIndex_<type>::value
Kenton Varda's avatar
Kenton Varda committed
263

Kenton Varda's avatar
Kenton Varda committed
264
#define CAPNP_DECLARE_INTERFACE(type, id) \
265 266 267 268
    template <> struct Kind_<type> { static constexpr Kind kind = Kind::INTERFACE; }; \
    template <> struct TypeId_<type> { static constexpr uint64_t typeId = 0x##id; }; \
    template <> struct RawSchema_<type> { \
      static inline const RawSchema& get() { return schemas::s_##id; } \
269
    }
Kenton Varda's avatar
Kenton Varda committed
270
#define CAPNP_DEFINE_INTERFACE(type) \
271
    constexpr Kind Kind_<type>::kind; \
272
    constexpr uint64_t TypeId_<type>::typeId
273

Kenton Varda's avatar
Kenton Varda committed
274
#endif  // CAPNP_GENERATED_HEADER_SUPPORT_H_