Generated getters and setters code format - c++

Is there a way to change format how netbeans 8.1 generate code? Instead of having
void setSomething1(bool something1)
{
something1_ = something1;
}
bool getSomething1() const
{
return something1_;
}
I would like to have this code
void setSomething1(bool something1) { something1_ = something1; }
bool getSomething1() const { return something1_; }
and also the set/get prefix could change or disappear
void setSomething1(bool something1) { something1_ = something1; }
bool something1() const { return something1_; }
netbeans only allow me to change braces position, same line or new line, but for this short functions i would like to compress it to one line only. With many setters and getters, the code starts to be confusing.
If netbeans itself cant do it, would it be possible via regular expression or some other workaroud?
Other (long body) functions will remain in the format. There it makes the code better to read.
retType name(args)
{
body;
}

This regex will satisfy the first requirement (Demo):
([\w ]+\s+[gs]et\w+\([\w ]*\)\s*(?:const)?)\s+\{\s+(.*)\s+\}
With $1 { $2 } as replacement
Then this one (note the trailing space):
get(\w+)\(
With $1 (trailing space aslo) as replacement

Related

How to replace #include <optional>

I come to you today with another question that my brain can't process by itself:
I got a cpp file that includes optional as a header file. Unfortunately, this works only on c++17 forwards, and I'm trying to compile it in c++14. This cpp file uses optional like this
std::optional<std::string> GetStringPropertyValueFromJson(const std::string& Property, const web::json::value& Json)
{
if (Json.has_field(utility::conversions::to_string_t(Property)))
{
auto& propertyValue = Json.at(utility::conversions::to_string_t(Property));
if (propertyValue.is_string())
{
return std::optional<std::string>{utility::conversions::to_utf8string(propertyValue.as_string())};
}
}
return std::nullopt;
}
and then the function is used to assign values like this:
std::string tokenType = GetStringPropertyValueFromJson("token_type", responseContent).value_or("");
std::string accessToken = GetStringPropertyValueFromJson("access_token", responseContent).value_or("");
Please help me with a proper substitution for OPTIONAL. Thanks and much love
PS: From what i've read, you can replace optional with pair somehow in order to get a similar result, but I don't really know how exactly.
PPS: I am new here so any tips on how to better write my questions or anything else are greatly appreciated :)
I guess in C++14 the optional header could be included by #include <experimental/optional>.
Change your method signature to
std::string GetStringPropertyValueFromJson(const std::string& Property, const web::json::value& Json)
and in the end just return the empty string
return "";
Then later in your code use it without std::optional::value_or:
std::string tokenType = GetStringPropertyValueFromJson("token_type", responseContent);
The logic is exactly the same and you don't use std::optional.
I see now your other question about possibility to use std::pair. Yes, you could also change your method to:
std::pair<std::string, bool> GetStringPropertyValueFromJson(const std::string& Property, const web::json::value& Json)
and return std::make_pair(valueFromJson, true) in case your json property has been found, or std::make_pair("", false) in case it was not. This also solves the problem with empty (but existing) json property.
A poor mans optional string that should be sufficient for your code is this:
struct my_nullopt {};
struct my_optional {
private:
std::string value;
bool has_value = false;
public:
my_optional(my_nullopt) {}
my_optional(const std::string& v) : value(v),has_value(true) {}
T value_or(const std::string& v) {
return has_value ? value : v;
}
};
Its a rather limited interface, for example it is not possible to set the value after construction. But it appears that you do not need that.
Alternatively you can use boost/optional.
Note that the tip you got about using a pair is just what I did above: The value and a bool. Just that std::pair is for cases where you cannot give better names than first and second (eg in generic code), but it is simple to provide a better interface than std::pair does here. With a pair the value_or would be something along the line of x.first ? x.second : "".
PS: Only in the end I realized that the code you present does not actually make use of what std::optional has to offer. As you are calling value_or(""), you cannot distinguish between a field with value "" or "" because the optional had no value. Because of that, the most simple solution is to use a plain std::string and return "" instead of std::nullopt.

Uncrustify code formatter removes space in C++ one-liner

