LSSTApplications  20.0.0
LSSTDataManagementBasePackage
FieldBase.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 #include <cstdint>
4 #include <limits>
5 
6 #include "boost/format.hpp"
7 #include "boost/preprocessor/seq/for_each.hpp"
8 #include "boost/preprocessor/tuple/to_seq.hpp"
9 
11 #include "lsst/afw/table/Flag.h"
12 
13 namespace lsst {
14 namespace afw {
15 namespace table {
16 
17 namespace {
18 
19 template <typename T>
20 struct TypeTraits;
21 
22 template <>
23 struct TypeTraits<std::uint8_t> {
24  static char const *getName() { return "B"; }
25 };
26 template <>
27 struct TypeTraits<std::uint16_t> {
28  static char const *getName() { return "U"; }
29 };
30 template <>
31 struct TypeTraits<std::int32_t> {
32  static char const *getName() { return "I"; }
33 };
34 template <>
35 struct TypeTraits<std::int64_t> {
36  static char const *getName() { return "L"; }
37 };
38 template <>
39 struct TypeTraits<float> {
40  static char const *getName() { return "F"; }
41 };
42 template <>
43 struct TypeTraits<double> {
44  static char const *getName() { return "D"; }
45 };
46 template <>
47 struct TypeTraits<lsst::geom::Angle> {
48  static char const *getName() { return "Angle"; }
49 };
50 
51 } // namespace
52 
53 //----- POD scalars -----------------------------------------------------------------------------------------
54 
55 template <typename T>
57  return TypeTraits<T>::getName();
58 }
59 
60 //----- POD array -------------------------------------------------------------------------------------------
61 
62 template <typename U>
63 std::string FieldBase<Array<U> >::getTypeString() {
64  return (boost::format("Array%s") % TypeTraits<U>::getName()).str();
65 }
66 
67 //----- String ----------------------------------------------------------------------------------------------
68 
70  if (size < 0)
72  "Size must be provided when constructing a string field.");
73 }
74 
76 
77 std::string FieldBase<std::string>::getValue(Element const *p, ndarray::Manager::Ptr const &m) const {
78  if (isVariableLength()) {
79  // p is a pointer to a std::string; return a copy
80  return std::string(*reinterpret_cast<std::string const *>(p));
81  } else {
82  // p is a char * that is null-terminated only if the string has fewer than _size chars;
83  // return a copy as a std::string
84  Element const *end = p + _size;
85  end = std::find(p, end, 0);
86  return std::string(p, end);
87  }
88 }
89 
90 void FieldBase<std::string>::setValue(Element *p, ndarray::Manager::Ptr const &,
91  std::string const &value) const {
92  if (isVariableLength()) {
93  // p is a pointer to a std::string; replace its contents with a copy of `value`
94  *reinterpret_cast<std::string *>(p) = value;
95  } else {
96  // copy the contents of `value` to p through p + _size, null extra characters, if any
97  if (value.size() > std::size_t(_size)) {
98  throw LSST_EXCEPT(
100  (boost::format("String (%d) is too large for field (%d).") % value.size() % _size).str());
101  }
102  std::copy(value.begin(), value.end(), p);
103  std::fill(p + value.size(), p + _size, char(0)); // null extra characters, if any
104  }
105 }
106 
107 //----- Explicit instantiation ------------------------------------------------------------------------------
108 
109 #define INSTANTIATE_FIELD_BASE(r, data, elem) template struct FieldBase<elem>;
110 
112  BOOST_PP_TUPLE_TO_SEQ(AFW_TABLE_FIELD_TYPE_N, AFW_TABLE_FIELD_TYPE_TUPLE))
113 } // namespace table
114 } // namespace afw
115 } // namespace lsst
lsst::afw::table::FieldBase::FieldBase
FieldBase()=default
std::string
STL class.
lsst::afw::table::FieldBase::setValue
void setValue(Element *p, ndarray::Manager::Ptr const &, Value v) const
Used to implement BaseRecord::set.
Definition: FieldBase.h:86
std::find
T find(T... args)
std::string::size
T size(T... args)
pex.config.history.format
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
lsst::afw
Definition: imageAlgorithm.dox:1
end
int end
Definition: BoundedField.cc:105
std::fill
T fill(T... args)
_size
table::Key< table::Array< int > > _size
Definition: PsfexPsf.cc:364
lsst::pex::exceptions::LengthError
Reports attempts to exceed implementation-defined length limits for some classes.
Definition: Runtime.h:76
lsst::afw::table::_
_
Definition: BaseColumnView.cc:188
std::copy
T copy(T... args)
AFW_TABLE_FIELD_TYPE_TUPLE
#define AFW_TABLE_FIELD_TYPE_TUPLE
Definition: types.h:44
lsst::afw::table::BOOST_PP_SEQ_FOR_EACH
BOOST_PP_SEQ_FOR_EACH(INSTANTIATE_COLUMNVIEW_SCALAR, _, BOOST_PP_TUPLE_TO_SEQ(AFW_TABLE_SCALAR_FIELD_TYPE_N, AFW_TABLE_SCALAR_FIELD_TYPE_TUPLE)) BOOST_PP_SEQ_FOR_EACH(INSTANTIATE_COLUMNVIEW_ARRAY
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
lsst::afw::table::FieldBase
Field base class default implementation (used for numeric scalars and lsst::geom::Angle).
Definition: FieldBase.h:43
lsst::afw::table::FieldBase< std::string >::Element
char Element
the type of subfields and array elements
Definition: FieldBase.h:233
lsst::afw::table::FieldBase::getTypeString
static std::string getTypeString()
Return a string description of the field type.
Definition: FieldBase.cc:56
std::string::begin
T begin(T... args)
std
STL namespace.
lsst::geom::Angle
A class representing an angle.
Definition: Angle.h:127
lsst::afw::table::FieldBase::getValue
Value getValue(Element const *p, ndarray::Manager::Ptr const &) const
Used to implement BaseRecord::get.
Definition: FieldBase.h:83
std::size_t
std::string::end
T end(T... args)
INSTANTIATE_FIELD_BASE
#define INSTANTIATE_FIELD_BASE(r, data, elem)
Definition: FieldBase.cc:109
FieldBase.h
AFW_TABLE_FIELD_TYPE_N
#define AFW_TABLE_FIELD_TYPE_N
Definition: types.h:39
m
int m
Definition: SpanSet.cc:49
Flag.h