pointer-helpers.h 6.36 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 24

#ifndef CAPNP_POINTER_HELPERS_H_
#define CAPNP_POINTER_HELPERS_H_

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

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include "layout.h"
#include "list.h"

namespace capnp {
namespace _ {  // private

// PointerHelpers is a template class that assists in wrapping/unwrapping the low-level types in
// layout.h with the high-level public API and generated types.  This way, the code generator
// and other templates do not have to specialize on each kind of pointer.

template <typename T>
struct PointerHelpers<T, Kind::STRUCT> {
  static inline typename T::Reader get(PointerReader reader, const word* defaultValue = nullptr) {
    return typename T::Reader(reader.getStruct(defaultValue));
  }
  static inline typename T::Builder get(PointerBuilder builder,
                                        const word* defaultValue = nullptr) {
    return typename T::Builder(builder.getStruct(structSize<T>(), defaultValue));
  }
  static inline void set(PointerBuilder builder, typename T::Reader value) {
    builder.setStruct(value._reader);
  }
Matthew Maurer's avatar
Matthew Maurer committed
51 52 53
  static inline void setCanonical(PointerBuilder builder, typename T::Reader value) {
    builder.setStruct(value._reader, true);
  }
54 55 56 57 58 59 60 61 62
  static inline typename T::Builder init(PointerBuilder builder) {
    return typename T::Builder(builder.initStruct(structSize<T>()));
  }
  static inline void adopt(PointerBuilder builder, Orphan<T>&& value) {
    builder.adopt(kj::mv(value.builder));
  }
  static inline Orphan<T> disown(PointerBuilder builder) {
    return Orphan<T>(builder.disown());
  }
63 64 65
  static inline _::StructReader getInternalReader(const typename T::Reader& reader) {
    return reader._reader;
  }
66 67 68
  static inline _::StructBuilder getInternalBuilder(typename T::Builder&& builder) {
    return builder._builder;
  }
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
};

template <typename T>
struct PointerHelpers<List<T>, Kind::LIST> {
  static inline typename List<T>::Reader get(PointerReader reader,
                                             const word* defaultValue = nullptr) {
    return typename List<T>::Reader(List<T>::getFromPointer(reader, defaultValue));
  }
  static inline typename List<T>::Builder get(PointerBuilder builder,
                                              const word* defaultValue = nullptr) {
    return typename List<T>::Builder(List<T>::getFromPointer(builder, defaultValue));
  }
  static inline void set(PointerBuilder builder, typename List<T>::Reader value) {
    builder.setList(value.reader);
  }
Matthew Maurer's avatar
Matthew Maurer committed
84 85 86
  static inline void setCanonical(PointerBuilder builder, typename List<T>::Reader value) {
    builder.setList(value.reader, true);
  }
87
  static void set(PointerBuilder builder, kj::ArrayPtr<const ReaderFor<T>> value) {
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    auto l = init(builder, value.size());
    uint i = 0;
    for (auto& element: value) {
      l.set(i++, element);
    }
  }
  static inline typename List<T>::Builder init(PointerBuilder builder, uint size) {
    return typename List<T>::Builder(List<T>::initPointer(builder, size));
  }
  static inline void adopt(PointerBuilder builder, Orphan<List<T>>&& value) {
    builder.adopt(kj::mv(value.builder));
  }
  static inline Orphan<List<T>> disown(PointerBuilder builder) {
    return Orphan<List<T>>(builder.disown());
  }
103 104 105 106 107 108
  static inline _::ListReader getInternalReader(const typename List<T>::Reader& reader) {
    return reader.reader;
  }
  static inline _::ListBuilder getInternalBuilder(typename List<T>::Builder&& builder) {
    return builder.builder;
  }
109 110 111 112 113 114 115
};

template <typename T>
struct PointerHelpers<T, Kind::BLOB> {
  static inline typename T::Reader get(PointerReader reader,
                                       const void* defaultValue = nullptr,
                                       uint defaultBytes = 0) {
116
    return reader.getBlob<T>(defaultValue, bounded(defaultBytes) * BYTES);
117 118 119 120
  }
  static inline typename T::Builder get(PointerBuilder builder,
                                        const void* defaultValue = nullptr,
                                        uint defaultBytes = 0) {
121
    return builder.getBlob<T>(defaultValue, bounded(defaultBytes) * BYTES);
122 123
  }
  static inline void set(PointerBuilder builder, typename T::Reader value) {
Matthew Maurer's avatar
Matthew Maurer committed
124 125 126
    builder.setBlob<T>(value);
  }
  static inline void setCanonical(PointerBuilder builder, typename T::Reader value) {
127 128 129
    builder.setBlob<T>(value);
  }
  static inline typename T::Builder init(PointerBuilder builder, uint size) {
130
    return builder.initBlob<T>(bounded(size) * BYTES);
131 132 133 134 135 136 137 138 139 140 141 142 143
  }
  static inline void adopt(PointerBuilder builder, Orphan<T>&& value) {
    builder.adopt(kj::mv(value.builder));
  }
  static inline Orphan<T> disown(PointerBuilder builder) {
    return Orphan<T>(builder.disown());
  }
};

struct UncheckedMessage {
  typedef const word* Reader;
};

144 145
template <> struct Kind_<UncheckedMessage> { static constexpr Kind kind = Kind::OTHER; };

146 147
template <>
struct PointerHelpers<UncheckedMessage> {
148 149 150
  // Reads an AnyPointer field as an unchecked message pointer.  Requires that the containing
  // message is itself unchecked.  This hack is currently private.  It is used to locate default
  // values within encoded schemas.
151 152 153 154 155 156 157 158 159 160

  static inline const word* get(PointerReader reader) {
    return reader.getUnchecked();
  }
};

}  // namespace _ (private)
}  // namespace capnp

#endif  // CAPNP_POINTER_HELPERS_H_