Implementing a good C++0x error_condition? - exception

I try to figure out how the new system_error together with error_code, error_category and not the least the (meant to implement portable error reporting) error_condition should be used.
I think by reading boost.system I understand how I should use error_codes and error_category. The description leaves out how this is used in conjunction when throwing an exception with ´system_error`, but from the interface from that class I can guess.
class system_error : public runtime_error {
public:
// [...]
system_error(error_code ec, const string& what_arg);
system_error(int ev, const error_category& ecat, const string& what_arg);
system_error(int ev, const error_category& ecat);
// [...]
So, I throw a system_error-exception with the right int+error_category or error_code with its error_category()-method.
But whats the way to provide the portable interface with error_condition?
Both error_code and error_category both have method default_error_condition:
class error_category {
public:
// [...]
virtual error_condition default_error_condition(int ev) const noexcept;
// [...]
class error_code {
public:
// [...]
error_condition default_error_condition(int ev) const noexcept;
// [...]
The error_category-Instances should be pre-created, for example (user-code):
struct AppCategory : public error_category {
const char *name() const noexcept override {
return "application"; }
string message(int ev) const override {
switch(ev) {
case 14: return "error message";
default: return "???";
}
}
error_condition default_error_condition(int ev) const noexcept override {
... ??? ...
}
};
My questions implementing this:
Should I implement default_error_condition in error_category, and how?
Or how do I connect error_codes to the proper error_conditions,
and should I pre-construct error_condition instances?
A class error_code is not supposed to be provided by the user (me), right?
Is there a good example where I can take a look at code how error_condition is supposed to be extended by the user, in conjunction with system_error-exceptions?

If the error codes for your error category can be mapped to one of the std::errc error codes then default_error_condition should do that mapping, and return a std::error_condition with a category of std::generic_category() and a value of the corresponding std::errc enum value. Otherwise it should return an error_condition referring to that category.

Related

How can I solve 'Duplicate Constructor' error in Haxe?

In Haxe, I created a class named MyClass like:
class MyClass {
var score: String;
public function new (score: Int) {
this.score = Std.string(score);
}
public function new (score: String) {
this.score = score;
}
}
I need multiple constructors but Haxe does not allow me to do. It throws this error from building phase:
*.hx:*: lines * : Duplicate constructor
The terminal process terminated with exit code: 1
How can I solve this problem?
This is known as method overloading, which is not supported by Haxe apart from externs (but might be in the future). There's multiple ways you could work around this.
A common workaround in the case of constructors would be to have a static "factory method" for the second constructor:
class MyClass {
var score:String;
public function new(score:String) {
this.score = score;
}
public static function fromInt(score:Int):MyClass {
return new MyClass(Std.string(score));
}
}
You could also have a single constructor that accepts both kinds of arguments:
class MyClass {
var score:String;
public function new(score:haxe.extern.EitherType<String, Int>) {
// technically there's no need for an if-else in this particular case, since there's
// no harm in calling `Std.string()` on something that's already a string
if (Std.is(score, String)) {
this.score = score;
} else {
this.score = Std.string(score);
}
}
}
However, I wouldn't recommend this approach, haxe.extern.EitherType is essentially Dynamic under the hood, which is bad for type safety and performance. Also, EitherType is technically only intended to be used on externs.
A more type-safe, but also slightly more verbose option would be haxe.ds.Either<String, Int>. Here you'd have to explicitly call the enum constructors: new MyClass(Left("100")) / new MyClass(Right(100)), and then use pattern matching to extract the value.
An abstract type that supports implicit conversions from String and Int might also be an option:
class Test {
static function main() {
var s1:Score = "100";
var s2:Score = 100;
}
}
abstract Score(String) from String {
#:from static function fromInt(i:Int):Score {
return Std.string(i);
}
}
Finally, there's also an experimental library that adds overloading support with macros, but I'm not sure if it supports constructors.
I recommend to use type parameter
class MyClass<T> {
var score:String;
public function new(score:T) {
this.score = Std.string(score);
}
}
You can also use type parameter at constructor
class MyClass {
var score:String;
public function new<T>(score:T) {
this.score = Std.string(score);
}
}
However, T used at constructor fails at runtime (CS and Java), it is not fixed yet (Haxe 4). Otherwise, you could do this
class MyClass {
var score:String;
#:generic public function new<#:const T>(score:T) {
this.score = Std.is(T, String) ? untyped score : Std.string(score);
}
}
which nicely produce code like this (CS)
__hx_this.score = ( (( T is string )) ? (score) : (global::Std.#string(score)) );
causing Std.string() to be called only if T is not a String.
Hej,
With a simple example as it is, you can just do something like that function new( ?s : String, ?n : Int ){} and Haxe will use the correct argument by type. But you'll be able to do new() and maybe you don't want.

How to initialize c++/cx class with aggregate initialization?

I have this ref class:
namespace N
{
public ref class S sealed
{
public:
property Platform::String^ x;
};
}
How do I initialize it in place with the aggregate initializer?
I have tried:
N::S s1 = { %Platform::String(L"text") };
but the compiler says
error C2440: 'initializing': cannot convert from 'initializer list' to
'N::S'
Also:
N::S s1 { %Platform::String(L"text") };
and the error is:
error C2664: 'N::S::S(const N::S %)': cannot convert argument 1 from
'Platform::String ^' to 'const N::S %'
This works greatly with the standard c++ like this:
struct T
{
wstring x;
};
T x { L"test" };
I do not want to use a constructor here.
I assume you mean you don't want a public constructor on the projected WinRT type -- no problem, you can use the internal keyword to mean "public inside C++ but not exposed through interop". That means you can even use native C++ types for your parameters if you like:
namespace Testing
{
public ref class MyTest sealed
{
public:
property String^ Foo {
String^ get() { return m_foo; }
void set(String^ value) { m_foo = value; }
}
internal:
// Would not compile if it was public, since wchar_t* isn't valid
MyTest(const wchar_t* value) { m_foo = ref new String(value); }
private:
String^ m_foo;
};
}
MainPage::MainPage()
{
// Projected type does NOT have this constructor
Testing::MyTest t{ L"Hello" };
OutputDebugString(t.Foo->Data());
t.Foo = "\nChanged";
OutputDebugString(t.Foo->Data());
}
Also you don't need to have the private variable to hold the string -- you could just use the auto-property as in your original code -- but I prefer to be explicit. It also means that if you needed to access the string a lot from within your C++ code you could provide an internal accessor function and not have to go through a vtable call to get at it.

Static call method in Dart Classes (make Classes callable)

For an embedded DSL I want classes to behave like a function. It seems easy for instances (https://www.dartlang.org/articles/emulating-functions/) but I couldn't make it happen for classes. I tried creating a static call a method but this didn't work either.
Is there a way or do I have to give the class another name and make Pconst a function, calling the constructor?
class Pconst {
final value;
Pconst(this.value);
static Pconst call(value) => new Pconst(value);
String toString() => "Pconst($value)";
}
void main() {
var test = Pconst(10);
// Breaking on exception: Class '_Type' has no instance method 'call'.
print("Hello, $test");
}
class TestA {
call(int a, int b) => a + b;
}
void main()
var TA = new TestA();
int integer = TA(3, 4);
print (integer);
}
The call() method is special, in that anyone who defines a call() method is presumed to dynamically emulate a function. This allows us to use instances of TestA as if they were functions that take two integer arguments.
I'd try something like this:
class _PConst{
final value;
_Pconst(this.value);
String toString() => "Pconst($value)";
}
PConst(value){
return new _PConst(value);
}
void main() {
var test = Pconst(10);
print("Hello, $test"); //should work ok
}
so your basically just hiding/wrapping your classes constructor behind a bog standard function.

Override bad_alloc Exception

I am trying to change the message for bad_alloc.
#include <iostream>
#include <iomanip>
#include <stdexcept>
using std::logic_error;
using std::bad_alloc;
class OutOfRange : public logic_error {
public:
OutOfRange(): logic_error("Bad pointer") {}
};
class OutOfMem : public bad_alloc {
public:
OutOfMem(): bad_alloc("not enough memory") {}
};
OutOfRange() works fine, but OutOfMem sends me an error:
No matching function for call to std::bad_alloc::bad_alloc(const char[21])
The compile error is telling you that that bad_alloc constructor does not take a char *.
e.g. See here
Instead, note that exception what method is vritual and use that instead.
class OutOfMem : public bad_alloc {
public:
OutOfMem() {}
const char *what() const {
return "not enough memory";
}
};
Edit: note you might have to state it doesn't throw as follows:
//... as before
virtual const char * what() const throw () {
return "not enough memory";
}
// as before ...

Getting a return value or exception from AspectJ?

I am able to get the signature and arguments from advised method calls, but I cannot figure out how to get the return values or exceptions. I'm kind of assuming that it can be done in some way using around and proceed.
You can use after() returning and after() throwing advices as in beginning of the following document. If you're using #AspectJ syntax please refer to #AfterReturning and #AfterThrowing annotations (you can find samples here).
You can also get return value using after returing advice.
package com.eos.poc.test;
public class AOPDemo {
public static void main(String[] args) {
AOPDemo demo = new AOPDemo();
String result= demo.append("Eclipse", " aspectJ");
}
public String append(String s1, String s2) {
System.out.println("Executing append method..");
return s1 + s2;
}
}
The defined aspect for getting return value:
public aspect DemoAspect {
pointcut callDemoAspectPointCut():
call(* com.eos.poc.test.AOPDemo.append(*,*));
after() returning(Object r) :callDemoAspectPointCut(){
System.out.println("Return value: "+r.toString()); // getting return value
}
Using an around() advice, you can get the return value of the intercepted method call by using proceed(). You can even change the value returned by the method if you want to.
For instance, suppose you have a method m() inside class MyClass:
public class MyClass {
int m() {
return 2;
}
}
Suppose you have the following aspect in its own .aj file:
public aspect mAspect {
pointcut mexec() : execution(* m(..));
int around() : mexec() {
// use proceed() to do the computation of the original method
int original_return_value = proceed();
// change the return value of m()
return original_return_value * 100;
}
}