Chris@64: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@64: // Licensed under the MIT License: Chris@64: // Chris@64: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@64: // of this software and associated documentation files (the "Software"), to deal Chris@64: // in the Software without restriction, including without limitation the rights Chris@64: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@64: // copies of the Software, and to permit persons to whom the Software is Chris@64: // furnished to do so, subject to the following conditions: Chris@64: // Chris@64: // The above copyright notice and this permission notice shall be included in Chris@64: // all copies or substantial portions of the Software. Chris@64: // Chris@64: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@64: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@64: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@64: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@64: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@64: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@64: // THE SOFTWARE. Chris@64: Chris@64: #ifndef CAPNP_POINTER_HELPERS_H_ Chris@64: #define CAPNP_POINTER_HELPERS_H_ Chris@64: Chris@64: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) Chris@64: #pragma GCC system_header Chris@64: #endif Chris@64: Chris@64: #include "layout.h" Chris@64: #include "list.h" Chris@64: Chris@64: namespace capnp { Chris@64: namespace _ { // private Chris@64: Chris@64: // PointerHelpers is a template class that assists in wrapping/unwrapping the low-level types in Chris@64: // layout.h with the high-level public API and generated types. This way, the code generator Chris@64: // and other templates do not have to specialize on each kind of pointer. Chris@64: Chris@64: template Chris@64: struct PointerHelpers { Chris@64: static inline typename T::Reader get(PointerReader reader, const word* defaultValue = nullptr) { Chris@64: return typename T::Reader(reader.getStruct(defaultValue)); Chris@64: } Chris@64: static inline typename T::Builder get(PointerBuilder builder, Chris@64: const word* defaultValue = nullptr) { Chris@64: return typename T::Builder(builder.getStruct(structSize(), defaultValue)); Chris@64: } Chris@64: static inline void set(PointerBuilder builder, typename T::Reader value) { Chris@64: builder.setStruct(value._reader); Chris@64: } Chris@64: static inline void setCanonical(PointerBuilder builder, typename T::Reader value) { Chris@64: builder.setStruct(value._reader, true); Chris@64: } Chris@64: static inline typename T::Builder init(PointerBuilder builder) { Chris@64: return typename T::Builder(builder.initStruct(structSize())); Chris@64: } Chris@64: static inline void adopt(PointerBuilder builder, Orphan&& value) { Chris@64: builder.adopt(kj::mv(value.builder)); Chris@64: } Chris@64: static inline Orphan disown(PointerBuilder builder) { Chris@64: return Orphan(builder.disown()); Chris@64: } Chris@64: static inline _::StructReader getInternalReader(const typename T::Reader& reader) { Chris@64: return reader._reader; Chris@64: } Chris@64: static inline _::StructBuilder getInternalBuilder(typename T::Builder&& builder) { Chris@64: return builder._builder; Chris@64: } Chris@64: }; Chris@64: Chris@64: template Chris@64: struct PointerHelpers, Kind::LIST> { Chris@64: static inline typename List::Reader get(PointerReader reader, Chris@64: const word* defaultValue = nullptr) { Chris@64: return typename List::Reader(List::getFromPointer(reader, defaultValue)); Chris@64: } Chris@64: static inline typename List::Builder get(PointerBuilder builder, Chris@64: const word* defaultValue = nullptr) { Chris@64: return typename List::Builder(List::getFromPointer(builder, defaultValue)); Chris@64: } Chris@64: static inline void set(PointerBuilder builder, typename List::Reader value) { Chris@64: builder.setList(value.reader); Chris@64: } Chris@64: static inline void setCanonical(PointerBuilder builder, typename List::Reader value) { Chris@64: builder.setList(value.reader, true); Chris@64: } Chris@64: static void set(PointerBuilder builder, kj::ArrayPtr> value) { Chris@64: auto l = init(builder, value.size()); Chris@64: uint i = 0; Chris@64: for (auto& element: value) { Chris@64: l.set(i++, element); Chris@64: } Chris@64: } Chris@64: static inline typename List::Builder init(PointerBuilder builder, uint size) { Chris@64: return typename List::Builder(List::initPointer(builder, size)); Chris@64: } Chris@64: static inline void adopt(PointerBuilder builder, Orphan>&& value) { Chris@64: builder.adopt(kj::mv(value.builder)); Chris@64: } Chris@64: static inline Orphan> disown(PointerBuilder builder) { Chris@64: return Orphan>(builder.disown()); Chris@64: } Chris@64: static inline _::ListReader getInternalReader(const typename List::Reader& reader) { Chris@64: return reader.reader; Chris@64: } Chris@64: static inline _::ListBuilder getInternalBuilder(typename List::Builder&& builder) { Chris@64: return builder.builder; Chris@64: } Chris@64: }; Chris@64: Chris@64: template Chris@64: struct PointerHelpers { Chris@64: static inline typename T::Reader get(PointerReader reader, Chris@64: const void* defaultValue = nullptr, Chris@64: uint defaultBytes = 0) { Chris@64: return reader.getBlob(defaultValue, bounded(defaultBytes) * BYTES); Chris@64: } Chris@64: static inline typename T::Builder get(PointerBuilder builder, Chris@64: const void* defaultValue = nullptr, Chris@64: uint defaultBytes = 0) { Chris@64: return builder.getBlob(defaultValue, bounded(defaultBytes) * BYTES); Chris@64: } Chris@64: static inline void set(PointerBuilder builder, typename T::Reader value) { Chris@64: builder.setBlob(value); Chris@64: } Chris@64: static inline void setCanonical(PointerBuilder builder, typename T::Reader value) { Chris@64: builder.setBlob(value); Chris@64: } Chris@64: static inline typename T::Builder init(PointerBuilder builder, uint size) { Chris@64: return builder.initBlob(bounded(size) * BYTES); Chris@64: } Chris@64: static inline void adopt(PointerBuilder builder, Orphan&& value) { Chris@64: builder.adopt(kj::mv(value.builder)); Chris@64: } Chris@64: static inline Orphan disown(PointerBuilder builder) { Chris@64: return Orphan(builder.disown()); Chris@64: } Chris@64: }; Chris@64: Chris@64: struct UncheckedMessage { Chris@64: typedef const word* Reader; Chris@64: }; Chris@64: Chris@64: template <> struct Kind_ { static constexpr Kind kind = Kind::OTHER; }; Chris@64: Chris@64: template <> Chris@64: struct PointerHelpers { Chris@64: // Reads an AnyPointer field as an unchecked message pointer. Requires that the containing Chris@64: // message is itself unchecked. This hack is currently private. It is used to locate default Chris@64: // values within encoded schemas. Chris@64: Chris@64: static inline const word* get(PointerReader reader) { Chris@64: return reader.getUnchecked(); Chris@64: } Chris@64: }; Chris@64: Chris@64: } // namespace _ (private) Chris@64: } // namespace capnp Chris@64: Chris@64: #endif // CAPNP_POINTER_HELPERS_H_