casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Opt.h
Go to the documentation of this file.
1 //# Opt.h: simple C++11 alternative to C++17 std::optional
2 //# Copyright (C) 2019
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 #ifndef CASATOOLS_DATA_OPT_H_
27 #define CASATOOLS_DATA_OPT_H_
28 #include <stdexcept>
29 
30 namespace casatools {
31 
32  template <typename T> class OptionValue {
33  public:
34  const T &get( ) {
35  if ( ! has_value_ )
36  throw std::runtime_error("attempt to retrieve value from none (option)");
37  return value_;
38  }
39  bool has_value( ) { return has_value_; }
40  private:
41  template<typename U> friend class opt;
42  OptionValue( ) : has_value_(false) { }
43  OptionValue( const T &val ): has_value_(true), value_(val) { }
44  bool has_value_;
45  T value_;
46  };
47 
48  template <typename T> class opt {
49  public:
50  static OptionValue<T> some(const T& val) { return OptionValue<T>(val); }
51  static OptionValue<T> none( ) { return OptionValue<T>( ); }
52  };
53 
54 }
55 
56 #endif
OptionValue(const T &val)
Definition: Opt.h:43
bool has_value()
Definition: Opt.h:39
static OptionValue< T > none()
Definition: Opt.h:51
static OptionValue< T > some(const T &val)
Definition: Opt.h:50