Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ros2: extract HostProcessValue from HostProcessPointer
Move the generic parts to a new HostProcessValue class. Add pointer-specific methods to HostProcessPointer, such as #isNullptr(). Signed-off-by: Christophe Bedard <bedard.christophe@gmail.com> Signed-off-by: Christophe Bedard <christophe.bedard@apex.ai> Change-Id: I18e5c092da9863c4d276f8295b423774f73ac619 Reviewed-on: https://git.eclipse.org/r/c/tracecompass.incubator/org.eclipse.tracecompass.incubator/+/202311 Tested-by: Trace Compass Bot <tracecompass-bot@eclipse.org> Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com> Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
- Loading branch information
1 parent
caf0f1e
commit ed82369
Showing
3 changed files
with
157 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
...ore/src/org/eclipse/tracecompass/incubator/internal/ros2/core/model/HostProcessValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2023 École Polytechnique de Montréal, Apex.AI, Inc. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License 2.0 which | ||
* accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
**********************************************************************/ | ||
|
||
package org.eclipse.tracecompass.incubator.internal.ros2.core.model; | ||
|
||
import org.eclipse.jdt.annotation.NonNull; | ||
import org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferWriter; | ||
|
||
import com.google.common.base.Objects; | ||
|
||
/** | ||
* Unique identifier that combines a value and a {@link HostProcess}, i.e., some | ||
* value + process ID + host ID. | ||
* | ||
* @author Christophe Bedard | ||
* @param <T> | ||
* the type of the wrapped value | ||
*/ | ||
public abstract class HostProcessValue<@NonNull T> { | ||
|
||
private static final @NonNull String STRING_ID_SEP = "|"; //$NON-NLS-1$ | ||
|
||
private final @NonNull HostProcess fHostProcess; | ||
private final @NonNull T fValue; | ||
private final int fSerializedValueSize; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param hostProcess | ||
* the host process object | ||
* @param value | ||
* the value | ||
*/ | ||
public HostProcessValue(@NonNull HostProcess hostProcess, @NonNull T value) { | ||
fHostProcess = hostProcess; | ||
fValue = value; | ||
|
||
// Concrete classes will compute the value's size properly | ||
fSerializedValueSize = fHostProcess.getSerializedValueSize(); | ||
} | ||
|
||
/** | ||
* @return the host process | ||
*/ | ||
public @NonNull HostProcess getHostProcess() { | ||
return fHostProcess; | ||
} | ||
|
||
/** | ||
* @return the PID | ||
*/ | ||
public @NonNull Long getPid() { | ||
return fHostProcess.getPid(); | ||
} | ||
|
||
/** | ||
* Concrete classes will provide their own appropriately-named getter | ||
* method. | ||
* | ||
* @return the value | ||
*/ | ||
protected @NonNull T getValue() { | ||
return fValue; | ||
} | ||
|
||
/** | ||
* @return the value as a string | ||
*/ | ||
protected abstract @NonNull String valueToString(); | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(fHostProcess, fValue); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null) { | ||
return false; | ||
} | ||
/** | ||
* Compare attributes directly; do not compare object classes. We want | ||
* two objects of different classes that extend HostProcessValue to be | ||
* equal if they have the same HostProcess and value. | ||
*/ | ||
HostProcessValue<?> o = (HostProcessValue<?>) obj; | ||
return o.fHostProcess.equals(fHostProcess) && o.fValue.equals(fValue); | ||
} | ||
|
||
/** | ||
* @return the string ID to uniquely represent this pointer | ||
*/ | ||
public @NonNull String getStringId() { | ||
return String.format( | ||
"%s%s%d%s%s", //$NON-NLS-1$ | ||
valueToString(), STRING_ID_SEP, getPid(), STRING_ID_SEP, getHostProcess().getHostId().getId()); | ||
} | ||
|
||
@Override | ||
public @NonNull String toString() { | ||
return String.format( | ||
"%s, pid=%d, hostId=[%s]", //$NON-NLS-1$ | ||
valueToString(), getPid(), getHostProcess().getHostId().toString()); | ||
} | ||
|
||
/** | ||
* @param buffer | ||
* the buffer | ||
*/ | ||
public void serializeValue(@NonNull ISafeByteBufferWriter buffer) { | ||
fHostProcess.serializeValue(buffer); | ||
// Concrete classes will serialize the value properly | ||
} | ||
|
||
/** | ||
* @return the serialized size | ||
*/ | ||
public int getSerializedValueSize() { | ||
return fSerializedValueSize; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters