/usr/include/capnp/any.h is in libcapnp-dev 0.4.0-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | // Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef CAPNP_ANY_H_
#define CAPNP_ANY_H_
#include "layout.h"
#include "pointer-helpers.h"
#include "orphan.h"
namespace capnp {
class StructSchema;
class ListSchema;
class InterfaceSchema;
class Orphanage;
class ClientHook;
class PipelineHook;
struct PipelineOp;
// =======================================================================================
// AnyPointer!
struct AnyPointer {
// Reader/Builder for the `AnyPointer` field type, i.e. a pointer that can point to an arbitrary
// object.
class Reader {
public:
typedef AnyPointer Reads;
Reader() = default;
inline Reader(_::PointerReader reader): reader(reader) {}
inline MessageSize targetSize() const;
// Get the total size of the target object and all its children.
inline bool isNull() const;
template <typename T>
inline ReaderFor<T> getAs() const;
// Valid for T = any generated struct type, interface type, List<U>, Text, or Data.
template <typename T>
inline ReaderFor<T> getAs(StructSchema schema) const;
// Only valid for T = DynamicStruct. Requires `#include <capnp/dynamic.h>`.
template <typename T>
inline ReaderFor<T> getAs(ListSchema schema) const;
// Only valid for T = DynamicList. Requires `#include <capnp/dynamic.h>`.
template <typename T>
inline ReaderFor<T> getAs(InterfaceSchema schema) const;
// Only valid for T = DynamicCapability. Requires `#include <capnp/dynamic.h>`.
kj::Own<ClientHook> getPipelinedCap(kj::ArrayPtr<const PipelineOp> ops) const;
// Used by RPC system to implement pipelining. Applications generally shouldn't use this
// directly.
private:
_::PointerReader reader;
friend struct AnyPointer;
friend class Orphanage;
friend class CapReaderContext;
};
class Builder {
public:
typedef AnyPointer Builds;
Builder() = delete;
inline Builder(decltype(nullptr)) {}
inline Builder(_::PointerBuilder builder): builder(builder) {}
inline MessageSize targetSize() const;
// Get the total size of the target object and all its children.
inline bool isNull();
inline void clear();
// Set to null.
template <typename T>
inline BuilderFor<T> getAs();
// Valid for T = any generated struct type, List<U>, Text, or Data.
template <typename T>
inline BuilderFor<T> getAs(StructSchema schema);
// Only valid for T = DynamicStruct. Requires `#include <capnp/dynamic.h>`.
template <typename T>
inline BuilderFor<T> getAs(ListSchema schema);
// Only valid for T = DynamicList. Requires `#include <capnp/dynamic.h>`.
template <typename T>
inline BuilderFor<T> getAs(InterfaceSchema schema);
// Only valid for T = DynamicCapability. Requires `#include <capnp/dynamic.h>`.
template <typename T>
inline BuilderFor<T> initAs();
// Valid for T = any generated struct type.
template <typename T>
inline BuilderFor<T> initAs(uint elementCount);
// Valid for T = List<U>, Text, or Data.
template <typename T>
inline BuilderFor<T> initAs(StructSchema schema);
// Only valid for T = DynamicStruct. Requires `#include <capnp/dynamic.h>`.
template <typename T>
inline BuilderFor<T> initAs(ListSchema schema, uint elementCount);
// Only valid for T = DynamicList. Requires `#include <capnp/dynamic.h>`.
template <typename T>
inline void setAs(ReaderFor<T> value);
// Valid for ReaderType = T::Reader for T = any generated struct type, List<U>, Text, Data,
// DynamicStruct, or DynamicList (the dynamic types require `#include <capnp/dynamic.h>`).
template <typename T>
inline void setAs(std::initializer_list<ReaderFor<ListElementType<T>>> list);
// Valid for T = List<?>.
inline void set(Reader value) { builder.copyFrom(value.reader); }
// Set to a copy of another AnyPointer.
template <typename T>
inline void adopt(Orphan<T>&& orphan);
// Valid for T = any generated struct type, List<U>, Text, Data, DynamicList, DynamicStruct,
// or DynamicValue (the dynamic types require `#include <capnp/dynamic.h>`).
template <typename T>
inline Orphan<T> disownAs();
// Valid for T = any generated struct type, List<U>, Text, Data.
template <typename T>
inline Orphan<T> disownAs(StructSchema schema);
// Only valid for T = DynamicStruct. Requires `#include <capnp/dynamic.h>`.
template <typename T>
inline Orphan<T> disownAs(ListSchema schema);
// Only valid for T = DynamicList. Requires `#include <capnp/dynamic.h>`.
template <typename T>
inline Orphan<T> disownAs(InterfaceSchema schema);
// Only valid for T = DynamicCapability. Requires `#include <capnp/dynamic.h>`.
inline Orphan<AnyPointer> disown();
// Disown without a type.
inline Reader asReader() const { return Reader(builder.asReader()); }
inline operator Reader() const { return Reader(builder.asReader()); }
inline void setInternal(_::StructReader value) { builder.setStruct(value); }
// For internal use.
//
// TODO(cleanup): RPC implementation uses this, but wouldn't have to if we had an AnyStruct
// type, which would be useful anyawy.
private:
_::PointerBuilder builder;
friend class Orphanage;
friend class CapBuilderContext;
};
class Pipeline {
public:
inline Pipeline(decltype(nullptr)) {}
inline explicit Pipeline(kj::Own<PipelineHook>&& hook): hook(kj::mv(hook)) {}
Pipeline noop();
// Just make a copy.
Pipeline getPointerField(uint16_t pointerIndex);
// Return a new Promise representing a sub-object of the result. `pointerIndex` is the index
// of the sub-object within the pointer section of the result (the result must be a struct).
//
// TODO(kenton): On GCC 4.8 / Clang 3.3, use rvalue qualifiers to avoid the need for copies.
// Also make `ops` into a Vector to optimize this.
kj::Own<ClientHook> asCap();
// Expect that the result is a capability and construct a pipelined version of it now.
inline kj::Own<PipelineHook> releasePipelineHook() { return kj::mv(hook); }
// For use by RPC implementations.
private:
kj::Own<PipelineHook> hook;
kj::Array<PipelineOp> ops;
inline Pipeline(kj::Own<PipelineHook>&& hook, kj::Array<PipelineOp>&& ops)
: hook(kj::mv(hook)), ops(kj::mv(ops)) {}
friend class LocalClient;
friend class PipelineHook;
};
};
template <>
class Orphan<AnyPointer> {
// An orphaned object of unknown type.
public:
Orphan() = default;
KJ_DISALLOW_COPY(Orphan);
Orphan(Orphan&&) = default;
Orphan& operator=(Orphan&&) = default;
template <typename T>
inline Orphan(Orphan<T>&& other): builder(kj::mv(other.builder)) {}
template <typename T>
inline Orphan& operator=(Orphan<T>&& other) { builder = kj::mv(other.builder); return *this; }
// Cast from typed orphan.
// It's not possible to get an AnyPointer::{Reader,Builder} directly since there is no
// underlying pointer (the pointer would normally live in the parent, but this object is
// orphaned). It is possible, however, to request typed readers/builders.
template <typename T>
inline BuilderFor<T> getAs();
template <typename T>
inline BuilderFor<T> getAs(StructSchema schema);
template <typename T>
inline BuilderFor<T> getAs(ListSchema schema);
template <typename T>
inline typename T::Client getAs(InterfaceSchema schema);
template <typename T>
inline ReaderFor<T> getAsReader() const;
template <typename T>
inline ReaderFor<T> getAsReader(StructSchema schema) const;
template <typename T>
inline ReaderFor<T> getAsReader(ListSchema schema) const;
template <typename T>
inline typename T::Client getAsReader(InterfaceSchema schema) const;
template <typename T>
inline Orphan<T> releaseAs();
template <typename T>
inline Orphan<T> releaseAs(StructSchema schema);
template <typename T>
inline Orphan<T> releaseAs(ListSchema schema);
template <typename T>
inline Orphan<T> releaseAs(InterfaceSchema schema);
// Down-cast the orphan to a specific type.
inline bool operator==(decltype(nullptr)) const { return builder == nullptr; }
inline bool operator!=(decltype(nullptr)) const { return builder != nullptr; }
private:
_::OrphanBuilder builder;
inline Orphan(_::OrphanBuilder&& builder)
: builder(kj::mv(builder)) {}
template <typename, Kind>
friend struct _::PointerHelpers;
friend class Orphanage;
template <typename U>
friend class Orphan;
friend class AnyPointer::Builder;
};
// =======================================================================================
// Pipeline helpers
//
// These relate to capabilities, but we don't declare them in capability.h because generated code
// for structs needs to know about these, even in files that contain no interfaces.
struct PipelineOp {
// Corresponds to rpc.capnp's PromisedAnswer.Op.
enum Type {
NOOP, // for convenience
GET_POINTER_FIELD
// There may be other types in the future...
};
Type type;
union {
uint16_t pointerIndex; // for GET_POINTER_FIELD
};
};
class PipelineHook {
// Represents a currently-running call, and implements pipelined requests on its result.
public:
virtual kj::Own<PipelineHook> addRef() = 0;
// Increment this object's reference count.
virtual kj::Own<ClientHook> getPipelinedCap(kj::ArrayPtr<const PipelineOp> ops) = 0;
// Extract a promised Capability from the results.
virtual kj::Own<ClientHook> getPipelinedCap(kj::Array<PipelineOp>&& ops);
// Version of getPipelinedCap() passing the array by move. May avoid a copy in some cases.
// Default implementation just calls the other version.
static inline kj::Own<PipelineHook> from(AnyPointer::Pipeline&& pipeline) {
return kj::mv(pipeline.hook);
}
};
// =======================================================================================
// Inline implementation details
inline MessageSize AnyPointer::Reader::targetSize() const {
return reader.targetSize().asPublic();
}
inline bool AnyPointer::Reader::isNull() const {
return reader.isNull();
}
template <typename T>
inline ReaderFor<T> AnyPointer::Reader::getAs() const {
return _::PointerHelpers<T>::get(reader);
}
inline MessageSize AnyPointer::Builder::targetSize() const {
return asReader().targetSize();
}
inline bool AnyPointer::Builder::isNull() {
return builder.isNull();
}
inline void AnyPointer::Builder::clear() {
return builder.clear();
}
template <typename T>
inline BuilderFor<T> AnyPointer::Builder::getAs() {
return _::PointerHelpers<T>::get(builder);
}
template <typename T>
inline BuilderFor<T> AnyPointer::Builder::initAs() {
return _::PointerHelpers<T>::init(builder);
}
template <typename T>
inline BuilderFor<T> AnyPointer::Builder::initAs(uint elementCount) {
return _::PointerHelpers<T>::init(builder, elementCount);
}
template <typename T>
inline void AnyPointer::Builder::setAs(ReaderFor<T> value) {
return _::PointerHelpers<T>::set(builder, value);
}
template <typename T>
inline void AnyPointer::Builder::setAs(
std::initializer_list<ReaderFor<ListElementType<T>>> list) {
return _::PointerHelpers<T>::set(builder, list);
}
template <typename T>
inline void AnyPointer::Builder::adopt(Orphan<T>&& orphan) {
_::PointerHelpers<T>::adopt(builder, kj::mv(orphan));
}
template <typename T>
inline Orphan<T> AnyPointer::Builder::disownAs() {
return _::PointerHelpers<T>::disown(builder);
}
inline Orphan<AnyPointer> AnyPointer::Builder::disown() {
return Orphan<AnyPointer>(builder.disown());
}
template <> struct ReaderFor_ <AnyPointer, Kind::UNKNOWN> { typedef AnyPointer::Reader Type; };
template <> struct BuilderFor_<AnyPointer, Kind::UNKNOWN> { typedef AnyPointer::Builder Type; };
template <>
struct Orphanage::GetInnerReader<AnyPointer, Kind::UNKNOWN> {
static inline _::PointerReader apply(const AnyPointer::Reader& t) {
return t.reader;
}
};
template <>
struct Orphanage::GetInnerBuilder<AnyPointer, Kind::UNKNOWN> {
static inline _::PointerBuilder apply(AnyPointer::Builder& t) {
return t.builder;
}
};
template <typename T>
inline BuilderFor<T> Orphan<AnyPointer>::getAs() {
return _::OrphanGetImpl<T>::apply(builder);
}
template <typename T>
inline ReaderFor<T> Orphan<AnyPointer>::getAsReader() const {
return _::OrphanGetImpl<T>::applyReader(builder);
}
template <typename T>
inline Orphan<T> Orphan<AnyPointer>::releaseAs() {
return Orphan<T>(kj::mv(builder));
}
// Using AnyPointer as the template type should work...
template <>
inline typename AnyPointer::Reader AnyPointer::Reader::getAs<AnyPointer>() const {
return *this;
}
template <>
inline typename AnyPointer::Builder AnyPointer::Builder::getAs<AnyPointer>() {
return *this;
}
template <>
inline typename AnyPointer::Builder AnyPointer::Builder::initAs<AnyPointer>() {
clear();
return *this;
}
template <>
inline void AnyPointer::Builder::setAs<AnyPointer>(AnyPointer::Reader value) {
return builder.copyFrom(value.reader);
}
template <>
inline void AnyPointer::Builder::adopt<AnyPointer>(Orphan<AnyPointer>&& orphan) {
builder.adopt(kj::mv(orphan.builder));
}
template <>
inline Orphan<AnyPointer> AnyPointer::Builder::disownAs<AnyPointer>() {
return Orphan<AnyPointer>(builder.disown());
}
template <>
inline Orphan<AnyPointer> Orphan<AnyPointer>::releaseAs() {
return kj::mv(*this);
}
} // namespace capnp
#endif // CAPNP_ANY_H_
|