00001 /* 00002 panaLiX, Adrian Panasiuk 2002-5,8 00003 http://panalix.sourceforge.net/ 00004 ad.ek336@gmail.com 00005 00006 small osdev project 00007 provided under GPLv3 license. 00008 */ 00009 00010 00011 #ifndef __PANALIX_OPTION_HPP 00012 #define __PANALIX_OPTION_HPP 00013 00014 #include "src/common/kmalloc.hpp" 00015 #include "src/common/error.hpp" 00016 00019 namespace Collection { 00020 00021 template <typename T> 00022 struct Option { 00023 private: 00024 bool isNonev; 00025 T v; 00026 public: 00027 void * operator new(size_t s) { 00028 return kmalloc(s, "option"); 00029 } 00030 struct Option & operator=(const struct Option &rhs) { 00031 this->isNonev = rhs.isNonev; 00032 this->v = rhs.v; 00033 return *this; 00034 } 00035 00036 00037 bool isNone() { 00038 return isNonev; 00039 } 00040 Option() { 00041 isNonev=true; 00042 } 00043 Option(T v) { 00044 this->v=v; 00045 isNonev=false; 00046 } 00047 T get() { 00048 if (isNone()) { 00049 debug_dump_call_trace(); 00050 sysfail("None.get()"); 00051 } 00052 return v; 00053 } 00054 void set(T x) { 00055 v=x; isNonev=false; 00056 } 00057 void setNone() { 00058 isNonev=true; 00059 } 00060 }; 00061 } 00062 00065 template <typename T> 00066 inline void print(struct Collection::Option<T> v) { 00067 if (v.isNone()) print("None"); 00068 else prin("Some ", v.get()); 00069 } 00072 #endif 00073