Data update
This commit is contained in:
parent
4d5544505c
commit
4924dd0264
3073 changed files with 55820 additions and 4408 deletions
|
|
@ -12,7 +12,7 @@ public :
|
|||
|
||||
Employee ( const std::string &dep , const std::string &namen )
|
||||
: department( dep ) , name( namen ) {
|
||||
my_id = count++ ;
|
||||
my_id = count++ ;
|
||||
}
|
||||
|
||||
std::string getName( ) const {
|
||||
|
|
@ -47,9 +47,9 @@ private :
|
|||
|
||||
template <class Archive>
|
||||
void serialize( Archive &ar, const unsigned int version ) {
|
||||
ar & my_id ;
|
||||
ar & name ;
|
||||
ar & department ;
|
||||
ar & my_id ;
|
||||
ar & name ;
|
||||
ar & department ;
|
||||
}
|
||||
|
||||
} ;
|
||||
|
|
@ -57,7 +57,7 @@ private :
|
|||
class Worker : public Employee {
|
||||
public :
|
||||
Worker( const std::string & dep, const std::string &namen ,
|
||||
double hourlyPay ) : Employee( dep , namen ) , salary( hourlyPay) { }
|
||||
double hourlyPay ) : Employee( dep , namen ) , salary( hourlyPay) { }
|
||||
|
||||
Worker( ) { }
|
||||
|
||||
|
|
@ -67,7 +67,7 @@ public :
|
|||
|
||||
void setSalary( double pay ) {
|
||||
if ( pay > 0 )
|
||||
salary = pay ;
|
||||
salary = pay ;
|
||||
}
|
||||
|
||||
virtual void print( ) {
|
||||
|
|
@ -79,8 +79,8 @@ private :
|
|||
friend class boost::serialization::access ;
|
||||
template <class Archive>
|
||||
void serialize ( Archive & ar, const unsigned int version ) {
|
||||
ar & boost::serialization::base_object<Employee>( *this ) ;
|
||||
ar & salary ;
|
||||
ar & boost::serialization::base_object<Employee>( *this ) ;
|
||||
ar & salary ;
|
||||
}
|
||||
} ;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
(define (person->string self) (format "%a : person." (person-name self)))
|
||||
(define (writer->string self) (format "%a: writer of %a."
|
||||
(person-name self)
|
||||
(writer-books self)))
|
||||
(person-name self)
|
||||
(writer-books self)))
|
||||
(define (father->string self) (format "%a: father of %a."
|
||||
(person-name self)
|
||||
(map person-name (father-children self))))
|
||||
|
||||
(person-name self)
|
||||
(map person-name (father-children self))))
|
||||
|
||||
; 'classes' definition, with inheritance.
|
||||
; a writer is a person, too.
|
||||
(struct person (name) #:tostring person->string)
|
||||
|
|
|
|||
|
|
@ -6,23 +6,23 @@
|
|||
-record( person, {entity, email} ).
|
||||
|
||||
task() ->
|
||||
Person = #person{entity=#entity{name="Cletus", date=20080808}, email="test+1@localhost.localdomain"},
|
||||
print( Person ),
|
||||
Entity = #entity{name="Entity", date=20111111},
|
||||
print( Entity ),
|
||||
ok = file:write_file( "objects.dat", erlang:term_to_binary([Person, Entity]) ),
|
||||
{ok, Binary} = file:read_file( "objects.dat" ),
|
||||
[New_person, New_entity] = erlang:binary_to_term( Binary ),
|
||||
io:fwrite( "Deserialized\n" ),
|
||||
print( New_person ),
|
||||
print( New_entity ).
|
||||
|
||||
Person = #person{entity=#entity{name="Cletus", date=20080808}, email="test+1@localhost.localdomain"},
|
||||
print( Person ),
|
||||
Entity = #entity{name="Entity", date=20111111},
|
||||
print( Entity ),
|
||||
ok = file:write_file( "objects.dat", erlang:term_to_binary([Person, Entity]) ),
|
||||
{ok, Binary} = file:read_file( "objects.dat" ),
|
||||
[New_person, New_entity] = erlang:binary_to_term( Binary ),
|
||||
io:fwrite( "Deserialized\n" ),
|
||||
print( New_person ),
|
||||
print( New_entity ).
|
||||
|
||||
|
||||
|
||||
print( #entity{name=Name, date=Date} ) ->
|
||||
io:fwrite( "Entity: " ),
|
||||
io:fwrite( "name: ~p, date: ~p~n", [Name, Date] );
|
||||
io:fwrite( "Entity: " ),
|
||||
io:fwrite( "name: ~p, date: ~p~n", [Name, Date] );
|
||||
print( #person{entity=Entity, email=Email} ) ->
|
||||
io:fwrite( "Person: " ),
|
||||
print( Entity ),
|
||||
io:fwrite( "\temail: ~p~n", [Email] ).
|
||||
io:fwrite( "Person: " ),
|
||||
print( Entity ),
|
||||
io:fwrite( "\temail: ~p~n", [Email] ).
|
||||
|
|
|
|||
|
|
@ -102,17 +102,17 @@ int main()
|
|||
|
||||
// let us create a fantasy animal
|
||||
Animal *anAnimal = [[Animal alloc]
|
||||
initWithName: @"Eptohippos"
|
||||
andLegs: 7
|
||||
];
|
||||
initWithName: @"Eptohippos"
|
||||
andLegs: 7
|
||||
];
|
||||
// for some reason an Eptohippos is not an horse with 7 legs,
|
||||
// and it is not a mammal, of course...
|
||||
|
||||
// let us create a fantasy mammal (which is an animal too)
|
||||
Mammal *aMammal = [[Mammal alloc]
|
||||
initWithName: @"Mammaluc"
|
||||
hasFur: YES
|
||||
];
|
||||
initWithName: @"Mammaluc"
|
||||
hasFur: YES
|
||||
];
|
||||
// let us add some eaten stuff...
|
||||
[aMammal addEatenThing: @"lamb"];
|
||||
[aMammal addEatenThing: @"table"];
|
||||
|
|
@ -129,7 +129,7 @@ int main()
|
|||
// now let us store the objects...
|
||||
NSMutableData *data = [[NSMutableData alloc] init];
|
||||
NSKeyedArchiver *arch = [[NSKeyedArchiver alloc]
|
||||
initForWritingWithMutableData: data];
|
||||
initForWritingWithMutableData: data];
|
||||
[arch encodeObject: anAnimal forKey: @"Eptohippos"];
|
||||
[arch encodeObject: aMammal forKey: @"Mammaluc"];
|
||||
[arch finishEncoding];
|
||||
|
|
@ -137,9 +137,9 @@ int main()
|
|||
|
||||
// now we want to retrieve the saved objects...
|
||||
NSData *ldata = [[NSData alloc]
|
||||
initWithContentsOfFile: @"objects.dat"];
|
||||
initWithContentsOfFile: @"objects.dat"];
|
||||
NSKeyedUnarchived *darch = [[NSKeyedUnarchiver alloc]
|
||||
initForReadingWithData: ldata];
|
||||
initForReadingWithData: ldata];
|
||||
Animal *archivedAnimal = [darch decodeObjectForKey: @"Eptohippos"];
|
||||
Mammal *archivedMammal = [darch decodeObjectForKey: @"Mammaluc"];
|
||||
[darch finishDecoding];
|
||||
|
|
|
|||
|
|
@ -6,14 +6,14 @@
|
|||
import pickle
|
||||
|
||||
class Entity:
|
||||
def __init__(self):
|
||||
self.name = "Entity"
|
||||
def printName(self):
|
||||
print self.name
|
||||
def __init__(self):
|
||||
self.name = "Entity"
|
||||
def printName(self):
|
||||
print self.name
|
||||
|
||||
class Person(Entity): #OldMan inherits from Entity
|
||||
def __init__(self): #override constructor
|
||||
self.name = "Cletus"
|
||||
def __init__(self): #override constructor
|
||||
self.name = "Cletus"
|
||||
|
||||
instance1 = Person()
|
||||
instance1.printName()
|
||||
|
|
|
|||
84
Task/Object-serialization/REBOL/object-serialization.rebol
Normal file
84
Task/Object-serialization/REBOL/object-serialization.rebol
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
Rebol [
|
||||
title: "Rosetta code: Object serialization"
|
||||
file: %Object_serialization.r3
|
||||
url: https://rosettacode.org/wiki/Object_serialization
|
||||
needs: 2.7.0
|
||||
]
|
||||
; ----------------------------
|
||||
; Define a base "class" Person
|
||||
; ----------------------------
|
||||
; In Rebol, `make object! [...]` creates an object with defined fields (and optionally functions).
|
||||
; Here, Person has two properties: name (string) and age (integer).
|
||||
Person: make object! [
|
||||
name: ""
|
||||
age: 0
|
||||
]
|
||||
|
||||
; ------------------------------------------------
|
||||
; "Inherit" a subclass Student from Person
|
||||
; ------------------------------------------------
|
||||
; Since Rebol doesn't have classical inheritance, `make Person [...]`
|
||||
; makes a copy of the Person object and lets you add or override fields.
|
||||
; Student adds an extra property: grade (e.g., "A", "B", etc.)
|
||||
Student: make Person [
|
||||
grade: ""
|
||||
]
|
||||
|
||||
; ------------------------------------------------
|
||||
; Inherit another subclass Teacher from Person
|
||||
; ------------------------------------------------
|
||||
; Teacher adds an extra property: subject (teaching subject)
|
||||
Teacher: make Person [
|
||||
subject: ""
|
||||
]
|
||||
|
||||
; ----------------------------------
|
||||
; Create specific instances of each
|
||||
; ----------------------------------
|
||||
; Values in brackets override default properties when creating the object.
|
||||
john: make Person [name: "John" age: 40]
|
||||
sally: make Student [name: "Sally" age: 18 grade: "A"]
|
||||
bob: make Teacher [name: "Bob" age: 50 subject: "Math"]
|
||||
|
||||
; ------------------------------------------------
|
||||
; Print text in yellow (assuming ANSI color functions are set up)
|
||||
; ------------------------------------------------
|
||||
print "Original instances:"
|
||||
|
||||
; ------------------------------------------------
|
||||
; `probe` prints the value in developer-readable format and returns it
|
||||
; This is useful for quickly seeing object content
|
||||
; ------------------------------------------------
|
||||
probe john
|
||||
probe sally
|
||||
probe bob
|
||||
|
||||
; ------------------------------------------------
|
||||
; Create a block containing all three objects
|
||||
; `reduce` evaluates each word and inserts its value into the block
|
||||
; ------------------------------------------------
|
||||
objects: reduce [john sally bob]
|
||||
|
||||
; ------------------------------------------------
|
||||
; Save all object data (including words, values, and types) to a file
|
||||
; `save/all` preserves the complete object structure
|
||||
; ------------------------------------------------
|
||||
save/all %objects.dat objects
|
||||
|
||||
; ------------------------------------------------
|
||||
; Show header for loaded instances
|
||||
; ------------------------------------------------
|
||||
print "Loaded instances:"
|
||||
|
||||
; ------------------------------------------------
|
||||
; Load objects back from the file
|
||||
; `load` reconstructs the original structures from the saved format
|
||||
; ------------------------------------------------
|
||||
read-objects: load %objects.dat
|
||||
|
||||
; ------------------------------------------------
|
||||
; Iterate over loaded objects and display them
|
||||
; ------------------------------------------------
|
||||
foreach obj read-objects [
|
||||
probe obj
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue