Enforcing formatting style block-comments - clang-format

I want to use clang-format to enforce either of the following formatting styles of block-comments:
Formatting style (1):
/*
* ...
*/
Formatting style (2):
/**
...
*/
How do I do this?
A more specific example: given this code
/**
Starting from formatting style (1).
*/
void foo();
/**
* Starting from formatting style (2).
*/
void bar();
I want to use clang-format to switch between outputs:
Formatting style (1):
/**
* Starting from formatting style (1).
*/
void foo();
/**
* Starting from formatting style (2).
*/
void bar();
Formatting style (2):
/**
Starting from formatting style (1).
*/
void foo();
/**
Starting from formatting style (2).
*/
void bar();

Related

Accessing to C object properties from Swift 4

I imported the libxml2 C library into a pure Swift project successfully but now I'm facing a new problem. I want to access to the properties of an xmlNode C Type, which is declared like this at the libxml library
struct _xmlNode {
void *_private; /* application data */
xmlElementType type; /* type number, must be second ! */
const xmlChar *name; /* the name of the node, or the entity */
struct _xmlNode *children; /* parent->childs link */
struct _xmlNode *last; /* last child link */
struct _xmlNode *parent; /* child->parent link */
struct _xmlNode *next; /* next sibling link */
struct _xmlNode *prev; /* previous sibling link */
struct _xmlDoc *doc; /* the containing document */
/* End of common part */
xmlNs *ns; /* pointer to the associated namespace */
xmlChar *content; /* the content */
struct _xmlAttr *properties;/* properties list */
xmlNs *nsDef; /* namespace definitions on this node */
void *psvi; /* for type/PSVI informations */
unsigned short line; /* line number */
unsigned short extra; /* extra data for XPath/XSLT */
};
For example, I want to access to the name property of a xmlNode object named node. I know that, using objective-C, that can be done like this node->name, but that is not allowed on Swift.
I found the move() function inside the object so I can do node.move().name but I'm not sure if that is the way to go.
Also, how can I cast the C types to Swift types easily?
Anyone can point me to the right way?
Thanks.

How to set a style class for a column in Vaadin Grid?

I'd like to assign a style class to a column in Grid. The Column class does not provide an addStyleName method that other Vaadin components do. Is there a way to do it?
You can only set an CellStyleGenerator or RowStyleGenerator for the grid. To set a class for a column, you have to do this:
grid.setCellStyleGenerator(new CellStyleGenerator() {
#Override
public String getStyle(CellReference cell) {
if ("myProperty".equals(cell.getPropertyId()))
return "my-style";
else
return null;
}
});
There can be only single CellStyleGenerator for single Grid. I often have complex code that configures the grid, and I configure it column by column. I use this utility class, that enables me to do so (requires Java8):
/**
* A {#link CellStyleGenerator}, that enables you to set <code>CellStyleGenerator</code>
* independently for each column. It also has a shorthand method to set a fixed style
* class for a column, which Vaadin currently does not allow to (as of Vaadin 7.6).
*
* For more information, see http://stackoverflow.com/a/36398300/952135
* #author http://stackoverflow.com/users/952135
*/
public class EasyCellStyleGenerator implements CellStyleGenerator {
private Map<Object, List<CellStyleGenerator>> generators;
#Override
public String getStyle(CellReference cellReference) {
if (generators != null) {
List<CellStyleGenerator> gens = generators.get(cellReference.getPropertyId());
if (gens != null)
return gens.stream()
.map(gen -> gen.getStyle(cellReference))
.filter(s -> s != null)
.collect(Collectors.joining(" "));
}
return null;
}
/**
* Adds a generator for a column. Allows generating different style for each cell,
* but is called only for the given column.
*/
public void addColumnCellStyleGenerator(Object propertyId,
CellStyleGenerator generator) {
if (generators == null) // lazy init of map
generators = new HashMap<>();
generators.computeIfAbsent(propertyId, k->new ArrayList<>()).add(generator);
}
/**
* Sets a fixed style class(es), that will be used for all cells of this column.
*/
public void addColumnFixedStyle(Object propertyId, String styleClasses) {
addColumnCellStyleGenerator(propertyId, cellReference -> styleClasses);
}
}

zf2 JSON-RPC server how to return custom error

