Skip to content

QOption class provides functionality for force handling execution errors. It is a container class requires unboxing to get the stored value.

License

Notifications You must be signed in to change notification settings

smurfomen/option

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

option class provides functionality for force handling execution errors. It is a container class requires unboxing to get the stored value.

Returning value

Bad way:

MyClass * getMyClass() {
  if(expression)
     return new MyClass(args..);
  else
     return nullptr;
}

Good way for example:

option<MyClass*> getMyClass() {
  if(expression)
     return new MyClass(args..);

  return none_option();
}

Use cases


Check before use

auto option = getMyClass();
if(option)
    option.unwrap()->myClassFoo(args..); // equial MyClass * mc = ...; mc->myClassFoo(args...);
// or
if(!option)
   doSomething();


Option can be unwrapped one time

auto option = getMyClass();
if(option) {
    option.unwrap()->myClassFoo(args..); // equial MyClass * mc = ...; mc->myClassFoo(args...);
    doSomething(option.unwrap()); // unwrap_exception will throw!
}


Get a value or throwing std::logic_error type exception

try {
    MyClass * mc = getMyClass().unwrap();
} catch (unwrap_exception & ue) {
    // Option is None value
}


Get a value or throwing MyCustomException

try {
    MyClass * mc = getMyClass().unwrap<MyCustomException>();
} catch (MyCustomException & mce) {
    // Option is None value
}


Get some value or default value if option is not Some

const char * connection = createConnectionString(params).unwrap_def("something default connection string");


Get some value or executing a nested lambda function and get the default value if option is not Some

// this way do not overhead when some case and none default object will not be created
const char * connection = createConnectionString(params).unwrap_or([]{ return "something default connection string"; });


Get some value or throwing std::logic_error type exception with an error message

try {
     MyClass * mc = getMyClass().expect("Something is wrong. Exception QUnwrapException will throw.");
} catch (unwrap_exception & ue) {
    // Something is wrong. Exception QUnwrapException will throw.
}


Get some value or throwing MyCustomException with an error message

try {
    MyClass * mc = getMyClass().expect<MyCustomException>("Something wrong. Exception MyCustomException.");    
} catch (MyCustomException & mce) {
    // Something wrong. Exception MyCustomException.
}


Match result and handle it with custom handlers

MyClass * request = ...';
bool success = getObject().match(
            [&](MyClass * pack) -> bool{
                return pack->export() && HandleResponse(pack);
            },

            [&]() -> bool {
                request->setLineStatus(timeout);
                return false;
            }
        );


Composing handling

option<MyClass*> option = getMyClass();
option.if_some([&](MyClass * obj){
    foo(obj);
}).if_none([]() {
    log("Error handle");
});

About

QOption class provides functionality for force handling execution errors. It is a container class requires unboxing to get the stored value.

Resources

License

Stars

Watchers

Forks

Packages

No packages published