How to prevent Uncrustify code formatter from keeping one space in C++ one-liner between { and the rest code?
So, it changes this code
const Foo &GetBar() const { return bar; }
to this
const Foo &GetBar() const {return bar; }
I search for return in default uncrustify config, but found nothing in common with my problem.
I think that this has more to do with brace spacing than with the return, so I'd check the space settings for braces. The first option I would try to play with would be sp_inside_braces (should be "ignore" by default).

Make clang-format allow return type of function on its own line

I'm trying to figure out if I can use clang-format to automatically format the code for my project. We have a coding style which enforces newlines after the function return type (but only in the definition, not in the declaration). I'd like to encode this in clang-format.
IOW, I want this:
uint32_t
my_function(uint32_t input)
{
return 0;
}
Not this:
uint32_t my_function(uint32_t input)
{
return 0;
}
AlwaysBreakAfterDefinitionReturnType(DRTBS_All) If you add that to the .clang-format file that contains all your formatting decisions it should do exactly what you want.

QValidator prevents intermediate typing

I have a QLineEdit that accepts a string that will be evaluated at a javascript expression like "[0,3]" and connected to fire changes using editingFinished(). I added a validator so the user couldn't leave the input with a bad expression, but apparently I'm misunderstanding how the validator works, because if I return QValidator::Invalid, when the expression wouldn't be valid, user's can't type any mistakes (character won't disappear on backspace). For example, temporarily changing "[0,3]" to "[0,]" to fill in another number.
I've tried changing the validator to return to QValidator::Intermediate on bad expressions thinking that would be a happy medium letter users alter text, but setting bad text back to its previous value on unfocused or return, but that seems to let user's put in anything that want. For example, they can type in "[0," and click on something else and the input still has "[0," in it as opposed to jumping back to the way it was. Am I misunderstanding how the intermediate type works?
QValidator::Invalid 0 The string is clearly invalid.
QValidator::Intermediate 1 The string is a plausible intermediate value.
QValidator::Acceptable 2 The string is acceptable as a final result;
i.e. it is valid.
Here is my current validator, which I just put on a QLineEdit:
class PointFValidator : public QValidator
{
QValidator::State validate(QString &input, int &position) const;
void fixup(QString &input) const;
};
QValidator::State PointFValidator::validate(QString &input, int &position) const
{
try {
evalPointF(input);
} catch (std::exception &e) {
return QValidator::Invalid;
}
return QValidator::Acceptable;
}
void PointFValidator::fixup(QString &input) const
{
}
And this is what actually tests the string to see if it's formatted correctly
QPointF evalPointF(QString s)
{
//initGuile();
QScriptEngine engine;
QString program = "function frame() { return 9; }\n\n" + s;
QScriptValue value = engine.evaluate(program);
QStringList pair = value.toString().split(",");
if (pair.length() < 2)
throw std::runtime_error("invalid pointf string");
return QPointF(pair[0].toFloat(), pair[1].toFloat());
}
Is the QValidator not what I need? Is it only for typing prevention? Do I need to listen to the change event, validate it myself, and set it back if it's not valid?
So trying to compile your code didn't work for me. The constness of the virtual functions in QValidator make it pretty much impossible to get your example code to compile.
So I would (like you mentioned at the end of your question) go and set up a signal to respond to the content changes of your QLineEdit, evaluate it, and then put the output somewhere helpful.
But based on the kinds of things you put in your evalPointF function, it looks like you are just writing an IDE for javascript.
Why not use the pattern that most IDE's already have of putting issues in another window, and using font's and formatting to modify the text, instead of actually changing the text?
Hope that helps.

D std.regex - return as text

Is there a possibility to return text which was used to create regular expression?
Something like this:
auto r = regex(r"[0-9]", "g"); // create regular expression
writeln(r.dumpAsText()); // this would write: [0-9]
There is nothing in http://dlang.org/phobos/std_regex.html on this. (or at least I did not notice)
No, because it compiles the regex, and I don't believe it even stores the string after compilation.
The best thing to do is just to store the string yourself on creation.
Source for struct Regex
As you can see, it doesn't store the pattern string, only the bytecode.
Typically using a subtype would work, but unfortunately ti doesn't due to failed template constraints. E.g. a plausible solution (that doesn't work right now) would be to wrap the regex as a subtype:
auto myregex(string arg1, string arg2)
{
struct RegexWrap
{
Regex!char reg;
alias reg this;
string dumpAsText;
}
return RegexWrap(regex(arg1, arg2), arg1);
}
void main()
{
auto r = myregex(r"[0-9]", "g"); // create regular expression
writeln(r.dumpAsText); // this would write: [0-9]
writeln(match("12345", r)); // won't work
}
The match function in std.regex won't work with this wrapper struct even when using a subtype, because it fails this template constraint:
public auto match(R, RegEx)(R input, RegEx re)
is(RegEx == Regex!(BasicElementOf!R)
Even if you changed the header to this, it still won't work:
public auto match(R)(R input, Regex!(BasicElementOf!R) re)
The only way it would work is if the type was explicit so the subtype could be passed:
public auto match(R)(R input, Regex!char re)
I find this to be an awkward part of D that could be improved.