5#include "boost/shared_ptr.hpp"
35class Block :
public ndarray::Manager {
37 using Ptr = boost::intrusive_ptr<Block>;
42 static void reclaim(
std::size_t recordSize,
void *
data, ndarray::Manager::Ptr
const &manager) {
43 Ptr block = boost::static_pointer_cast<Block>(manager);
44 if (
reinterpret_cast<char *
>(
data) + recordSize == block->_next) {
45 block->_next -= recordSize;
52 Ptr block = boost::static_pointer_cast<Block>(manager);
53 if (!block ||
static_cast<std::size_t>(block->_end - block->_next) < recordSize * recordCount) {
54 block = Ptr(
new Block(recordSize, recordCount));
60 Ptr block = boost::static_pointer_cast<Block>(manager);
61 return static_cast<std::size_t>(block->_end - block->_next) / recordSize;
66 static void *get(
std::size_t recordSize, ndarray::Manager::Ptr &manager) {
67 Ptr block = boost::static_pointer_cast<Block>(manager);
68 if (!block || block->_next == block->_end) {
72 void *
r = block->_next;
73 block->_next += recordSize;
80 static void padSchema(Schema &
schema) {
81 static int const MIN_RECORD_ALIGN =
sizeof(AllocType);
94 : _mem(new AllocType[(recordSize * recordCount) / sizeof(AllocType)]),
95 _next(reinterpret_cast<char *>(_mem.get())),
96 _end(_next + recordSize * recordCount) {
97 assert((recordSize * recordCount) %
sizeof(AllocType) == 0);
114 return Block::getBufferSize(_schema.
getRecordSize(), _manager);
122 table->getSchema().getAliasMap()->setTable(table);
130 output->assign(input);
136 output->assign(input,
mapper);
141 return std::make_shared<io::FitsWriter>(
fitsfile, flags);
146 table->getSchema().getAliasMap()->setTable(table);
153 : _schema(
schema), _metadata(metadata) {
154 Block::padSchema(_schema);
162 if (_metadata) _metadata = std::static_pointer_cast<daf::base::PropertyList>(_metadata->deepCopy());
171struct RecordDestroyer {
172 template <
typename T>
175 template <
typename T>
176 void operator()(SchemaItem<Array<T> >
const &item)
const {
177 using Element = ndarray::Array<T, 1, 1>;
178 if (item.key.isVariableLength()) {
179 (*
reinterpret_cast<Element *
>(
data + item.key.getOffset())).~Element();
183 void operator()(SchemaItem<std::string>
const &item)
const {
184 if (item.key.isVariableLength()) {
187 (*
reinterpret_cast<string *
>(
data + item.key.getOffset())).~string();
196detail::RecordData BaseTable::_makeNewRecordData() {
198 return detail::RecordData{
204void BaseTable::_destroy(BaseRecord &record) {
205 assert(record._table.get() ==
this);
206 RecordDestroyer f = {
reinterpret_cast<char *
>(record._data)};
208 if (record._manager == _manager) Block::reclaim(_schema.
getRecordSize(), record._data, _manager);
221template class CatalogT<BaseRecord>;
222template class CatalogT<BaseRecord const>;
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Tag types used to declare specialized field types.
Base class for all records.
Base class for all tables.
virtual std::shared_ptr< BaseRecord > _makeRecord()
Default-construct an associated record (protected implementation).
virtual std::shared_ptr< io::FitsWriter > makeFitsWriter(fits::Fits *fitsfile, int flags) const
void preallocate(std::size_t nRecords)
Allocate contiguous space for new records in advance.
std::shared_ptr< BaseRecord > copyRecord(BaseRecord const &input)
Deep-copy a record, requiring that it have the same schema as this table.
BaseTable(Schema const &schema, std::shared_ptr< daf::base::PropertyList > metadata=nullptr)
Construct from a schema.
std::size_t getBufferSize() const
Return the number of additional records space has been already been allocated for.
static Schema makeMinimalSchema()
Return a minimal schema for Base tables and records.
static int nRecordsPerBlock
Number of records in each memory block.
static std::shared_ptr< BaseTable > make(Schema const &schema)
Construct a new table.
virtual std::shared_ptr< BaseTable > _clone() const
Clone implementation with noncovariant return types.
std::shared_ptr< BaseRecord > makeRecord()
Default-construct an associated record.
Defines the fields and offsets for a table.
void forEach(F &func) const
Apply a functor to each SchemaItem in the Schema.
void disconnectAliases()
Sever the connection between this schema and any others with which it shares aliases.
std::shared_ptr< AliasMap > getAliasMap() const
Return the map of aliases.
std::size_t getRecordSize() const
Return the raw size of a record in bytes.
A mapping between the keys of two Schemas, used to copy data between them.
static void padSchema(Schema &schema, std::size_t bytes)
T shared_from_this(T... args)