LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
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
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
Field base class default implementation (used for numeric scalars and lsst::geom::Angle).
Definition: FieldBase.h:43
table::Key< table::Array< int > > _size
Definition: PsfexPsf.cc:364
T copy(T... args)
Reports attempts to exceed implementation-defined length limits for some classes. ...
Definition: Runtime.h:76
STL namespace.
T end(T... args)
#define AFW_TABLE_FIELD_TYPE_TUPLE
Definition: types.h:44
A class representing an angle.
Definition: Angle.h:127
STL class.
#define INSTANTIATE_FIELD_BASE(r, data, elem)
Definition: FieldBase.cc:109
A base class for image defects.
void setValue(Element *p, ndarray::Manager::Ptr const &, Value v) const
Used to implement BaseRecord::set.
Definition: FieldBase.h:86
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
T find(T... args)
T size(T... args)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
T begin(T... args)
Value getValue(Element const *p, ndarray::Manager::Ptr const &) const
Used to implement BaseRecord::get.
Definition: FieldBase.h:83
int m
Definition: SpanSet.cc:49
T fill(T... args)
static std::string getTypeString()
Return a string description of the field type.
Definition: FieldBase.cc:56
char Element
the type of subfields and array elements
Definition: FieldBase.h:233
int end
#define AFW_TABLE_FIELD_TYPE_N
Definition: types.h:39