I'm looking for the proper way to return a custom error from a JSON-RPC exposed class.
JSON-RPC has a special format for reporting error conditions. All errors need to provide, minimally, an error message and error code; optionally, they can provide additional data, such as a backtrace.
Error codes are derived from those recommended by the XML-RPC EPI project. Zend\Json\Server appropriately assigns the code based on the error condition. For application exceptions, the code ‘-32000’ is used.
I will use the divide method of the sample code from documentation to explain:
<?php
/**
* Calculator - sample class to expose via JSON-RPC
*/
class Calculator
{
/**
* Return sum of two variables
*
* #param int $x
* #param int $y
* #return int
*/
public function add($x, $y)
{
return $x + $y;
}
/**
* Return difference of two variables
*
* #param int $x
* #param int $y
* #return int
*/
public function subtract($x, $y)
{
return $x - $y;
}
/**
* Return product of two variables
*
* #param int $x
* #param int $y
* #return int
*/
public function multiply($x, $y)
{
return $x * $y;
}
/**
* Return the division of two variables
*
* #param int $x
* #param int $y
* #return float
*/
public function divide($x, $y)
{
if ($y == 0) {
// Say "y must not be zero" in proper JSON-RPC error format
// e.g. something like {"error":{"code":-32600,"message":"Invalid Request","data":null},"id":null}
} else {
return $x / $y;
}
}
}
$server = new Zend\Json\Server\Server();
$server->setClass('Calculator');
if ('GET' == $_SERVER['REQUEST_METHOD']) {
// Indicate the URL endpoint, and the JSON-RPC version used:
$server->setTarget('/json-rpc.php')
->setEnvelope(Zend\Json\Server\Smd::ENV_JSONRPC_2);
// Grab the SMD
$smd = $server->getServiceMap();
// Return the SMD to the client
header('Content-Type: application/json');
echo $smd;
return;
}
$server->handle();
p.s. Yes I tried Google search.
Disclaimer: I have no experience in using Zend\Json\Server whatsoever :)
If you talk about an error response, I can correlate that to the Server::fault() method (also available on Github). So I assume if fault() is called and injected into the respones, it would return the response with error messages according to your referred recommended XML-RPC server standard.
The handler method proxies the actual work to _handle() (linked to the source) where a try/catch is encapsuling the dispatching to the (in your case) Calculator class.
The fault is called based on the exception message and exception code. As such I think it's simply throwing an exception and setting the right message/code there:
use Zend\Json\Server\Error;
class Calculator
{
public function divide($x, $y)
{
if (0 === $y) {
throw new InvalidArgumentException(
'Denominator must be a non-zero numerical',
Error::ERROR_INVALID_PARAMS
);
}
// Rest here
}
// Rest here
}
PS. I also changed your error code here, as to me, it feels -32602 (invalid params) is more appropriate than -32600 (invalid request.

NoSuchMethodError with dart.js

I'm trying to use paper.js with Dart through js.dart.
A lot seems to work, but I also need the method importSVG from paper.js. When I try to access it with js.context.paper.project.importSVG(query("#svg")); I get NoSuchMethodError. It's somehow because the method is injected into project -- see code from paper.js below.
How do I access the importSVG method from Dart?
/* paper.js */
new function() {
function importSVG(node, clearDefs) {
// ...
}
Item.inject(/** #lends Item# */{
/**
* Converts the passed node node into a Paper.js item and adds it to the
* children of this item.
*
* #param {SVGSVGElement} node the SVG DOM node to convert
* #return {Item} the converted Paper.js item
*/
importSVG: function(node) {
return this.addChild(importSVG(node, true));
}
});
Project.inject(/** #lends Project# */{
/**
* Converts the passed node node into a Paper.js item and adds it to the
* active layer of this project.
*
* #param {SVGSVGElement} node the SVG DOM node to convert
* #return {Item} the converted Paper.js item
*/
importSVG: function(node) {
this.activate();
return importSVG(node, true);
}
});
};
Your Dart call seems correct. The comments on the question tend to show that there's a problem with the javascript import/declaration of paper.project.importSVG.

Creating javadoc with tags using ant

I have commented my java source code with javadoc using tags like {#see myPackage.MyClass}.
I have to generate javadoc with ant from terminal but I have got this warning:
[javadoc] src/calendar/annotation/DataType.java:11: warning - Tag #see cannot be used in inline documentation. It can only be used in the following types of documentation: overview, package, class/interface, constructor, field, method.
In build.xml is this line:
<javadoc sourcepath="${sourceDir}" destdir="${docDir}" windowtitle="MyProject" />
Can anybody help me, please?
Edit:
I am using it correctly. For example I have
/**
* <p>Metoda provede požadovaný dotaz do databáze za použití předaných parametrů.
* Pokud jsou parametry nedostatečně nedefinované, SQL dotaz neexistuje nebo nastane
* problém s komunikací, dojde k vygenerování {#see calendar.exception.LoadException}.
* Pokud žádné entity neodpovídají požadavku, dochází k vrácení prázdného seznamu.
* V případě, že nějaké entity odpovídají požadovanému pravidlu, jsou načteny především ty,
* které již jsou definovány v persistenční vrstvě. Pokud tam entity nejsou zavedeny, dochází
* k jejich načtení z databáze.</p>
* #param entityClass
* #param query
* #param params
* #return
* #throws calendar.exception.LoadException
*/
<EntityClass extends AbstractEntity> Collection<EntityClass> find( Class<EntityClass> entityClass, String query, Map<String, Object> params ) throws LoadException;
Shouldn't that be #link instead of #see?
http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javadoc.html#%7B%40link%7D
In Datatype.java, you are using #see in a comment in your code in a place Javadoc does not allow. Specifically, it looks like you have something like:
/**
*...#see...
*/
void foo() {
}
Where it should be
/**
* ...
* #see bla
/*
void foo() {
...
}

Resources