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