Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
dynamic-graph
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Olivier Stasse
dynamic-graph
Commits
13d1f9ab
Commit
13d1f9ab
authored
14 years ago
by
Florent Lamiraux
Browse files
Options
Downloads
Patches
Plain Diff
Re-implement value getter in a nicer way.
* include/dynamic-graph/value.h, * src/command/value.cpp.
parent
b010446b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/dynamic-graph/value.h
+22
-33
22 additions, 33 deletions
include/dynamic-graph/value.h
src/command/value.cpp
+44
-3
44 additions, 3 deletions
src/command/value.cpp
with
66 additions
and
36 deletions
include/dynamic-graph/value.h
+
22
−
33
View file @
13d1f9ab
...
@@ -26,6 +26,17 @@
...
@@ -26,6 +26,17 @@
namespace
dynamicgraph
{
namespace
dynamicgraph
{
namespace
command
{
namespace
command
{
class
Value
;
class
AnyType
{
public:
AnyType
(
const
Value
&
value
);
operator
int
()
const
;
operator
double
()
const
;
operator
std
::
string
()
const
;
private:
const
Value
&
value_
;
};
class
DYNAMICGRAPH_EXPORT
Value
{
class
DYNAMICGRAPH_EXPORT
Value
{
public:
public:
enum
Type
{
enum
Type
{
...
@@ -36,8 +47,9 @@ namespace dynamicgraph {
...
@@ -36,8 +47,9 @@ namespace dynamicgraph {
NB_TYPES
NB_TYPES
};
};
~
Value
();
~
Value
();
/// template constructor
Value
(
const
int
&
value
);
template
<
class
T
>
Value
(
const
T
&
value
);
Value
(
const
double
&
value
);
Value
(
const
std
::
string
&
value
);
/// Copy constructor
/// Copy constructor
Value
(
const
Value
&
value
);
Value
(
const
Value
&
value
);
// Construct an empty value (None)
// Construct an empty value (None)
...
@@ -45,46 +57,23 @@ namespace dynamicgraph {
...
@@ -45,46 +57,23 @@ namespace dynamicgraph {
/// Return the type of the value
/// Return the type of the value
Type
type
()
const
;
Type
type
()
const
;
/// Return the value if it is a double and throw otherwise
/// Return the value as a castable value into the approriate type
double
doubleValue
()
const
;
/// double x = value();
/// Return the value if it is a int and throw otherwise
const
AnyType
value
()
const
;
int
intValue
()
const
;
/// Return the value if it is a string and throw otherwise
std
::
string
stringValue
()
const
;
/// Return the name of the type
/// Return the name of the type
static
std
::
string
typeName
(
Type
type
);
static
std
::
string
typeName
(
Type
type
);
/// Output in a stream
/// Output in a stream
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
Value
&
value
);
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
Value
&
value
);
private
:
private
:
friend
class
AnyType
;
const
double
doubleValue
()
const
;
const
int
intValue
()
const
;
const
std
::
string
stringValue
()
const
;
Type
type_
;
Type
type_
;
const
void
*
value_
;
const
void
*
value_
;
};
};
}
// namespace command
// Template constructors
template
<
>
inline
Value
::
Value
(
const
int
&
value
)
{
std
::
cout
<<
"Constructor of int value"
<<
std
::
endl
;
value_
=
new
int
(
value
);
type_
=
INT
;
}
template
<
>
inline
Value
::
Value
(
const
double
&
value
)
{
std
::
cout
<<
"Constructor of double value"
<<
std
::
endl
;
value_
=
new
double
(
value
);
type_
=
DOUBLE
;
}
template
<
>
inline
Value
::
Value
(
const
std
::
string
&
value
)
{
std
::
cout
<<
"Constructor of string value"
<<
std
::
endl
;
value_
=
new
std
::
string
(
value
);
type_
=
STRING
;
}
template
<
class
T
>
Value
::
Value
(
const
T
&
value
)
{
assert
(
false
);
}
}
// namespace command
}
//namespace dynamicgraph
}
//namespace dynamicgraph
#endif //DYNAMIC_GRAPH_VALUE_H
#endif //DYNAMIC_GRAPH_VALUE_H
This diff is collapsed.
Click to expand it.
src/command/value.cpp
+
44
−
3
View file @
13d1f9ab
...
@@ -21,6 +21,22 @@
...
@@ -21,6 +21,22 @@
namespace
dynamicgraph
{
namespace
dynamicgraph
{
namespace
command
{
namespace
command
{
AnyType
::
AnyType
(
const
Value
&
value
)
:
value_
(
value
)
{
}
AnyType
::
operator
int
()
const
{
return
value_
.
intValue
();
}
AnyType
::
operator
double
()
const
{
return
value_
.
doubleValue
();
}
AnyType
::
operator
std
::
string
()
const
{
return
value_
.
stringValue
();
}
Value
::~
Value
()
Value
::~
Value
()
{
{
switch
(
type_
)
{
switch
(
type_
)
{
...
@@ -36,6 +52,26 @@ namespace dynamicgraph {
...
@@ -36,6 +52,26 @@ namespace dynamicgraph {
}
}
}
}
Value
::
Value
(
const
int
&
value
)
{
std
::
cout
<<
"Constructor of int value"
<<
std
::
endl
;
value_
=
new
int
(
value
);
type_
=
INT
;
}
Value
::
Value
(
const
double
&
value
)
{
std
::
cout
<<
"Constructor of double value"
<<
std
::
endl
;
value_
=
new
double
(
value
);
type_
=
DOUBLE
;
}
Value
::
Value
(
const
std
::
string
&
value
)
{
std
::
cout
<<
"Constructor of string value"
<<
std
::
endl
;
value_
=
new
std
::
string
(
value
);
type_
=
STRING
;
}
Value
::
Value
(
const
Value
&
value
)
:
type_
(
value
.
type_
)
Value
::
Value
(
const
Value
&
value
)
:
type_
(
value
.
type_
)
{
{
switch
(
value
.
type_
)
{
switch
(
value
.
type_
)
{
...
@@ -59,12 +95,17 @@ namespace dynamicgraph {
...
@@ -59,12 +95,17 @@ namespace dynamicgraph {
std
::
cout
<<
"Value empty constructor"
<<
std
::
endl
;
std
::
cout
<<
"Value empty constructor"
<<
std
::
endl
;
}
}
const
AnyType
Value
::
value
()
const
{
return
AnyType
(
*
this
);
}
Value
::
Type
Value
::
type
()
const
Value
::
Type
Value
::
type
()
const
{
{
return
type_
;
return
type_
;
}
}
double
Value
::
doubleValue
()
const
const
double
Value
::
doubleValue
()
const
{
{
double
result
;
double
result
;
if
(
type_
==
DOUBLE
)
if
(
type_
==
DOUBLE
)
...
@@ -75,7 +116,7 @@ namespace dynamicgraph {
...
@@ -75,7 +116,7 @@ namespace dynamicgraph {
"value is not a double"
);
"value is not a double"
);
}
}
int
Value
::
intValue
()
const
const
int
Value
::
intValue
()
const
{
{
if
(
type_
==
INT
)
if
(
type_
==
INT
)
return
*
((
int
*
)
value_
);
return
*
((
int
*
)
value_
);
...
@@ -83,7 +124,7 @@ namespace dynamicgraph {
...
@@ -83,7 +124,7 @@ namespace dynamicgraph {
"value is not an int"
);
"value is not an int"
);
}
}
std
::
string
Value
::
stringValue
()
const
const
std
::
string
Value
::
stringValue
()
const
{
{
if
(
type_
==
STRING
)
if
(
type_
==
STRING
)
return
*
((
std
::
string
*
)
value_
);
return
*
((
std
::
string
*
)
value_
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment