The Pedigree Project  0.1
Result.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef KERNEL_UTILITIES_RESULT_H
21 #define KERNEL_UTILITIES_RESULT_H
22 
23 #include "pedigree/kernel/utilities/assert.h"
24 #include "pedigree/kernel/utilities/utility.h"
25 
35 template <class T, class E>
36 class Result
37 {
38  public:
39  static Result<T, E> withValue(const T &v)
40  {
41  return Result<T, E>(v);
42  }
43 
44  static Result<T, E> withError(const E &e)
45  {
46  return Result<T, E>(e, true);
47  }
48 
49  const T &value() const
50  {
51  assert(hasValue());
52  return m_Value;
53  }
54 
55  const E &error() const
56  {
57  assert(hasError());
58  return m_Error;
59  }
60 
61  bool hasError() const
62  {
63  return m_HasError;
64  }
65 
66  bool hasValue() const
67  {
68  return !hasError();
69  }
70 
71  private:
72  Result() = delete;
73 
74  Result(const T &v) : m_Value(v), m_Error(), m_HasError(false)
75  {
76  }
77 
78  Result(const E &e, bool)
79  : m_Value(m_DefaultValue), m_Error(e), m_HasError(true)
80  {
81  }
82 
83  T m_Value;
84  E m_Error;
85  bool m_HasError;
86 
87  typedef typename pedigree_std::remove_reference<T>::type BaseValueType;
88  static const BaseValueType m_DefaultValue;
89 };
90 
91 template <class T, class E>
92 const typename Result<T, E>::BaseValueType
93  Result<T, E>::m_DefaultValue = Result<T, E>::BaseValueType();
94 
95 #endif // KERNEL_UTILITIES_RESULT_H
Definition: Result.h:36
#define assert(x)
Definition: assert.h:37