source: trunk/bindings/cpp/NeXusStream.cpp @ 1822

Revision 1259, 6.2 KB checked in by Freddie Akeroyd, 3 years ago (diff)

Start adding bits to implement definitions. Refs #179.

  • Property svn:keywords set to HeadURL LastChangedBy LastChangedRevision LastChangedDate
Line 
1//
2//  NeXus - Neutron & X-ray Common Data Format
3// 
4//  $Id: Makefile.am 598 2005-08-19 16:19:15Z faa59 $
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/////////////////// Subversion Repository Details ////////////////////////
29// Repository Location     $HeadURL$
30// Revision of last commit $LastChangedRevision$
31// Date of last commit     $LastChangedDate$
32// Last changed by         $LastChangedBy$
33//////////////////////////////////////////////////////////////////////////
34
35/**
36 * \file NeXusStream.cpp
37 * Implementation of IOStream like interface to NeXus files
38 * \author Freddie Akeroyd, STFC ISIS Facility, GB
39 * \version $LastChangedRevision$
40 * \date    $LastChangedDate$
41 */
42
43#include <iostream>
44#include "napiconfig.h"
45#include "NeXusStream.hpp"
46#include "NeXusException.hpp"
47namespace NeXus {
48namespace Stream {
49
50HolderBase::HolderBase(const std::string& name) : m_name(name)
51{
52}
53
54void HolderBase::setName(const std::string& name)
55{
56    m_name = name;
57}
58   
59template<typename NumT>
60AttrHolder<NumT>::AttrHolder(const std::string& name, NumT& value) : HolderBase(name), m_c_value(NULL), m_value(&value)
61{
62}
63
64template<typename NumT>
65AttrHolder<NumT>::AttrHolder(const std::string& name, const NumT& value) : HolderBase(name), m_c_value(&value), m_value(NULL)
66{
67}
68
69template<typename NumT>
70AttrHolder<NumT>::AttrHolder(NumT& value) : HolderBase(""), m_c_value(NULL), m_value(&value)
71{
72}
73
74template<typename NumT>
75AttrHolder<NumT>::AttrHolder(const NumT& value) : HolderBase(""), m_c_value(&value), m_value(NULL)
76{
77}
78
79template<typename NumT>
80NXnumtype AttrHolder<NumT>::getType()
81{
82    return NeXus::getType<NumT>();
83}
84
85template<>
86NXnumtype AttrHolder<std::string>::getType()
87{
88    return NeXus::getType<char>();
89}
90
91template<>
92void AttrHolder<std::string>::readFromFile(File& nf) const
93{
94    if (m_value != NULL)
95    {
96        nf.getAttr(m_name, *m_value);
97    }
98    else
99    {
100        throw Exception("AttrHolder<NumT>::readFromFile - not able to read into a constant");
101    }
102}
103
104template<typename NumT>
105void AttrHolder<NumT>::readFromFile(File& nf) const
106{
107    if (m_value != NULL)
108    {
109        nf.getAttr(m_name, *m_value);
110    }
111    else
112    {
113        throw Exception("AttrHolder<NumT>::readFromFile - not able to read into a constant");
114    }
115}
116
117template<typename NumT>
118void AttrHolder<NumT>::writeToFile(File& nf) const
119{
120    if (m_value != NULL)
121    {
122        nf.putAttr(m_name, *m_value);
123    }
124    else if (m_c_value != NULL)
125    {
126        nf.putAttr(m_name, *m_c_value);
127    }
128    else
129    {
130        throw Exception("AttrHolder<NumT>::writeToFile - no value to write");
131    }
132}
133
134
135template<typename NumT>
136void DataHolder<NumT>::readFromFile(File& nf) const
137{
138    if (m_value != NULL)
139    {
140        nf.openData(m_name);
141        nf.getData(*m_value);
142        nf.closeData();
143    }
144    else if (m_c_value != NULL)
145    {
146        throw Exception("DataHolder<NumT>::readFromFile - not able to read into a constant");
147    }
148    else
149    {
150        nf.openData(m_name);
151    }
152}
153
154template<typename NumT>
155void DataHolder<NumT>::writeToFile(File& nf) const
156{
157    if (m_value != NULL)
158    {
159        nf.writeData(m_name, *m_value);
160    }
161    else if (m_c_value != NULL)
162    {
163        nf.writeData(m_name, *m_c_value);
164    }
165    else
166    {
167        throw Exception("DataHolder<NumT>::writeToFile - no value to write");
168    }
169}
170
171template<typename NumT>
172DataHolder<NumT>::DataHolder(const std::string& name, std::vector<NumT>& value) : HolderBase(name), m_c_value(NULL), m_value(&value)
173{
174}
175
176template<typename NumT>
177DataHolder<NumT>::DataHolder(const std::string& name) : HolderBase(name), m_c_value(NULL), m_value(NULL)
178{
179}
180
181template<typename NumT>
182DataHolder<NumT>::DataHolder(const std::string& name, const std::vector<NumT>& value) : HolderBase(name), m_c_value(&value), m_value(NULL)
183{
184}
185
186template<typename NumT>
187DataHolder<NumT>::DataHolder(std::vector<NumT>& value) : HolderBase(""), m_c_value(NULL), m_value(&value)
188{
189}
190
191template<typename NumT>
192DataHolder<NumT>::DataHolder(const std::vector<NumT>& value) : HolderBase(""), m_c_value(&value), m_value(NULL)
193{
194}
195
196template class NXDLL_EXPORT AttrHolder<double>;
197template class NXDLL_EXPORT AttrHolder<int>;
198template class NXDLL_EXPORT AttrHolder<std::string>;
199
200template class NXDLL_EXPORT DataHolder<double>;
201template class NXDLL_EXPORT DataHolder<int>;
202template class NXDLL_EXPORT DataHolder<char>;
203
204void Data::readFromFile(File& nf) const
205{
206    m_holder->readFromFile(nf);
207    if (m_attr.size() > 0)
208    {
209        nf.openData(m_holder->getName());
210        ObjectWithAttr::readFromFile(nf);
211        nf.closeData();
212    }
213}
214
215void Data::writeToFile(File& nf) const
216{
217    m_holder->writeToFile(nf);
218    if (m_attr.size() > 0)
219    {
220        nf.openData(m_holder->getName());
221        ObjectWithAttr::writeToFile(nf);
222        nf.closeData();
223    }
224}
225
226File& operator<<(File& nf, const ISerialisable& obj)
227{
228    obj.writeToFile(nf);
229    return nf;
230}
231
232File& operator>>(File& nf, const ISerialisable& obj)
233{
234    obj.readFromFile(nf);
235    return nf;
236}
237
238File& operator<<(File& nf, const StreamModifier sm)
239{
240    switch(sm)
241    {
242        case Close:
243            if (nf.isDataSetOpen())
244            {
245                nf.closeData();
246            }
247            else
248            {   
249                nf.closeGroup();
250            }
251            break;
252
253        default:
254            break;
255    }
256    return nf;
257}
258
259File& operator>>(File& nf, const StreamModifier sm)
260{
261    switch(sm)
262    {
263        case Close:
264            if (nf.isDataSetOpen())
265            {
266                nf.closeData();
267            }
268            else
269            {   
270                nf.closeGroup();
271            }
272            break;
273
274        default:
275            break;
276    }
277    return nf;
278}
279
280} // Stream
281} // NeXus
Note: See TracBrowser for help on using the repository browser.