| 1 | #ifndef NEXUS_STREAM_HPP |
|---|
| 2 | #define NEXUS_STREAM_HPP |
|---|
| 3 | // |
|---|
| 4 | // NeXus - Neutron & X-ray Common Data Format |
|---|
| 5 | // |
|---|
| 6 | // IOStream like interface to NeXus C++ Bindings |
|---|
| 7 | // |
|---|
| 8 | // Copyright (C) 2008 Freddie Akeroyd, STFC ISIS facility |
|---|
| 9 | // |
|---|
| 10 | // This library is free software; you can redistribute it and/or |
|---|
| 11 | // modify it under the terms of the GNU Lesser General Public |
|---|
| 12 | // License as published by the Free Software Foundation; either |
|---|
| 13 | // version 2 of the License, or (at your option) any later version. |
|---|
| 14 | // |
|---|
| 15 | // This library is distributed in the hope that it will be useful, |
|---|
| 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 18 | // Lesser General Public License for more details. |
|---|
| 19 | // |
|---|
| 20 | // You should have received a copy of the GNU Lesser General Public |
|---|
| 21 | // License along with this library; if not, write to the Free |
|---|
| 22 | // Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
|---|
| 23 | // MA 02111-1307 USA |
|---|
| 24 | // |
|---|
| 25 | // For further information, see http://www.nexusformat.org/ |
|---|
| 26 | // |
|---|
| 27 | // |
|---|
| 28 | |
|---|
| 29 | /////////////////// Subversion Repository Details //////////////////////// |
|---|
| 30 | // Repository Location $HeadURL$ |
|---|
| 31 | // Revision of last commit $LastChangedRevision$ |
|---|
| 32 | // Date of last commit $LastChangedDate$ |
|---|
| 33 | // Last changed by $LastChangedBy$ |
|---|
| 34 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * \file NeXusStream.hpp |
|---|
| 38 | * Header for IOStream like interface to NeXus files |
|---|
| 39 | * \author Freddie Akeroyd, STFC ISIS Facility, GB |
|---|
| 40 | * \version $LastChangedRevision$ |
|---|
| 41 | * \date $LastChangedDate$ |
|---|
| 42 | * \defgroup cpp_stream IOstream like interface |
|---|
| 43 | * \ingroup cpp_main |
|---|
| 44 | */ |
|---|
| 45 | |
|---|
| 46 | #include <list> |
|---|
| 47 | #include <vector> |
|---|
| 48 | #include "NeXusFile.hpp" |
|---|
| 49 | |
|---|
| 50 | namespace NeXus |
|---|
| 51 | { |
|---|
| 52 | namespace Stream |
|---|
| 53 | { |
|---|
| 54 | /** |
|---|
| 55 | * interface implemented by all serialisable NeXus components |
|---|
| 56 | */ |
|---|
| 57 | class NXDLL_EXPORT ISerialisable |
|---|
| 58 | { |
|---|
| 59 | public: |
|---|
| 60 | virtual void readFromFile(File& nf) const = 0; |
|---|
| 61 | virtual void writeToFile(File& nf) const = 0; |
|---|
| 62 | }; |
|---|
| 63 | |
|---|
| 64 | /// \ingroup cpp_stream |
|---|
| 65 | enum StreamModifier { Close=0 }; |
|---|
| 66 | |
|---|
| 67 | /** |
|---|
| 68 | * Base class for serialisable named and typed parameter |
|---|
| 69 | */ |
|---|
| 70 | class NXDLL_EXPORT HolderBase : public ISerialisable |
|---|
| 71 | { |
|---|
| 72 | protected: |
|---|
| 73 | std::string m_name; |
|---|
| 74 | |
|---|
| 75 | public: |
|---|
| 76 | HolderBase() : m_name("") {} |
|---|
| 77 | HolderBase(const std::string& name); |
|---|
| 78 | void setName(const std::string& name); |
|---|
| 79 | std::string getName() { return m_name; } |
|---|
| 80 | virtual NXnumtype getType() = 0; |
|---|
| 81 | virtual HolderBase* clone() = 0; |
|---|
| 82 | virtual ~HolderBase() {} |
|---|
| 83 | }; |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | * Serialisable NeXus attribute |
|---|
| 87 | */ |
|---|
| 88 | template<typename NumT> |
|---|
| 89 | class NXDLL_EXPORT AttrHolder : public HolderBase |
|---|
| 90 | { |
|---|
| 91 | protected: |
|---|
| 92 | const NumT* m_c_value; |
|---|
| 93 | NumT* m_value; |
|---|
| 94 | AttrHolder() : HolderBase(), m_c_value(NULL), m_value(NULL) { } |
|---|
| 95 | AttrHolder(const std::string& name, const NumT* cv, NumT* v) : HolderBase(name), m_c_value(cv), m_value(v) { } |
|---|
| 96 | |
|---|
| 97 | public: |
|---|
| 98 | AttrHolder(const std::string& name, NumT& value); |
|---|
| 99 | AttrHolder(const std::string& name, const NumT& value); |
|---|
| 100 | AttrHolder(NumT& value); |
|---|
| 101 | AttrHolder(const NumT& value); |
|---|
| 102 | NXnumtype getType(); |
|---|
| 103 | virtual void readFromFile(File& nf) const; |
|---|
| 104 | virtual void writeToFile(File& nf) const; |
|---|
| 105 | AttrHolder* clone() { return new AttrHolder(m_name, m_c_value, m_value); } |
|---|
| 106 | virtual ~AttrHolder() { m_value = NULL; m_c_value = NULL; } |
|---|
| 107 | }; |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * Serialisable attribute |
|---|
| 111 | * \ingroup cpp_stream |
|---|
| 112 | */ |
|---|
| 113 | class NXDLL_EXPORT Attr : public ISerialisable |
|---|
| 114 | { |
|---|
| 115 | protected: |
|---|
| 116 | HolderBase* m_holder; |
|---|
| 117 | |
|---|
| 118 | public: |
|---|
| 119 | Attr() : m_holder(NULL) { } |
|---|
| 120 | template <typename NumT> |
|---|
| 121 | Attr(NumT& d) { m_holder = new AttrHolder<NumT>(d); } |
|---|
| 122 | template <typename NumT> |
|---|
| 123 | Attr(const NumT& d) { m_holder = new AttrHolder<NumT>(d); } |
|---|
| 124 | template <typename NumT> |
|---|
| 125 | Attr(const std::string& name, NumT& d) { m_holder = new AttrHolder<NumT>(name, d); } |
|---|
| 126 | template <typename NumT> |
|---|
| 127 | Attr(const std::string& name, const NumT& d) { m_holder = new AttrHolder<NumT>(name, d); } |
|---|
| 128 | Attr(const std::string& name, Attr& d) { m_holder = d.m_holder->clone(); setName(name); } |
|---|
| 129 | Attr(const std::string& name, const Attr& d) { m_holder = d.m_holder->clone(); setName(name); } |
|---|
| 130 | Attr(const Attr& a) : m_holder(NULL) { m_holder = a.m_holder->clone(); } |
|---|
| 131 | Attr& operator=(const Attr& a) { if (this != &a) { delete m_holder; m_holder = a.m_holder->clone(); } return *this; } |
|---|
| 132 | void setName(const std::string& name) { m_holder->setName(name); } |
|---|
| 133 | virtual void readFromFile(File& nf) const { m_holder->readFromFile(nf); } |
|---|
| 134 | virtual void writeToFile(File& nf) const { m_holder->writeToFile(nf); } |
|---|
| 135 | virtual ~Attr() { delete m_holder; m_holder = NULL; } |
|---|
| 136 | }; |
|---|
| 137 | |
|---|
| 138 | /** |
|---|
| 139 | * Serialisable NeXus class with associated attributes |
|---|
| 140 | */ |
|---|
| 141 | class NXDLL_EXPORT ObjectWithAttr : public ISerialisable |
|---|
| 142 | { |
|---|
| 143 | protected: |
|---|
| 144 | std::list<Attr> m_attr; |
|---|
| 145 | |
|---|
| 146 | void processAttr(const std::string& attr1_name, const Attr& attr1_value, const std::string& attr2_name, const Attr& attr2_value) |
|---|
| 147 | { |
|---|
| 148 | if (attr1_name.size() > 0) |
|---|
| 149 | { |
|---|
| 150 | m_attr.push_back(Attr(attr1_name, attr1_value)); |
|---|
| 151 | } |
|---|
| 152 | if (attr2_name.size() > 0) |
|---|
| 153 | { |
|---|
| 154 | m_attr.push_back(Attr(attr2_name, attr2_value)); |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | public: |
|---|
| 159 | |
|---|
| 160 | ObjectWithAttr(const std::string& attr1_name = "", const Attr& attr1_value = Attr(), const std::string& attr2_name = "", const Attr& attr2_value = Attr()) |
|---|
| 161 | { |
|---|
| 162 | processAttr(attr1_name, attr1_value, attr2_name, attr2_value); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | virtual void readFromFile(File& nf) const |
|---|
| 166 | { |
|---|
| 167 | for(std::list<Attr>::const_iterator it = m_attr.begin(); it != m_attr.end(); it++) |
|---|
| 168 | { |
|---|
| 169 | it->readFromFile(nf); |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | virtual void writeToFile(File& nf) const |
|---|
| 174 | { |
|---|
| 175 | for(std::list<Attr>::const_iterator it = m_attr.begin(); it != m_attr.end(); it++) |
|---|
| 176 | { |
|---|
| 177 | it->writeToFile(nf); |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | virtual ~ObjectWithAttr() { } |
|---|
| 182 | }; |
|---|
| 183 | |
|---|
| 184 | /** |
|---|
| 185 | * Serialisable NeXus group object |
|---|
| 186 | * \ingroup cpp_stream |
|---|
| 187 | */ |
|---|
| 188 | class NXDLL_EXPORT Group : public ObjectWithAttr |
|---|
| 189 | { |
|---|
| 190 | protected: |
|---|
| 191 | std::string m_name; |
|---|
| 192 | std::string m_class; |
|---|
| 193 | |
|---|
| 194 | public: |
|---|
| 195 | Group(const std::string& name, const std::string& nxclass, const std::string& attr1_name = "", const Attr& attr1_value = Attr(), |
|---|
| 196 | const std::string& attr2_name="", const Attr& attr2_value = Attr()) : |
|---|
| 197 | ObjectWithAttr(attr1_name, attr1_value, attr2_name, attr2_value), m_name(name), m_class(nxclass) |
|---|
| 198 | { |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | virtual void readFromFile(File& nf) const |
|---|
| 202 | { |
|---|
| 203 | nf.openGroup(m_name, m_class); |
|---|
| 204 | ObjectWithAttr::readFromFile(nf); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | virtual void writeToFile(File& nf) const |
|---|
| 208 | { |
|---|
| 209 | nf.makeGroup(m_name, m_class, true); |
|---|
| 210 | ObjectWithAttr::writeToFile(nf); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | virtual ~Group() {} |
|---|
| 214 | }; |
|---|
| 215 | |
|---|
| 216 | /** |
|---|
| 217 | * Serialisable NeXus data |
|---|
| 218 | */ |
|---|
| 219 | template<typename NumT> |
|---|
| 220 | class NXDLL_EXPORT DataHolder : public HolderBase |
|---|
| 221 | { |
|---|
| 222 | protected: |
|---|
| 223 | const std::vector<NumT>* m_c_value; |
|---|
| 224 | std::vector<NumT>* m_value; |
|---|
| 225 | DataHolder() : HolderBase(), m_c_value(NULL), m_value(NULL) { } |
|---|
| 226 | DataHolder(const std::string& name, const std::vector<NumT>* cv, std::vector<NumT>* v) : HolderBase(name), m_c_value(cv), m_value(v) { } |
|---|
| 227 | |
|---|
| 228 | public: |
|---|
| 229 | DataHolder(const std::string& name); |
|---|
| 230 | DataHolder(const std::string& name, std::vector<NumT>& value); |
|---|
| 231 | DataHolder(const std::string& name, const std::vector<NumT>& value); |
|---|
| 232 | DataHolder(std::vector<NumT>& value); |
|---|
| 233 | DataHolder(const std::vector<NumT>& value); |
|---|
| 234 | NXnumtype getType() { return NeXus::getType<NumT>(); } |
|---|
| 235 | virtual void readFromFile(File& nf) const; |
|---|
| 236 | virtual void writeToFile(File& nf) const; |
|---|
| 237 | DataHolder* clone() { return new DataHolder(m_name, m_c_value, m_value); } |
|---|
| 238 | virtual ~DataHolder() {} |
|---|
| 239 | }; |
|---|
| 240 | |
|---|
| 241 | /** |
|---|
| 242 | * Serialisable data object that contains attributes |
|---|
| 243 | * \ingroup cpp_stream |
|---|
| 244 | */ |
|---|
| 245 | class NXDLL_EXPORT Data : public ObjectWithAttr |
|---|
| 246 | { |
|---|
| 247 | HolderBase* m_holder; |
|---|
| 248 | |
|---|
| 249 | public: |
|---|
| 250 | Data() : ObjectWithAttr(), m_holder(NULL) {} |
|---|
| 251 | Data(const std::string& name) : ObjectWithAttr() |
|---|
| 252 | { |
|---|
| 253 | m_holder = new DataHolder<int>(name); // TODO: move name out of holder and use m_holder = NULL |
|---|
| 254 | } |
|---|
| 255 | template <typename NumT> |
|---|
| 256 | Data(const std::string& name, std::vector<NumT>& data, const std::string& attr1_name="", const Attr& attr1_value=Attr(), |
|---|
| 257 | const std::string& attr2_name="", const Attr& attr2_value = Attr()) : |
|---|
| 258 | ObjectWithAttr(attr1_name, attr1_value, attr2_name, attr2_value) |
|---|
| 259 | { |
|---|
| 260 | m_holder = new DataHolder<NumT>(name, data); |
|---|
| 261 | } |
|---|
| 262 | template <typename NumT> |
|---|
| 263 | Data(const std::string& name, const std::vector<NumT>& data, const std::string& attr1_name="", const Attr& attr1_value=Attr(), |
|---|
| 264 | const std::string& attr2_name="", const Attr& attr2_value = Attr()) : |
|---|
| 265 | ObjectWithAttr(attr1_name, attr1_value, attr2_name, attr2_value) |
|---|
| 266 | { |
|---|
| 267 | m_holder = new DataHolder<NumT>(name, data); |
|---|
| 268 | } |
|---|
| 269 | Data(const Data& d) : ObjectWithAttr(d), m_holder(NULL) { m_holder = d.m_holder->clone(); } |
|---|
| 270 | Data& operator=(const Data& d) { if (this != &d) { delete m_holder; m_holder = d.m_holder->clone(); } return *this; } |
|---|
| 271 | virtual void readFromFile(File& nf) const; |
|---|
| 272 | virtual void writeToFile(File& nf) const; |
|---|
| 273 | virtual ~Data() { delete m_holder; } |
|---|
| 274 | }; |
|---|
| 275 | |
|---|
| 276 | /// \ingroup cpp_stream |
|---|
| 277 | NXDLL_EXPORT File& operator<<(File& nf, const ISerialisable& obj); |
|---|
| 278 | |
|---|
| 279 | /// \ingroup cpp_stream |
|---|
| 280 | NXDLL_EXPORT File& operator>>(File& nf, const ISerialisable& obj); |
|---|
| 281 | |
|---|
| 282 | /// \ingroup cpp_stream |
|---|
| 283 | NXDLL_EXPORT File& operator<<(File& nf, const StreamModifier sm); |
|---|
| 284 | |
|---|
| 285 | /// \ingroup cpp_stream |
|---|
| 286 | NXDLL_EXPORT File& operator>>(File& nf, const StreamModifier sm); |
|---|
| 287 | |
|---|
| 288 | } // Stream |
|---|
| 289 | } // NeXus |
|---|
| 290 | |
|---|
| 291 | #endif /* NEXUS_STREAM_HPP */ |
|---|